blob: f96b9fbe9271c0295911431e39496d9d5363048f [file] [log] [blame]
Austin Schuhc6423e62017-02-11 16:56:30 -08001#ifndef FRC971_ZEROING_WRAP_H_
2#define FRC971_ZEROING_WRAP_H_
3
4namespace frc971 {
5namespace zeroing {
6
Stephan Massalt4d1e74f2020-01-11 17:50:39 -08007// 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
13class 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 Schuhc6423e62017-02-11 16:56:30 -080041// Returns a modified value which has been wrapped such that it is +- period/2
42// away from nearest.
43double Wrap(double nearest, double value, double period);
Austin Schuh4fae0fc2018-03-27 23:51:42 -070044float Wrap(float nearest, float value, float period);
Austin Schuhc6423e62017-02-11 16:56:30 -080045
Austin Schuhd82068e2019-01-26 20:05:42 -080046inline double UnWrap(double nearest, double value, double period) {
47 return Wrap(nearest, value, period);
48}
49inline float UnWrap(float nearest, float value, float period) {
50 return Wrap(nearest, value, period);
51}
52
Austin Schuhc6423e62017-02-11 16:56:30 -080053} // namespace zeroing
54} // namespace frc971
55
56#endif // FRC971_ZEROING_WRAP_H_