-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrfirmware.cpp
350 lines (304 loc) · 7.99 KB
/
rfirmware.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
339
340
341
342
343
344
345
346
347
348
349
350
#include <Arduino.h>
#include <SoftwareSerial.h>
#include "PSX.h"
#include "InverseKinematics.h"
#include "FastTrig.h"
#include "CalculationStructs.h"
#include "Constants.h"
#define STATE_COUNT 3
enum MovementState {ROTATE, TRANSLATE, WALK};
Psx controller;
FastTrig trig;
SoftwareSerial sscSerial(13, 12);
MovementInput positionInput;
MovementInput rotationInput;
Vector3L travelLength;
PositionTable table;
InverseKinematics kinematics(trig);
BalanceState balanceState;
Gait gait;
MovementState movementState;
word moveTime;
word previousMoveTime;
byte inputDelay;
int speedControl = 100;
bool isMoving;
bool shouldContinueWalking;
unsigned long timerStart;
unsigned long timerEnd;
byte cycleTime;
int yOffset;
int yShift;
bool shouldBalance;
bool debugMode;
bool shouldShowMenu;
bool isOnline;
void scheduleAngle(int angle, int legIndex, byte pinOffset) {
sscSerial.print("#");
sscSerial.print(pgm_read_byte(&PIN_LAYOUT[legIndex * 3]) + pinOffset, DEC);
sscSerial.print("P");
sscSerial.print(angle, DEC);
}
void scheduleMove(int legIndex, short coxaAngle, short femurAngle, short tibiaAngle) {
byte sign = (legIndex <= 2) ? -1 : 1;
int coxaOut = ((long) (sign * coxaAngle + 900)) * 1000 / PWM_DIV + PF_ADJUST;
int femurOut = ((long) ((sign * femurAngle + 900)) * 1000 / PWM_DIV + PF_ADJUST);
int tibiaOut = ((long) (sign * tibiaAngle + 900)) * 1000 / PWM_DIV + PF_ADJUST;
scheduleAngle(coxaOut, legIndex, 0);
scheduleAngle(femurOut, legIndex, 1);
scheduleAngle(tibiaOut, legIndex, 2);
}
void commit() {
sscSerial.print("T");
sscSerial.println(moveTime, DEC);
}
void freeServos(void) {
for (byte i = 0; i < 32; i++) {
sscSerial.print("#");
sscSerial.print(i, DEC);
sscSerial.print("P0");
}
sscSerial.print("T200\r\n");
}
void setSpeedControlFactor(int val) {
speedControl = max(0, min(2000, val));
Serial.print("Set speed control factor to ");
Serial.println(val);
}
void resetState() {
freeServos();
for (int i = 0; i <= 5; i++) {
table.xPos[i] = (short) pgm_read_word(&INIT_POSX[i]);
table.yPos[i] = (short) pgm_read_word(&INIT_POSY[i]);
table.zPos[i] = (short) pgm_read_word(&INIT_POSZ[i]);
table.coxaAngles[i] = 0;
table.femurAngles[i] = 0;
table.tibiaAngles[i] = 0;
}
positionInput.zero();
rotationInput.zero();
travelLength.zero();
kinematics.setRotationOffsets(0, 0, 0);
// selectedLeg = 255;
// previousSelectedLeg = 255;
gait.legLiftHeight = 50;
gait.setGait(1);
moveTime = 500;
speedControl = 200;
inputDelay = 128;
yOffset = 65;
yShift = 0;
movementState = WALK;
shouldBalance = true;
isOnline = false;
shouldShowMenu = true;
}
void setup() {
Serial.begin(115200);
sscSerial.begin(115200);
Serial.println("startup");
resetState();
controller.setupPins(PS2_DAT, PS2_CMD, PS2_ATT, PS2_CLK);
controller.initcontroller(0x1);
}
void handleInput() {
if(controller.isDown(BUTTON_START)) {
if(isOnline) {
// turns it off
resetState();
delay(500);
} else {
isOnline = true;
delay(500);
}
return;
}
if(!isOnline) {
return;
}
if(controller.isDown(psxR2)) {
int nextState = (movementState + 1);
if(nextState >= STATE_COUNT) {
nextState = 0;
}
movementState = static_cast<MovementState>(nextState);
Serial.print("Switched to state ");
Serial.println(nextState);
delay(500);
}
if(controller.isDown(BUTTON_SQUARE)) {
shouldBalance = !shouldBalance;
Serial.print("Balancing mode is now ");
Serial.println(shouldBalance ? "enabled." : "disabled.");
}
if (controller.isDown(BUTTON_TRIANGLE)) {
if (yOffset > 0) {
yOffset = 0;
} else {
yOffset = 35;
}
}
if (controller.isDown(BUTTON_UP)) {
yOffset = max(min(yOffset + 5, MAX_BODY_Y_OFFSET), 0);
}
if (controller.isDown(BUTTON_DOWN)) {
yOffset = max(yOffset - 5, 0);
}
if (controller.isDown(BUTTON_RIGHT)) {
if (speedControl > 0) {
setSpeedControlFactor(speedControl - 50);
delay(250);
}
}
if (controller.isDown(BUTTON_LEFT)) {
if (speedControl < 2000) {
setSpeedControlFactor(speedControl + 50);
delay(250);
}
}
yShift = 0;
if (movementState == WALK) {
if (controller.isDown(psxR1)) { // 2
gait.alternateLegLiftHeight();
}
} else if (movementState == TRANSLATE) { // 1
positionInput.x = (controller.lX - 128) / 2;
positionInput.z = -(controller.lY - 128) / 3;
positionInput.print();
rotationInput.y = (controller.rX - 128) * 2;
yShift = (-(controller.rY - 128) / 2);
} else if(movementState == ROTATE) { // 0
positionInput.x = (controller.lY - 128);
positionInput.y = (controller.rX - 128) * 2;
positionInput.z = (controller.lX - 128);
yShift = (-(controller.rY - 128) / 2);
}
inputDelay = 128 - max(max(abs(controller.lX), abs(controller.lY)), abs(controller.rX));
positionInput.y = max(yOffset + yShift, 0);
}
bool showMenu() {
if(shouldShowMenu) {
Serial.println("######====Command Monitor====######");
Serial.println(" 1. Change servo positions");
Serial.println(" 2. Debug");
Serial.println(" 3. Turn on.");
Serial.println(" 4. Reset SSC servo offets. (debug only)");
Serial.println(" 5. Show SSC servo offets.");
Serial.println(" 6. Show loaded servo offsets.");
Serial.println(" 7. Force reload loaded servo offsets.");
Serial.println(" 8. Free servos.");
Serial.println(" 9. Dump register contents.");
shouldShowMenu = false;
}
byte buff[5];
int read;
delay(100);
read = Serial.available();
if (read > 0) {
read = 0;
for (read = 0; read < sizeof(buff); read++) {
int ch = Serial.read();
if ((ch == -1) || ((ch >= 10) && (ch <= 15)))
break;
buff[read] = ch;
}
buff[read] = '\0';
if(read == 1) {
shouldShowMenu = true;
int opt = buff[0];
switch(opt) {
case 'd':
case 'D':
debugMode = !debugMode;
Serial.print("Debug mode is now ");
Serial.println((debugMode) ? "enabled" : "disabled.");
break;
case '1':
// configureServos();
break;
case '2':
// test();
break;
case '3':
isOnline = true;
break;
case '4':
if(debugMode) {
// resetSSCOffsets();
} else {
Serial.println("Debug mode must be enabled to erase offsets.");
}
break;
case '5':
// printSSCOffsets();
break;
case '6':
// printLoadedOffsets();
break;
case '7':
// loadPWMOffsets();
break;
case '8':
freeServos();
break;
case '9':
// dumpRegisters();
break;
}
}
return true;
}
return false;
}
void loop() {
controller.poll();
handleInput();
if(!isOnline) {
showMenu();
return;
}
timerStart = millis();
gait.sequence(travelLength);
if(shouldBalance) {
kinematics.calculateBalanceTilts(&balanceState, gait, table);
}
int q = kinematics.calculatePositions(gait, table, balanceState, positionInput, rotationInput);
if(q == SOLUTION_ERROR) {
Serial.println("Solution error.");
} else if(q == SOLUTION_WARN) {
Serial.println("Solution warn.");
}
table.checkAngleBounds();
moveTime = gait.calculateMoveTime(travelLength, speedControl, inputDelay);
for(int i=0; i <= 5; i++) {
scheduleMove(i, table.coxaAngles[i], table.femurAngles[i], table.tibiaAngles[i]);
}
shouldContinueWalking = false;
// checking if gait pos/rot > or < 0
for (int i = 0; i <= 5; i++) {
if ((gait.gaitXPos[i] > 2) || (gait.gaitXPos[i] < -2)
|| (gait.gaitYPos[i] > 2) || (gait.gaitYPos[i] < -2)
|| (gait.gaitZPos[i] > 2) || (gait.gaitZPos[i] < -2)
|| (gait.gaitYRot[i] > 2) || (gait.gaitYRot[i] < -2)) {
shouldContinueWalking = true;
break;
}
}
if (isMoving || shouldContinueWalking) {
word waitTime;
isMoving = shouldContinueWalking;
// endtime and waittime
timerEnd = millis();
if (timerEnd > timerStart) {
cycleTime = timerEnd - timerStart;
} else {
cycleTime = 0xffffffffL - timerEnd + timerStart + 1;
}
// if it is less, use the last cycle time...
// wait for previous commands to be completed while walking
waitTime = (min(max ((previousMoveTime - cycleTime), 1), gait.speed));
delay(waitTime);
}
commit();
previousMoveTime = moveTime;
}