blob: 084c9a71a5602f330ba4a4dc55bc49400aca95c3 [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
Alex Perrycb7da4b2019-08-28 19:35:56 -07006#include "frc971/control_loops/control_loops_generated.h"
Brian Silvermane0a95462014-02-17 00:41:09 -08007
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
Alex Perrycb7da4b2019-08-28 19:35:56 -070030 void Update(const HallEffectStruct *position) {
Ben Fredricksoneaecbbb2014-02-23 12:12:03 +000031 last_value_ = value_;
Alex Perrycb7da4b2019-08-28 19:35:56 -070032 value_ = position->current();
33 posedge_value_ = position->posedge_value();
34 negedge_value_ = position->negedge_value();
35 posedges_.update(position->posedge_count());
36 negedges_.update(position->negedge_count());
Brian Silvermane0a95462014-02-17 00:41:09 -080037 }
38
Alex Perrycb7da4b2019-08-28 19:35:56 -070039 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();
44 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_