Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 1 | #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 | |
| 8 | namespace frc971 { |
| 9 | |
Austin Schuh | f84a130 | 2014-02-19 00:23:30 -0800 | [diff] [blame] | 10 | // TODO(brians): Have a Reset() for when the cape resets. |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 11 | class 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_; } |
| 23 | |
| 24 | void Update(const HallEffectStruct &position) { |
| 25 | value_ = position.current; |
| 26 | posedges_.update(position.posedge_count); |
| 27 | negedges_.update(position.negedge_count); |
| 28 | } |
| 29 | |
Austin Schuh | f84a130 | 2014-02-19 00:23:30 -0800 | [diff] [blame] | 30 | void Reset() { |
| 31 | posedges_.Reset(); |
| 32 | negedges_.Reset(); |
| 33 | } |
| 34 | |
| 35 | bool ready() { return posedges_.ready() && negedges_.ready(); } |
| 36 | |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 37 | private: |
| 38 | class { |
| 39 | public: |
| 40 | void update(int32_t count) { |
Austin Schuh | f84a130 | 2014-02-19 00:23:30 -0800 | [diff] [blame] | 41 | if (first_) { |
| 42 | count_ = count; |
| 43 | LOG(DEBUG, "First time through the hall effect, resetting\n"); |
| 44 | } |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 45 | previous_count_ = count_; |
| 46 | count_ = count; |
Austin Schuh | f84a130 | 2014-02-19 00:23:30 -0800 | [diff] [blame] | 47 | first_ = false; |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Austin Schuh | f84a130 | 2014-02-19 00:23:30 -0800 | [diff] [blame] | 50 | void Reset() { first_ = true; } |
| 51 | |
| 52 | bool count_changed() const { return !first_ && previous_count_ != count_; } |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 53 | |
| 54 | int32_t count() const { return count_; } |
| 55 | |
Austin Schuh | f84a130 | 2014-02-19 00:23:30 -0800 | [diff] [blame] | 56 | bool ready() { return !first_; } |
| 57 | |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 58 | private: |
| 59 | int32_t count_ = 0; |
| 60 | int32_t previous_count_ = 0; |
Austin Schuh | f84a130 | 2014-02-19 00:23:30 -0800 | [diff] [blame] | 61 | bool first_ = true; |
Brian Silverman | e0a9546 | 2014-02-17 00:41:09 -0800 | [diff] [blame] | 62 | } posedges_, negedges_; |
| 63 | bool value_ = false; |
| 64 | }; |
| 65 | |
| 66 | } // namespace frc971 |
| 67 | |
| 68 | #endif // FRC971_CONTROL_LOOPS_HALL_EFFECT_H_ |