-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
747 lines (606 loc) · 20.2 KB
/
main.cpp
File metadata and controls
747 lines (606 loc) · 20.2 KB
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
// changes made to move move commands to their own command
// 27 oct 16, swapped assigned of the motor channels as PCB now mounted upside down
// 06 nov 16, added i2c interface between this boiard and top (head) pcb
// - EYES and RGB led supported
#include "mbed.h"
#include "droid_hw.h"
#include "BLEDevice.h"
#include "UARTService.h"
#include <string>
DigitalOut topled(LED2);
DigitalOut bottomled(LED1);
DigitalOut myled (LED_PIN);
DigitalOut ledRed (LED_R);
DigitalOut ledGreen (LED_G);
DigitalOut ledBlue (LED_B);
DigitalIn bumper1 (BMP1);
DigitalIn bumper2 (BMP2);
DigitalOut laserL (LASER_L);
DigitalOut laserR (LASER_R);
DigitalOut irMuxSelect (IRMUX_PIN);
I2C i2c (SDA, SCL);
DigitalOut khz (KHZ_PIN);
Serial ir(IRTX_PIN, IRRX_PIN);
DigitalOut m1a(M1A_PIN);
PwmOut m1b(M1B_PIN);
DigitalOut m2a(M2A_PIN);
PwmOut m2b(M2B_PIN);
PwmOut speaker(SPR_PIN);
enum {
NONE,
DRIVE,
WAGGLE
} action_t;
//InterruptIn event(P0_20);
BLEDevice ble;
UARTService *uart;
Ticker timer_tick;
const int i2c_addr = 0x9E; // define the I2C Address
char i2c_buffer[30] = {};
uint8_t mac[6] = {};
int limit = 20;
int enable = 0;
int msec = 0;
int m1cmd = 0;
int m2cmd = 0;
int m1ttl = 0;
int m2ttl = 0;
int rc = 0; // return code for function calls
int rxchar;
typedef struct
{
int wridx;
int rdidx;
int cmd[16];
int ttl[16];
} motor_msg_q_type;
motor_msg_q_type motorLq;
motor_msg_q_type motorRq;
typedef struct
{
int wridx;
int rdidx;
int cmd[16];
int on_ttl[16];
int off_ttl[16];
} led_msg_q_type;
led_msg_q_type laserLq; // command structure for left laser
led_msg_q_type laserRq; // command structure for right laser
typedef enum {
WAG_INIT,
WAG_WAGGLE,
WAG_DELAY,
WAG_DONE
} waggle_state;
typedef struct {
waggle_state state; // state of waggle
int val; // intensity of waggle
int dur; // duration of waggle
int t_rem; // remaining time of waggle
int per; // period of oscillation
int p_mod;
int p_rem; // remaining time of current period
int rep; // number of repetitions
int del; // delay between repetitions
int del_rem; // remaining time of delay
int dir; // direction of current oscillation
} waggle_t;
const static unsigned MAX_SIZEOF_RX_PAYLOAD = 20;
const static unsigned MAX_SIZEOF_TX_PAYLOAD = 20;
static char DEVICE_NAME[9] = {0x44, 0x72, 0x6f, 0x69, 0x64, 0x2d, 0x34, 0x35, 0x00};
static volatile bool triggerSensorPolling = false;
char rxPayload[MAX_SIZEOF_RX_PAYLOAD] = {0,};
char txPayload[MAX_SIZEOF_TX_PAYLOAD] = {0,};
char rxbuffer[MAX_SIZEOF_RX_PAYLOAD] = {0,};
void sio_putstring (char * string)
{
int ix;
for (ix = 0; ix < 15; ix++)
{
ir.putc(string[ix]);
}
}
void motor1ctrl (int speed)
{
if (speed == 0)
{
m1a = 1;
m1b.pulsewidth_us(5080);
return;
}
if (speed > 127)
{
speed = 127;
}
else if (speed < -127)
{
speed = -127;
}
if (speed > 0)
{
m1a = 1;
m1b.pulsewidth_us(5080 - speed * 40);
}
else
{
speed = -speed;
m1a = 0;
m1b.pulsewidth_us(speed * 40);
}
}
void motor2ctrl (int speed)
{
if (speed == 0)
{
m2a = 1;
m2b.pulsewidth_us(5080);
return;
}
if (speed > 127)
{
speed = 127;
}
else if (speed < -127)
{
speed = -127;
}
if (speed > 0)
{
m2a = 1;
m2b.pulsewidth_us(5080 - speed * 40);
}
else
{
speed = -speed;
m2a = 0;
m2b.pulsewidth_us(speed * 40);
}
}
// This function is called when a character goes into the RX buffer.
void rxCallback() {
rxchar = ir.getc();
}
void hardwareInit( )
{
// set up motor control channels
m1a = 1;
m2a = 1;
m1b.period_us(5080);
m2b.period_us(5080);
motor1ctrl (0);
motor2ctrl (0);
speaker.period_us(5080);
speaker.pulsewidth_us(25);
// initialize the UART for IR operations
ir.baud(baudrate);
ir.attach(&rxCallback, Serial::RxIrq);
myled = 0;
ledRed = 1;
ledGreen = 1;
ledBlue = 1;
laserL = 1;
laserR = 1;
}
// When we disconnect from the BT connection, the device must start to re-advertise
void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
{
ble.startAdvertising(); // restart advertising
}
// Something is incomming on the BT connection, read data from the TX characteristic
// Batch it up into 20byte chunks and then invoke the processInput method
void onDataWritten(const GattCharacteristicWriteCBParams *params)
{
if ((uart != NULL) && (params->charHandle == uart->getTXCharacteristicHandle())) {
uint16_t bytesRead = params->len;
if (bytesRead < MAX_SIZEOF_RX_PAYLOAD) {
strncpy(rxPayload, (char *)params->data, MAX_SIZEOF_RX_PAYLOAD - 1);
//rxPayload[bytesRead] = '\0';
//FG processInput();
triggerSensorPolling= true;
// copy received data to local buffer
uint16_t ix;
for (ix = 0; ix < bytesRead; ix++)
{
rxbuffer[ix] = (char) params->data[ix];
}
// respond
// ble.updateCharacteristicValue(uart->getRXCharacteristicHandle(), params->data, bytesRead);
}
}
}
// Send something back over the BT connection
void WriteToUART(char STR[])
{
// RESPOND...
ble.updateCharacteristicValue(uart->getRXCharacteristicHandle(), (uint8_t *)STR, strlen(STR));
}
void toggle()
{
enable = 1;
}
waggle_t *init_waggle(waggle_t *wag, int val, int dur, int rep = 1, int del = 0)
{
if (wag == NULL) wag = (waggle_t*)calloc(1, sizeof(waggle_t));
if (val > 255) val = 255;
wag->val = (val * 64 / 255) + 63;
wag->dur = dur;
wag->t_rem = dur;
wag->per = ((500 - 100) * (255 - val) / 255) + 100;
wag->per = wag->per - (wag->per % 10);
wag->p_mod = (wag->dur % (2 * wag->per)) / 2;
wag->p_mod = wag->p_mod - (wag->p_mod % 10);
wag->p_rem = wag->per;
wag->rep = rep;
wag->del = del - (wag->del % 10);
wag->del_rem = wag->del;
wag->dir = 1;
wag->state = WAG_INIT;
for (int i = 0; i < 16; i++) {
motorLq.cmd[i] = 0;
motorLq.ttl[i] = 0;
motorRq.cmd[i] = 0;
motorRq.ttl[i] = 0;
}
return wag;
}
waggle_t *init_shiver(waggle_t *wag, int val, int dur, int rep = 1, int del = 0)
{
if (wag == NULL) wag = (waggle_t*)calloc(1, sizeof(waggle_t));
if (val > 255) val = 255;
wag->val = (val * 64 / 255) + 63;
wag->dur = dur;
wag->t_rem = dur;
wag->per = (int)((100 - 50) * (float)(255 - val) / 255) + 50;
wag->per = wag->per - (wag->per % 10);
wag->p_mod = (wag->dur % (2 * wag->per)) / 2;
wag->p_mod = wag->p_mod - (wag->p_mod % 10);
wag->p_rem = wag->per;
wag->rep = rep;
wag->del = del - (wag->del % 10);
wag->del_rem = wag->del;
wag->dir = 1;
wag->state = WAG_INIT;
for (int i = 0; i < 16; i++) {
motorLq.cmd[i] = 0;
motorLq.ttl[i] = 0;
motorRq.cmd[i] = 0;
motorRq.ttl[i] = 0;
}
return wag;
}
void exec_waggle(waggle_t *wag)
{
switch (wag->state) {
case WAG_INIT:
if (wag->rep > 0) {
wag->rep = wag->rep - 1;
wag->state = WAG_WAGGLE;
wag->t_rem = wag->dur;
wag->p_rem = wag->per;
wag->dir = 1;
} else {
wag->state = WAG_DONE;
break;
}
case WAG_WAGGLE:
if (wag->t_rem >= 10) {
if (wag->p_rem < 10) {
if (wag->t_rem > 2*wag->p_mod) {
wag->p_rem = wag->per;
} else {
wag->p_rem = wag->p_mod;
}
wag->dir = wag->dir * -1;
}
motor1ctrl (wag->dir * wag->val);
motor2ctrl (-wag->dir * wag->val);
// motorLq.cmd[0] = wag->dir * wag->val;
// motorLq.ttl[0] = 10;
// motorRq.cmd[0] = -wag->dir * wag->val;
// motorRq.ttl[0] = 10;
wag->t_rem = wag->t_rem - 10;
wag->p_rem = wag->p_rem - 10;
} else if (wag->rep > 0) {
wag->state = WAG_DELAY;
motor1ctrl (0);
motor2ctrl (0);
motorLq.cmd[0] = 0;
motorLq.ttl[0] = 0;
motorRq.cmd[0] = 0;
motorRq.ttl[0] = 0;
wag->del_rem = wag->del;
} else {
wag->state = WAG_DONE;
motor1ctrl (0);
motor2ctrl (0);
motorLq.cmd[0] = 0;
motorLq.ttl[0] = 0;
motorRq.cmd[0] = 0;
motorRq.ttl[0] = 0;
}
break;
case WAG_DELAY:
if (wag->del_rem >= 10) {
wag->del_rem = wag->del_rem - 10;
} else {
wag->state = WAG_INIT;
}
break;
case WAG_DONE:
default:
break;
}
}
// This is the main kahuna
int main() {
char idata[3];
idata[0] = 0x16;
idata[1] = 0x55;
idata[2] = 0x55;
motorLq.rdidx = 0;
motorLq.wridx = 0;
motorLq.cmd[0] = 0;
motorLq.ttl[0] = 0;
motorRq.rdidx = 0;
motorRq.wridx = 0;
motorRq.cmd[0] = 0;
motorRq.ttl[0] = 0;
laserLq.rdidx = 0;
laserLq.wridx = 0;
laserLq.cmd[0] = 0;
laserLq.on_ttl[0] = 0;
laserLq.off_ttl[0] = 0;
laserRq.rdidx = 0;
laserRq.wridx = 0;
laserRq.cmd[0] = 0;
laserRq.on_ttl[0] = 0;
laserRq.off_ttl[0] = 0;
hardwareInit();
ble.init();
ble.onDisconnection(disconnectionCallback);
ble.onDataWritten(onDataWritten);
ble.getAddress(0,mac);
idata[0] = mac[0] ^ mac[1];
idata[1] = mac[2] ^ mac[3];
idata[2] = mac[4] ^ mac[5];
idata[0]= idata[0] ^ idata[1] ^ idata[2];
idata[1] = idata[0]; // make temp copy
idata[0] = (idata[0] & 0xf0) >> 4;
if (idata[0] > 9)
{
idata[0] = idata[0] + 0x37;
}
else
{
idata[0] = idata[0] + 0x30;
}
idata[1] = idata[1] & 0x0f;
if (idata[1] > 9)
{
idata[1] = idata[1] + 0x37;
}
else
{
idata[1] = idata[1] + 0x30;
}
DEVICE_NAME[6] = idata[0];
DEVICE_NAME[7] = idata[1];
// ble.startAdvertising();
/* setup advertising */
ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,(uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME) - 1);
// We're using Nordic's custom UART Service, Thanks !
ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,(const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
ble.startAdvertising();
timer_tick.attach(&toggle, 0.01); // the address of the function to be attached (toggle)
//event.fall(&toggle);
uart = new UARTService(ble);
m1cmd = 0;
m2cmd = 0;
//FG debug motor1ctrl (-100);
//FG debug motor2ctrl (-100);
//motorRq.cmd[0] = -127;
// motorRq.ttl[0] = 50;
// motorLq.cmd[0] = 127;
// motorLq.ttl[0] = 50;
// motorRq.cmd[1] = 127;
// motorRq.ttl[1] = 50;
// motorLq.cmd[1] = -127;
// motorLq.ttl[1] = 50;
// motorRq.cmd[2] = -127;
// motorRq.ttl[2] = 50;
// motorLq.cmd[2] = 127;
// motorLq.ttl[2] = 50;
// motorRq.cmd[3] = 127;
// motorRq.ttl[3] = 50;
// motorLq.cmd[3] = -127;
// motorLq.ttl[3] = 50;
// motorRq.cmd[4] = -127;
// motorRq.ttl[4] = 50;
// motorLq.cmd[4] = 127;
// motorLq.ttl[4] = 50;
// motorRq.cmd[5] = 127;
// motorRq.ttl[5] = 50;
// motorLq.cmd[5] = -127;
// motorLq.ttl[5] = 50;
// motorRq.cmd[6] = -127;
// motorRq.ttl[6] = 50;
// motorLq.cmd[6] = 127;
// motorLq.ttl[6] = 50;
// motorRq.cmd[7] = 127;
// motorRq.ttl[7] = 50;
// motorLq.cmd[7] = -127;
// motorLq.ttl[7] = 50;
waggle_t *wag = init_shiver(NULL, 127, 1000, 3, 500);
while (true)
{
if (enable == 1)
{
enable = 0;
exec_waggle(wag);
// process the commands
if (motorLq.cmd[0] != 0 || motorLq.ttl[0] != 0)
{
motorLq.ttl[0] = motorLq.ttl[0] - 10;
motor1ctrl (motorLq.cmd[0]);
if (motorLq.ttl[0] == 0)
{
motor1ctrl (0);
motorLq.cmd[0] = 0;
}
else
{
motor1ctrl (motorLq.cmd[0]);
}
}
if (motorRq.cmd[0] != 0 || motorRq.ttl[0] != 0)
{
motorRq.ttl[0] = motorRq.ttl[0] - 10;
motor2ctrl (motorRq.cmd[0]);
if (motorRq.ttl[0] == 0)
{
motor2ctrl (0);
motorRq.cmd[0] = 0;
}
else
{
motor2ctrl (motorRq.cmd[0]);
}
}
msec++;
if (msec == limit)
{
//RGB led test code = 6 nov 2016
i2c_buffer[0] = 0x06;
i2c_buffer[1] = 0x06;
i2c_buffer[2] = 0x04;
i2c_buffer[3] = 0x03; // 3 = toggle
i2c_buffer[4] = 0x00;
i2c_buffer[5] = 0x00;
i2c_buffer[6] = 0x28;
i2c_buffer[7] = 0x29;
rc = i2c.write(i2c_addr, i2c_buffer, 8, 0);
msec = 0;
myled = !myled;
khz = 0; //!khz;
irMuxSelect = 0; //!irMuxSelect;
ir.putc('w');
}
}
// Now check if we have received anyting from the UART && if we are still connected
//if (triggerSensorPolling && ble.getGapState().connected)
if (triggerSensorPolling)
{
triggerSensorPolling = false;
ir.putc (rxbuffer[7]);
ir.putc (rxbuffer[8]);
ir.putc (rxbuffer[9]);
// process incomming string
strcpy(txPayload, "hello");
if (rxbuffer[7] == 'S') // this is Stop command
{
motorLq.cmd[0] = 0;
motorLq.ttl[0] = 0;
motorRq.cmd[0] = 0;
motorRq.ttl[0] = 0;
motor1ctrl (0);
motor2ctrl (0);
if (wag != NULL) wag->state = WAG_DONE;
ir.putc('S');
strcpy(txPayload, "S done");
}
if (rxbuffer[7] == 'M') // this is a motor control command
{
if (rxbuffer[8] != 0x00) // this is motor commandfor left motor
{
motorLq.cmd[0] = (signed char) rxbuffer[8]; // this is left motor speed
motorLq.ttl[0] = (int) ((rxbuffer[12] << 8) | rxbuffer[13]);
if (rxbuffer[10] == 0x01) // check direction
{
motorLq.cmd[0] = -motorLq.cmd[0];
}
}
if (rxbuffer[9] != 0x00) // this is motor commandfor right motor
{
motorRq.cmd[0] = (signed char) rxbuffer[9]; // this is right motor speed
motorRq.ttl[0] = (int) ((rxbuffer[12] << 8) | rxbuffer[13]);
if (rxbuffer[11] == 0x01) // check direction
{
motorRq.cmd[0] = -motorRq.cmd[0];
}
}
ir.putc( (unsigned char) motorLq.cmd[0]);
ir.putc( (unsigned char) motorRq.cmd[0]);
strcpy(txPayload, "M done");
}
if (rxbuffer[7] == 'W') // this is a motor control command
{
init_waggle(wag, rxbuffer[8], (rxbuffer[9] << 8) | rxbuffer[10], rxbuffer[11], (rxbuffer[12] << 8) | rxbuffer[13]);
strcpy(txPayload, "W done");
}
if (rxbuffer[7] == 0x45) // this is Eye command
{
// note: Left/Right EYE are part of head, and message must be passed thru via I2C interface
i2c_buffer[0] = 0x0c; // offset (location) of data in the head data table
i2c_buffer[1] = 0x06; // length of payload adata
i2c_buffer[2] = rxbuffer[9]; // first row data, left / right eye select
i2c_buffer[3] = rxbuffer[10]; // second row data
i2c_buffer[4] = rxbuffer[11]; // third row data
i2c_buffer[5] = rxbuffer[12]; // fourth row data
i2c_buffer[6] = rxbuffer[13]; // fifth row data
i2c_buffer[7] = rxbuffer[14]; // sixth row data
rc = i2c.write(i2c_addr, i2c_buffer, 8, 0);
strcpy(txPayload, "E done");
}
if (rxbuffer[7] == 0x54) // this is Toggle command
{
if ( (rxbuffer[9]) == 0x02) // this is left laser
{
laserLq.cmd[0] = rxbuffer[10];
laserLq.on_ttl[0] = (int) ((rxbuffer[11] << 8) | rxbuffer[12]);
laserLq.off_ttl[0] = (int) ((rxbuffer[13] << 8) | rxbuffer[14]);
strcpy(txPayload, "T L done");
}
if ( (rxbuffer[9]) == 0x03) // this is right laser
{
laserRq.cmd[0] = rxbuffer[10];
laserRq.on_ttl[0] = (int) ((rxbuffer[11] << 8) | rxbuffer[12]);
laserRq.off_ttl[0] = (int) ((rxbuffer[13] << 8) | rxbuffer[14]);
strcpy(txPayload, "T R done");
}
// multicolor LED
if ( ((rxbuffer[9]) == 0x04) || ((rxbuffer[9]) == 0x05) || ((rxbuffer[9]) == 0x06) )
{
i2c_buffer[0] = 0x06; // offset (location) of data in the head data table
i2c_buffer[1] = 0x06; // length of payload adata
i2c_buffer[2] = rxbuffer[9]; // device selection
i2c_buffer[3] = rxbuffer[10]; // command or state
i2c_buffer[4] = rxbuffer[11]; // command on time
i2c_buffer[5] = rxbuffer[12];
i2c_buffer[6] = rxbuffer[13]; // command off time
i2c_buffer[7] = rxbuffer[14];
rc = i2c.write(i2c_addr, i2c_buffer, 8, 0);
strcpy(txPayload, "T B done");
}
}
//WriteToUART(txPayload); // respond
if (laserLq.cmd[0] != 0)
{
if (laserLq.cmd[0] == 0x02) laserL = !laserL; // toggle laser
laserLq.cmd[0] = 0;
i2c.write( 0xC0, idata, 3 );
}
if (laserRq.cmd[0] != 0)
{
if (laserRq.cmd[0] == 0x02) laserR = !laserR; // toggle laser
laserRq.cmd[0] = 0;
}
}
else
{
ble.waitForEvent();
}
}
}