-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_wall_follower.cpp
213 lines (178 loc) · 5.94 KB
/
test_wall_follower.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Build with:
// g++ test_wall_follower.cpp -o test_wall_follower -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
#define PI 3.14159265
float DISTANCE_FROM_IR_SENSORS = 4.6; //
// float QUAD_TERM = ;
// float LINEAR_TERM = ;
// float CONST_TERM = ;
int BACK_INFRARED_PIN = 3;
int FRONT_INFRARED_PIN = 2;
int HEAD_INFRARED_PIN = 1;
float alpha = .3;
int running = 1;
void sig_handler(int signo) {
if (signo == SIGINT) {
printf("closing spi nicely\n");
mraa::Pwm pwm = mraa::Pwm(9);
mraa::Pwm pwm2 = mraa::Pwm(6);
pwm.write(0);
pwm2.write(0);
running = 0;
}
}
void setMotorSpeed(mraa::Pwm &pwm, mraa::Gpio &dir, double speed) {
if (speed > 1.0) {
speed = 1.0;
} else if (speed < -1.0) {
speed = -1.0;
}
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){
// printf("Quad 1: Estimated Angle in Radians:%f\n", diffDistance/DISTANCE_FROM_IR_SENSORS);
return asin(diffDistance/DISTANCE_FROM_IR_SENSORS) * 180.0 / PI;
} else{
// printf("Quad 2: Estimated Angle in Radians:%f\n", diffDistance/DISTANCE_FROM_IR_SENSORS);
return -1.0 * asin(diffDistance/DISTANCE_FROM_IR_SENSORS) * 180.0 / PI;
}
}
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 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
}
}
int main() {
// Handle Ctrl-C quit
signal(SIGINT, sig_handler);
// mraa::Gpio *chipSelect = new mraa::Gpio(10);
// chipSelect->dir(mraa::DIR_OUT);
// chipSelect->write(1);
// mraa::Spi *spi = new mraa::Spi(0);
// spi->bitPerWord(32);
// char rxBuf[2];
// char writeBuf[4];
// unsigned int sensorRead = 0x20000000;
// writeBuf[0] = sensorRead & 0xff;
// writeBuf[1] = (sensorRead >> 8) & 0xff;
// writeBuf[2] = (sensorRead >> 16) & 0xff;
// writeBuf[3] = (sensorRead >> 24) & 0xff;
// float total = 0;
// struct timeval tv;
// int init = 0;
// float rf;
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 speed = .1;
float desiredAngle = 0.0;
float diffAngle = 0.0;
float integral = 0;
float power = 0;
float derivative = 0;
float timeBetweenReadings = 0;
float gyroBias = 1.0;
float forwardBias = 0.15;
float P_CONSTANT = 25;
float I_CONSTANT = 0;
float D_CONSTANT = -1;
float backDistance = 3.0;
float frontDistance = 3.0;
float P_CONSTANT_WALL_FOLLOWER = .05;
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) + (infraReadingToDistanceBack(backInfraredReading) * (1.0 - alpha));
frontDistance = (frontDistance * alpha) + (infraReadingToDistanceFront(frontInfraredReading) * (1.0 - alpha) * .94);
printf("Distances: Front: %f, Back: %f\n", backDistance, frontDistance);
float infraAngle = angleFromWall(backDistance, frontDistance);
printf("estimated angle: %f\n", infraAngle);
// power = speed * ((P_CONSTANT * diffAngle / 360.0) + (I_CONSTANT * integral) + (D_CONSTANT * derivative / 180.0)); //make sure to convert angles > 360 to proper angles
if (!isnan(infraAngle)){
power = speed * (P_CONSTANT_WALL_FOLLOWER * infraAngle);
}
// if (fabs(infraAngle) < 5.0){
// power = 0;
// }
if (power > .3) {
power = .3;
} else if (power < -.3) {
power = -.3;
}
if (headInfraredReading > 500.0){
setMotorSpeed(pwm, dir, power + forwardBias);
setMotorSpeed(pwm2, dir2, power - forwardBias);
} else {
printf("Power reduced!\n");
power = .15;
setMotorSpeed(pwm, dir, power);
setMotorSpeed(pwm2, dir2, power);
usleep(1000 * 20);
}
printf("Set power to: %f\n", power);
usleep(1000 * 10);
}
return 0;
}