Austin Schuh | c6423e6 | 2017-02-11 16:56:30 -0800 | [diff] [blame] | 1 | #ifndef FRC971_ZEROING_WRAP_H_ |
| 2 | #define FRC971_ZEROING_WRAP_H_ |
| 3 | |
| 4 | namespace frc971 { |
| 5 | namespace zeroing { |
| 6 | |
Stephan Massalt | 4d1e74f | 2020-01-11 17:50:39 -0800 | [diff] [blame] | 7 | // UnwrapSensor takes in a sensor value from a sensor that loops in a certain |
| 8 | // interval. ex(the sensor moves from 0 to 10 and back to 0 while moving the |
| 9 | // same direction) By checking for big gaps in sensor readings it assumes you |
| 10 | // have wrapped either back or forwards and handles accordingly. It returns the |
| 11 | // overall sensor value. |
| 12 | |
| 13 | class UnwrapSensor { |
| 14 | public: |
| 15 | // The sensor_offset (+ or -) present the sensor value that is 'zero' |
| 16 | // The sensor_range presents the absolute value of the sensor range from 0 to |
| 17 | // sensor_range. This will be adjusted using the sensor_offset |
| 18 | UnwrapSensor(double sensor_offset, double sensor_range); |
| 19 | |
| 20 | // Takes a wrapped sensor value and unwraps it to give you its total position. |
| 21 | double Unwrap(double current_sensor_value); |
| 22 | |
| 23 | void Reset(); |
| 24 | |
| 25 | int sensor_wrapped() const { return wrap_count_; } |
| 26 | |
| 27 | private: |
| 28 | const double sensor_offset_, sensor_range_; |
| 29 | |
| 30 | // The last value given from set_position, starts at offset |
| 31 | double sensor_last_value_ = sensor_offset_; |
| 32 | |
| 33 | // Log if sensor is in wrapped state in either direction |
| 34 | int wrap_count_ = 0; |
| 35 | |
| 36 | // function waits for first call with a value to set sensor_last_value_. Will |
| 37 | // start to calculate the spring unwrap at the second function call. |
| 38 | bool uninitialized_ = true; |
| 39 | }; |
| 40 | |
Austin Schuh | c6423e6 | 2017-02-11 16:56:30 -0800 | [diff] [blame] | 41 | // Returns a modified value which has been wrapped such that it is +- period/2 |
| 42 | // away from nearest. |
| 43 | double Wrap(double nearest, double value, double period); |
Austin Schuh | 4fae0fc | 2018-03-27 23:51:42 -0700 | [diff] [blame] | 44 | float Wrap(float nearest, float value, float period); |
Austin Schuh | c6423e6 | 2017-02-11 16:56:30 -0800 | [diff] [blame] | 45 | |
Austin Schuh | d82068e | 2019-01-26 20:05:42 -0800 | [diff] [blame] | 46 | inline double UnWrap(double nearest, double value, double period) { |
| 47 | return Wrap(nearest, value, period); |
| 48 | } |
| 49 | inline float UnWrap(float nearest, float value, float period) { |
| 50 | return Wrap(nearest, value, period); |
| 51 | } |
| 52 | |
Austin Schuh | c6423e6 | 2017-02-11 16:56:30 -0800 | [diff] [blame] | 53 | } // namespace zeroing |
| 54 | } // namespace frc971 |
| 55 | |
| 56 | #endif // FRC971_ZEROING_WRAP_H_ |