-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturret-atas.ino
executable file
·557 lines (491 loc) · 14.8 KB
/
turret-atas.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/* Turret Control
* Created by: Naufalino Fadel Hutomo
*
*/
//Library
#include <Servo.h>
#include <string.h>
#include <Wire.h>
#include <avr/wdt.h> //library watchdog timer
#define wdt_reset() __asm__ __volatile__ ("wdr")
#define servoTILT 5 //digital pin for camera tilt setpoint signal
#define servoGUN 6 //servo for control gun
#define stepPin 3 //pulse pin for controlliong turret stepper
#define dirPin 4 //pulse pin for controlliong turret stepper
#define stepPin1 8 //pulse pin for controlling stepper, stepPin1. Red
#define stepPin2 9 //pulse pin for controlling stepper, IN2. Blue
#define stepPin3 10 //pulse pin for controlling stepper, IN3. Green
#define stepPin4 11 //pulse pin for controlling stepper, IN4. Black
//#define compassPin A3// pin to control which compass is active, low for compass_cam, high for compass_turret
//Function Definition
void get_setpoint();
void move_cams(float degree);
void move_turret(float degree);
void move_tilt(float degree);
void move_stepper(float degree);
void move_gun(float degree);
void move_all();
//Servo servo_cam; // servo object for camera yaw
Servo servo_tilt; // servo object for camera yaw
Servo servo_gun; // // servo object for gun pitch
//CONSTANT
const float step_RES = 0.088 ; // step resolution is 360/64 step (from motor) / 64 (gearbox). If you want to change the resolution, change the driver configuration. Driver configuration 010 -> quarter step
const float yaw_RES = 0.45; //1.45 = 1.8/4 //nema 17 resolution is 1.8 degree, with gear ratio between motor and turret is 1:4
String readStr;
float cam_set = 0; //for camera pan set condition
float cam_prev = 0 , cam_prev2=0;
float cam_act=0, cam_act_init=0; //heading measurement of camera
float cam_sum=0, cam_last_pid=0; // for PID calculation of camera movement
int cam_stepState;
float tilt_set = 0; //for camera tilt set condition
float tilt_prev = 0 , tilt_prev2=0;
float yaw_set = 0; //for turret yaw set condition
float yaw_act=0,yaw_act_prev=0, yaw_act_prev2=0; //heading measurement of yaw
float yaw_prev = 0;
float gun_set = 0; //for gun pitch set condition
float gun_prev = 0 , gun_prev2=0;
bool step_dir = HIGH; //CW is HIGH
void setup() {
//WD/
wdt_disable();
// Sets pins Mode
//pinMode(stepPin,OUTPUT);
pinMode(stepPin1, OUTPUT);
pinMode(stepPin2, OUTPUT);
pinMode(stepPin3, OUTPUT);
pinMode(stepPin4, OUTPUT);
//servo_cam.attach(servoCAM);
servo_tilt.attach(servoTILT);
servo_gun.attach(servoGUN);
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
Serial.begin(115200); // serial comm to raspi
Serial.print("Setup start");
//Calibrating all actuator
move_gun(0);
move_cams(0);
move_tilt(0);
//WD/
wdt_enable(WDTO_8S); //enable watchdog timer, timeout value 8s
}
void loop() {
if (Serial.available()>0)
{
if(get_setPoint())
{
move_all(); // move all actuator
}
}
}
void watchdog_setup(int t){
//This function set the register for watchdog timer
//WDP3 - WDP2 - WPD1 - WDP0 - time
// 0 0 0 0 16 ms
// 0 0 0 1 32 ms
// 0 0 1 0 64 ms
// 0 0 1 1 0.125 s
// 0 1 0 0 0.25 s
// 0 1 0 1 0.5 s
// 0 1 1 0 1.0 s
// 0 1 1 1 2.0 s
// 1 0 0 0 4.0 s
// 1 0 0 1 8.0 s
// Reset the watchdog reset flag
bitClear(MCUSR, WDRF);
// Start timed sequence
bitSet(WDTCSR, WDCE); //Watchdog Change Enable to clear WD
bitSet(WDTCSR, WDE); //Enable WD
switch (t){
case 8 :
// Set new watchdog timeout value to 8 second
bitSet(WDTCSR, WDP3);
bitSet(WDTCSR, WDP0);
break;
default: //default is 2s
bitSet(WDTCSR, WDP2);
bitSet(WDTCSR, WDP1);
bitSet(WDTCSR, WDP0);
}
// Enable interrupts instead of reset
//bitSet(WDTCSR, WDIE);
}
ISR(WDT_vect) {
// Don't do anything here but we must include this
// block of code otherwise the interrupt calls an
// uninitialized interrupt handler.
}
bool get_setPoint(){ //get setpoint from serial data from server
int idx[4];
char c = Serial.read(); //gets one byte from serial buffer
String s[4]; //array of substring
String temp;
if (c == '\n') //while not end of serial data
{
//do stuff
///DEBUGSerial.println();
///DEBUGSerial.print("captured String is : ");
///DEBUGSerial.println(readStr); //prints string to serial port out
idx[0] = readStr.indexOf(','); //finds location of first ,
s[0] = readStr.substring(0, idx[0]); //captures first data String
idx[1] = readStr.indexOf(',', idx[0]+1 ); //finds location of second ,
s[1] = readStr.substring(idx[0]+1, idx[1]); //captures second data String
idx[2] = readStr.indexOf(',', idx[1]+1 );
s[2] = readStr.substring(idx[1]+1, idx[2]);
s[3] = readStr.substring(idx[2]+1); //captures remain part of data after last ,
for (int i=0; i<4;i++)
{
///DEBUGSerial.print(" first char = ");
///DEBUG Serial.print(s[i]);
switch(s[i][0])
{
case 'c' :
temp = s[i].substring(1);
cam_set = (float) temp.toFloat();
///DEBUGSerial.print(" cam set = ");
///DEBUGSerial.println(cam_set);
break;
case 'g' :
temp = s[i].substring(1);
gun_set = (float) temp.toFloat();
///DEBUGSerial.print(" gun set = ");
///DEBUGSerial.println(gun_set);
break;
case 't' :
temp = s[i].substring(1);
tilt_set = (float) temp.toFloat();
///DEBUGSerial.print(" tilt set = ");
///DEBUGSerial.println(tilt_set);
break;
case 'y' :
temp = s[i].substring(1);
yaw_set = (float) temp.toFloat(); //this data came from nodeMCU that control stepper
///DEBUGSerial.print(" yaw act = ");
///DEBUGSerial.println(yaw_act);
break;
default: break;
}
}
readStr=""; //clears variable for new input
Serial.flush();
//WD
wdt_reset(); //reset the watch dog timer
return true;
}
else
{
readStr += c; //makes the string readString
return false;
}
}
/*
void move_cam(float degree){
float err;
const float tolerance = 3.0;
const float Ki_cam= 0.002, Kp_cam=1.2, Kd_cam=16;
float cam_setd = -degree+ 90; //map to servo degree between 0-180//reverse, EDIT if not reversed
// servo_cam.write(cam_setd);
updatePrev('c');
}*/
void move_tilt(float degree){
float tilt_setd = degree+ 90; //map to servo degree between 0-180. //direction of servo is reversed because of current servo configuration.
// set limit for pitch
if (tilt_setd < 70) //degree < -20
{
tilt_setd = 70;
}else if (tilt_setd > 150) //degree > 60
{
tilt_setd = 150;
}
servo_tilt.write(tilt_setd);
updatePrev('t');
//DEBUG
//Serial.print("\n tilt ");Serial.print(tilt_prev2);Serial.print(tilt_prev);Serial.print(tilt_set);
}
void stepper(){
switch(cam_stepState){
case 0:
digitalWrite(stepPin1, LOW);
digitalWrite(stepPin2, LOW);
digitalWrite(stepPin3, LOW);
digitalWrite(stepPin4, HIGH);
break;
case 1:
digitalWrite(stepPin1, LOW);
digitalWrite(stepPin2, LOW);
digitalWrite(stepPin3, HIGH);
digitalWrite(stepPin4, HIGH);
break;
case 2:
digitalWrite(stepPin1, LOW);
digitalWrite(stepPin2, LOW);
digitalWrite(stepPin3, HIGH);
digitalWrite(stepPin4, LOW);
break;
case 3:
digitalWrite(stepPin1, LOW);
digitalWrite(stepPin2, HIGH);
digitalWrite(stepPin3, HIGH);
digitalWrite(stepPin4, LOW);
break;
case 4:
digitalWrite(stepPin1, LOW);
digitalWrite(stepPin2, HIGH);
digitalWrite(stepPin3, LOW);
digitalWrite(stepPin4, LOW);
break;
case 5:
digitalWrite(stepPin1, HIGH);
digitalWrite(stepPin2, HIGH);
digitalWrite(stepPin3, LOW);
digitalWrite(stepPin4, LOW);
break;
case 6:
digitalWrite(stepPin1, HIGH);
digitalWrite(stepPin2, LOW);
digitalWrite(stepPin3, LOW);
digitalWrite(stepPin4, LOW);
break;
case 7:
digitalWrite(stepPin1, HIGH);
digitalWrite(stepPin2, LOW);
digitalWrite(stepPin3, LOW);
digitalWrite(stepPin4, HIGH);
break;
default:
digitalWrite(stepPin1, LOW);
digitalWrite(stepPin2, LOW);
digitalWrite(stepPin3, LOW);
digitalWrite(stepPin4, LOW);
break;
}
stepper_next();
}
void stepper_next(){
if(step_dir==HIGH){ cam_stepState++;}
if(step_dir==LOW){ cam_stepState--; }
if(cam_stepState>7){cam_stepState=0;}
if(cam_stepState<0){cam_stepState=7; }
}
void move_cams(float degreeSet){
//Serial.println("move stepper");
float err;
float dump;
int step_req;
err = normalDeg(degreeSet - cam_act);
//CW is step_dir LOW
if (err < 0)
{
step_dir=HIGH;
step_req = round(-err/step_RES);
}else
{
step_dir=LOW;
step_req = round(err/step_RES);
}
for(int x = 1; x <= step_req; x++) { //give pulse until degree achieved
//currentMillis = micros();
delay(2);
//frequensy of pulse to move stepper. Config based on datasheet
//if(currentMillis-last_time>=1000){
stepper();
}
if (err>0) //calculate camera direction after movement
{
cam_act = normalDeg(cam_act + step_req * step_RES);
}else
{
cam_act = normalDeg(cam_act - step_req * step_RES);
}
updatePrev('c');
///DEBUG
Serial.print("cam_act = ");
///DEBUG
Serial.print(cam_act);
///DEBUG
Serial.print(" \tcam_set = ");
///DEBUG
Serial.println(cam_set);
///DEBUG Serial.print(" \tstep = ");
///DEBUG Serial.println(step_req);
///DEBUGSerial.print("\n cam ");Serial.print(cam_prev2);Serial.print(cam_prev);Serial.print(cam_act);
}
void move_turret(float degree){
//Function to move turret with nema 23 stepper
float err = normalDeg(degree - yaw_act);
int step_count;
if (err>=0)
{
//digitalWrite(dirPin,LOW); // Enables the motor to move with vector direction upward the axis. In this case, move gear ccw -> move turret in clockwise direction
step_count = round(err / yaw_RES);
}else
{
//digitalWrite(dirPin,HIGH); // Enables the motor to move turret in counterclockwise direction
step_count = round(-err / yaw_RES);
}
/*DEBUG PURPOSE
Serial.print(" step_count = ");
Serial.print(step_count);
*/
//give pulse to stepper
/* COMMENT THIS LINE BC THE TURRET IS MOVED SEPARATELY BY NODEMCU
for (int i=1; i < step_count; i++)
{
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
} */
if (err>0)
{
yaw_act = normalDeg(yaw_act + step_count * yaw_RES);
cam_act = normalDeg(cam_act + step_count * yaw_RES); //to compensate camera direction due to turret movement
}else
{
yaw_act = normalDeg(yaw_act - step_count * yaw_RES);
cam_act = normalDeg(cam_act - step_count * yaw_RES); //to compensate camera direction due to turret movement
}
//move camera to initial setpoint before turret moved
//move_cams(cam_set);
///DEBUG Serial.print("move turret \t");
///DEBUG Serial.print("yaw act = ");
///DEBUG Serial.print(yaw_act);
///DEBUG Serial.print(" \tyaw_set = ");
///DEBUG Serial.print(yaw_set);
///DEBUG Serial.print(" \tcam_act = ");
///DEBUG Serial.print(cam_act);
/*DEBUG PURPOSE
Serial.print("yaw_act = ");
Serial.print(yaw_act);
*/
}
void move_gun(float degree){
float gun_setd = degree; //map to servo degree between 0-180. Reverse, EDIT this line if the servo is not reversed
// set limit for pitch
gun_setd *= 4.5; //4.5 is the ratio between servo degree and gun degree. The factor comes from experiment.
gun_setd += 90; // Map to servo degree between 0-180
if (gun_setd < 20) //degree < -20
{
gun_setd = 20;
}else if (gun_setd > 170) //degree > 60
{
gun_setd = 170;
}
servo_gun.write(gun_setd);
updatePrev('g');
//DEBUGSerial.print("\n gun ");Serial.print(gun_prev2);Serial.print(gun_prev);Serial.print(gun_set);
}
void move_all(){
if(tilt_set != tilt_prev) //if the value change
{
///DEBUGtilt_set = filter('t');
move_tilt(tilt_set);
}
/**/
if(yaw_set != yaw_act) //if the value change
{
move_turret(yaw_set);
}
if(cam_set != cam_act) //if the value change
{
///DEBUGcam_set = filter('c');
move_cams(cam_set);
}
if(gun_set != gun_prev) //if the value change
{
///DEBUGgun_set = filter('g');
move_gun(gun_set);
}
}
float normalDeg(float deg)
{
//normalized degree value into -179.99 until 180
while (deg <= -180)
{
deg += 360;
}
while (deg > 180)
{
deg -= 360;
}
return deg;
}
bool isTolerant(float ex, float real, float tol)
{
// check whether the value is under accuracy of measurement tool
bool ret = false;
if ( ((real - ex) < tol) && ((ex-real) < tol) )
{
ret = true;
}
return ret;
}
float filter (char c)
{
//RECONSIDER ABOUT THE ALGORITHM
// filter the serial data come to Arduino
float prev, prev2, now; // data that need to be filtered
float grad1, grad2;
switch (c){
case 'c':
now= cam_set;
prev = cam_prev;
prev2 = cam_prev2;
break;
case 't':
now= tilt_set;
prev = tilt_prev;
prev2 = tilt_prev2;
break;
case 'y':
now= yaw_act;
prev = yaw_act_prev;
prev2 = yaw_act_prev2;
break;
case 'g':
now= gun_set;
prev = gun_prev;
prev2 = gun_prev2;
break;
default:
break;
}
//check the noise because of parsing error that cause shifting
if ((now > 200) or (now<-200)) // data out of range. Degree are between 0-180 and -179-0
{
return prev;
}
//check the noise because of parsing error
grad1 = (now-prev);
grad2 = (prev-prev2);
if ( ( (now >= (9*prev)) && (now > 10+prev) ) or ((now <= (prev/9) ) && (now < prev-10) ) ) // if the gradient is too high
{
//check whether the signal is a peak noise or a consistent increasing/decreasing signal
return prev;
}else {
return now;
}
}
float updatePrev (char c)
{
// update the previous value after move the actuator
switch (c){
case 'c':
cam_prev2 = cam_prev;
cam_prev = cam_act;
break;
case 't':
tilt_prev2 = tilt_prev;
tilt_prev = tilt_set;
break;
case 'y':
yaw_act_prev2 = yaw_act_prev;
yaw_act_prev = yaw_act;
break;
case 'g':
gun_prev2 = gun_prev;
gun_prev = gun_set;
break;
default:
break;
}
}