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
|
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
!
-
|
!
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
!
-
!
-
|
|
|
|
|
|
-
|
|
|
!
-
|
|
|
|
|
|
|
|
|
!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
!
-
|
-
|
!
|
!
-
-
|
|
!
|
!
-
|
|
!
-
|
|
-
|
|
!
|
|
|
|
|
-
|
|
|
|
!
|
|
!
-
|
!
-
|
!
-
|
!
-
|
|
!
-
|
-
|
|
!
-
|
|
!
!
-
|
|
|
|
|
!
-
|
|
|
|
|
|
!
-
|
|
|
|
-
|
-
-
|
-
!
!
-
|
-
!
!
|
!
|
-
|
|
-
-
|
-
|
-
|
!
-
|
-
|
-
|
|
!
|
!
!
|
|
!
!
!
|
-
|
-
|
-
|
!
-
|
-
|
-
|
|
!
|
!
!
|
|
|
!
!
|
-
|
!
|
-
|
|
|
!
!
-
|
|
|
!
-
-
-
!
-
|
!
-
|
-
|
|
|
!
!
!
-
|
|
-
!
|
|
|
|
|
|
|
|
-
|
|
|
!
|
-
|
!
|
|
-
|
|
!
-
|
|
!
|
!
| #include <Wire.h>
#include <wI2cLcdACM1602.h>
wI2cLcdACM1602 lcd;
char StrBuf[17];
uint8_t read_line[128];
int read_line_len;
String line_1st;
uint8_t prev_line[128];
int prev_line_len;
String line_2nd;
String inputString;
void putLine(String x){
line_1st=line_2nd;
line_2nd=x;
char lcdl[16];
char ch;
int i;
for(i = 0; i < 16; i++) {lcdl[i]=' ';}
for(i=0; i<16;i++){lcdl[i]=line_1st.charAt(i);}
lcd.setCursor(0,0);
for(i=0;i<16;i++) {lcd.print(lcdl[i]);}
for(i = 0; i < 16; i++) {lcdl[i]=' ';}
for(i=0; i<16;i++){lcdl[i]=line_2nd.charAt(i);}
lcd.setCursor(0,1);
for(i=0;i<16;i++) {lcd.print(lcdl[i]);}
}
int LED_IS_ON;
#define ADDRESS 0x52
uint16_t distance;
uint16_t distance_tmp;
uint8_t data_cnt;
#include "SparkFun_ProDriver_TC78H670FTG_Arduino_Library.h" PRODRIVER myProDriver;
uint32_t current_pulse;
double position; uint32_t zero_pulse;
int direction;
void setup() {
Serial.begin(115200);
Serial.println("slide table ex03, SparkFun ProDriver TC78H670FTG, distance...mtof171010c0");
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
direction=0;
current_pulse=0;
position=0.0;
myProDriver.begin();
Wire.begin();
pinMode(13, OUTPUT);
lcd.begin();
lcd.clear(); lcd.noBlink(); lcd.noCursor();
delay(1000);
}
String rb(String x){
String rtn=x;
while(rtn.startsWith(" ")){
rtn=rtn.substring(1);
}
return rtn;
}
boolean rkey(String key, String x, String *rest){
if(x.startsWith(key)){
rest[0]=x.substring(key.length());
return true;
}
return false;
}
boolean isDec(char c){
if('0'<=c && c<='9') return true;
return false;
}
boolean rpint(String x, int *n, String *rest){
int nx=0;
int cx=0;
if(!isDec(x.charAt(0))){
rest[0]=x;
return false;
}
cx=x.charAt(0)-'0';
nx=cx;
n[0]=nx;
if(x.length()<=0) return true;
x=x.substring(1);
while(isDec(x.charAt(0))){
cx=x.charAt(0)-'0';
nx=nx*10+cx;
if(x.length()<=0) break;
x=x.substring(1);
}
rest[0]=x;
n[0]=nx;
}
void return_result(String x){
Serial.println(x);
}
void setPulse(uint16_t x){
current_pulse =x;
}
void setPosition(double x){
position =x;
}
int16_t pos2pul(double x){
uint32_t rtn=(int)(100.0*x);
return rtn;
}
void action(int32_t x){
Serial.println("action("+String(x)+")");
if(x>0){
myProDriver.step(x, direction); current_pulse=current_pulse+x;
}
else{
myProDriver.step(-x, 1-direction); current_pulse=current_pulse+x;
}
}
void moveTo(double x){
Serial.println("moveto("+String(x)+")");
double relative=x-position;
int32_t r_pulse=pos2pul(relative);
action(r_pulse);
position=x;
}
void parse_line(String line){
String l=rb(line);
String rest[1];
int nx[1];
Serial.println("l="+l);
if(rkey("get ",l,rest)){
l=rb(rest[0]);
if(rkey("current",l,rest)){
String sx="current="+String(position);
return_result(sx);
}
if(rkey("distance",l,rest)){
String sx="distance="+String(distance)+" mm.";
return_result(sx);
}
Serial.println("....");
}
else
if(rkey("set ",l,rest)){
l=rb(rest[0]);
int n;
if(rkey("current ",l,rest)){
l=rb(rest[0]);
if(rpint(l,nx,rest)){
n=nx[0];
position=(double)n;
if(rkey(".",l,rest)){
double sub1=0.0;
if(rpint(l,nx,rest)){
n=nx[0];
for(int i=1;i<=10;i++){
sub1=(double)n/(pow(10.0,i));
if(sub1<1.0) break;
}
position=position+sub1;
}
}
String sx="set current position="+String(position);
return_result(sx);
}
}
}
else
if(rkey("moveTo ",l,rest)){
l=rb(rest[0]);
if(rpint(l,nx,rest)){
int n=nx[0];
double px=(double)n;
if(rkey(".",l,rest)){
double sub1=0.0;
if(rpint(l,nx,rest)){
n=nx[0];
for(int i=1;i<=10;i++){
sub1=(double)n/(pow(10.0,i));
if(sub1<1.0) break;
}
px=px+sub1;
}
}
String sx="move current position to="+String(position);
moveTo(px);
return_result(sx);
}
}
else
if(rkey("reverse",l,rest)){
direction=1-direction;
}
else
if(rkey("reset",l,rest)){
direction=0;
current_pulse=0;
position=0.0;
}
}
unsigned long last_tx_time = 0;
void xserialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\n') {
putLine(inputString);
parse_line(inputString);
inputString="";
}
}
}
void loop() {
digitalWrite(13, LOW);
delay(5);
Wire.beginTransmission(ADDRESS);
Wire.write(0xD3);
Wire.endTransmission(false);
Wire.requestFrom(ADDRESS, 2);
data_cnt = 0;
distance = 0;
distance_tmp = 0;
while(Wire.available())
{
distance_tmp = Wire.read();
distance = (distance << (data_cnt*8)) | distance_tmp;
data_cnt++;
}
digitalWrite(13, HIGH);
delay(45);
if(!digitalRead(9)){
myProDriver.step(200, 0); delay(20);
}
if(!digitalRead(10)){
myProDriver.step(200, 1); delay(20);
}
xserialEvent();
}
|