forked from TurBoss/turbo-grua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturbo-grua.ino
169 lines (145 loc) · 4.2 KB
/
turbo-grua.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
#include "pinout.h"
#include "robotGeometry.h"
#include "interpolation.h"
#include "fanControl.h"
#include "RampsStepper.h"
#include "queue.h"
#include "command.h"
RampsStepper stepperRotate(Z_STEP_PIN, Z_DIR_PIN, Z_ENABLE_PIN);
RampsStepper stepperLower(Y_STEP_PIN, Y_DIR_PIN, Y_ENABLE_PIN);
RampsStepper stepperHigher(X_STEP_PIN, X_DIR_PIN, X_ENABLE_PIN);
RampsStepper stepperExtruder(E_STEP_PIN, E_DIR_PIN, E_ENABLE_PIN);
RobotGeometry geometry;
Interpolation interpolator;
Queue<Cmd> queue(15);
Command command;
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
//reduction of steppers..
stepperHigher.setReductionRatio(32.0 / 10.0, 200 * 16); //big gear: 32, small gear: 9, steps per rev: 200, microsteps: 16
stepperLower.setReductionRatio(32.0 / 10.0, 200 * 16);
stepperRotate.setReductionRatio(32.0 / 10.0, 200 * 16);
//start positions..
stepperHigher.setPositionRad(PI / 2.0); //90°
stepperLower.setPositionRad(0); // 0°
stepperRotate.setPositionRad(0); // 0°
//enable and init..
setStepperEnable(false);
interpolator.setInterpolation(0, 240, 240, 0, 0, 240, 240, 0);
//interpolator.setInterpolation(0, 0, 0, 0, 0, 0, 0, 0);
Serial.println("start");
}
void setStepperEnable(bool enable) {
stepperRotate.enable(enable);
stepperLower.enable(enable);
stepperHigher.enable(enable);
}
void loop () {
//update and Calculate all Positions, Geometry and Drive all Motors...
interpolator.updateActualPosition();
geometry.set(interpolator.getXPosmm(), interpolator.getYPosmm(), interpolator.getZPosmm());
stepperRotate.stepToPositionRad(geometry.getRotRad());
stepperLower.stepToPositionRad (geometry.getLowRad());
stepperHigher.stepToPositionRad(geometry.getHighRad());
stepperRotate.update();
stepperLower.update();
stepperHigher.update();
if (!queue.isFull()) {
if (command.handleGcode()) {
Serial.print("HANDLE CMD: ");
Serial.println(command.getCmd().id);
queue.push(command.getCmd());
printOk();
}
}
if ((!queue.isEmpty()) && interpolator.isFinished()) {
executeCommand(queue.pop());
}
if (millis() % 500 < 250) {
digitalWrite(LED_BUILTIN, HIGH);
} else {
digitalWrite(LED_BUILTIN, LOW);
}
}
void cmdMove(Cmd (&cmd)) {
Serial.print(cmd.valueX);
Serial.print(", ");
Serial.print(cmd.valueY);
Serial.print(", ");
Serial.print(cmd.valueZ);
Serial.print(", ");
Serial.print(cmd.valueE);
Serial.print(", ");
Serial.println(cmd.valueF);
interpolator.setInterpolation(cmd.valueX, cmd.valueY, cmd.valueZ, 0, cmd.valueF);
}
void cmdDwell(Cmd (&cmd)) {
delay(int(cmd.valueT * 1000));
}
void cmdGripperOn(Cmd (&cmd)) {
}
void cmdGripperOff(Cmd (&cmd)) {
}
void cmdStepperOn() {
setStepperEnable(true);
}
void cmdStepperOff() {
setStepperEnable(false);
}
void cmdFanOn() {
}
void cmdFanOff() {
}
void handleAsErr(Cmd (&cmd)) {
printComment("Unknown Cmd " + String(cmd.id) + String(cmd.num) + " (queued)");
printFault();
}
void executeCommand(Cmd cmd) {
if (cmd.id == -1) {
String msg = "parsing Error";
printComment(msg);
handleAsErr(cmd);
return;
}
if (cmd.valueX == NAN) {
cmd.valueX = interpolator.getXPosmm();
}
if (cmd.valueY == NAN) {
cmd.valueY = interpolator.getYPosmm();
}
if (cmd.valueZ == NAN) {
cmd.valueZ = interpolator.getZPosmm();
}
if (cmd.valueE == NAN) {
cmd.valueE = interpolator.getEPosmm();
}
Serial.print("EXEC CMD: ");
Serial.println(cmd.id);
//decide what to do
if (cmd.id == 'G') {
switch (cmd.num) {
case 0: cmdMove(cmd); break;
case 1: cmdMove(cmd); break;
case 4: cmdDwell(cmd); break;
//case 21: break; //set to mm
//case 90: cmdToAbsolute(); break;
//case 91: cmdToRelative(); break;
//case 92: cmdSetPosition(cmd); break;
default: handleAsErr(cmd);
}
} else if (cmd.id == 'M') {
switch (cmd.num) {
//case 0: cmdEmergencyStop(); break;
case 3: cmdGripperOn(cmd); break;
case 5: cmdGripperOff(cmd); break;
case 17: cmdStepperOn(); break;
case 18: cmdStepperOff(); break;
case 106: cmdFanOn(); break;
case 107: cmdFanOff(); break;
default: handleAsErr(cmd);
}
} else {
handleAsErr(cmd);
}
}