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