forked from berndporr/alphabot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalphabot.cpp
More file actions
167 lines (144 loc) · 3.85 KB
/
alphabot.cpp
File metadata and controls
167 lines (144 loc) · 3.85 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
#include "alphabot.h"
#include <pigpio.h>
#include <math.h>
#include <iostream>
#include <unistd.h>
void AlphaBot::initPWM(int gpio) {
gpioSetPWMfrequency(gpio,50);
int rr = gpioGetPWMrealRange(gpio);
if ( ( rr > 255) && (rr < 20000) ) gpioSetPWMrange(gpio, rr);
setLeftWheelSpeed(0);
setRightWheelSpeed(0);
}
void AlphaBot::start(long _samplingInterval, int _timerNumber) {
int cfg = gpioCfgGetInternals();
cfg |= PI_CFG_NOSIGHANDLER;
gpioCfgSetInternals(cfg);
int r = gpioInitialise();
if (r < 0) {
throw "Cannot init pigpio.";
}
initPWM(GPIO_MOTORL);
initPWM(GPIO_MOTORR);
// collision sensor
gpioSetMode(GPIO_COLLISION_L,PI_INPUT);
gpioSetMode(GPIO_COLLISION_R,PI_INPUT);
// ADC
gpioSetMode(GPIO_ADC_CS,PI_OUTPUT);
gpioWrite(GPIO_ADC_CS,1);
gpioSetMode(GPIO_ADC_ADDR,PI_OUTPUT);
gpioSetMode(GPIO_ADC_IOCLK,PI_OUTPUT);
gpioSetMode(GPIO_ADC_DOUT,PI_INPUT);
samplingInterval = _samplingInterval;
timerNumber = _timerNumber;
r = gpioSetTimerFuncEx(timerNumber,samplingInterval,pigpioTimerCallback,(void*)this);
if (r < 0) {
throw "Cannot start timer.";
}
running = true;
}
void AlphaBot::stop() {
if (!running) return;
gpioSetTimerFuncEx(timerNumber,samplingInterval,NULL,(void*)this);
setLeftWheelSpeed(0);
setRightWheelSpeed(0);
running = false;
gpioTerminate();
}
unsigned AlphaBot::readADC(unsigned channel) {
if (!running) return 0;
unsigned us = 1;
gpioWrite(GPIO_ADC_CS,0);
usleep(us);
channel = channel << 4;
for (unsigned i = 0; i < 4; i ++) {
if(channel & 0x80)
gpioWrite(GPIO_ADC_ADDR,1);
else
gpioWrite(GPIO_ADC_ADDR,0);
usleep(us);
gpioWrite(GPIO_ADC_IOCLK,1);
usleep(us);
gpioWrite(GPIO_ADC_IOCLK,0);
usleep(us);
channel = channel << 1;
}
for (unsigned i = 0; i < 6;i ++) {
usleep(us);
gpioWrite(GPIO_ADC_IOCLK,1);
usleep(us);
gpioWrite(GPIO_ADC_IOCLK,0);
}
gpioWrite(GPIO_ADC_CS,1);
usleep(us);
gpioWrite(GPIO_ADC_CS,0);
unsigned value = 0;
for (unsigned i = 0; i < 2; i ++) {
gpioWrite(GPIO_ADC_IOCLK,1);
usleep(us);
value <<= 1;
usleep(us);
if (gpioRead(GPIO_ADC_DOUT))
value |= 0x1;
gpioWrite(GPIO_ADC_IOCLK,0);
usleep(us);
}
for (unsigned i = 0; i < 8; i ++) {
gpioWrite(GPIO_ADC_IOCLK,1);
usleep(us);
value <<= 1;
if (gpioRead(GPIO_ADC_DOUT))
value |= 0x1;
gpioWrite(GPIO_ADC_IOCLK,0);
usleep(us);
}
usleep(us);
gpioWrite(GPIO_ADC_CS,1);
return value;
}
void AlphaBot::timerEvent() {
if (!running) return;
// distance readings
leftDistance = readADC(ADC_DIST_L);
rightDistance = readADC(ADC_DIST_R);
// battery
batteryLevel = readADC(ADC_VIN);
// IR sensors
for(unsigned i=0;i<(nIR);i++) {
ir[i] = (float)(readADC(i)) / ADCmax;
}
// callback
if (nullptr != stepCallback)
stepCallback->step(*this);
}
void AlphaBot::setRightWheelSpeed(float speed) {
if (!running) return;
speed = -speed;
if (speed < -1)
speed = -1;
if (speed > 1)
speed = 1;
rightWheelSpeed = speed;
float max = (float)gpioGetPWMrange(GPIO_MOTORR);
speed = speed * speed2pwm;
gpioPWM(GPIO_MOTORR,(int)round((motor_rest_pulseduration+speed)*max));
}
void AlphaBot::setLeftWheelSpeed(float speed) {
if (!running) return;
if (speed < -1)
speed = -1;
if (speed > 1)
speed = 1;
leftWheelSpeed = speed;
float max = (float)gpioGetPWMrange(GPIO_MOTORL);
speed = speed * speed2pwm;
gpioPWM(GPIO_MOTORL,(int)round((motor_rest_pulseduration+speed)*max));
}
bool AlphaBot::getCollisionLeft() {
if (!running) return false;
return gpioRead(GPIO_COLLISION_L) == 0;
}
bool AlphaBot::getCollisionRight() {
if (!running) return false;
return gpioRead(GPIO_COLLISION_R) == 0;
}