-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_wall_follower_distance.cpp
199 lines (168 loc) · 6.63 KB
/
test_wall_follower_distance.cpp
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
// Build with:
// g++ test_wall_follower_distance.cpp -o test_wall_follower_distance -lmraa
// SPI pins are:
// - IO10: SS
// - IO11: MOSI
// - IO12: MISO
// - IO13: SCLK
#include <unistd.h>
#include <stdint.h>
#include <signal.h>
#include <sys/time.h>
#include <cassert>
#include <cmath>
#include <csignal>
#include <iostream>
#include <math.h>
#include "mraa.hpp"
#define MS 1000
#define GYRO_DATA_OKAY_MASK 0x0C000000
#define GYRO_DATA_OKAY 0x04000000
float DISTANCE_FROM_IR_SENSORS = 4.6;
int BACK_INFRARED_PIN = 3;
int FRONT_INFRARED_PIN = 2;
int HEAD_INFRARED_PIN = 1;
float alpha_infrareds = .3;
int running = 1;
void sig_handler(int signo) {
if (signo == SIGINT) {
printf("closing spi nicely\n");
running = 0;
mraa::Pwm pwm = mraa::Pwm(9);
mraa::Pwm pwm2 = mraa::Pwm(6);
pwm.write(0);
pwm2.write(0);
}
}
void setMotorSpeed(mraa::Pwm &pwm, mraa::Gpio &dir, double speed) {
assert(-1.0 <= speed && speed <= 1.0);
if (speed < 0) {
dir.write(1);
} else {
dir.write(0);
}
pwm.write(fabs(speed));
}
float angleFromWall(float backInfraDistance, float frontInfraDistance){
bool inQuadrantOne = true;
if (backInfraDistance > frontInfraDistance){
inQuadrantOne = true;
} else {
inQuadrantOne = false;
}
float diffDistance = fabs(backInfraDistance - frontInfraDistance);
if(inQuadrantOne){
return asin(diffDistance/DISTANCE_FROM_IR_SENSORS);
} else{
return -1.0 * acos(diffDistance/DISTANCE_FROM_IR_SENSORS);
}
}
float infraReadingToDistanceBack(float infraReading){
// return (QUAD_TERM * infraReading * infraReading) + (LINEAR_TERM * infraReading) + CONST_TERM;
if (infraReading!=0){
return (970.0/infraReading) - .5; //y = 970/x - .3 fits our data
} else {
return 10.0; //big number
}
}
float infraReadingToDistanceFront(float infraReading){
// return (QUAD_TERM * infraReading * infraReading) + (LINEAR_TERM * infraReading) + CONST_TERM;
if (infraReading!=0){
return (970.0/infraReading) - .5;; //y = 970/x - .5
} else {
return 10.0; //big number
}
}
float infraReadingToDistanceHead(float infraReading){
// return (QUAD_TERM * infraReading * infraReading) + (LINEAR_TERM * infraReading) + CONST_TERM;
if (infraReading!=50){
return (900.0/(infraReading - 50.0)); //y = 900/(x-50)
} else {
return 10.0; //big number
}
}
int main() {
// Handle Ctrl-C quit
signal(SIGINT, sig_handler);
//Motor Stuff
mraa::Pwm pwm = mraa::Pwm(9);
pwm.write(0.0);
pwm.enable(true);
//assert(pwm != NULL);
mraa::Gpio dir = mraa::Gpio(8);
//assert(dir != NULL);
dir.dir(mraa::DIR_OUT);
dir.write(0);
mraa::Pwm pwm2 = mraa::Pwm(6);
pwm2.write(0.0);
pwm2.enable(true);
//assert(pwm2 != NULL);
mraa::Gpio dir2 = mraa::Gpio(5);
//assert(dir != NULL);
dir2.dir(mraa::DIR_OUT);
dir2.write(0);
mraa::Aio aioBackInfrared = mraa::Aio(BACK_INFRARED_PIN);
mraa::Aio aioFrontInfrared = mraa::Aio(FRONT_INFRARED_PIN);
mraa::Aio aioHeadInfrared = mraa::Aio(HEAD_INFRARED_PIN);
float speedWallFollower = .1;
float powerWallFollower = 0;
float forwardBiasWallFollower = .15;
float backDistance = 0;
float frontDistance = 0;
float headDistance = 10.0;
float desiredDistance = 5;
float P_CONSTANT_WALL_FOLLOWER = .15;
while (running) {
float backInfraredReading = aioBackInfrared.read();
float frontInfraredReading = aioFrontInfrared.read();
float headInfraredReading = aioHeadInfrared.read();
printf("Infra readings: back: %f, front: %f, head: %f\n", backInfraredReading, frontInfraredReading, headInfraredReading);
backDistance = (backDistance * alpha_infrareds) + (infraReadingToDistanceBack(backInfraredReading) * (1.0 - alpha_infrareds));
frontDistance = (frontDistance * alpha_infrareds) + (infraReadingToDistanceFront(frontInfraredReading) * (1.0 - alpha_infrareds) * .94);
headDistance = (headDistance * alpha_infrareds) + (infraReadingToDistanceHead(headInfraredReading) * (1.0 - alpha_infrareds));
printf("Distances: Back: %f, Front: %f, Head: %f\n", backDistance, frontDistance, headDistance);
float averageDistance = (backDistance + frontDistance) / 2.0;
float diffDistance = desiredDistance - averageDistance;
powerWallFollower = speedWallFollower * (P_CONSTANT_WALL_FOLLOWER * diffDistance);
if (powerWallFollower > .3) {
powerWallFollower = .3;
} else if (powerWallFollower < -.3) {
powerWallFollower = -.3;
}
if (headDistance < 6.0 && headDistance > 0){
printf("PowerWallFollower reduced!\n"); //head hit a wall
powerWallFollower = .2;
setMotorSpeed(pwm, dir, powerWallFollower);
setMotorSpeed(pwm2, dir2, powerWallFollower);
usleep(1000 * 20);
} else if ((backDistance > 20) && (frontDistance < 20 && frontDistance > 0 )){
powerWallFollower = .15; //only front distance gives good readings, turn left
setMotorSpeed(pwm, dir, powerWallFollower + forwardBiasWallFollower);
setMotorSpeed(pwm2, dir2, powerWallFollower - forwardBiasWallFollower);
} else if ((frontDistance > 20) && (backDistance < 20 && backDistance > 0 )){
setMotorSpeed(pwm, dir, forwardBiasWallFollower);
setMotorSpeed(pwm2, dir2, -1.0 * forwardBiasWallFollower);
usleep(300 * 1000);
powerWallFollower = -.15; //only back distance gives good readings, turn right
setMotorSpeed(pwm, dir, powerWallFollower + forwardBiasWallFollower);
setMotorSpeed(pwm2, dir2, powerWallFollower - forwardBiasWallFollower);
usleep(300 * 1000);
} else if ((frontDistance > 20 || frontDistance < 0) && (backDistance > 20 || backDistance < 0)) {
powerWallFollower = .15; //sensors read garbage
setMotorSpeed(pwm, dir, powerWallFollower);
setMotorSpeed(pwm2, dir2, -1 * powerWallFollower);
}
else if ((frontDistance < 20 && frontDistance > 0) && (backDistance < 20 && backDistance > 0)) {
setMotorSpeed(pwm, dir, powerWallFollower + forwardBiasWallFollower); //normal behavior
setMotorSpeed(pwm2, dir2, powerWallFollower - forwardBiasWallFollower);
} else {
setMotorSpeed(pwm, dir, 0);
setMotorSpeed(pwm2, dir2, 0);
running = 0;
}
printf("Set power to: %f\n", powerWallFollower);
}
setMotorSpeed(pwm, dir, 0);
setMotorSpeed(pwm2, dir2, 0);
return 0;
}