-
Notifications
You must be signed in to change notification settings - Fork 0
/
demolib.c
672 lines (512 loc) · 15.1 KB
/
demolib.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
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
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
*/
/*
This version is for pigpio version 3+
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include "pigpio.h"
/* ===========================================================================
THIS PROGRAM NEEDS THE I2C DEVICE AND DEVELOPMENT LIBRARY
TO GET THE NEEDED FILES DO
sudo apt-get install libi2c-dev
BEFORE RUNNING THE PROGRAM ENSURE THAT THE I2C DEVICE IS PRESENT
sudo modprobe i2c-bcm2708
sudo modprobe i2c-dev
sudo chmod o+rw /dev/i2c*
=========================================================================== */
/*
P1 Name gpio used for
3 SDA 0/2 i2c
5 SCL 1/3 i2c
7 --- 4 LASER
8 TXD 14 LED1
10 RXD 15 LED2
11 --- 17 SERVO 1
12 --- 18 SERVO 2
13 --- 21/27 SERVO 3
15 --- 22 LED3
16 --- 23 TI Launchpad
18 --- 24 Sonar trigger
19 MOSI 10 Sonar echo
21 MISO 9 Motor B In 1
22 --- 25 LDR
23 SCLK 11 Motor B In 2
24 CE0 8 Motor A In1
26 CE1 7 Motor A In2
*/
#define LASER 4
#define MOTOR_A_IN2 7
#define MOTOR_A_IN1 8
#define MOTOR_B_IN1 9
#define SONAR_ECHO 10
#define MOTOR_B_IN2 11
#define LED1 14
#define LED2 15
#define SERVO1 17
#define SERVO2 18
#define SERVO3 21
#define LED3 22
#define LAUNCHPAD 23
#define SONAR_TRIGGER 24
#define LDR 25
#define LEDS 4
short rawAcc[3];
short rawGyr[3];
short rawMag[3];
#define ROLL 0
#define PITCH 1
#define YAW 2
#define ACC_ORIENTATION(X, Y, Z) \
{rawAcc[ROLL] = -X; rawAcc[PITCH] = -Y; rawAcc[YAW] = Z;}
#define GYRO_ORIENTATION(X, Y, Z) \
{rawGyr[ROLL] = Y; rawGyr[PITCH] = -X; rawGyr[YAW] = -Z;}
#define MAG_ORIENTATION(X, Y, Z) \
{rawMag[ROLL] = X; rawMag[PITCH] = Y; rawMag[YAW] = -Z;}
#define CALIBRATIONS 200
#define ADXL345_I2C_ADDR 0x53
#define ITG3200_I2C_ADDR 0x68
static int version, micros=5, millis=100;
static volatile unsigned long launchpadPulses;
static volatile unsigned long launchpad5;
static volatile unsigned long launchpad10;
static volatile unsigned long launchpad15;
static volatile unsigned long launchpadOutRange;
static volatile int launchpadErr;
static volatile uint32_t LDRrechargeTick;
/* forward prototypes */
void LEDlaserTick (void);
void motorTick(void);
void i2cTick (void);
void servoTick (void);
void sonarLDRtick(void);
void launchpadAlert(int gpio, int level, uint32_t tick);
void sonarAlert(int gpio, int level, uint32_t tick);
void LDRalert(int gpio, int level, uint32_t tick);
void putTTY(char * buf);
void putTTYstr(int row, int col, char * buf);
int main(int argc, char *argv[])
{
char str[256];
if (argc > 1) micros = atoi(argv[1]);
if (argc > 2) millis = atoi(argv[2]);
putTTY("\033c"); /* clear console */
gpioCfgBufferSize(millis);
gpioCfgClock(micros, PI_CLOCK_PCM, PI_CLOCK_PLLD);
/* before using the library you must call gpioInitialise */
version = gpioInitialise();
if (version >= 0)
{
/* initialise pins, only gpio numbers are supported */
gpioSetMode(SERVO1, PI_OUTPUT);
gpioSetMode(SERVO2, PI_OUTPUT);
gpioSetMode(SERVO3, PI_OUTPUT);
gpioSetMode(LASER, PI_OUTPUT);
gpioSetMode(LED1, PI_OUTPUT);
gpioSetMode(LED2, PI_OUTPUT);
gpioSetMode(LED3, PI_OUTPUT);
gpioSetMode(MOTOR_A_IN1, PI_OUTPUT);
gpioSetMode(MOTOR_A_IN2, PI_OUTPUT);
gpioSetMode(MOTOR_B_IN1, PI_OUTPUT);
gpioSetMode(MOTOR_B_IN2, PI_OUTPUT);
gpioSetMode(SONAR_TRIGGER, PI_OUTPUT);
gpioWrite (SONAR_TRIGGER, PI_OFF);
gpioSetMode(SONAR_ECHO, PI_INPUT);
gpioSetMode(LAUNCHPAD, PI_INPUT);
gpioSetMode(LDR, PI_INPUT);
/* update i2c fifty times a second, timer #0 */
gpioSetTimerFunc(0, 20, i2cTick);
//gpioSetTimerFunc(0, 1000, servoTick);
/* update LEDs and laser once a second, timer #1 */
gpioSetTimerFunc(1, 1000, LEDlaserTick);
/* update motors every three seconds, timer #2 */
gpioSetTimerFunc(2, 3000, motorTick);
/* update sonar/LDR 10 times a second, timer #3 */
gpioSetTimerFunc(3, 100, sonarLDRtick);
/* an attachecd TI launchpad is transmitting high pulses of
15, 35, 55, 75, ..., 975, 995 microseconds repeating with 50
microseconds off between each pulse */
gpioSetAlertFunc(LAUNCHPAD, launchpadAlert);
/* monitor sonar echos */
gpioSetAlertFunc(SONAR_ECHO, sonarAlert);
/* monitor LDR level changes */
gpioSetAlertFunc(LDR, LDRalert);
while (1)
{
sleep(1);
sprintf(str, "TI pulses %8ld", launchpadPulses);
putTTYstr(9, 1, str);
sprintf(str, "+/-5 %8ld", launchpad5);
putTTYstr(10, 6, str);
sprintf(str, "+/-10 %8ld", launchpad10);
putTTYstr(11, 5, str);
sprintf(str, "+/-15 %8ld", launchpad15);
putTTYstr(12, 5, str);
sprintf(str, "Others %8ld (last %d) ",
launchpadOutRange, launchpadErr);
putTTYstr(13, 4, str);
}
}
gpioTerminate();
return 0;
}
void LEDlaserTick(void)
{
static int gpio[LEDS]={LED1, LED2, LED3, LASER};
static int pos [LEDS]={ 0, 3, 6, 9};
static int inc [LEDS]={ 1, 1, 1, 1};
static int vals[] = {0, 1, 2, 4, 8, 16, 32, 64, 128, 249};
int i;
for (i=0; i<LEDS; i++)
{
gpioPWM(gpio[i], vals[pos[i]]);
pos[i] += inc[i];
if ( (pos[i]>=(sizeof(vals)/4)) || (pos[i]<0) )
{
inc[i] = -inc[i];
pos[i] += inc[i];
}
}
}
void sonarLDRtick(void)
{
/* trigger a sonar reading */
gpioWrite(SONAR_TRIGGER, PI_ON);
usleep(20);
gpioWrite(SONAR_TRIGGER, PI_OFF);
/* trigger a LDR reading */
gpioSetMode(LDR, PI_OUTPUT); /* drain capacitor */
gpioWrite(LDR, PI_OFF);
usleep(200);
LDRrechargeTick = gpioTick();
gpioSetMode(LDR, PI_INPUT); /* start capacitor recharge */
}
void motorTick(void)
{
static int gpio_in1[2]={MOTOR_A_IN1, MOTOR_B_IN1};
static int gpio_in2[2]={MOTOR_A_IN2, MOTOR_B_IN2};
static int speed [2]={ 80, 80};
static int inc [2]={ -50, 50};
int i;
char str[256];
for (i=0; i<2; i++)
{
speed[i]+=inc[i];
if (speed[i]<0)
{
gpioPWM(gpio_in1[i], -speed[i]);
gpioPWM(gpio_in2[i], 0);
if (speed[i] < -205) inc[i] = -inc[i];
sprintf(str, "MOT%d IN1=%3d IN2=%3d", i+1, -speed[i], 0);
}
else
{
gpioPWM(gpio_in2[i], speed[i]);
gpioPWM(gpio_in1[i], 0);
if (speed[i] > 205) inc[i] = -inc[i];
sprintf(str, "MOT%d IN1=%3d IN2=%3d", i+1, 0, speed[i]);
}
if (i) putTTYstr(7, 1, str); else putTTYstr(5, 1, str);
}
}
/* loads of code to read/write i2c */
void selectDevice(int i2c, int addr, char * name)
{
if (ioctl(i2c, I2C_SLAVE, addr) < 0)
{
fprintf(stderr, "%s not present\n", name);
}
}
void writeToDevice(int i2c, char * buf, int len)
{
static int reported = 0;
if (write(i2c, buf, len) != len)
{
if (!reported)
{
fprintf(stderr, "Can't write to device\n");
reported = 1;
}
}
else reported = 0;
}
void readADXL345(int i2c)
{
char buf[8];
static int reported = 0;
selectDevice(i2c, ADXL345_I2C_ADDR, "ADXL345");
writeToDevice(i2c, "\x32", 1);
if (read(i2c, buf, 6) != 6)
{
if (!reported)
{
fprintf(stderr, "Unable to read from ADXL345\n");
reported = 1;
}
}
else
{
reported = 0;
ACC_ORIENTATION (
((buf[1]<<8) | buf[0]),
((buf[3]<<8) | buf[2]),
((buf[5]<<8) | buf[4]) );
}
}
void readITG3200(int i2c)
{
char buf[8];
static int reported = 0;
selectDevice(i2c, ITG3200_I2C_ADDR, "ITG3200");
writeToDevice(i2c, "\x1D", 1);
if (read(i2c, buf, 6) != 6)
{
if (!reported)
{
fprintf(stderr, "Unable to read from ITG3200\n");
reported = 1;
}
}
else
{
reported = 0;
GYRO_ORIENTATION (
((buf[0]<<8) | buf[1]),
((buf[2]<<8) | buf[3]),
((buf[4]<<8) | buf[5]) );
}
}
int initI2Cdevices(void)
{
int i2c;
if ((i2c = open("/dev/i2c-0", O_RDWR)) < 0)
{
perror("Failed to open i2c bus");
exit(1);
}
/* initialise ADXL345 */
selectDevice(i2c, ADXL345_I2C_ADDR, "ADXL345");
writeToDevice(i2c, "\x2d\x00", 2);
writeToDevice(i2c, "\x2d\x10", 2);
writeToDevice(i2c, "\x2d\x08", 2);
writeToDevice(i2c, "\x31\x00", 2);
writeToDevice(i2c, "\x31\x0b", 2);
/* initialise ITG3200 */
selectDevice(i2c, ITG3200_I2C_ADDR, "ITG3200");
writeToDevice(i2c, "\x16\b00011000", 2);
return i2c;
}
/* an attached IMU (GY-85) supplies orientation information which
is used to position the servos */
float estimateAngle(int acc, int gyro, float oldAng, int elapsed)
{
float angleAcc, angleInc, estAngle;
float secs;
secs = (float) elapsed / 1e6f;
angleAcc = (float) acc * 90.0f / 256.0f;
angleInc = (float) gyro * secs * 2000.0f / 32768.0f;
estAngle = 0.75 * (oldAng + angleInc) + 0.25 * angleAcc;
return estAngle;
}
void servoTick(void)
{
static int wid1=1500, wid2=1500, wid3=1500;
static int inc1=50, inc2=75, inc3=100;
gpioServo(SERVO1, wid1);
gpioServo(SERVO2, wid2);
gpioServo(SERVO3, wid3);
wid1+=inc1; if ((wid1<1000) || (wid1>2000)) {inc1 = -inc1; wid1+=inc1;}
wid2+=inc2; if ((wid2<1000) || (wid2>2000)) {inc2 = -inc2; wid2+=inc2;}
wid3+=inc3; if ((wid3<1000) || (wid3>2000)) {inc3 = -inc3; wid3+=inc3;}
}
void i2cTick(void)
{
static int inited = 0;
static int calibrated = 0;
static int calibrations = 0;
static int accCalibX = 0, accCalibY = 0, accCalibZ = 0;
static int gyroCalibX = 0, gyroCalibY = 0, gyroCalibZ = 0;
static int i2c;
static float X=0.0, Y=0.0, Z=0.0;
static uint32_t lastTick;
uint32_t tick;
int elapsed;
int pulse;
char str[256];
if (inited)
{
tick = gpioTick();
elapsed = tick - lastTick;
lastTick = tick;
readADXL345(i2c);
readITG3200(i2c);
if (calibrated)
{
X = estimateAngle(
rawAcc[ROLL], rawGyr[ROLL] -gyroCalibX, X, elapsed);
Y = estimateAngle(
rawAcc[PITCH], rawGyr[PITCH] - gyroCalibY, Y, elapsed);
Z = estimateAngle(
rawAcc[YAW], rawGyr[YAW] - gyroCalibZ, Z, elapsed);
pulse = 1500 + (Y * 1000 / 90);
if (pulse < 500) pulse = 500;
if (pulse > 2500) pulse = 2500;
gpioServo(SERVO1, pulse);
pulse = 1500 - (X * 500 / 90);
if (pulse < 1000) pulse = 1000;
if (pulse > 2000) pulse = 2000;
gpioServo(SERVO2, pulse);
/* prefer Z but that doesn't change much */
pulse = 1500 - (Y * 500 / 90);
if (pulse < 800) pulse = 800;
if (pulse > 2200) pulse = 2200;
gpioServo(SERVO3, pulse);
sprintf(str, "X=%4.0f Y=%4.0f Z=%4.0f ", X, Y, Z);
putTTYstr(1, 1, str);
}
else
{
accCalibX+=rawAcc[ROLL];
accCalibY+=rawAcc[PITCH];
accCalibZ+=rawAcc[YAW];
gyroCalibX+=rawGyr[ROLL];
gyroCalibY+=rawGyr[PITCH];
gyroCalibZ+=rawGyr[YAW];
if (++calibrations >= CALIBRATIONS)
{
accCalibX /= CALIBRATIONS;
accCalibY /= CALIBRATIONS;
accCalibZ /= CALIBRATIONS;
gyroCalibX /= CALIBRATIONS;
gyroCalibY /= CALIBRATIONS;
gyroCalibZ /= CALIBRATIONS;
calibrated = 1;
}
}
}
else
{
i2c = initI2Cdevices();
gpioServo(SERVO1, 1500);
gpioServo(SERVO2, 1500);
gpioServo(SERVO3, 1500);
inited = 1;
}
}
void sonarAlert(int gpio, int level, uint32_t tick)
{
static uint32_t startTick;
int diffTick;
char str[256];
if (level == PI_ON)
{
startTick = tick;
}
else if (level == PI_OFF)
{
diffTick = tick - startTick;
if (diffTick < 26100)
{
sprintf(str, "Sonar %3d cms", (diffTick+29)/58);
putTTYstr(15, 1, str);
}
}
}
void LDRalert(int pin, int level, uint32_t tick)
{
int diffTick;
char str[256];
if (level == PI_ON)
{
diffTick = tick - LDRrechargeTick;
sprintf(str, "LDR %4d micros", diffTick);
putTTYstr(17, 1, str);
}
}
void launchpadAlert(int pin, int level, uint32_t tick)
{
static int inited = 0, lastTick, lastPulseLen;
int pulseLen, pulseDif;
if (inited)
{
pulseLen = tick - lastTick;
lastTick = tick;
if (level==0)
{
if (lastPulseLen)
{
pulseDif = pulseLen - lastPulseLen;
/* allow for wrap around */
if (pulseDif < 0) pulseDif += 1096;
/* now centre around expected value */
pulseDif -= 33;
if (pulseDif < 0) pulseDif = -pulseDif;
launchpadPulses++;
if (pulseDif <= 5)
{
launchpad5++;
}
else if (pulseDif <= 10)
{
launchpad10++;
}
else if (pulseDif <= 15)
{
launchpad15++;
}
else
{
launchpadOutRange++;
launchpadErr = pulseDif;
}
}
lastPulseLen = pulseLen;
}
}
else
{
lastTick = tick;
lastPulseLen = 0;
launchpadPulses = 0;
launchpad5 = 0;
launchpad10 = 0;
launchpad15 = 0;
launchpadOutRange = 0;
inited = 1;
}
}
void putTTY(char * buf)
{
write(1, buf, strlen(buf));
}
void putTTYstr(int row, int col, char * buf)
{
char str[256];
sprintf(str, "\033[%d;%dH%s", row, col, buf);
putTTY(str);
}