blob: d1b6efe11c8f1779b76b9d32f24283a256b51974 [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
Brian Silvermane4c701d2014-04-10 19:29:25 -070010// TODO(aschuh): Can we filter for 2 cycles instead of just 1?
Brian Silvermane0a95462014-02-17 00:41:09 -080011class HallEffectTracker {
12 public:
13 int32_t get_posedges() const { return posedges_.count(); }
14 int32_t get_negedges() const { return negedges_.count(); }
15
16 bool either_count_changed() const {
17 return posedges_.count_changed() || negedges_.count_changed();
18 }
19 bool posedge_count_changed() const { return posedges_.count_changed(); }
20 bool negedge_count_changed() const { return negedges_.count_changed(); }
21
22 bool value() const { return value_; }
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +000023 bool last_value() const { return last_value_; }
Brian Silvermand3efb182015-05-13 23:04:29 -040024 bool is_posedge() const { return value() && !last_value(); }
25 bool is_negedge() const { return !value() && last_value(); }
26
27 double posedge_value() const { return posedge_value_; }
28 double negedge_value() const { return negedge_value_; }
Brian Silvermane0a95462014-02-17 00:41:09 -080029
30 void Update(const HallEffectStruct &position) {
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +000031 last_value_ = value_;
Brian Silvermane0a95462014-02-17 00:41:09 -080032 value_ = position.current;
Brian Silvermand3efb182015-05-13 23:04:29 -040033 posedge_value_ = position.posedge_value;
34 negedge_value_ = position.negedge_value;
Brian Silvermane0a95462014-02-17 00:41:09 -080035 posedges_.update(position.posedge_count);
36 negedges_.update(position.negedge_count);
37 }
38
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +000039 void Reset(const HallEffectStruct &position) {
40 posedges_.Reset(position.posedge_count);
41 negedges_.Reset(position.negedge_count);
42 value_ = position.current;
43 last_value_ = position.current;
Brian Silvermand3efb182015-05-13 23:04:29 -040044 posedge_value_ = position.posedge_value;
45 negedge_value_ = position.negedge_value;
Austin Schuhf84a1302014-02-19 00:23:30 -080046 }
47
Brian Silvermane0a95462014-02-17 00:41:09 -080048 private:
49 class {
50 public:
51 void update(int32_t count) {
52 previous_count_ = count_;
53 count_ = count;
54 }
55
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +000056 void Reset(int32_t count) { count_ = count; }
Austin Schuhf84a1302014-02-19 00:23:30 -080057
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +000058 bool count_changed() const { return previous_count_ != count_; }
Brian Silvermane0a95462014-02-17 00:41:09 -080059
60 int32_t count() const { return count_; }
61
62 private:
63 int32_t count_ = 0;
64 int32_t previous_count_ = 0;
Brian Silvermane0a95462014-02-17 00:41:09 -080065 } posedges_, negedges_;
Brian Silvermand3efb182015-05-13 23:04:29 -040066
Brian Silvermane0a95462014-02-17 00:41:09 -080067 bool value_ = false;
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +000068 bool last_value_ = false;
Brian Silvermand3efb182015-05-13 23:04:29 -040069
70 double posedge_value_ = 0, negedge_value_ = 0;
Brian Silvermane0a95462014-02-17 00:41:09 -080071};
72
73} // namespace frc971
74
75#endif // FRC971_CONTROL_LOOPS_HALL_EFFECT_H_