-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcomms.ino
379 lines (317 loc) · 10.5 KB
/
comms.ino
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//--------------------------------------------------------
// Evil Minion 5 Axis robot firmware
// 2015 September 3
// see http://evilminion.info/ for more information.
//--------------------------------------------------------
#include "pid.h"
#include "motor.h"
#include "sensor.h"
#include "flash.h"
char buffer[MAX_BUF+1]; // Serial buffer
int sofar; // Serial buffer progress
static long last_cmd_time; // prevent timeouts
uint32_t line_number;
char absolute_mode = DEFAULT_IS_ABSOLUTE_ON;
void comms_setup() {
Serial.begin(BAUD_RATE);
line_number=0;
}
/**
* Look for character /code/ in the buffer and read the float that immediately follows it.
* @return the value found. If nothing is found, /val/ is returned.
* @input code the character to look for.
* @input val the return value if /code/ is not found.
**/
float parse_number(char code,float val) {
char *ptr=buffer; // start at the beginning of buffer
while(ptr && *ptr && ptr<buffer+sofar) { // walk to the end
if(*ptr==code) { // if you find code on your walk,
return atof(ptr+1); // convert the digits that follow into a float and return it
}
ptr=strchr(ptr,' ')+1; // take a step from here to the letter after the next space
}
return val; // end reached, nothing found, return default val.
}
/**
* Look for character /code/ in the buffer.
* @return /true/ if the code is found.
* @input code the character to look for.
**/
float has_code(char code) {
char *ptr=buffer; // start at the beginning of buffer
while(ptr && *ptr && ptr<buffer+sofar) { // walk to the end
if(*ptr==code) { // if you find code on your walk,
return true;
}
ptr=strchr(ptr,' ')+1; // take a step from here to the letter after the next space
}
return false;
}
/**
* Print a helpful message to serial. The first line must never be changed to play nice with the JAVA software.
*/
void help() {
Serial.print(F("\n\nHELLO WORLD! I AM MINION #"));
Serial.println(robot_uid,DEC);
Serial.println(F("== http://www.marginallyclever.com/ =="));
Serial.println(F("I understand the following commands:"));
Serial.println(F("Nx *y (this is line number x, with checksum y)\n"\
"UID * (write robot UID * to EEPROM)\n"\
"M17 (disable motors)\n"\
"M18 (enable motors)\n"\
"M100 (help)\n"\
"M110 N* (set line number to *)\n"\
"M114 (where)\n"\
"G90 (absolute mode)\n"\
"G91 (relative mode)\n"\
"G92 [Aa] (teleport)\n"\
"R0 [Aa] [Bb] [Cc] [Dd] [Ed] (move motors so that sensors read [abcde])\n"\
"R1 (continuous angle reporting)\n"\
"R3 (toggle compliant mode)\n"\
"R5 P* (adjust compliance limit to *)\n"\
"R10 S* (adjust servo angle to *)\n"\
"R60 [Aa] [Bb] [Cc] [Dd] [Ed] (set sensors to new value [abcde])\n"\
"R61 (display sensor adjustment amounts)\n"\
"R70 (write sensor adjustments to EEPROM)\n"));
}
void where() {
int i;
for(i=0;i<NUM_AXIES;++i) {
Serial.print(motor_letters[i]);
Serial.print(sensors_filtered[i]);
Serial.print(' ');
}
Serial.print('S');
Serial.print(servo_angle);
Serial.print('\n');
}
/**
* prepares the input buffer to receive a new message and tells the serial connected device it is ready for more.
*/
void ready() {
sofar=0; // clear input buffer
Serial.print(F(">\n")); // signal ready to receive input
last_cmd_time = millis();
}
/**
* Prevent illegal moves that would obviously damage the machine
* @input index the joint index
* @input angle the desired angle
* @return the corrected angle, clamped within an acceptable range.
*/
float software_angle_limits(int index,float angle) {
switch(index) {
case 0: break; // A has NO LIMIT.
case 1: // B
if( angle > ANGLE_B_MAX ) angle = ANGLE_B_MAX;
if( angle < ANGLE_B_MIN ) angle = ANGLE_B_MIN;
break;
case 2: // C
if( angle > ANGLE_C_MAX ) angle = ANGLE_C_MAX;
if( angle < ANGLE_C_MIN ) angle = ANGLE_C_MIN;
break;
case 3: // D
if( angle > ANGLE_D_MAX ) angle = ANGLE_D_MAX;
if( angle < ANGLE_D_MIN ) angle = ANGLE_D_MIN;
break;
case 4: // E
if( angle > ANGLE_E_MAX ) angle = ANGLE_E_MAX;
if( angle < ANGLE_E_MIN ) angle = ANGLE_E_MIN;
break;
}
return angle;
}
void process_move_motors() {
int i;
float v;
for(i=0;i<NUM_AXIES;++i) {
if(!has_code(motor_letters[i])) continue;
if( absolute_mode==0 ) {
v = parse_number(motor_letters[i], 0 );
v += destination[i];
} else {
v = parse_number(motor_letters[i], destination[i] );
}
destination[i] = software_angle_limits(i,v);
// Serial.print(F("Moving "));
// Serial.print(absolute_mode==0?F("REL "):F("ABS "));
// Serial.print(motor_letters[i]);
// Serial.print(F(" to "));
// Serial.print(v);
// Serial.print(F(" ~ "));
// Serial.println(destination[i]);
PID_init(pid[i]);
move_active[i]=1;
}
}
void process_servo_command() {
if(!has_code('S')) return;
int v = parse_number('S',servo_angle);
servo_move(v);
}
void process_sensors_adjust() {
int i;
for(i=0;i<NUM_AXIES;++i) {
if(!has_code(motor_letters[i])) continue;
// get the new angle
float newAngle = parse_number(motor_letters[i], 0 );
// get the original sensor angle (without adjustment)
float original_angle = sensors_filtered[i] - sensors_adjust[i];
// find the difference
float diff = newAngle - original_angle;
// Serial.print("i="); Serial.println(i);
// Serial.print("motor_letters[i]="); Serial.println(motor_letters[i]);
// Serial.print("sensors_adjust[i]="); Serial.println(sensors_adjust[i]);
// Serial.print("sensor_angle(i)="); Serial.println(sensor_angle(i));
// Serial.print("newAngle="); Serial.println(newAngle);
// Serial.print("diff="); Serial.println(diff);
// save it
sensors_adjust[i] = diff;
}
}
void print_sensors_adjust() {
int i;
for(i=0;i<NUM_AXIES;++i) {
Serial.print(motor_letters[i]);
Serial.print(sensors_adjust[i]);
Serial.print(' ');
}
Serial.println();
}
void parse_teleport() {
sensors_filtered[0] = parse_number('A',0);
}
/**
* process commands in the serial receive buffer
*/
void process_command() {
// blank lines
if(buffer[0]==';') return;
long cmd;
// is there a line number?
cmd=parse_number('N',-1);
if(cmd!=-1 && buffer[0]=='N') { // line number must appear first on the line
if( cmd != line_number ) {
// wrong line number error
Serial.print(F("BADLINENUM "));
Serial.println(line_number);
return;
}
// is there a checksum?
if(strchr(buffer,'*')!=0) {
// yes. is it valid?
char checksum=0;
int c=0;
while(buffer[c]!='*' && c<MAX_BUF) checksum ^= buffer[c++];
c++; // skip *
int against = strtod(buffer+c,NULL);
if( checksum != against ) {
Serial.print(F("BADCHECKSUM "));
Serial.println(line_number);
return;
}
} else {
Serial.print(F("NOCHECKSUM "));
Serial.println(line_number);
return;
}
line_number++;
}
if(!strncmp(buffer,"UID",3) && robot_uid==0) {
robot_uid=atoi(strchr(buffer,' ')+1);
saveUID();
}
cmd=parse_number('M',-1);
switch(cmd) {
//case 6: tool_change(parse_number('T',current_tool)); break;
case 17: motor_disable(); break;
case 18: motor_enable(); break;
case 100: help(); break;
//case 101: processconfig(); break;
case 110: line_number = parse_number('N',line_number); break;
case 114: where(); break;
}
cmd=parse_number('G',-1);
switch(cmd) {
/*
case 0:
case 1: { // line
Vector3 offset=get_end_plus_offset();
acceleration = min(max(parse_number('A',acceleration),1),2000);
line_safe( parse_number('X',(absolute_mode?offset.x:0)*10)*0.1 + (absolute_mode?0:offset.x),
parse_number('Y',(absolute_mode?offset.y:0)*10)*0.1 + (absolute_mode?0:offset.y),
parse_number('Z',(absolute_mode?offset.z:0)) + (absolute_mode?0:offset.z),
parse_number('F',feed_rate) );
break;
}
case 2:
case 3: { // arc
Vector3 offset=get_end_plus_offset();
acceleration = min(max(parse_number('A',acceleration),1),2000);
setFeedRate(parse_number('F',feed_rate));
arc(parse_number('I',(absolute_mode?offset.x:0)*10)*0.1 + (absolute_mode?0:offset.x),
parse_number('J',(absolute_mode?offset.y:0)*10)*0.1 + (absolute_mode?0:offset.y),
parse_number('X',(absolute_mode?offset.x:0)*10)*0.1 + (absolute_mode?0:offset.x),
parse_number('Y',(absolute_mode?offset.y:0)*10)*0.1 + (absolute_mode?0:offset.y),
parse_number('Z',(absolute_mode?offset.z:0)) + (absolute_mode?0:offset.z),
(cmd==2) ? -1 : 1,
parse_number('F',feed_rate) );
break;
}
case 4: { // dwell
wait_for_empty_segment_buffer();
pause(parse_number('S',0) + parse_number('P',0)*1000.0f);
break;
}
case 28: FindHome(); break;
case 54:
case 55:
case 56:
case 57:
case 58:
case 59: { // 54-59 tool offsets
int tool_id=cmd-54;
set_tool_offset(tool_id,parse_number('X',tool_offset[tool_id].x),
parse_number('Y',tool_offset[tool_id].y),
parse_number('Z',tool_offset[tool_id].z));
break;
}*/
case 90: absolute_mode=1; break; // absolute mode
case 91: absolute_mode=0; break; // relative mode
case 92: parse_teleport(); break;
// case 4: SD_StartPrintingFile(strchr(buffer,' ')+1); break; // read file
}
cmd=parse_number('R',-1);
switch(cmd) {
case 0: process_move_motors(); break;
case 1: continuous_reporting=1-continuous_reporting; break;
case 3:
compliant_mode=1-compliant_mode;
Serial.print(F("Compliance "));
Serial.println(compliant_mode==1?F("ON"):F("OFF"));
break;
case 5: compliance_limit = parse_number('P',compliance_limit); break;
case 10: process_servo_command(); break;
case 60: process_sensors_adjust(); break;
case 61: print_sensors_adjust(); break;
case 70: saveAdjustments(); break;
}
}
// listen for serial commands
// See: http://www.marginallyclever.com/2011/10/controlling-your-arduino-through-the-serial-monitor/
void tick_comms() {
while(Serial.available() > 0) {
char c = Serial.read();
if(c=='\r') continue;
if(sofar<MAX_BUF) buffer[sofar++]=c;
if(c=='\n' || c=='\r') {
buffer[sofar]=0;
// echo confirmation
// Serial.println(buffer);
// do something with the command
process_command();
ready();
}
}
}