-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDuCatStepperBot.ino
378 lines (339 loc) · 8.45 KB
/
DuCatStepperBot.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
#include <Servo.h>
#include <SoftwareSerial.h>
#define SECOND_PERIOD_ms 1000
#define STEP_PERIOD_MAX 20
#define STEP_PERIOD_MIN 2
#define PEN_PIN 5
#define PEN_UP 20
#define PEN_DN 0
#define SER_CONN 1
int8_t ui8_StepPeriod_ms = STEP_PERIOD_MAX;
int8_t AngleOnly = 0;
int16_t i16_AngleSteps;
uint16_t ui16_DistanceSteps;
float fCurrentX;
float fCurrentY;
float fTargetX;
float fTargetY;
float fCurrentAngle;
float fTargetAngle;
Servo PenServo; // Servo for Pen Up/Down
int8_t i8_StepMot1; //on Port B0-3
int8_t i8_StepMot2; //on Port C0-3
#define MAX_STEPS 4
uint8_t ui8_StepsArray[MAX_STEPS];
SoftwareSerial SoftSerial(2, 3); // RX, TX
void vSetupServoTim2Pin3(void) //OC2B on PD3
{
pinMode(3, OUTPUT);
TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
TCCR2B = _BV(CS22);
OCR2B = 20;
}
void StepMotors(void)
{ //There will allways be Angle first then Distance (after angle finished)
if (i16_AngleSteps != 0)
{
if (i16_AngleSteps > 0)
{
i8_StepMot1++;
if (i8_StepMot1 >= MAX_STEPS) { i8_StepMot1 = 0; }
i8_StepMot2--;
if (i8_StepMot2 < 0) { i8_StepMot2 = MAX_STEPS - 1; }
i16_AngleSteps--;
}
else
{
i8_StepMot1--;
if (i8_StepMot1 < 0) { i8_StepMot1 = MAX_STEPS - 1; }
i8_StepMot2++;
if (i8_StepMot2 >= MAX_STEPS) { i8_StepMot2 = 0; }
i16_AngleSteps++;
}
PORTB = ui8_StepsArray[i8_StepMot1];
PORTC = ui8_StepsArray[i8_StepMot2];
if (AngleOnly == 1)
{
if (i16_AngleSteps == 0)
{
fCurrentX = fTargetX;
fCurrentY = fTargetY;
fCurrentAngle = fTargetAngle;
SoftSerial.print("\r\n>");
AngleOnly = 0;
}
}
}
else if (ui16_DistanceSteps)
{
i8_StepMot1++;
if (i8_StepMot1 >= MAX_STEPS) { i8_StepMot1 = 0; }
i8_StepMot2++;
if (i8_StepMot2 >= MAX_STEPS) { i8_StepMot2 = 0; }
PORTB = ui8_StepsArray[i8_StepMot1];
PORTC = ui8_StepsArray[i8_StepMot2];
ui16_DistanceSteps--;
if (ui16_DistanceSteps == 0)
{
fCurrentX = fTargetX;
fCurrentY = fTargetY;
fCurrentAngle = fTargetAngle;
SoftSerial.print("\r\n>");
}
}
else
{
// SoftSerial.print("\r\n>");
}
}
#define K_DISTANCE_STEPS 13.8888888
//KA= D1/(2*KD) where D1 = distance between wheels and KD = K_DISTANCE_STEPS = 79/(2*13.8(8)) = 2.844
#define K_ANGLE_STEPS 554 //274.71 555
void ui16CalculateSteps()
{
if ((ui16_DistanceSteps == 0) && (i16_AngleSteps == 0) && (AngleOnly != 1))
{
ui16_DistanceSteps = K_DISTANCE_STEPS * sqrt(pow(fTargetX - fCurrentX, 2) + pow(fTargetY - fCurrentY, 2));
fTargetAngle = atan2(fTargetY - fCurrentY, fTargetX - fCurrentX);
i16_AngleSteps = K_ANGLE_STEPS * (fTargetAngle - fCurrentAngle);
SoftSerial.print("D: "); SoftSerial.print(ui16_DistanceSteps); SoftSerial.print(" A: "); SoftSerial.print(i16_AngleSteps);
}
else
{ SoftSerial.println("Err1>"); }
}
#define K_ANGLE_STEPS2 9.683
void ui16CalculateAngle()
{
if ((ui16_DistanceSteps == 0) && (i16_AngleSteps == 0))
{
AngleOnly = 1;
fTargetAngle = fTargetAngle / 57.317;
i16_AngleSteps = (fTargetAngle - fCurrentAngle) * K_ANGLE_STEPS;
SoftSerial.print("A: "); SoftSerial.print(i16_AngleSteps);
}
else
{ SoftSerial.println("Err2>"); }
}
unsigned long previousMillisStep = millis();
unsigned long previousMillisStep2 = millis();
void setup()
{
fCurrentX = 0;
fCurrentY = 0;
fTargetX = 0;
fTargetY = 0;
PenServo.attach(PEN_PIN); // attaches the servo on pin 9 to the servo object
PenServo.write(PEN_UP);
//pinMode(PEN_PIN, OUTPUT);
//digitalWrite(PEN_PIN, LOW);
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
for (int i = 8; i <= 11; i++)
{
pinMode(i, OUTPUT);
}
i16_AngleSteps = 0;
ui16_DistanceSteps = 0;
if (SER_CONN) { Serial.begin(9600); }
SoftSerial.begin(2400); //57600
// SoftSerial.write("AT+BAUD4\r\n");
// SoftSerial.println("AT+UART=1200,0,0");
SoftSerial.listen();
i8_StepMot1 = 0;
i8_StepMot2 = 0;
ui8_StepsArray[0] = 0b0001; ui8_StepsArray[1] = 0b0010; ui8_StepsArray[2] = 0b0100; ui8_StepsArray[3] = 0b1000;
SoftSerial.print(">");
// SoftSerial.print("@");
}
void parseCoordinate(char * cp_Cmd)
{
char * cp_Tmp;
char * cp_Str;
cp_Str = strtok_r(cp_Cmd, " ", &cp_Tmp); //now the number is in "cp_Str"
while (cp_Str != NULL) { //Subsequent calls, the first number (above) is gone
cp_Str = strtok_r(0, " ", &cp_Tmp); //Here we get X, Y or Z in a G1 command
//Serial.printf("%s;",str);
if (tolower(cp_Str[0]) == 'x')
{ fTargetX = atof(cp_Str + 1); }
else if (tolower(cp_Str[0]) == 'y')
{ fTargetY = atof(cp_Str + 1); }
else if (tolower(cp_Str[0]) == 'z')
{ /*tarZ = atof(cp_Str + 1); */ }
}
ui16CalculateSteps(); //Also starts movement on next rotation/line
}
void parseGcode(char * cp_Cmd)
{
int iCode;
iCode = atoi(cp_Cmd);
switch (iCode)
{
case 1: // xyz move
parseCoordinate(cp_Cmd);
break;
case 28: // home
fTargetX = 0; fTargetY = 0;
// mPenUp();
/// prepareMove();
/// initPosition();
break;
}
}
void parsePen(char * cmd)
{
char * tmp;
char * str;
str = strtok_r(cmd, " ", &tmp);
int pos = atoi(tmp);
if (pos == 0)
{
PenServo.attach(PEN_PIN);
PenServo.write(PEN_UP);
delay(400);
PenServo.detach();
SoftSerial.write("Up");
} //Pin Up
else
{
PenServo.attach(PEN_PIN);
PenServo.write(PEN_DN);
delay(400);
PenServo.detach();
SoftSerial.write("Down");
} //Pin Down
SoftSerial.print("\r\n>");
}
void parseMcode(char * cmd)
{
int code;
code = atoi(cmd);
switch (code) {
case 1:
parsePen(cmd);
break;
}
}
void parseAngle(char * cmd)
{
char * cp_Tmp;
char * cp_Str;
cp_Str = strtok_r(cmd, " ", &cp_Tmp); //now the number is in "cp_Str"
fTargetAngle = atof(cp_Str);
ui16CalculateAngle();
}
#define ACC_DEC_DIST 60
#define STEP_PERIOD_ACC 20 //How often (ms) we accellerate/decellerate
#define STEP_PERIOD_STEP 1
void vStepAccDec(void)
{
previousMillisStep2 = millis();
if (i16_AngleSteps != 0)
{
if ((abs(i16_AngleSteps) < ACC_DEC_DIST) && (ui8_StepPeriod_ms < STEP_PERIOD_MAX))
{
ui8_StepPeriod_ms += STEP_PERIOD_STEP;
}
else if (ui8_StepPeriod_ms > STEP_PERIOD_MIN)
{
ui8_StepPeriod_ms -= STEP_PERIOD_STEP;
}
}
else if (ui16_DistanceSteps > 0)
{
if ((ui16_DistanceSteps < ACC_DEC_DIST) && (ui8_StepPeriod_ms < STEP_PERIOD_MAX))
{
ui8_StepPeriod_ms += STEP_PERIOD_STEP;
}
else if (ui8_StepPeriod_ms > STEP_PERIOD_MIN)
{
ui8_StepPeriod_ms -= STEP_PERIOD_STEP;
}
// Serial.println(ui8_StepPeriod_ms);
}
}
void clear(void)
{
fCurrentX = 0;
fCurrentY = 0;
fCurrentAngle = 0;
SoftSerial.print("\r\n>");
}
void parseCmd(char * cmd)
{
if (SER_CONN) { Serial.write(cmd); }
if (tolower(cmd[0]) == 'g')
{ // gcode
parseGcode(cmd + 1);
// Serial.println("OK");
}
else if (tolower(cmd[0]) == 'm')
{ // mcode
parseMcode(cmd + 1);
// Serial.println("OK");
}
else if (tolower(cmd[0]) == 'c')
{ // clear
clear();
// Serial.println("OK");
}
else if (tolower(cmd[0]) == 'a')
{ // clear
parseAngle(cmd + 1);
// Serial.println("OK");
}
else
{ SoftSerial.print("\r\n>"); }
}
char buf[64];
uint8_t bufindex;
char buf2[64];
uint8_t bufindex2;
char cChar;
void loop()
{
if ((unsigned long)(millis() - previousMillisStep) >= ui8_StepPeriod_ms) //SECOND_PERIOD_ms MOT_STEP_PERIOD_ms
{ previousMillisStep = millis(); StepMotors(); /*Serial.print(">"); */ }
if ((unsigned long)(millis() - previousMillisStep2) >= STEP_PERIOD_ACC) //We accelerate/decelerate
{ vStepAccDec(); }
if (SoftSerial.available() > 0)
{
cChar = SoftSerial.read();
if (cChar == 'n')
{ SoftSerial.println("V0.0.5\r\n>"); }
if ((cChar == '\n'))
{
buf[bufindex] = 0;
parseCmd(buf);
// SoftSerial.print("\r\n>");
bufindex = 0;
}
else
{
buf[bufindex++] = cChar;
}
}
/* if (SoftSerial.available() > 0)
{
char cChar = SoftSerial.read();
if (cChar == 'n')
{ SoftSerial.println("V0.0.5\r\n>"); }
if (SER_CONN) { Serial.write(cChar); }
SoftSerial.print(cChar);
if ((cChar == '\n') || (cChar == '#'))
{
// SoftSerial.print('n');
// if (SER_CONN) { Serial.print("n"); }
// if (SER_CONN) { Serial.write("x"); }
if (SER_CONN) { Serial.print(buf); }
parseCmd(buf);
memset(buf, 0, 64);
bufindex = 0;
}
else
{
buf[bufindex++] = cChar;
}
} */
}