forked from jinschoi/SphereBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SphereBot.ino
451 lines (392 loc) · 11.9 KB
/
SphereBot.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
* Copyright 2011 by Eberhard Rensch <http://pleasantsoftware.com/developer/3d>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
* Part of this code is based on/inspired by the Helium Frog Delta Robot Firmware
* by Martin Price <http://www.HeliumFrog.com>
*
* Updated to run on Adafruit motor shield by Jin Choi <[email protected]>.
* Utated to support both versions of Adafruit motor shield by GrAndAG
*
* !!!!!!!!
* This sketch needs the following non-standard library (install it in the Arduino library directory):
* Adafruit Motor Shield:
* v1: https://github.com/adafruit/Adafruit-Motor-Shield-library
* v2: https://github.com/adafruit/Adafruit_Motor_Shield_V2_Library
*
* Also tune your configuration in "Configuration.h" file.
*
* !!!!!!!!
*/
#include "Configuration.h"
#include <Wire.h>
/* DualStepper */
#include "DualStepper.h"
/* Servo library */
#include <Servo.h>
#include <EEPROM.h>
enum {
VALUES_SAVED_EEPROM_LOCATION, MIN_PEN_EEPROM_LOCATION, MAX_PEN_EEPROM_LOCATION, PEN_UP_EEPROM_LOCATION
};
#define EEPROM_MAGIC_NUMBER 53
byte min_pen_position;
byte max_pen_position;
byte pen_up_position;
byte current_pen_position;
/* --------- */
/* Set up steppers */
#if ADAFRUIT_MOTOR_SHIELD_VERSION == 1
SingleStepper *xStepper = new SingleStepper(new AF_Stepper(STEPS_PER_REVOLUTION, ROTATION_AXIS_PORT));
SingleStepper *yStepper = new SingleStepper(new AF_Stepper(STEPS_PER_REVOLUTION, PEN_AXIS_PORT));
#else
Adafruit_MotorShield MS = Adafruit_MotorShield();
SingleStepper *xStepper = new SingleStepper(MS.getStepper(STEPS_PER_REVOLUTION, ROTATION_AXIS_PORT));
SingleStepper *yStepper = new SingleStepper(MS.getStepper(STEPS_PER_REVOLUTION, PEN_AXIS_PORT));
#endif
DualStepper *steppers = new DualStepper(xStepper, yStepper, STEPS_PER_REVOLUTION * MICROSTEPS);
Servo servo;
// comm variables
const int MAX_CMD_SIZE = 256;
char buffer[MAX_CMD_SIZE]; // buffer for serial commands
char serial_char; // value for each byte read in from serial comms
int serial_count = 0; // current length of command
char *strchr_pointer; // just a pointer to find chars in the cmd string like X, Y, Z, E, etc
boolean comment_mode = false;
// end comm variables
// GCode States
boolean absoluteMode = true;
double feedrate = 160.0; // steps/s
double zoom = DEFAULT_ZOOM_FACTOR;
// ------
void load_pen_configuration()
{
// Check EEPROM location 0 for presence of a magic number. If it's there, we have saved values.
if (EEPROM.read(VALUES_SAVED_EEPROM_LOCATION) == EEPROM_MAGIC_NUMBER) {
min_pen_position = EEPROM.read(MIN_PEN_EEPROM_LOCATION);
max_pen_position = EEPROM.read(MAX_PEN_EEPROM_LOCATION);
pen_up_position = EEPROM.read(PEN_UP_EEPROM_LOCATION);
} else {
min_pen_position = MIN_PEN_POSITION;
max_pen_position = MAX_PEN_POSITION;
pen_up_position = PEN_UP_POSITION;
}
}
void save_pen_configuration() {
EEPROM.update(MIN_PEN_EEPROM_LOCATION, min_pen_position);
EEPROM.update(MAX_PEN_EEPROM_LOCATION, max_pen_position);
EEPROM.update(PEN_UP_EEPROM_LOCATION, pen_up_position);
EEPROM.update(VALUES_SAVED_EEPROM_LOCATION, EEPROM_MAGIC_NUMBER);
}
void clear_pen_configuration() {
min_pen_position = MIN_PEN_POSITION;
max_pen_position = MAX_PEN_POSITION;
pen_up_position = PEN_UP_POSITION;
EEPROM.update(VALUES_SAVED_EEPROM_LOCATION, 0xff);
}
void move_pen(byte pos) {
if (pos > current_pen_position) {
// ease it down
int pen_delay = PEN_DOWN_MOVE_TIME / 10;
float pen_increment = (pos - current_pen_position) / 10.0;
for (int i = 1; i < 10; i++) { // loop takes it to one step less than full travel
servo.write(current_pen_position + pen_increment * i);
delay(pen_delay);
}
servo.write(pos); // Finish off exactly; no round off errors.
} else {
// slam it up
servo.write(pos);
}
current_pen_position = pos;
}
void setup()
{
load_pen_configuration();
Serial.begin(115200);
clear_buffer();
#if ADAFRUIT_MOTOR_SHIELD_VERSION == 2
MS.begin();
TWBR = ((F_CPU / 400000L) - 16) / 2; // Change the i2c clock to 400KHz for faster stepping.
#endif
Serial.println("Ready");
steppers->setMaxSpeed(MAX_FEEDRATE);
servo.attach(SERVO_PIN);
servo.write(pen_up_position);
current_pen_position = pen_up_position;
delay(100);
}
void loop() // input loop, looks for manual input and then checks to see if and serial commands are coming in
{
get_command(); // check for Gcodes
}
void get_command() // gets commands from serial connection and then calls up subsequent functions to deal with them
{
if (Serial.available() > 0) // each time we see something
{
serial_char = Serial.read(); // read individual byte from serial connection
if (serial_char == '\n' || serial_char == '\r') // end of a command character
{
buffer[serial_count]=0;
process_commands(buffer, serial_count);
clear_buffer();
comment_mode = false; // reset comment mode before each new command is processed
}
else // not end of command
{
if (serial_char == ';' || serial_char == '(') // semicolon signifies start of comment
{
comment_mode = true;
}
if (comment_mode != true) // ignore if a comment has started
{
buffer[serial_count] = serial_char; // add byte to buffer string
serial_count++;
if (serial_count > MAX_CMD_SIZE) // overflow, dump and restart
{
clear_buffer();
Serial.flush();
}
}
}
}
}
void clear_buffer() // empties command buffer from serial connection
{
serial_count = 0; // reset buffer placement
}
boolean getValue(char key, char command[], double* value)
{
// find key parameter
strchr_pointer = strchr(buffer, key);
if (strchr_pointer != NULL) // We found a key value
{
*value = (double)strtod(&command[strchr_pointer - command + 1], NULL);
return true;
}
return false;
}
inline double clamp(double x, double a, double b)
{
return x < a ? a : (x > b ? b : x);
}
void process_commands(char command[], int command_length) // deals with standardized input from serial connection
{
if (command_length>0 && command[0] == 'G') // G code
{
int codenum = (int)strtod(&command[1], NULL);
double tempX = steppers->xPos();
double tempY = steppers->yPos();
double xVal;
boolean hasXVal = getValue('X', command, &xVal);
if(hasXVal) xVal*=zoom;
double yVal;
boolean hasYVal = getValue('Y', command, &yVal);
if(hasYVal) yVal*=zoom;
double iVal;
boolean hasIVal = getValue('I', command, &iVal);
if(hasIVal) iVal*=zoom;
double jVal;
boolean hasJVal = getValue('J', command, &jVal);
if(hasJVal) jVal*=zoom;
double rVal;
boolean hasRVal = getValue('R', command, &rVal);
if(hasRVal) rVal*=zoom;
double pVal;
boolean hasPVal = getValue('P', command, &pVal);
getValue('F', command, &feedrate);
if(absoluteMode)
{
if(hasXVal)
tempX=xVal;
if(hasYVal)
tempY=yVal;
}
else
{
if(hasXVal)
tempX+=xVal;
if(hasYVal)
tempY+=yVal;
}
tempY = clamp(tempY, MIN_PEN_AXIS_STEP, MAX_PEN_AXIS_STEP);
switch(codenum)
{
case 0: // G0, Rapid positioning
steppers->moveTo(tempX, tempY, MAX_FEEDRATE);
break;
case 1: // G1, linear interpolation at specified speed
if (current_pen_position <= pen_up_position) {
// potentially wrap around the sphere if pen is up
steppers->travelTo(tempX, tempY, feedrate);
} else {
steppers->moveTo(tempX, tempY, feedrate);
}
break;
case 2: // G2, Clockwise arc
case 3: // G3, Counterclockwise arc
if(hasIVal && hasJVal)
{
double centerX=steppers->xPos()+iVal;
double centerY=steppers->yPos()+jVal;
drawArc(centerX, centerY, tempX, tempY, (codenum==2));
}
else if(hasRVal)
{
//drawRadius(tempX, tempY, rVal, (codenum==2));
}
break;
case 4: // G4, Delay P ms
if(hasPVal)
{
delay(pVal);
}
break;
case 90: // G90, Absolute Positioning
absoluteMode = true;
break;
case 91: // G91, Incremental Positioning
absoluteMode = false;
break;
}
}
else if (command_length>0 && command[0] == 'M') // M code
{
double value;
int codenum = (int)strtod(&command[1], NULL);
switch(codenum) {
case 18: // Disable Drives
xStepper->release();
yStepper->release();
break;
case 300: // Servo Position
if(getValue('S', command, &value))
{
if (value > 180)
value = pen_up_position;
value = clamp(value, min_pen_position, max_pen_position);
move_pen(value);
}
break;
case 301: // Set min pen position.
if (getValue('P', command, &value)) {
min_pen_position = value;
}
break;
case 302: // Set max pen position.
if (getValue('P', command, &value)) {
max_pen_position = value;
}
break;
case 303: // set default pen up position.
if (getValue('P', command, &value)) {
pen_up_position = value;
}
break;
case 402: // Propretary: Set global zoom factor
if(getValue('S', command, &value)) {
zoom = value;
}
break;
case 500:
save_pen_configuration();
break;
case 501:
load_pen_configuration();
break;
case 502:
clear_pen_configuration();
break;
}
}
else if (command_length>0 && command[0] == 'N') // N code
{
// skip line number
int i = 1;
while (i<command_length && command[i]!=' ') ++i;
if (i<command_length-1) {
process_commands(command+i+1, command_length-i-1);
return;
}
}
// done processing commands
if (Serial.available() <= 0) {
Serial.print("ok:");
Serial.println(command);
}
}
/* This code was ported from the Makerbot/ReplicatorG java sources */
void drawArc(double centerX, double centerY, double endpointX, double endpointY, boolean clockwise)
{
// angle variables.
double angleA;
double angleB;
double angle;
double radius;
double length;
// delta variables.
double aX;
double aY;
double bX;
double bY;
// figure out our deltas
double currentX = steppers->xPos();
double currentY = steppers->yPos();
aX = currentX - centerX;
aY = currentY - centerY;
bX = endpointX - centerX;
bY = endpointY - centerY;
// Clockwise
if (clockwise) {
angleA = atan2(bY, bX);
angleB = atan2(aY, aX);
}
// Counterclockwise
else {
angleA = atan2(aY, aX);
angleB = atan2(bY, bX);
}
// Make sure angleB is always greater than angleA
// and if not add 2PI so that it is (this also takes
// care of the special case of angleA == angleB,
// ie we want a complete circle)
if (angleB <= angleA)
angleB += 2. * M_PI;
angle = angleB - angleA;
// calculate a couple useful things.
radius = sqrt(aX * aX + aY * aY);
length = radius * angle;
// for doing the actual move.
int steps;
int s;
int step;
// Maximum of either 2.4 times the angle in radians
// or the length of the curve divided by the curve section constant
steps = (int)ceil(max(angle * 2.4, length));
// this is the real draw action.
double newPointX = 0.;
double newPointY = 0.;
for (s = 1; s <= steps; s++) {
// Forwards for CCW, backwards for CW
if (!clockwise)
step = s;
else
step = steps - s;
// calculate our waypoint.
newPointX = centerX + radius * cos(angleA + angle * ((double) step / steps));
newPointY= centerY + radius * sin(angleA + angle * ((double) step / steps));
// start the move
steppers->moveTo(newPointX, newPointY, feedrate);
}
}