Make zeroing constants structs flatbuffers
This defines all the zeroing structs as flatbuffers and then converts
everything to use the FlatbufferT types.
This is in service of moving zeroing constants into flatbuffers.
Change-Id: Ied7ac78cfcba0f66b4b684fd9e49a49276e55169
Signed-off-by: James Kuszmaul <jabukuszmaul+collab@gmail.com>
diff --git a/frc971/zeroing/BUILD b/frc971/zeroing/BUILD
index 8ca7f67..81ed998 100644
--- a/frc971/zeroing/BUILD
+++ b/frc971/zeroing/BUILD
@@ -1,5 +1,12 @@
+load("//aos/flatbuffers:generate.bzl", "static_flatbuffer")
+
package(default_visibility = ["//visibility:public"])
+static_flatbuffer(
+ name = "constants_fbs",
+ srcs = ["constants.fbs"],
+)
+
cc_library(
name = "averager",
hdrs = [
diff --git a/frc971/zeroing/absolute_and_absolute_encoder_test.cc b/frc971/zeroing/absolute_and_absolute_encoder_test.cc
index b28f637..5dac38b 100644
--- a/frc971/zeroing/absolute_and_absolute_encoder_test.cc
+++ b/frc971/zeroing/absolute_and_absolute_encoder_test.cc
@@ -42,6 +42,7 @@
0.4 * single_turn_distance_per_revolution;
AbsoluteAndAbsoluteEncoderZeroingConstants constants{
+ {},
kSampleSize,
distance_per_revolution,
measured_absolute_position,
@@ -89,6 +90,7 @@
0.4 * single_turn_distance_per_revolution;
AbsoluteAndAbsoluteEncoderZeroingConstants constants{
+ {},
kSampleSize,
distance_per_revolution,
measured_absolute_position,
@@ -144,6 +146,7 @@
0.4 * single_turn_distance_per_revolution;
AbsoluteAndAbsoluteEncoderZeroingConstants constants{
+ {},
kSampleSize,
distance_per_revolution,
measured_absolute_position,
@@ -175,9 +178,16 @@
// Makes sure we detect an error if the ZeroingEstimator gets sent a NaN.
TEST_F(AbsoluteAndAbsoluteEncoderZeroingTest,
TestAbsoluteAndAbsoluteEncoderZeroingWithNaN) {
- AbsoluteAndAbsoluteEncoderZeroingConstants constants{
- kSampleSize, 1, 0.3, 1, 0.3, 2.5, 0.1, kMovingBufferSize,
- kIndexErrorFraction};
+ AbsoluteAndAbsoluteEncoderZeroingConstants constants{{},
+ kSampleSize,
+ 1,
+ 0.3,
+ 1,
+ 0.3,
+ 2.5,
+ 0.1,
+ kMovingBufferSize,
+ kIndexErrorFraction};
AbsoluteAndAbsoluteEncoderZeroingEstimator estimator(constants);
@@ -215,6 +225,7 @@
0.4 * single_turn_distance_per_revolution;
AbsoluteAndAbsoluteEncoderZeroingConstants constants{
+ {},
kSampleSize,
distance_per_revolution,
measured_absolute_position,
@@ -276,6 +287,7 @@
0.4 * single_turn_distance_per_revolution;
AbsoluteAndAbsoluteEncoderZeroingConstants constants{
+ {},
kSampleSize,
distance_per_revolution,
measured_absolute_position,
diff --git a/frc971/zeroing/absolute_encoder_test.cc b/frc971/zeroing/absolute_encoder_test.cc
index fdc1fa0..6e78faa 100644
--- a/frc971/zeroing/absolute_encoder_test.cc
+++ b/frc971/zeroing/absolute_encoder_test.cc
@@ -31,10 +31,14 @@
const double start_pos = 2.1;
double measured_absolute_position = 0.3 * index_diff;
- AbsoluteEncoderZeroingConstants constants{
- kSampleSize, index_diff, measured_absolute_position,
- kMiddlePosition, 0.1, kMovingBufferSize,
- kIndexErrorFraction};
+ AbsoluteEncoderZeroingConstants constants{{},
+ kSampleSize,
+ index_diff,
+ measured_absolute_position,
+ kMiddlePosition,
+ 0.1,
+ kMovingBufferSize,
+ kIndexErrorFraction};
sim.Initialize(start_pos, index_diff / 3.0, 0.0,
constants.measured_absolute_position);
@@ -61,10 +65,14 @@
double measured_absolute_position = 0.3 * index_diff;
const double kMiddlePosition = 2.5;
- AbsoluteEncoderZeroingConstants constants{
- kSampleSize, index_diff, measured_absolute_position,
- kMiddlePosition, 0.1, kMovingBufferSize,
- kIndexErrorFraction};
+ AbsoluteEncoderZeroingConstants constants{{},
+ kSampleSize,
+ index_diff,
+ measured_absolute_position,
+ kMiddlePosition,
+ 0.1,
+ kMovingBufferSize,
+ kIndexErrorFraction};
sim.Initialize(start_pos, index_diff / 3.0, 0.0,
constants.measured_absolute_position);
@@ -100,10 +108,14 @@
double measured_absolute_position = 0.3 * index_diff;
const double kMiddlePosition = 2.5;
- AbsoluteEncoderZeroingConstants constants{
- kSampleSize, index_diff, measured_absolute_position,
- kMiddlePosition, 0.1, kMovingBufferSize,
- kIndexErrorFraction};
+ AbsoluteEncoderZeroingConstants constants{{},
+ kSampleSize,
+ index_diff,
+ measured_absolute_position,
+ kMiddlePosition,
+ 0.1,
+ kMovingBufferSize,
+ kIndexErrorFraction};
sim.Initialize(start_pos, index_diff / 3.0, 0.0,
constants.measured_absolute_position);
@@ -122,8 +134,14 @@
// Makes sure we detect an error if the ZeroingEstimator gets sent a NaN.
TEST_F(AbsoluteEncoderZeroingTest, TestAbsoluteEncoderZeroingWithNaN) {
- AbsoluteEncoderZeroingConstants constants{
- kSampleSize, 1, 0.3, 1.0, 0.1, kMovingBufferSize, kIndexErrorFraction};
+ AbsoluteEncoderZeroingConstants constants{{},
+ kSampleSize,
+ 1,
+ 0.3,
+ 1.0,
+ 0.1,
+ kMovingBufferSize,
+ kIndexErrorFraction};
AbsoluteEncoderZeroingEstimator estimator(constants);
diff --git a/frc971/zeroing/constants.fbs b/frc971/zeroing/constants.fbs
new file mode 100644
index 0000000..63e9136
--- /dev/null
+++ b/frc971/zeroing/constants.fbs
@@ -0,0 +1,135 @@
+namespace frc971.zeroing;
+
+table HallEffectZeroingConstants {
+ // The absolute position of the lower edge of the hall effect sensor.
+ lower_hall_position:double (id: 0);
+ // The absolute position of the upper edge of the hall effect sensor.
+ upper_hall_position:double (id: 1);
+ // The difference in scaled units between two hall effect edges. This is the
+ // number of units/cycle.
+ index_difference:double (id: 2);
+ // Number of cycles we need to see the hall effect high.
+ hall_trigger_zeroing_length:uint64 (id: 3);
+ // Direction the system must be moving in order to zero. True is positive,
+ // False is negative direction.
+ zeroing_move_direction:bool (id: 4);
+}
+
+table PotAndIndexPulseZeroingConstants {
+ // The number of samples in the moving average filter.
+ average_filter_size:uint64 (id: 0);
+ // The difference in scaled units between two index pulses.
+ index_difference:double (id: 1);
+ // The absolute position in scaled units of one of the index pulses.
+ measured_index_position:double (id: 2);
+ // Value between 0 and .5 which determines a fraction of the index_diff
+ // you want to use.
+ allowable_encoder_error:double (id: 3);
+}
+
+table EncoderPlusIndexZeroingConstants {
+ // The amount of index pulses in the joint's range of motion.
+ index_pulse_count:int (id: 0);
+ // The difference in scaled units between two index pulses.
+ index_difference:double (id: 1);
+ // The absolute position in scaled units of one of the index pulses.
+ measured_index_position:double (id: 2);
+ // The index pulse that is known, going from lowest in the range of motion to
+ // highest (Starting at 0).
+ known_index_pulse:int (id: 3);
+ // Value between 0 and 0.5 which determines a fraction of the index_diff
+ // you want to use. If an index pulse deviates by more than this amount from
+ // where we expect to see one then we flag an error.
+ allowable_encoder_error:double (id: 4);
+}
+
+table PotAndAbsoluteEncoderZeroingConstants {
+ // The number of samples in the moving average filter.
+ average_filter_size:uint64 (id: 0);
+ // The distance that the absolute encoder needs to complete a full rotation.
+ one_revolution_distance:double (id: 1);
+ // Measured absolute position of the encoder when at zero.
+ measured_absolute_position:double (id: 2);
+
+ // Treshold for deciding if we are moving. moving_buffer_size samples need to
+ // be within this distance of each other before we use the middle one to zero.
+ zeroing_threshold:double (id: 3);
+ // Buffer size for deciding if we are moving.
+ moving_buffer_size:uint64 (id: 4);
+
+ // Value between 0 and 1 indicating what fraction of one_revolution_distance
+ // it is acceptable for the offset to move.
+ allowable_encoder_error:double (id: 5);
+}
+
+table RelativeEncoderZeroingConstants {}
+
+table ContinuousAbsoluteEncoderZeroingConstants {
+ // The number of samples in the moving average filter.
+ average_filter_size:uint64 (id: 0);
+ // The distance that the absolute encoder needs to complete a full rotation.
+ // It is presumed that this will always be 2 * pi for any subsystem using this
+ // class, unless you have a continuous system that for some reason doesn't
+ // have a logical period of 1 revolution in radians.
+ one_revolution_distance:double (id: 1);
+ // Measured absolute position of the encoder when at zero.
+ measured_absolute_position:double (id: 2);
+
+ // Threshold for deciding if we are moving. moving_buffer_size samples need to
+ // be within this distance of each other before we use the middle one to zero.
+ zeroing_threshold:double (id: 3);
+ // Buffer size for deciding if we are moving.
+ moving_buffer_size:uint64 (id: 4);
+
+ // Value between 0 and 1 indicating what fraction of a revolution
+ // it is acceptable for the offset to move.
+ allowable_encoder_error:double (id: 5);
+}
+
+table AbsoluteEncoderZeroingConstants {
+ // The number of samples in the moving average filter.
+ average_filter_size:uint64 (id: 0);
+ // The distance that the absolute encoder needs to complete a full rotation.
+ one_revolution_distance:double (id: 1);
+ // Measured absolute position of the encoder when at zero.
+ measured_absolute_position:double (id: 2);
+ // Position of the middle of the range of motion in output coordinates.
+ middle_position:double (id: 3);
+
+ // Threshold for deciding if we are moving. moving_buffer_size samples need to
+ // be within this distance of each other before we use the middle one to zero.
+ zeroing_threshold:double (id: 4);
+ // Buffer size for deciding if we are moving.
+ moving_buffer_size:uint64 (id: 5);
+
+ // Value between 0 and 1 indicating what fraction of one_revolution_distance
+ // it is acceptable for the offset to move.
+ allowable_encoder_error:double (id: 6);
+}
+
+table AbsoluteAndAbsoluteEncoderZeroingConstants {
+ // The number of samples in the moving average filter.
+ average_filter_size:uint64 (id: 0);
+ // The distance that the absolute encoder needs to complete a full rotation.
+ one_revolution_distance:double (id: 1);
+ // Measured absolute position of the encoder when at zero.
+ measured_absolute_position:double (id: 2);
+
+ // The distance that the single turn absolute encoder needs to complete a full
+ // rotation.
+ single_turn_one_revolution_distance:double (id: 3);
+ // Measured absolute position of the single turn encoder when at zero.
+ single_turn_measured_absolute_position:double (id: 4);
+ // Position of the middle of the range of motion in output coordinates.
+ single_turn_middle_position:double (id: 5);
+
+ // Threshold for deciding if we are moving. moving_buffer_size samples need to
+ // be within this distance of each other before we use the middle one to zero.
+ zeroing_threshold:double (id: 6);
+ // Buffer size for deciding if we are moving.
+ moving_buffer_size:uint64 (id: 7);
+
+ // Value between 0 and 1 indicating what fraction of one_revolution_distance
+ // it is acceptable for the offset to move.
+ allowable_encoder_error:double (id: 8);
+}
diff --git a/frc971/zeroing/continuous_absolute_encoder_test.cc b/frc971/zeroing/continuous_absolute_encoder_test.cc
index 3869393..75d6de0 100644
--- a/frc971/zeroing/continuous_absolute_encoder_test.cc
+++ b/frc971/zeroing/continuous_absolute_encoder_test.cc
@@ -34,8 +34,8 @@
double measured_absolute_position = 0.3 * index_diff;
ContinuousAbsoluteEncoderZeroingConstants constants{
- kSampleSize, index_diff, measured_absolute_position,
- 0.1, kMovingBufferSize, kIndexErrorFraction};
+ {}, kSampleSize, index_diff, measured_absolute_position,
+ 0.1, kMovingBufferSize, kIndexErrorFraction};
sim.Initialize(start_pos, index_diff / 3.0, 0.0,
constants.measured_absolute_position);
@@ -65,8 +65,8 @@
double measured_absolute_position = 0.3 * index_diff;
ContinuousAbsoluteEncoderZeroingConstants constants{
- kSampleSize, index_diff, measured_absolute_position,
- 0.1, kMovingBufferSize, kIndexErrorFraction};
+ {}, kSampleSize, index_diff, measured_absolute_position,
+ 0.1, kMovingBufferSize, kIndexErrorFraction};
sim.Initialize(start_pos, index_diff / 3.0, 0.0,
constants.measured_absolute_position);
@@ -103,8 +103,8 @@
double measured_absolute_position = 0.3 * index_diff;
ContinuousAbsoluteEncoderZeroingConstants constants{
- kSampleSize, index_diff, measured_absolute_position,
- 0.1, kMovingBufferSize, kIndexErrorFraction};
+ {}, kSampleSize, index_diff, measured_absolute_position,
+ 0.1, kMovingBufferSize, kIndexErrorFraction};
sim.Initialize(start_pos, index_diff / 3.0, 0.0,
constants.measured_absolute_position);
@@ -143,8 +143,8 @@
double measured_absolute_position = 0.3 * index_diff;
ContinuousAbsoluteEncoderZeroingConstants constants{
- kSampleSize, index_diff, measured_absolute_position,
- 0.1, kMovingBufferSize, kIndexErrorFraction};
+ {}, kSampleSize, index_diff, measured_absolute_position,
+ 0.1, kMovingBufferSize, kIndexErrorFraction};
sim.Initialize(start_pos, index_diff / 3.0, 0.0,
constants.measured_absolute_position);
@@ -165,7 +165,7 @@
TEST_F(ContinuousAbsoluteEncoderZeroingTest,
TestContinuousAbsoluteEncoderZeroingWithNaN) {
ContinuousAbsoluteEncoderZeroingConstants constants{
- kSampleSize, 1, 0.3, 0.1, kMovingBufferSize, kIndexErrorFraction};
+ {}, kSampleSize, 1, 0.3, 0.1, kMovingBufferSize, kIndexErrorFraction};
ContinuousAbsoluteEncoderZeroingEstimator estimator(constants);
diff --git a/frc971/zeroing/pot_and_absolute_encoder_test.cc b/frc971/zeroing/pot_and_absolute_encoder_test.cc
index 5c05cc1..4cd3821 100644
--- a/frc971/zeroing/pot_and_absolute_encoder_test.cc
+++ b/frc971/zeroing/pot_and_absolute_encoder_test.cc
@@ -33,8 +33,8 @@
double measured_absolute_position = 0.3 * index_diff;
PotAndAbsoluteEncoderZeroingConstants constants{
- kSampleSize, index_diff, measured_absolute_position,
- 0.1, kMovingBufferSize, kIndexErrorFraction};
+ {}, kSampleSize, index_diff, measured_absolute_position,
+ 0.1, kMovingBufferSize, kIndexErrorFraction};
sim.Initialize(start_pos, index_diff / 3.0, 0.0,
constants.measured_absolute_position);
@@ -62,8 +62,8 @@
double measured_absolute_position = 0.3 * index_diff;
PotAndAbsoluteEncoderZeroingConstants constants{
- kSampleSize, index_diff, measured_absolute_position,
- 0.1, kMovingBufferSize, kIndexErrorFraction};
+ {}, kSampleSize, index_diff, measured_absolute_position,
+ 0.1, kMovingBufferSize, kIndexErrorFraction};
sim.Initialize(start_pos, index_diff / 3.0, 0.0,
constants.measured_absolute_position);
@@ -99,8 +99,8 @@
double measured_absolute_position = 0.3 * index_diff;
PotAndAbsoluteEncoderZeroingConstants constants{
- kSampleSize, index_diff, measured_absolute_position,
- 0.1, kMovingBufferSize, kIndexErrorFraction};
+ {}, kSampleSize, index_diff, measured_absolute_position,
+ 0.1, kMovingBufferSize, kIndexErrorFraction};
sim.Initialize(start_pos, index_diff / 3.0, 0.0,
constants.measured_absolute_position);
@@ -121,7 +121,7 @@
TEST_F(PotAndAbsoluteEncoderZeroingTest,
TestPotAndAbsoluteEncoderZeroingWithNaN) {
PotAndAbsoluteEncoderZeroingConstants constants{
- kSampleSize, 1, 0.3, 0.1, kMovingBufferSize, kIndexErrorFraction};
+ {}, kSampleSize, 1, 0.3, 0.1, kMovingBufferSize, kIndexErrorFraction};
PotAndAbsoluteEncoderZeroingEstimator estimator(constants);
diff --git a/frc971/zeroing/pot_and_index_test.cc b/frc971/zeroing/pot_and_index_test.cc
index b2b00ce..f2e464f 100644
--- a/frc971/zeroing/pot_and_index_test.cc
+++ b/frc971/zeroing/pot_and_index_test.cc
@@ -27,7 +27,7 @@
PositionSensorSimulator sim(index_diff);
sim.Initialize(3.6 * index_diff, index_diff / 3.0);
PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{
- kSampleSize, index_diff, 0.0, kIndexErrorFraction});
+ {}, kSampleSize, index_diff, 0.0, kIndexErrorFraction});
// The zeroing code is supposed to perform some filtering on the difference
// between the potentiometer value and the encoder value. We assume that 300
@@ -51,7 +51,7 @@
PositionSensorSimulator sim(index_diff);
sim.Initialize(position, index_diff / 3.0);
PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{
- kSampleSize, index_diff, 0.0, kIndexErrorFraction});
+ {}, kSampleSize, index_diff, 0.0, kIndexErrorFraction});
// Make sure that the zeroing code does not consider itself zeroed until we
// collect a good amount of samples. In this case we're waiting until the
@@ -70,7 +70,7 @@
PositionSensorSimulator sim(index_diff);
sim.Initialize(3.6, index_diff / 3.0);
PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{
- kSampleSize, index_diff, 0.0, kIndexErrorFraction});
+ {}, kSampleSize, index_diff, 0.0, kIndexErrorFraction});
// The zeroing code is supposed to perform some filtering on the difference
// between the potentiometer value and the encoder value. We assume that 300
@@ -104,7 +104,7 @@
PositionSensorSimulator sim(index_diff);
sim.Initialize(3.5 * index_diff, index_diff / 3.0);
PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{
- kSampleSize, index_diff, 0.0, kIndexErrorFraction});
+ {}, kSampleSize, index_diff, 0.0, kIndexErrorFraction});
// The zeroing code is supposed to perform some filtering on the difference
// between the potentiometer value and the encoder value. We assume that 300
@@ -138,7 +138,7 @@
PositionSensorSimulator sim(index_diff);
sim.Initialize(3.5 * index_diff, index_diff / 3.0);
PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{
- kSampleSize, index_diff, 0.0, kIndexErrorFraction});
+ {}, kSampleSize, index_diff, 0.0, kIndexErrorFraction});
for (unsigned int i = 0; i < kSampleSize / 2; i++) {
MoveTo(&sim, &estimator, 3.5 * index_diff);
@@ -158,7 +158,7 @@
PositionSensorSimulator sim(index_diff);
sim.Initialize(3.1 * index_diff, index_diff / 3.0);
PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{
- kSampleSize, index_diff, 0.0, kIndexErrorFraction});
+ {}, kSampleSize, index_diff, 0.0, kIndexErrorFraction});
MoveTo(&sim, &estimator, 3.1 * index_diff);
@@ -174,7 +174,7 @@
PositionSensorSimulator sim(index_diff);
sim.Initialize(3.1 * index_diff, index_diff / 3.0);
PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{
- kSampleSize, index_diff, 0.0, kIndexErrorFraction});
+ {}, kSampleSize, index_diff, 0.0, kIndexErrorFraction});
// Make sure to fill up the averaging filter with samples.
for (unsigned int i = 0; i < kSampleSize; i++) {
@@ -209,7 +209,7 @@
PositionSensorSimulator sim(index_diff);
sim.Initialize(3.3 * index_diff, index_diff / 3.0, known_index_pos);
PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{
- kSampleSize, index_diff, known_index_pos, kIndexErrorFraction});
+ {}, kSampleSize, index_diff, known_index_pos, kIndexErrorFraction});
// Make sure to fill up the averaging filter with samples.
for (unsigned int i = 0; i < kSampleSize; i++) {
@@ -234,7 +234,7 @@
TEST_F(PotAndIndexZeroingTest, BasicErrorAPITest) {
const double index_diff = 1.0;
PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{
- kSampleSize, index_diff, 0.0, kIndexErrorFraction});
+ {}, kSampleSize, index_diff, 0.0, kIndexErrorFraction});
PositionSensorSimulator sim(index_diff);
sim.Initialize(1.5 * index_diff, index_diff / 3.0, 0.0);
@@ -262,7 +262,7 @@
PositionSensorSimulator sim(index_diff);
sim.Initialize(10 * index_diff, index_diff / 3.0, known_index_pos);
PotAndIndexPulseZeroingEstimator estimator(PotAndIndexPulseZeroingConstants{
- sample_size, index_diff, known_index_pos, kIndexErrorFraction});
+ {}, sample_size, index_diff, known_index_pos, kIndexErrorFraction});
for (size_t i = 0; i < sample_size; i++) {
MoveTo(&sim, &estimator, 13 * index_diff);