-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
384 lines (327 loc) · 9.63 KB
/
main.c
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
#include "common.h"
#define FUNC_CHS 0
#define FUNC_CAR 1
#define FUNC_SELECT FUNC_CAR
#if (FUNC_SELECT == FUNC_CAR)
#define MOTOR_PERIOD 150//3000
#define MOTOR_CHS (0x0F)
#define SERVO_PERIOD 30000
#define SERVO_CHS (0x01<<4)
#define SERVO_DEF 1700
#define SERVO_MAX 2050
#define SERVO_MIN 1350
#define LEDS_PERIOD 100
#define LEDS_CHS (0x01FF<<5)
#define MAX_SPEED 5
static pthread_t tid_key;
static pthread_t tid_remoter;
static int start = 0;
static void *thread_key(void *arg)
{
struct input_event ev;
int speed = 0;
int fd = kbd_init(KBD_KEY_PATH);
printf("%s\n", __func__);
if (fd < 0) {
printf("kbd_init failed: %s\n", KBD_KEY_PATH);
return 0;
}
for (;;) {
if (kbd_read(fd, &ev) < 0) {
printf("kbd_read failed!\n");
continue;
}
if (EV_KEY != ev.type) {
continue;
}
switch (ev.code) {
case BTN_1:
if (1 == ev.value) {
start = !start;
printf("start=%d\n", start);
motor_state(start);
pwm_ctrl(MOTOR_CHS, start);
leds_ctrl(start?speed:0);
if (start) {
speed = 0;
pwm_duty(MOTOR_CHS, (MOTOR_PERIOD/5)*(MAX_SPEED-speed));
}
}
break;
case BTN_2:
if (1 == ev.value) {
if (++speed >= (MAX_SPEED+1)) {
speed = 0;
}
printf("start=%d, speed=%d\n", start, (MAX_SPEED-speed));
pwm_duty(MOTOR_CHS, (MOTOR_PERIOD/5)*(MAX_SPEED-speed));
leds_ctrl(speed);
}
break;
}
}
return 0;
}
static void *thread_remoter(void *arg)
{
struct input_event ev;
int motor_duty = 0;
int servo_duty = SERVO_DEF;
int fd = 0;//kbd_init(KBD_REMOTER_PATH);
printf("%s\n", __func__);
for (;;) {
fd = kbd_init(KBD_REMOTER_PATH);
if (fd > 0)
break;
printf("kbd_init failed: %s\n", KBD_REMOTER_PATH);
sleep(2);
}
for (;;) {
if (kbd_read(fd, &ev) < 0) {
printf("kbd_read failed!\n");
sleep(2);
fd = kbd_init(KBD_REMOTER_PATH);
continue;
}
if (EV_KEY != ev.type) {
//printf("ev.type=0x%x\n", ev.type);
continue;
}
switch (ev.code) {
case KEY_UP:
case KEY_W:
printf("up -- ");
if (0 == ev.value) {
motor_duty = MOTOR_PERIOD;
pwm_duty(MOTOR_CHS, motor_duty);
pwm_ctrl(MOTOR_CHS, 0x0);
motor_state(0);
printf("stop!\n\n");
}
else if (1 == ev.value) {
motor_duty = 0;
pwm_duty(MOTOR_CHS, motor_duty);
pwm_ctrl(MOTOR_CHS, 0x01);
motor_state(1);
printf("run = %d\n", motor_duty);
}
else if (2 == ev.value) {
if (motor_duty > 0) {
motor_duty -= 200;
}
pwm_duty(MOTOR_CHS, motor_duty);
printf("burst = %d\n", motor_duty);
}
break;
case KEY_DOWN:
case KEY_S:
printf("dn -- ");
if (0 == ev.value) {
motor_duty = MOTOR_PERIOD;
pwm_duty(MOTOR_CHS, motor_duty);
pwm_ctrl(MOTOR_CHS, 0x0);
motor_state(0);
printf("stop!\n\n");
}
else if (1 == ev.value) {
motor_duty = 0;
pwm_duty(MOTOR_CHS, motor_duty);
pwm_ctrl(MOTOR_CHS, 0x01);
motor_state(2);
printf("run = %d\n", motor_duty);
}
else if (2 == ev.value) {
if (motor_duty > 0) {
motor_duty -= 200;
}
pwm_duty(MOTOR_CHS, motor_duty);
printf("burst = %d\n", motor_duty);
}
break;
case KEY_LEFT:
case KEY_A:
printf("lt -- ");
if (0 == ev.value) {
servo_duty = SERVO_DEF;
pwm_duty(SERVO_CHS, servo_duty);
printf("stop!\n\n");
}
else {
/*if (servo_duty < SERVO_MAX) {
servo_duty += 100;
}*/
servo_duty = SERVO_MIN;
pwm_duty(SERVO_CHS, servo_duty);
printf("burst = %d\n", servo_duty);
}
pwm_ctrl(SERVO_CHS, 0x1);
break;
case KEY_RIGHT:
case KEY_D:
printf("rt -- ");
if (0 == ev.value) {
servo_duty = SERVO_DEF;
pwm_duty(SERVO_CHS, servo_duty);
printf("stop!\n\n");
}
else {
/*if (servo_duty > SERVO_MIN) {
servo_duty -= 100;
}*/
servo_duty = SERVO_MAX;
pwm_duty(SERVO_CHS, servo_duty);
printf("burst = %d\n", servo_duty);
}
pwm_ctrl(SERVO_CHS, 0x1);
break;
/*case KEY_ESC:
goto STOP;*/
}
}
return 0;
}
int main(int argc, char *argv[])
{
struct input_event ev;
printf("robotic %s\n", __func__);
if (pru_start(PATH_PRU0_0) < 0)
goto STOP;
if (pru_start(PATH_PRU0_1) < 0)
goto STOP;
if (pru_start(PATH_PRU1_0) < 0)
goto STOP;
if (pru_start(PATH_PRU1_1) < 0)
goto STOP;
if (leds_init() < 0)
goto STOP;
if (pwm_init() < 0)
goto STOP;
// motor
motor_init();
pwm_duty(MOTOR_CHS, MOTOR_PERIOD);
pwm_peroid(MOTOR_CHS, MOTOR_PERIOD);
pwm_ctrl(MOTOR_CHS, 0x0);
// servo
pwm_duty(SERVO_CHS, SERVO_DEF);
pwm_peroid(SERVO_CHS, SERVO_PERIOD);
pwm_ctrl(SERVO_CHS, 0x1);
// LEDS
pwm_duty(LEDS_CHS, LEDS_PERIOD/2);
pwm_peroid(LEDS_CHS, LEDS_PERIOD);
pwm_ctrl(LEDS_CHS, 0x1);
if (pthread_create(&tid_key, NULL, thread_key, NULL) < 0) {
printf("Failed: create thread_key!\n");
goto STOP;
}
if (pthread_create(&tid_remoter, NULL, thread_remoter, NULL) < 0) {
printf("Failed: create thread_remoter!\n");
goto STOP;
}
for (;;) {
if (!start) {
sleep(1);
continue;
}
if (start) {
pwm_duty(LEDS_CHS, 0);
sleep(2);
pwm_duty(LEDS_CHS, LEDS_PERIOD/2);
sleep(2);
}
}
STOP:
printf("Stopped!!!\n");
leds_deinit();
motor_deinit();
pwm_ctrl(PWM_ALL, 0x00);
pwm_deinit();
pru_stop(PATH_PRU0_0);
pru_stop(PATH_PRU0_1);
pru_stop(PATH_PRU1_0);
pru_stop(PATH_PRU1_1);
}
#else
uint32_t select_chs(void)
{
uint32_t chs = 0;
char str[64];
char *ptr = NULL;
printf("Please select pwm chs[0-%d]:(0,1,2,3...)\n", PWM_MAX_CHS-1);
fgets(str, sizeof(str), stdin);
if ((str[0] == 'q') || (str[0] == 'Q'))
return 0;
ptr = str;
while (*ptr) {
int i = atoi(ptr);
if (i < 32) {
chs |= ((uint32_t)0x01<<i);
}
ptr = strchr(ptr, ',');
if (NULL == ptr)
break;
ptr++;
if (('\r' == *ptr) || ('\n' == *ptr))
break;
}
return chs;
}
int main(int argc, char *argv[])
{
uint32_t chs = 0;
uint32_t duty = 0;
uint32_t period = 3000;
uint32_t ctrl = 0;
pru_start(PATH_PRU0_0);
pru_start(PATH_PRU0_1);
pru_start(PATH_PRU1_0);
pru_start(PATH_PRU1_1);
pwm_init();
chs = select_chs();
if (0 == chs)
goto STOP;
printf("Selected:0x%04X\n('q' - exit, '+' - inc, '-' - dec, 'r' - run/stop)\n", chs);
pwm_duty(chs, duty);
pwm_peroid(chs, period);
pwm_ctrl(chs, 0x00);
motor_init();
motor_state(2);
for (;;) {
switch (getchar())
{
case 'q':
case 'Q':
goto STOP;
case 'r':
case 'R':
ctrl = !ctrl;
pwm_ctrl(chs, ctrl);
printf("ctrl=%d\n", ctrl);
break;
case '+':
if (duty < period) {
duty+=100;
pwm_duty(chs, duty);
printf("duty=%d\n", duty);
}
break;
case '-':
if (duty > 0) {
duty-=100;
pwm_duty(chs, duty);
printf("duty=%d\n", duty);
}
break;
}
}
STOP:
printf("Stopped!!!\n");
motor_deinit();
pwm_ctrl(PWM_ALL, 0x00);
pwm_deinit();
pru_stop(PATH_PRU0_0);
pru_stop(PATH_PRU0_1);
pru_stop(PATH_PRU1_0);
pru_stop(PATH_PRU1_1);
return 0;
}
#endif