-
Notifications
You must be signed in to change notification settings - Fork 0
/
PTZ_simple.ino
370 lines (340 loc) · 9.98 KB
/
PTZ_simple.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
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
#include <elapsedMillis.h>
elapsedMillis timeElapsed; //declare global if you don't want it reset every time loop runs
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *xMotor = AFMS.getStepper(400, 1);
Adafruit_StepperMotor *yMotor = AFMS.getStepper(400, 2);
bool xDir;
bool yDir;
bool xJoyCW;
bool xJoyCCW;
bool yJoyCW;
bool yJoyCCW;
bool mSet1;
bool mSet2;
bool mGoTo1;
bool mGoTo2;
long positions[2];
long xSet1 = 0;
long ySet1 = 0;
long xSet2 = 0;
long ySet2 = 0;
volatile long actualXPos = 0;
volatile long actualYPos = 0;
volatile long actualZPos = 0;
long positionX2reach;
long positionY2reach;
long positionZ2reach;
long newXPosition;
long newYPosition;
long newZPosition;
long constrainXDistance;
long constrainYDistance;
long constrainZDistance;
long travelledXDistance;
long travelledYDistance;
long travelledZDistance;
long distanceToXPos;
long distanceToYPos;
long distanceToZPos;
float totalXDistance;
float totalYDistance;
float totalZDistance;
long recXPos1;
long recYPos1;
long recZPos1;
long recXPos2;
long recYPos2;
long recZPos2;
long recXPos3;
long recYPos3;
long recZPos3;
long recXPos4;
long recYPos4;
long recZPos4;
long recXPos5;
long recYPos5;
long recZPos5;
int oldinByte;
unsigned long previousMillis = 0;
unsigned long currentMillis;
const int minX = 60;
const int maxX = 150;
const int minY = 60;
const int maxY = 150;
const int constrainedLimit = 120; // how many steps does the acceleration last
const int startingXSpeed = 30; // X accel start speed
const int startingYSpeed = 30; // Y accel start speed
//const int myArray[30]={0,1,1,2,3,4,5,6,7,8,10,11,12,14,15,16,18,19,20,22,23,24,25,26,27,28,29,29,30}; // half sinewave (30 p-p)
//const int myArray[30]={0,0,0,1,1,1,2,3,3,4,5,6,7,8,9,10,11,12,14,15,16,18,19,21,22,24,25,27,28,30}; // quarter sinewave (60 p-p)
//const int myArray[30]={0,0,0,0,1,1,1,1,2,2,2,3,3,4,4,5,6,6,7,8,8,9,10,10,11,12,13,13,14,15}; // quarter sinewave (30 p-p)
int logAccel;
int Xread;
int Yread;
int err;
int e2;
boolean autoPos;
boolean motorXDir;
boolean motorYDir;
boolean newMove = false;
boolean buttonPressed = false;
boolean joyXCentral = false;
boolean joyYCentral = false;
boolean xLeading;
void setup() {
Serial.begin(9600);
AFMS.begin();
//TWBR = ((F_CPU /400000l) - 16) / 2; // Change the i2c clock to 400KHz
pinMode (2, INPUT_PULLUP);
pinMode (3, INPUT_PULLUP);
pinMode (4, INPUT_PULLUP);
pinMode (5, INPUT_PULLUP);
pinMode (6, INPUT_PULLUP);
pinMode (7, INPUT_PULLUP);
pinMode (8, INPUT_PULLUP);
pinMode (9, INPUT_PULLUP);
}
void loop() {
xJoyCW = digitalRead(2);
xJoyCCW = digitalRead(3);
yJoyCW = digitalRead(4);
yJoyCCW = digitalRead(5);
mSet1 = digitalRead(6);
mSet2 = digitalRead(7);
mGoTo1 = digitalRead(8);
mGoTo2 = digitalRead(9);
if (!xJoyCW) {
autoPos = false;
xMotor->onestep(FORWARD, DOUBLE);
actualXPos++;
}
if (!xJoyCCW) {
autoPos = false;
xMotor->onestep(BACKWARD, DOUBLE);
actualXPos--;
}
if (!yJoyCW) {
autoPos = false;
yMotor->onestep(FORWARD, DOUBLE);
actualYPos++;
}
if (!yJoyCCW) {
autoPos = false;
yMotor->onestep(BACKWARD, DOUBLE);
actualYPos--;
}
if (!mSet1) {
Serial.println("Record Pos1");
recXPos1 = actualXPos;
recYPos1 = actualYPos;
Serial.println(actualXPos);
Serial.println(actualYPos);
}
if (!mSet2) {
Serial.println("Record Pos2");
recXPos2 = actualXPos;
recYPos2 = actualYPos;
Serial.println(actualXPos);
Serial.println(actualYPos);
}
if (!mGoTo1 && !buttonPressed) {
travelledXDistance = 0;
Serial.println("Go To Pos1");
newXPosition = recXPos1;
newYPosition = recYPos1;
Serial.print("x: ");
Serial.print(newXPosition);
Serial.print(", y: ");
Serial.println(newYPosition);
newMove = true;
autoPos = true;
buttonPressed = true;
}
if (!mGoTo2 && !buttonPressed) {
travelledXDistance = 0;
Serial.println("Go To Pos2");
newXPosition = recXPos2;
newYPosition = recYPos2;
Serial.print("x: ");
Serial.print(newXPosition);
Serial.print(", y: ");
Serial.println(newYPosition);
newMove = true;
autoPos = true;
buttonPressed = true;
}
if (autoPos) {
goToPosition(newXPosition, newYPosition, newYPosition);
}
}
void goToPosition(long positionX2reach, long positionY2reach, long positionZ2reach) {
if (newMove) {
travelledXDistance = 0;
totalXDistance = abs(actualXPos - positionX2reach);
totalYDistance = abs(actualYPos - positionY2reach);
if (distanceToXPos > distanceToYPos) {
xLeading = true;
}
err = (totalXDistance>totalYDistance ? totalXDistance : -totalYDistance)/2;
newMove = false;
}
if (actualXPos > positionX2reach) {
motorXDir = false;
}
else if (actualXPos < positionX2reach) {
motorXDir = true;
}
if (actualYPos > positionY2reach) {
motorYDir = false;
}
else if (actualYPos < positionY2reach) {
motorYDir = true;
}
if (xLeading) {
if (actualXPos != positionX2reach) {
e2 = err;
distanceToXPos = abs(actualXPos - positionX2reach);
distanceToYPos = abs(actualYPos - positionY2reach);
travelledXDistance = totalXDistance-distanceToXPos;
if (travelledXDistance <= distanceToXPos) {
constrainXDistance = constrain(travelledXDistance, 0, constrainedLimit);
int stepperAccel = map(constrainXDistance, 0, constrainedLimit, startingXSpeed, 0);
//logAccel = myArray[stepperAccel];
if (timeElapsed >= stepperAccel) { // replace stepperAccel with logAccel
if (e2 >-distanceToXPos) {
err -= distanceToYPos;
if (motorXDir) {
xMotor->step(1, FORWARD, DOUBLE);
actualXPos++;
}
else {
xMotor->step(1, BACKWARD, DOUBLE);
actualXPos--;
}
}
if (e2 < distanceToYPos) {
err += distanceToXPos;
if (motorYDir) {
yMotor->step(1, FORWARD, DOUBLE);
actualYPos++;
}
else {
yMotor->step(1, BACKWARD, DOUBLE);
actualYPos--;
}
}
timeElapsed = 0;
buttonPressed = false;
}
}
if (travelledXDistance > distanceToXPos) {
constrainXDistance = constrain(distanceToXPos, 0, constrainedLimit);
int stepperAccel = map(constrainXDistance, 0, constrainedLimit, startingXSpeed, 0);
//logAccel = myArray[stepperAccel];
if (timeElapsed >= stepperAccel) { // replace stepperAccel with logAccel
if (e2 >-distanceToXPos) {
err -= distanceToYPos;
if (motorXDir) {
xMotor->step(1, FORWARD, DOUBLE);
actualXPos++;
}
else {
xMotor->step(1, BACKWARD, DOUBLE);
actualXPos--;
}
}
if (e2 < distanceToYPos) {
err += distanceToXPos;
if (motorYDir) {
yMotor->step(1, FORWARD, DOUBLE);
actualYPos++;
}
else {
yMotor->step(1, BACKWARD, DOUBLE);
actualYPos--;
}
}
timeElapsed = 0;
buttonPressed = false;
}
}
}
}
if (!xLeading) {
if (actualYPos != positionY2reach) {
e2 = err;
distanceToXPos = abs(actualXPos - positionX2reach);
distanceToYPos = abs(actualYPos - positionY2reach);
travelledYDistance = totalYDistance-distanceToYPos;
if (travelledYDistance <= distanceToYPos) {
constrainYDistance = constrain(travelledYDistance, 0, constrainedLimit);
int stepperAccel = map(constrainYDistance, 0, constrainedLimit, startingYSpeed, 0);
//logAccel = myArray[stepperAccel];
if (timeElapsed >= stepperAccel) { // replace stepperAccel with logAccel
if (e2 >-distanceToXPos) {
err -= distanceToYPos;
if (motorXDir) {
xMotor->step(1, FORWARD, DOUBLE);
actualXPos++;
}
else {
xMotor->step(1, BACKWARD, DOUBLE);
actualXPos--;
}
}
if (e2 < distanceToYPos) {
err += distanceToXPos;
if (motorYDir) {
yMotor->step(1, FORWARD, DOUBLE);
actualYPos++;
}
else {
yMotor->step(1, BACKWARD, DOUBLE);
actualYPos--;
}
}
timeElapsed = 0;
buttonPressed = false;
}
}
if (travelledYDistance > distanceToYPos) {
constrainYDistance = constrain(distanceToYPos, 0, constrainedLimit);
int stepperAccel = map(constrainYDistance, 0, constrainedLimit, startingYSpeed, 0);
//logAccel = myArray[stepperAccel];
if (timeElapsed >= stepperAccel) { // replace stepperAccel with logAccel
if (e2 >-distanceToXPos) {
err -= distanceToYPos;
if (motorXDir) {
xMotor->step(1, FORWARD, DOUBLE);
actualXPos++;
}
else {
xMotor->step(1, BACKWARD, DOUBLE);
actualXPos--;
}
}
if (e2 < distanceToYPos) {
err += distanceToXPos;
if (motorYDir) {
yMotor->step(1, FORWARD, DOUBLE);
actualYPos++;
}
else {
yMotor->step(1, BACKWARD, DOUBLE);
actualYPos--;
}
}
timeElapsed = 0;
buttonPressed = false;
}
}
}
}
if (actualXPos==positionX2reach && actualYPos==positionY2reach) {
xMotor->release();
yMotor->release();
autoPos = false;
buttonPressed = false;
}
}