-
Notifications
You must be signed in to change notification settings - Fork 3
/
tacovr.ino
266 lines (215 loc) · 7.01 KB
/
tacovr.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
#include <nI2C.h>
#include <AccelStepper.h>
CI2C::Handle pixyR;
CI2C::Handle pixyL;
AccelStepper stepperR(AccelStepper::DRIVER, 3, 2);
AccelStepper stepperL(AccelStepper::DRIVER, 5, 4);
AccelStepper stepperZ(AccelStepper::DRIVER, 7, 6);
uint8_t buf[256];
uint8_t buf2[256];
uint8_t getbl[6];
uint8_t lampon[6];
int CALIBXL = 143; // fill in before use!!
int CALIBYL = 63; // fill in before use!!
int CALIBXR = 147; // fill in before use!!
int CALIBYR = 68; // fill in before use!!
//int triggerxl = CALIBXL;
//int triggeryl = CALIBYL;
//int triggerxr = CALIBXR;
//int triggeryr = CALIBYR;
int MAXSPEED = 2000; // max 2800
int MAXACCEL = 500; // max 25000
int MAXRSPEED = 500; // max 1000
int MAXRACCEL = 500; // max 1000
int newxr = 0;
int newyr = 0;
int newxl = 0;
int newyl = 0;
int INIT_L = 1;
int INIT_R = 2;
int WAIT_L = 3;
int STEP_L = 4;
int WAIT_R = 5;
int STEP_R = 6;
int state = 0;
uint32_t lastpoll = 0;
void setup()
{
// getBlocks request
getbl[0] = 174;
getbl[1] = 193;
getbl[2] = 32;
getbl[3] = 2;
getbl[4] = 1; // only return sig 1
getbl[5] = 1; // return max 1 block
// lights on request
lampon[0] = 174;
lampon[1] = 193;
lampon[2] = 22;
lampon[3] = 2;
lampon[4] = 1;
lampon[5] = 1;
Serial.begin(115200);
pixyR = nI2C->RegisterDevice(0x54, 1, CI2C::Speed::FAST);
pixyL = nI2C->RegisterDevice(0x55, 1, CI2C::Speed::FAST);
nI2C->Write(pixyR, lampon, 6);
nI2C->Write(pixyL, lampon, 6);
stepperL.setMaxSpeed(MAXSPEED);
stepperL.setAcceleration(MAXACCEL);
stepperR.setMaxSpeed(MAXSPEED);
stepperR.setAcceleration(MAXACCEL);
stepperZ.setMaxSpeed(MAXRSPEED);
stepperZ.setAcceleration(MAXRACCEL);
Serial.println("Let's go");
state = INIT_L;
Serial.println("INIT_L");
}
void RxCallbackL(const uint8_t status) {
if (status == 0)
{
//Serial.println("R: received:");
if(buf2[2] != 33) {
Serial.println("L: unexpected payload type!");
Serial.println(buf2[2]);
}
else if(buf2[3] != 14) {
if(buf2[3] != 0) {
Serial.println("L: unexpected payload size!");
Serial.println(buf2[3]);
}
}
else {
uint8_t newy = buf2[11]<<8|buf2[10];
newyl = newy;
int posy = stepperZ.currentPosition();
uint8_t newx = buf2[9]<<8|buf2[8];
newxl = newx;
int posx = stepperL.currentPosition();
// if (state == WAIT_L && (abs(newy-triggeryl) > 5 || abs(newx-triggerxl) > 5)) {
if (state == WAIT_L && (abs(newy-CALIBYL) > 8 || abs(newx-CALIBXL) > 8)) {
state = STEP_L;
Serial.println("STEP_L");
}
if(state == STEP_L || (state == INIT_L && abs(newy-CALIBYL) <= 20 && abs(newx-CALIBXL) <= 20)) {
// stop condition
if(abs(newy-CALIBYL) <= 5 && abs(newx-CALIBXL) <= 5) {
stepperL.moveTo(posx);
stepperZ.moveTo(posy);
if(state == INIT_L) {
state = INIT_R;
Serial.println("INIT_R");
} else {
state = WAIT_R;
Serial.println("WAIT_R");
// triggerxr = newxr;
// triggeryr = newyr;
}
}
// otherwise move
else {
int newposy = posy-10*(newy-CALIBYL); // TODO more precise factor
stepperZ.moveTo(newposy);
int newposx = posx+10*(CALIBXL-newx); // TODO more precise factor
if(newposx > 900)
newposx = 900;
else if (newposx < -900)
newposx = -900;
stepperR.moveTo(-newposx);
stepperL.moveTo(newposx);
}
}
}
}
else
{
Serial.print("R: rx error: ");
Serial.println(status);
}
}
void RxCallbackR(const uint8_t status) {
if (status == 0)
{
//Serial.println("R: received:");
if(buf[2] != 33) {
Serial.println("R: unexpected payload type!");
Serial.println(buf[2]);
}
else if(buf[3] != 14) {
if(buf[3] != 0) {
Serial.println("R: unexpected payload size!");
Serial.println(buf[3]);
}
}
else {
uint8_t newy = buf[11]<<8|buf[10];
int posy = stepperZ.currentPosition();
newyr = newy;
uint8_t newx = buf[9]<<8|buf[8];
int posx = stepperR.currentPosition();
newxl = newx;
// if (state == WAIT_R && (abs(newy-triggeryr) > 5 || abs(newx-triggerxr) > 5)) {
if (state == WAIT_R && (abs(newy-CALIBYR) > 8 || abs(newx-CALIBXR) > 8)) {
state = STEP_R;
Serial.println("STEP_R");
}
if (state == STEP_R || (state == INIT_R && abs(newy-CALIBYR) <= 20 && abs(newx-CALIBXR) <= 20)) {
// check stop condition
if(abs(newy-CALIBYR) <= 5 && abs(newx-CALIBXR) <= 5) { // TODO add speed (close to) 0 check
stepperR.moveTo(posx);
stepperZ.moveTo(posy);
state = WAIT_L;
Serial.println("WAIT_L");
// triggerxl = newxl;
// triggeryl = newyl;
}
// otherwise move
else {
int newposy = posy-10*(CALIBYR-newy); // TODO more precise factor
stepperZ.moveTo(newposy);
int newposx = posx+10*(CALIBXR-newx); // TODO more precise factor
if(newposx > 900)
newposx = 900;
else if (newposx < -900)
newposx = -900;
stepperR.moveTo(newposx);
stepperL.moveTo(-newposx);
}
}
}
}
else
{
Serial.print("R: rx error: ");
Serial.println(status);
}
}
void loop()
{
uint32_t m = micros();
if(m > lastpoll+1000000/60) {
lastpoll = m;
uint8_t status = nI2C->Write(pixyR, getbl, 6);
if (status) {
Serial.print("R: write status: ");
Serial.println(status);
}
status = nI2C->Write(pixyL, getbl, 6);
if (status) {
Serial.print("L: write status: ");
Serial.println(status);
}
status = nI2C->Read(pixyR, (uint8_t*)buf, 20, RxCallbackR);
if (status) {
Serial.print("R: read status: ");
Serial.println(status);
}
status = nI2C->Read(pixyL, (uint8_t*)buf2, 20, RxCallbackL);
if (status) {
Serial.print("L: read status: ");
Serial.println(status);
}
}
stepperR.run();
stepperL.run();
stepperZ.run();
}