-
Notifications
You must be signed in to change notification settings - Fork 3
/
polar-scanner.ino
325 lines (291 loc) · 6.47 KB
/
polar-scanner.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
/*
* Copyright (c) 2017, Luc Yriarte
* License: BSD <http://www.opensource.org/licenses/bsd-license.php>
*/
#include <stdio.h>
#include <Servo.h>
/* **** **** **** **** **** ****
* Constants
* **** **** **** **** **** ****/
#define BPS_HOST 9600
#define COMMS_BUFFER_SIZE 1024
#define SERIAL_HOST_DELAY_MS 10000
#define SCAN_STEP_DELAY_MS 600
/*
* ultra sonic telemeter mesure
*/
#define INECHO 10
#define TRIGGER 11
#define ECHO_TIMEOUT 100000
#define ECHO2CM(x) (x/60)
#define MAX_CM 1000
/*
* mesure configuration variables
*/
enum {
MAX,
COUNT,
NVARS
};
int mesureConfig[NVARS];
/*
* servo
*/
#define SRV 12
/*
* stepper
*/
#define STEP1 9
#define STEP2 8
#define STEP3 7
#define STEP4 6
#define STEPPERMS 5
/*
* automaton states
*/
enum {
START,
ERROR,
IN_CMD,
IN_VAR,
IN_MAX,
IN_COUNT,
IN_SERVO,
IN_STEPPER,
IN_SCAN_PARAMETERS,
SENSOR_MESURE
};
/* **** **** **** **** **** ****
* Global variables
* **** **** **** **** **** ****/
/*
* servo
*/
Servo servo;
/*
* stepper
*/
byte steps8[] = {
HIGH, LOW, LOW, LOW,
HIGH, HIGH, LOW, LOW,
LOW, HIGH, LOW, LOW,
LOW, HIGH, HIGH, LOW,
LOW, LOW, HIGH, LOW,
LOW, LOW, HIGH, HIGH,
LOW, LOW, LOW, HIGH,
HIGH, LOW, LOW, HIGH,
};
/*
* automaton status
*/
int currentState;
/*
* serial comms buffer
*/
char commsBuffer[COMMS_BUFFER_SIZE];
/* **** **** **** **** **** ****
* Functions
* **** **** **** **** **** ****/
void setup() {
pinMode(INECHO, INPUT);
pinMode(TRIGGER, OUTPUT);
pinMode(STEP1, OUTPUT);
pinMode(STEP2, OUTPUT);
pinMode(STEP3, OUTPUT);
pinMode(STEP4, OUTPUT);
servo.attach(SRV);
Serial.begin(BPS_HOST);
digitalWrite(TRIGGER, LOW);
currentState = START;
mesureConfig[MAX] = MAX_CM;
mesureConfig[COUNT] = 1;
}
/*
* servo command
*/
void servoCommand(int angle) {
servo.write(angle);
}
/*
* stepper command
*/
void step8(int pin1, int pin2, int pin3, int pin4) {
int i=0;
while (i<32) {
digitalWrite(pin1, steps8[i++]);
digitalWrite(pin2, steps8[i++]);
digitalWrite(pin3, steps8[i++]);
digitalWrite(pin4, steps8[i++]);
delay(STEPPERMS);
}
}
void stepperCommand(int steps) {
while (steps) {
if (steps > 0) {
step8(STEP1,STEP2,STEP3,STEP4);
steps--;
}
else {
step8(STEP4,STEP3,STEP2,STEP1);
steps++;
}
}
}
/*
* telemeter mesure
*/
int telemeterMesure() {
unsigned long echoDuration;
unsigned int mesureCm;
unsigned int mesureSum = 0;
for (int i=0; i<mesureConfig[COUNT]; i++) {
digitalWrite(TRIGGER, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER, LOW);
echoDuration = pulseIn(INECHO, HIGH, ECHO_TIMEOUT);
mesureCm = echoDuration ? ECHO2CM(echoDuration) : mesureConfig[MAX];
mesureCm = min(mesureCm,mesureConfig[MAX]);
mesureSum += mesureCm;
}
mesureCm = mesureSum / mesureConfig[COUNT];
return mesureCm;
}
/*
* telemeter scan
*/
int telemeterscan(int stepperStep, int stepperN, int servoStep, int servoN, int servoFrom) {
int stepperInc, servoInc;
int buflen;
buflen = sprintf(commsBuffer, "\n");
for (stepperInc=0; stepperInc<stepperN; stepperInc++) {
for (servoInc=0; servoInc<servoN; servoInc++) {
servoCommand(servoFrom + (servoInc * servoStep));
delay(SCAN_STEP_DELAY_MS);
if (COMMS_BUFFER_SIZE - buflen < 10) {
Serial.print(commsBuffer);
buflen=0;
}
buflen += sprintf(commsBuffer+buflen, "%04d ", telemeterMesure());
}
buflen += sprintf(commsBuffer+buflen, "\n");
stepperCommand(stepperStep);
}
Serial.print(commsBuffer);
}
/*
* userIO
*/
char * userInput(char * message) {
unsigned long timeLoopStart,timeLoop;
int nread = 0;
if (message)
Serial.print(message);
timeLoopStart = timeLoop = millis();
// Serial timeout triggers on full commsBuffer, just wait for first command
while (nread == 0 && timeLoop - timeLoopStart < SERIAL_HOST_DELAY_MS) {
// drop older contents on buffer overflow
while ((nread = Serial.readBytes(commsBuffer, COMMS_BUFFER_SIZE)) == COMMS_BUFFER_SIZE);
timeLoop = millis();
}
commsBuffer[nread] = 0;
return nread == 0 ? NULL : commsBuffer;
}
/*
* automaton
*/
int stateTransition(int currentState) {
int newState = ERROR;
char * input = NULL;
int value = 0;
switch (currentState) {
case START:
newState = IN_CMD;
break;
case ERROR:
Serial.println("ERROR");
newState = START;
break;
case IN_CMD:
if (!(input = userInput("CMD: "))) {
newState = IN_CMD;
break;
}
if (!strcmp(input,"SERVO"))
newState = IN_SERVO;
else if (!strcmp(input,"STEPPER"))
newState = IN_STEPPER;
else if (!strcmp(input,"MESURE"))
newState = SENSOR_MESURE;
else if (!strcmp(input,"SCAN"))
newState = IN_SCAN_PARAMETERS;
else if (!strcmp(input,"SET"))
newState = IN_VAR;
break;
case IN_VAR:
if (!(input = userInput("VARIABLE: ")))
while (!(input = userInput(NULL)));
if (!strcmp(input,"MAX"))
newState = IN_MAX;
else if (!strcmp(input,"COUNT"))
newState = IN_COUNT;
break;
case IN_MAX:
if (!(input = userInput("DISTANCE MAX: ")))
while (!(input = userInput(NULL)));
mesureConfig[MAX] = atoi(input);
newState = IN_CMD;
break;
case IN_COUNT:
if (!(input = userInput("MESURE COUNT: ")))
while (!(input = userInput(NULL)));
mesureConfig[COUNT] = atoi(input);;
newState = IN_CMD;
break;
case IN_SERVO:
if (!(input = userInput("ANGLE: ")))
while (!(input = userInput(NULL)));
value = atoi(input);
servoCommand(value);
newState = IN_CMD;
break;
case IN_STEPPER:
if (!(input = userInput("STEPS: ")))
while (!(input = userInput(NULL)));
value = atoi(input);
stepperCommand(value);
newState = IN_CMD;
break;
case IN_SCAN_PARAMETERS:
int stSt, stNb, srSt, srNb, srFr;
if (!(input = userInput("STEPPER STEP: ")))
while (!(input = userInput(NULL)));
stSt = atoi(input);
if (!(input = userInput("STEPPER NBR: ")))
while (!(input = userInput(NULL)));
stNb = atoi(input);
if (!(input = userInput("SERVO STEP: ")))
while (!(input = userInput(NULL)));
srSt = atoi(input);
if (!(input = userInput("SERVO NBR: ")))
while (!(input = userInput(NULL)));
srNb = atoi(input);
if (!(input = userInput("SERVO FROM: ")))
while (!(input = userInput(NULL)));
srFr = atoi(input);
telemeterscan(stSt, stNb, srSt, srNb, srFr);
newState = IN_CMD;
break;
case SENSOR_MESURE:
value = telemeterMesure();
Serial.println(value);
newState = IN_CMD;
break;
default:
newState = ERROR;
break;
}
return newState;
}
void loop() {
currentState = stateTransition(currentState);
}