blob: 53c45dce540241501eaec04de2fd3794cb0b009e [file] [log] [blame]
James Kuszmaulb5e497c2013-03-02 15:14:13 -08001#ifndef FRC971_CONTROL_LOOPS_HALL_EFFECT_H_
2#define FRC971_CONTROL_LOOPS_HALL_EFFECT_H_
3
4#include <memory>
5#include <array>
6
7#include "aos/common/control_loop/ControlLoop.h"
8#include "frc971/control_loops/state_feedback_loop.h"
9
10namespace frc971 {
11namespace control_loops {
12
13// A parent class for creating things such as the Angle Adjust and Wrist
14// which zero to some n number of hall effect sensors.
15// kNumHallEffect is the number of hall effect sensors being used.
16template <int kNumHallEffect>
17class HallEffectLoop {
18 public:
19 // zero_down refers to whether the device should zero by traveling
20 // down or up. max_zeroing_voltage is the maximum voltage to be applied
21 // while zeroing.
22 HallEffectLoop(StateFeedbackLoop<2, 1, 1> *state_feedback_loop,
23 bool zero_down, double max_zeroing_voltage);
24 // UpdateZeros deals with all of the zeroing code and takes the hall
25 // effect constants.
26 // hall_effect_angle is the angle of the appropriate edge of the sensor.
27 // This should match the zero_down variable. If it should zero by going
28 // down it should be the upper edge, or vice-versa.
29 // hall_effect and calibration are the hall effect values and calibration
30 // values from the queue.
31 // zeroing_speed is from the constants file and is the speed at which
32 // to move the device when zeroing, for safety.
33 // position is the encoder value, which should be position->before_angle
34 // from the queues.
35 // good_position is whether or not RunItertion had a good position value.
36 // Note: Does NOT perform the Update operation the state feedback loop.
37 void UpdateZeros(
38 ::std::array<double, kNumHallEffect> hall_effect_angle,
39 ::std::array<bool, kNumHallEffect> hall_effect,
40 ::std::array<double, kNumHallEffect> calibration,
41 double zeroing_speed,
42 double position, bool good_position=true);
43
44 static const double dt;
45
46 const double kMaxZeroingVoltage;
47
48 // Whether to zero down or up.
49 bool zero_down_;
50
51 // Enum to store the state of the internal zeroing state machine.
52 // UNINITIALIZED is if the arm (or device) is not zeroed and disabled.
53 // MOVING_OFF is if the arm is on the Hall Effect sensor and still in
54 // the zeroing process.
55 // ZEROING is if it is off the sensor and trying to find it.
56 // READY is if it is zeroed and good for operation.
57 // ESTOP doesn't do anything in terms of updating the goal or the such.
58 enum State {
59 UNINITIALIZED,
60 MOVING_OFF,
61 ZEROING,
62 READY,
63 ESTOP
64 };
65
66 // Internal state for zeroing.
67 State state_;
68
69 // Returns the number of the activated Hall Effect sensor, given
70 // an array of all the hall effect sensors.
71 // if no sensor is currently activated, then return -1.
72 int HallEffect() const;
73 // Returns which Hall Effect sensor has a calibration value between
74 // last_off_position_ and the current_position. If none does,
75 // then returns -1.
76 int WhichHallEffect() const;
77
78 // Limits the zeroing_position to prevent it from increasing beyond
79 // where the goal is high enough to get the max voltage you want while
80 // zeroing.
81 void LimitZeroingGoal();
82
83 // The state feedback control loop to talk to.
84 ::std::unique_ptr<StateFeedbackLoop<2, 1, 1>> loop_;
85
86 // Cache values for hall effect sensors.
87 ::std::array<double, kNumHallEffect> hall_effect_angle_;
88 ::std::array<bool, kNumHallEffect> hall_effect_;
89 ::std::array<double, kNumHallEffect> calibration_;
90 double zeroing_speed_;
91 // Most recent encoder value and the one before it.
92 double current_position_;
93 double last_off_position_;
94 double absolute_position_;
95 // The sensor last used for calibration.
96 int last_calibration_sensor_;
97 // Position that is incremented for the goal when zeroing at Hall Effect.
98 double zeroing_position_;
99 // The offset of the encoder value from the actual position.
100 double zero_offset_;
101 double old_zero_offset_;
102};
103
104template <int kNumHallEffect>
105/*static*/ const double HallEffectLoop<kNumHallEffect>::dt = 0.01;
106
107} // namespace control_loops
108} // namespace frc971
109#endif // FRC971_CONTROL_LOOPS_HALL_EFFECT_H_