blob: a99e9ecd524f516a89b29916e66e1b43371a8bd2 [file] [log] [blame]
James Kuszmaul4a4622b2013-03-02 16:28:29 -08001#include "frc971/control_loops/angle_adjust.h"
2#include "frc971/control_loops/hall_effect_loop.h"
3#include "frc971/control_loops/hall_effect_loop-inl.h"
4
5#include <algorithm>
6
7#include "aos/aos_core.h"
8
9#include "aos/common/messages/RobotState.q.h"
10#include "aos/common/control_loop/control_loops.q.h"
11#include "aos/common/logging/logging.h"
12
13#include "frc971/constants.h"
14#include "frc971/control_loops/angle_adjust_motor_plant.h"
15
16namespace frc971 {
17namespace control_loops {
18
19AngleAdjustMotor::AngleAdjustMotor(
20 control_loops::AngleAdjustLoop *my_angle_adjust)
21 : aos::control_loops::ControlLoop<control_loops::AngleAdjustLoop>(
22 my_angle_adjust),
23 hall_effect_(new StateFeedbackLoop<2, 1, 1>(MakeAngleAdjustLoop()), true),
24 error_count_(0),
25 time_(0.0) {
26 if (testing) {
27 hall_effect_.loop_->StartDataFile("angle_adjust.csv");
28 }
29}
30
31bool AngleAdjustMotor::FetchConstants() {
32 if (!constants::angle_adjust_horizontal_lower_limit(
33 &horizontal_lower_limit_)) {
34 LOG(ERROR, "Failed to fetch the horizontal lower limit constant.\n");
35 return false;
36 }
37 if (!constants::angle_adjust_horizontal_upper_limit(
38 &horizontal_upper_limit_)) {
39 LOG(ERROR, "Failed to fetch the horizontal upper limit constant.\n");
40 return false;
41 }
42 if (!constants::angle_adjust_horizontal_hall_effect_stop_angle(
43 &horizontal_hall_effect_stop_angle_)) {
44 LOG(ERROR, "Failed to fetch the hall effect stop angle constants.\n");
45 return false;
46 }
47 if (!constants::angle_adjust_horizontal_zeroing_speed(
48 &horizontal_zeroing_speed_)) {
49 LOG(ERROR, "Failed to fetch the horizontal zeroing speed constant.\n");
50 return false;
51 }
52
53 return true;
54}
55
56double AngleAdjustMotor::ClipGoal(double goal) const {
57 return std::min(horizontal_upper_limit_,
58 std::max(horizontal_lower_limit_, goal));
59}
60
61double AngleAdjustMotor::LimitVoltage(double absolute_position,
62 double voltage) const {
63 if (hall_effect_.state_ == HallEffectLoop<2>::READY) {
64 if (absolute_position >= horizontal_upper_limit_) {
65 voltage = std::min(0.0, voltage);
66 }
67 if (absolute_position <= horizontal_lower_limit_) {
68 voltage = std::max(0.0, voltage);
69 }
70 }
71
72 double limit = (hall_effect_.state_ == HallEffectLoop<2>::READY) ? 12.0 : 5.0;
73 // TODO(aschuh): Remove this line when we are done testing.
74 //limit = std::min(0.3, limit);
75 voltage = std::min(limit, voltage);
76 voltage = std::max(-limit, voltage);
77 return voltage;
78}
79
80// Positive angle is up, and positive power is up.
81void AngleAdjustMotor::RunIteration(
82 const ::aos::control_loops::Goal *goal,
83 const control_loops::AngleAdjustLoop::Position *position,
84 ::aos::control_loops::Output *output,
85 ::aos::control_loops::Status * /*status*/) {
86
87 // Disable the motors now so that all early returns will return with the
88 // motors disabled.
89 if (output) {
90 output->voltage = 0;
91 }
92
93 // Cache the constants to avoid error handling down below.
94 if (!FetchConstants()) {
95 LOG(WARNING, "Failed to fetch constants.\n");
96 return;
97 }
98
99 // Uninitialize the bot if too many cycles pass without an encoder.
100 if (position == NULL) {
101 LOG(WARNING, "no new pos given\n");
102 error_count_++;
103 } else {
104 error_count_ = 0;
105 }
106 if (error_count_ >= 4) {
107 LOG(WARNING, "err_count is %d so forcing a re-zero\n", error_count_);
108 hall_effect_.state_ = HallEffectLoop<2>::UNINITIALIZED;
109 }
110
111 double absolute_position = hall_effect_.loop_->X_hat(0, 0);
112 // Compute the absolute position of the angle adjust.
113 if (position) {
114 hall_effect_sensors_[0] = position->bottom_hall_effect;
115 hall_effect_sensors_[1] = position->middle_hall_effect;
116 calibration_values_[0] = position->bottom_calibration;
117 calibration_values_[1] = position->middle_calibration;
118 absolute_position = position->before_angle;
119 }
120
121 hall_effect_.UpdateZeros(horizontal_hall_effect_stop_angle_,
122 hall_effect_sensors_,
123 calibration_values_,
124 horizontal_zeroing_speed_,
125 absolute_position,
126 position != NULL);
127
128 if (hall_effect_.state_ == HallEffectLoop<2>::READY) {
129 const double limited_goal = ClipGoal(goal->goal);
130 hall_effect_.loop_->R << limited_goal, 0.0;
131 }
132
133 // Update the observer.
134 hall_effect_.loop_->Update(position != NULL, output == NULL);
135
136 if (position) {
137 LOG(DEBUG, "pos=%f bottom_hall: %s middle_hall: %s\n",
138 position->before_angle,
139 position->bottom_hall_effect ? "true" : "false",
140 position->middle_hall_effect ? "true" : "false");
141 }
142
143 if (hall_effect_.state_ == HallEffectLoop<2>::READY) {
144 LOG(DEBUG, "calibrated with: %s hall effect\n",
145 hall_effect_.last_calibration_sensor_ ? "bottom" : "middle");
146 }
147
148 if (output) {
149 output->voltage = LimitVoltage(hall_effect_.absolute_position_,
150 hall_effect_.loop_->U(0, 0));
151 }
152
153 if (testing) {
154 hall_effect_.loop_->RecordDatum("angle_adjust.csv", time_);
155 }
156 time_ += dt;
157} // RunIteration
158
159} // namespace control_loops
160} // namespace frc971