blob: 63d6d193fc6fa7aaf476476b5005c82c82db08f1 [file] [log] [blame]
Brian Silverman8d3816a2017-07-03 18:52:15 -07001#ifndef MOTORS_ALGORITHMS_H_
2#define MOTORS_ALGORITHMS_H_
3
4#include <stdint.h>
5
6namespace frc971 {
Brian Silvermana96c1a42018-05-12 12:11:31 -07007namespace motors {
Brian Silverman8d3816a2017-07-03 18:52:15 -07008
9struct 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
20struct 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.
27BalancedReadings BalanceReadings(ReadingsToBalance to_balance);
28
Brian Silverman6260c092018-01-14 15:21:36 -080029inline 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 Silvermana96c1a42018-05-12 12:11:31 -070042} // namespace motors
Brian Silverman8d3816a2017-07-03 18:52:15 -070043} // namespace frc971
44
45#endif // MOTORS_ALGORITHMS_H_