Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 1 | #ifndef MOTORS_ALGORITHMS_H_ |
| 2 | #define MOTORS_ALGORITHMS_H_ |
| 3 | |
| 4 | #include <stdint.h> |
| 5 | |
| 6 | namespace frc971 { |
Brian Silverman | a96c1a4 | 2018-05-12 12:11:31 -0700 | [diff] [blame] | 7 | namespace motors { |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 8 | |
| 9 | struct ReadingsToBalance { |
| 10 | // Adds a single reading at index. |
| 11 | void Add(int index, int32_t value) { |
| 12 | sums[index] += value; |
| 13 | ++weights[index]; |
| 14 | } |
| 15 | |
| 16 | int32_t sums[3]; |
| 17 | int weights[3]; |
| 18 | }; |
| 19 | |
| 20 | struct BalancedReadings { |
| 21 | float readings[3]; |
| 22 | }; |
| 23 | |
| 24 | // Returns three readings which add up to 0 and are the same distances apart as |
| 25 | // the input ones (by weight). The distances between the averages of the inputs |
| 26 | // and the corresponding outputs will be inversely proportional to the weights. |
| 27 | BalancedReadings BalanceReadings(ReadingsToBalance to_balance); |
| 28 | |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 29 | inline BalancedReadings BalanceSimpleReadings(const uint16_t readings[3]) { |
| 30 | float offset = 0; |
| 31 | for (int i = 0; i < 3; ++i) { |
| 32 | offset += static_cast<float>(readings[i]); |
| 33 | } |
| 34 | |
| 35 | BalancedReadings r; |
| 36 | for (int i = 0; i < 3; ++i) { |
| 37 | r.readings[i] = static_cast<float>(readings[i]) + offset / -3; |
| 38 | } |
| 39 | return r; |
| 40 | } |
| 41 | |
Brian Silverman | a96c1a4 | 2018-05-12 12:11:31 -0700 | [diff] [blame] | 42 | } // namespace motors |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 43 | } // namespace frc971 |
| 44 | |
| 45 | #endif // MOTORS_ALGORITHMS_H_ |