This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandSet.cpp
339 lines (266 loc) · 8.21 KB
/
CommandSet.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include "CommandSet.h"
#include "Processes.h"
#include "robot.h"
#define FW_DEBUG
static SerialCommand sCmd; // The SerialCommand object
void CommandSet::setup()
{
/* Setup callbacks for SerialCommand commands */
sCmd.addCommand("L", this->led); // Toggles LED
sCmd.addCommand("ping", this->ping); // Check serial link
sCmd.addCommand("help", this->help);
/* Movement commands */
sCmd.addCommand("M", this->move); // Runs wheel motors
sCmd.addCommand("S", this->stop); // Force stops all motors
sCmd.addCommand("G", this->go); // Runs wheel motors with correction
/* Read from rotary encoders */
sCmd.addCommand("speeds", this->speeds);
/* Misc commands */
sCmd.addCommand("pixels", this->pixels); // Set LED colour
sCmd.addCommand("grab", this->grab); // Grab 0 or 1
sCmd.addCommand("kick", this->kick); // Kick
sCmd.addCommand("shuntkick", this->shuntkick); // Shuntkick
sCmd.addCommand("rotate", this->rotate); // Rotate
/* Debug and inspection commands */
sCmd.addCommand("ps", this->proc_dump); // Show process status
sCmd.addCommand("ptog", this->proc_toggle); // Enable or disable by pid on the fly
sCmd.setDefaultHandler(this->unrecognized); // Handler for command that isn't matched
}
void CommandSet::readSerial()
{
// Poll serial buffer & check against command set
sCmd.readSerial();
}
/*
* This gets set as the default input handler, and gets called when no other command matches.
*/
void CommandSet::unrecognized(const char* command)
{
/* NACK */
Serial.print(F("N - "));
Serial.println(command);
}
/*** SETUP CALLBACKS FOR SERIAL COMMANDS ***********************************************************/
void CommandSet::led()
{
digitalWrite(state.status_led_pin, state.status_led = state.status_led ? LOW : HIGH);
}
void CommandSet::ping()
{
Serial.println(F("pong"));
}
void CommandSet::help()
{
Serial.println(F("Valid input commands: (some have arguments)"));
delay(10);
sCmd.dumpCommandSet();
}
/*** LOW LEVEL MOTOR COMMAND ***********************************************************************/
void CommandSet::move()
{
int new_powers[motor_count];
for (int i=0; i < motor_count; i++){
new_powers[i] = atoi(sCmd.next());
/* Apply direction correction here */
new_powers[i] *= state.motors[i]->direction;
}
Serial.println(F("A"));
#ifdef FW_DEBUG
Serial.println(F("Moving"));
#endif
/* update speeds of all drive motors */
for(int i=0; i < motor_count; i++){
state.motors[i]->power = state.motors[i]->desired_power = new_powers[i];
state.motors[i]->disp_delta = 0;
}
write_powers();
}
void CommandSet::stop()
{
Serial.println(F("A"));
#ifdef FW_DEBUG
Serial.println(F("Force stopping"));
#endif
write_powers(0);
motorAllStop();
}
/*
* This method basically just allows us to execute the go command internally.
*/
void CommandSet::_go(float x_vel, float y_vel, float r_vel)
{
int power = atoi(sCmd.next());
power = power > 0 && power <= 255 ? power : 255;
float new_powers[motor_count];
new_powers[0] =
state.velo_coupling_mat[0][0] * x_vel +
state.velo_coupling_mat[0][1] * y_vel +
state.velo_coupling_mat[0][2] * r_vel;
new_powers[1] =
state.velo_coupling_mat[1][0] * x_vel +
state.velo_coupling_mat[1][1] * y_vel +
state.velo_coupling_mat[1][2] * r_vel;
new_powers[2] =
state.velo_coupling_mat[2][0] * x_vel +
state.velo_coupling_mat[2][1] * y_vel +
state.velo_coupling_mat[2][2] * r_vel;
float largest = 0.0;
for (int i=0; i<3; i++){
if (largest < fabs(new_powers[i]))
largest = fabs(new_powers[i]);
}
for (int i=0; i<3; i++){
new_powers[i] = round(new_powers[i] * (1.0/largest) * (float) power);
}
for(int i=0; i < motor_count; i++){
state.motors[i]->power = state.motors[i]->desired_power = (int) new_powers[i];
state.motors[i]->disp_delta = 0;
}
write_powers();
}
void CommandSet::go()
{
/* holonomics and shit */
float x_vel = atof(sCmd.next());
float y_vel = atof(sCmd.next());
float r_vel = atof(sCmd.next());
Serial.println(F("A"));
#ifdef FW_DEBUG
Serial.println(F("Going"));
#endif
_go(x_vel, y_vel, r_vel);
}
/*** READ ROTARY ENCODERS **************************************************************************/
void CommandSet::speeds()
{
Serial.print(millis());
Serial.print(F("ms: "));
for (int i = 0; i < motor_count; i++) {
Serial.print(state.motors[i]->speed);
Serial.print(F(", "));
}
Serial.println();
}
/*** MISC COMMANDS *********************************************************************************/
void CommandSet::pixels()
{
byte red = (byte) atoi(sCmd.next());
byte green = (byte) atoi(sCmd.next());
byte blue = (byte) atoi(sCmd.next());
Serial.println(F("A"));
#ifdef FW_DEBUG
Serial.print(F("Set pixel colour to "));
Serial.print(red,HEX);
Serial.print(green,HEX);
Serial.println(blue,HEX);
#endif
for (uint16_t i = 0; i < 10; i++) {
state.strip.setPixelColor(i, red, green, blue);
}
state.strip.show();
}
void CommandSet::grab()
{
const int direction = atoi(sCmd.next());
if (state.grabber_state != Open && state.grabber_state != Closed) {
Serial.println(F("N - grab"));
return;
}
Serial.println(F("A"));
#ifdef FW_DEBUG
Serial.print(F("grabbing "));
Serial.println(direction);
#endif
const pid_t pid = state.grab_handler->id;
const int motor_power = 200;
/* If we're open, doing that again will bugger the vision plate */
if (direction) {
state.grabber_state = Closing;
motorForward(grabber_port, motor_power);
processes.change(pid, 300L);
} else if (state.grabber_state != Open) {
state.grabber_state = Opening;
motorBackward(grabber_port, motor_power);
processes.change(pid, 280L);
} else {
return;
}
processes.enable(pid);
processes.forward(pid);
}
void CommandSet::_kick()
{
const pid_t pid = state.kick_handler->id;
processes.enable(pid);
processes.forward(pid);
state.kicker_state = Kicking;
digitalWrite(kicker_digital_pin, HIGH);
}
void CommandSet::kick()
{
if (state.grabber_state != Open || state.kicker_state == Kicking) {
Serial.println(F("N - kick"));
return;
}
Serial.println(F("A"));
#ifdef FW_DEBUG
Serial.println(F("kicking"));
#endif
_kick();
}
void CommandSet::shuntkick()
{
if (state.grabber_state != Open || state.kicker_state == Kicking) {
Serial.println(F("N - shuntkick"));
return;
}
Serial.println(F("A"));
#ifdef FW_DEBUG
Serial.println(F("shuntkicking"));
#endif
const pid_t pid = state.shuntkick_handler->id;
_go(0, 255, 0);
processes.enable(pid);
processes.forward(pid);
}
void CommandSet::rotate()
{
const int motor_power = atoi(sCmd.next());
const long delta = atoi(sCmd.next());
Serial.println(F("A"));
#ifdef FW_DEBUG
Serial.print(F("rotating at "));
Serial.print(motor_power);
Serial.print(F(" to "));
Serial.print(delta);
Serial.println(F(" stops"));
#endif
for (size_t i=0; i < motor_count; i++){
state.motors[i]->power = motor_power;
state.initial_displacement[i] = state.motors[i]->disp;
}
state.rotation_delta = delta;
processes.enable((state.rotation_process)->id);
write_powers();
}
void CommandSet::proc_dump()
{
processes.status();
}
void CommandSet::proc_toggle()
{
pid_t pid = (pid_t) atoi(sCmd.next());
if (pid == NULL) {
Serial.println("N - ptog");
return;
}
process* proc = processes.get_by_id(pid);
if (proc == NULL) {
Serial.print(F("Unknown pid "));
Serial.println(pid);
return;
}
proc->enabled ? processes.disable(pid) : processes.enable(pid);
Serial.print(F("toggled pid "));
Serial.println(pid);
}