blob: 4e3981e0ccd859436a0cd5a6aef7309b5f854ddb [file] [log] [blame]
Brian Silvermane0a95462014-02-17 00:41:09 -08001#ifndef FRC971_CONTROL_LOOPS_HALL_EFFECT_H_
2#define FRC971_CONTROL_LOOPS_HALL_EFFECT_H_
3
4#include <stdint.h>
5
6#include "frc971/control_loops/control_loops.q.h"
7
8namespace frc971 {
9
Austin Schuhf84a1302014-02-19 00:23:30 -080010// TODO(brians): Have a Reset() for when the cape resets.
Brian Silvermane4c701d2014-04-10 19:29:25 -070011// TODO(aschuh): Can we filter for 2 cycles instead of just 1?
Brian Silvermane0a95462014-02-17 00:41:09 -080012class HallEffectTracker {
13 public:
14 int32_t get_posedges() const { return posedges_.count(); }
15 int32_t get_negedges() const { return negedges_.count(); }
16
17 bool either_count_changed() const {
18 return posedges_.count_changed() || negedges_.count_changed();
19 }
20 bool posedge_count_changed() const { return posedges_.count_changed(); }
21 bool negedge_count_changed() const { return negedges_.count_changed(); }
22
23 bool value() const { return value_; }
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +000024 bool last_value() const { return last_value_; }
Brian Silvermane0a95462014-02-17 00:41:09 -080025
26 void Update(const HallEffectStruct &position) {
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +000027 last_value_ = value_;
Brian Silvermane0a95462014-02-17 00:41:09 -080028 value_ = position.current;
29 posedges_.update(position.posedge_count);
30 negedges_.update(position.negedge_count);
31 }
32
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +000033 void Reset(const HallEffectStruct &position) {
34 posedges_.Reset(position.posedge_count);
35 negedges_.Reset(position.negedge_count);
36 value_ = position.current;
37 last_value_ = position.current;
Austin Schuhf84a1302014-02-19 00:23:30 -080038 }
39
Brian Silvermane0a95462014-02-17 00:41:09 -080040 private:
41 class {
42 public:
43 void update(int32_t count) {
44 previous_count_ = count_;
45 count_ = count;
46 }
47
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +000048 void Reset(int32_t count) { count_ = count; }
Austin Schuhf84a1302014-02-19 00:23:30 -080049
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +000050 bool count_changed() const { return previous_count_ != count_; }
Brian Silvermane0a95462014-02-17 00:41:09 -080051
52 int32_t count() const { return count_; }
53
54 private:
55 int32_t count_ = 0;
56 int32_t previous_count_ = 0;
Brian Silvermane0a95462014-02-17 00:41:09 -080057 } posedges_, negedges_;
58 bool value_ = false;
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +000059 bool last_value_ = false;
Brian Silvermane0a95462014-02-17 00:41:09 -080060};
61
62} // namespace frc971
63
64#endif // FRC971_CONTROL_LOOPS_HALL_EFFECT_H_