blob: 94bff1530068c7722ca0a66dc306aa823b3f2517 [file] [log] [blame]
Austin Schuhd36c3692014-02-18 21:00:57 -08001#include "frc971/control_loops/claw/claw.q.h"
2#include "frc971/control_loops/control_loops.q.h"
3
4#include "aos/linux_code/init.h"
5#include "frc971/constants.h"
6
7namespace frc971 {
8
9typedef constants::Values::Claws Claws;
10
Austin Schuhdda283a2014-02-21 22:59:38 -080011class Sensor {
12 public:
13 Sensor(const double start_position,
14 const HallEffectStruct &initial_hall_effect)
15 : start_position_(start_position),
16 last_hall_effect_(initial_hall_effect),
17 last_posedge_count_(initial_hall_effect.posedge_count),
18 last_negedge_count_(initial_hall_effect.negedge_count) {
19 last_on_min_position_ = start_position;
20 last_on_max_position_ = start_position;
21 last_off_min_position_ = start_position;
22 last_off_max_position_ = start_position;
23 }
Austin Schuhd36c3692014-02-18 21:00:57 -080024
Austin Schuhdda283a2014-02-21 22:59:38 -080025 bool DoGetPositionOfEdge(
26 const control_loops::HalfClawPosition &claw_position,
27 const HallEffectStruct &hall_effect, Claws::AnglePair *limits) {
28 bool print = false;
29
30 if (hall_effect.posedge_count != last_posedge_count_) {
31 const double avg_off_position = (last_off_min_position_ + last_off_max_position_) / 2.0;
32 if (claw_position.posedge_value < avg_off_position) {
33 printf("Posedge upper current %f posedge %f avg_off %f [%f, %f]\n",
34 claw_position.position, claw_position.posedge_value,
35 avg_off_position, last_off_min_position_,
36 last_off_max_position_);
37 limits->upper_decreasing_angle = claw_position.posedge_value - start_position_;
38 } else {
39 printf("Posedge lower current %f posedge %f avg_off %f [%f, %f]\n",
40 claw_position.position, claw_position.posedge_value,
41 avg_off_position, last_off_min_position_,
42 last_off_max_position_);
43 limits->lower_angle =
44 claw_position.posedge_value - start_position_;
45 }
46 print = true;
Austin Schuhd36c3692014-02-18 21:00:57 -080047 }
Austin Schuhdda283a2014-02-21 22:59:38 -080048 if (hall_effect.negedge_count != last_negedge_count_) {
49 const double avg_on_position = (last_on_min_position_ + last_on_max_position_) / 2.0;
50 if (claw_position.negedge_value > avg_on_position) {
51 printf("Negedge upper current %f negedge %f last_on %f [%f, %f]\n",
52 claw_position.position, claw_position.negedge_value,
53 avg_on_position, last_on_min_position_,
54 last_on_max_position_);
55 limits->upper_angle =
56 claw_position.negedge_value - start_position_;
57 } else {
58 printf("Negedge lower current %f negedge %f last_on %f [%f, %f]\n",
59 claw_position.position, claw_position.negedge_value,
60 avg_on_position, last_on_min_position_,
61 last_on_max_position_);
62 limits->lower_decreasing_angle = claw_position.negedge_value - start_position_;
63 }
64 print = true;
Austin Schuhd36c3692014-02-18 21:00:57 -080065 }
Austin Schuhdda283a2014-02-21 22:59:38 -080066
67 if (hall_effect.current) {
68 if (!last_hall_effect_.current) {
69 last_on_min_position_ = last_on_max_position_ = claw_position.position;
70 } else {
71 last_on_min_position_ =
72 ::std::min(claw_position.position, last_on_min_position_);
73 last_on_max_position_ =
74 ::std::max(claw_position.position, last_on_max_position_);
75 }
76 } else {
77 if (last_hall_effect_.current) {
78 last_off_min_position_ = last_off_max_position_ = claw_position.position;
79 } else {
80 last_off_min_position_ =
81 ::std::min(claw_position.position, last_off_min_position_);
82 last_off_max_position_ =
83 ::std::max(claw_position.position, last_off_max_position_);
84 }
85 }
86
87 last_hall_effect_ = hall_effect;
88 last_posedge_count_ = hall_effect.posedge_count;
89 last_negedge_count_ = hall_effect.negedge_count;
90
91 return print;
Austin Schuhd36c3692014-02-18 21:00:57 -080092 }
93
Austin Schuhdda283a2014-02-21 22:59:38 -080094 private:
95 const double start_position_;
96 HallEffectStruct last_hall_effect_;
97 int32_t last_posedge_count_;
98 int32_t last_negedge_count_;
99 double last_on_min_position_;
100 double last_off_min_position_;
101 double last_on_max_position_;
102 double last_off_max_position_;
103};
Austin Schuhd36c3692014-02-18 21:00:57 -0800104
Austin Schuhdda283a2014-02-21 22:59:38 -0800105class ClawSensors {
106 public:
107 ClawSensors(const double start_position,
108 const control_loops::HalfClawPosition &initial_claw_position)
109 : start_position_(start_position),
110 front_(start_position, initial_claw_position.front),
111 calibration_(start_position, initial_claw_position.calibration),
112 back_(start_position, initial_claw_position.back) {}
Austin Schuhd36c3692014-02-18 21:00:57 -0800113
Austin Schuhdda283a2014-02-21 22:59:38 -0800114 bool GetPositionOfEdge(const control_loops::HalfClawPosition &claw_position,
115 Claws::Claw *claw) {
116
117 bool print = false;
118 if (front_.DoGetPositionOfEdge(claw_position,
119 claw_position.front, &claw->front)) {
120 print = true;
121 } else if (calibration_.DoGetPositionOfEdge(claw_position,
122 claw_position.calibration,
123 &claw->calibration)) {
124 print = true;
125 } else if (back_.DoGetPositionOfEdge(claw_position,
126 claw_position.back, &claw->back)) {
127 print = true;
128 }
129
130 double position = claw_position.position - start_position_;
131
132 if (position > claw->upper_limit) {
133 claw->upper_hard_limit = claw->upper_limit = position;
134 print = true;
135 }
136 if (position < claw->lower_limit) {
137 claw->lower_hard_limit = claw->lower_limit = position;
138 print = true;
139 }
140 return print;
Austin Schuhd36c3692014-02-18 21:00:57 -0800141 }
142
Austin Schuhdda283a2014-02-21 22:59:38 -0800143 private:
144 const double start_position_;
145 Sensor front_;
146 Sensor calibration_;
147 Sensor back_;
148};
Austin Schuhd36c3692014-02-18 21:00:57 -0800149
150int Main() {
Brian Silverman428de562014-04-10 15:59:19 -0700151 control_loops::claw_queue_group.position.FetchNextBlocking();
Austin Schuhd36c3692014-02-18 21:00:57 -0800152
153 const double top_start_position =
154 control_loops::claw_queue_group.position->top.position;
155 const double bottom_start_position =
156 control_loops::claw_queue_group.position->bottom.position;
157
Austin Schuhdda283a2014-02-21 22:59:38 -0800158 ClawSensors top(top_start_position,
159 control_loops::claw_queue_group.position->top);
160 ClawSensors bottom(bottom_start_position,
161 control_loops::claw_queue_group.position->bottom);
162
Austin Schuhd36c3692014-02-18 21:00:57 -0800163 Claws limits;
164
165 limits.claw_zeroing_off_speed = 0.5;
166 limits.claw_zeroing_speed = 0.1;
167 limits.claw_zeroing_separation = 0.1;
168
169 // claw separation that would be considered a collision
170 limits.claw_min_separation = 0.0;
171 limits.claw_max_separation = 0.0;
172
173 // We should never get closer/farther than these.
174 limits.soft_min_separation = 0.0;
175 limits.soft_max_separation = 0.0;
176
177 limits.upper_claw.lower_hard_limit = 0.0;
178 limits.upper_claw.upper_hard_limit = 0.0;
179 limits.upper_claw.lower_limit = 0.0;
180 limits.upper_claw.upper_limit = 0.0;
181 limits.upper_claw.front.lower_angle = 0.0;
182 limits.upper_claw.front.upper_angle = 0.0;
183 limits.upper_claw.front.lower_decreasing_angle = 0.0;
184 limits.upper_claw.front.upper_decreasing_angle = 0.0;
185 limits.upper_claw.calibration.lower_angle = 0.0;
186 limits.upper_claw.calibration.upper_angle = 0.0;
187 limits.upper_claw.calibration.lower_decreasing_angle = 0.0;
188 limits.upper_claw.calibration.upper_decreasing_angle = 0.0;
189 limits.upper_claw.back.lower_angle = 0.0;
190 limits.upper_claw.back.upper_angle = 0.0;
191 limits.upper_claw.back.lower_decreasing_angle = 0.0;
192 limits.upper_claw.back.upper_decreasing_angle = 0.0;
193
194 limits.lower_claw.lower_hard_limit = 0.0;
195 limits.lower_claw.upper_hard_limit = 0.0;
196 limits.lower_claw.lower_limit = 0.0;
197 limits.lower_claw.upper_limit = 0.0;
198 limits.lower_claw.front.lower_angle = 0.0;
199 limits.lower_claw.front.upper_angle = 0.0;
200 limits.lower_claw.front.lower_decreasing_angle = 0.0;
201 limits.lower_claw.front.upper_decreasing_angle = 0.0;
202 limits.lower_claw.calibration.lower_angle = 0.0;
203 limits.lower_claw.calibration.upper_angle = 0.0;
204 limits.lower_claw.calibration.lower_decreasing_angle = 0.0;
205 limits.lower_claw.calibration.upper_decreasing_angle = 0.0;
206 limits.lower_claw.back.lower_angle = 0.0;
207 limits.lower_claw.back.upper_angle = 0.0;
208 limits.lower_claw.back.lower_decreasing_angle = 0.0;
209 limits.lower_claw.back.upper_decreasing_angle = 0.0;
210
211 limits.claw_unimportant_epsilon = 0.01;
212 limits.start_fine_tune_pos = -0.2;
213 limits.max_zeroing_voltage = 4.0;
214
215 control_loops::ClawGroup::Position last_position =
216 *control_loops::claw_queue_group.position;
217
218 while (true) {
Brian Silverman428de562014-04-10 15:59:19 -0700219 control_loops::claw_queue_group.position.FetchNextBlocking();
220 bool print = false;
221 if (top.GetPositionOfEdge(control_loops::claw_queue_group.position->top,
222 &limits.upper_claw)) {
223 print = true;
224 printf("Got an edge on the upper claw\n");
Austin Schuhd36c3692014-02-18 21:00:57 -0800225 }
Brian Silverman428de562014-04-10 15:59:19 -0700226 if (bottom.GetPositionOfEdge(
227 control_loops::claw_queue_group.position->bottom,
228 &limits.lower_claw)) {
229 print = true;
230 printf("Got an edge on the lower claw\n");
231 }
232 const double top_position =
233 control_loops::claw_queue_group.position->top.position -
234 top_start_position;
235 const double bottom_position =
236 control_loops::claw_queue_group.position->bottom.position -
237 bottom_start_position;
238 const double separation = top_position - bottom_position;
239 if (separation > limits.claw_max_separation) {
240 limits.soft_max_separation = limits.claw_max_separation = separation;
241 print = true;
242 }
243 if (separation < limits.claw_min_separation) {
244 limits.soft_min_separation = limits.claw_min_separation = separation;
245 print = true;
246 }
247
248 if (print) {
249 printf("{%f,\n", limits.claw_zeroing_off_speed);
250 printf("%f,\n", limits.claw_zeroing_speed);
251 printf("%f,\n", limits.claw_zeroing_separation);
252 printf("%f,\n", limits.claw_min_separation);
253 printf("%f,\n", limits.claw_max_separation);
254 printf("%f,\n", limits.soft_min_separation);
255 printf("%f,\n", limits.soft_max_separation);
256 printf(
257 "{%f, %f, %f, %f, {%f, %f, %f, %f}, {%f, %f, %f, %f}, {%f, %f, %f, "
258 "%f}},\n",
259 limits.upper_claw.lower_hard_limit,
260 limits.upper_claw.upper_hard_limit, limits.upper_claw.lower_limit,
261 limits.upper_claw.upper_limit, limits.upper_claw.front.lower_angle,
262 limits.upper_claw.front.upper_angle,
263 limits.upper_claw.front.lower_decreasing_angle,
264 limits.upper_claw.front.upper_decreasing_angle,
265 limits.upper_claw.calibration.lower_angle,
266 limits.upper_claw.calibration.upper_angle,
267 limits.upper_claw.calibration.lower_decreasing_angle,
268 limits.upper_claw.calibration.upper_decreasing_angle,
269 limits.upper_claw.back.lower_angle,
270 limits.upper_claw.back.upper_angle,
271 limits.upper_claw.back.lower_decreasing_angle,
272 limits.upper_claw.back.upper_decreasing_angle);
273
274 printf(
275 "{%f, %f, %f, %f, {%f, %f, %f, %f}, {%f, %f, %f, %f}, {%f, %f, %f, "
276 "%f}},\n",
277 limits.lower_claw.lower_hard_limit,
278 limits.lower_claw.upper_hard_limit, limits.lower_claw.lower_limit,
279 limits.lower_claw.upper_limit, limits.lower_claw.front.lower_angle,
280 limits.lower_claw.front.upper_angle,
281 limits.lower_claw.front.lower_decreasing_angle,
282 limits.lower_claw.front.upper_decreasing_angle,
283 limits.lower_claw.calibration.lower_angle,
284 limits.lower_claw.calibration.upper_angle,
285 limits.lower_claw.calibration.lower_decreasing_angle,
286 limits.lower_claw.calibration.upper_decreasing_angle,
287 limits.lower_claw.back.lower_angle,
288 limits.lower_claw.back.upper_angle,
289 limits.lower_claw.back.lower_decreasing_angle,
290 limits.lower_claw.back.upper_decreasing_angle);
291 printf("%f, // claw_unimportant_epsilon\n",
292 limits.claw_unimportant_epsilon);
293 printf("%f, // start_fine_tune_pos\n", limits.start_fine_tune_pos);
294 printf("%f,\n", limits.max_zeroing_voltage);
295 printf("}\n");
296 }
297
298 last_position = *control_loops::claw_queue_group.position;
Austin Schuhd36c3692014-02-18 21:00:57 -0800299 }
300 return 0;
301}
302
303} // namespace frc971
304
305int main() {
306 ::aos::Init();
307 int returnvalue = ::frc971::Main();
308 ::aos::Cleanup();
309 return returnvalue;
310}