-
Notifications
You must be signed in to change notification settings - Fork 1
/
wf2.cpp
150 lines (146 loc) · 2.81 KB
/
wf2.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
#include "motion.cpp"
#include <math.h>
#include <iostream>
#include "shortIR.cpp"
#define PAR 1
#define SMC 2
#define SMC_align 22
#define BGC_align 33
#define BGC 3
#define LOST 0
double wrKp=0.4, wrKi=0.0, wrKd=0.00;
const double wKp=1;
const double wfSpeed=1;
const double frontThresh=8;
const double sideThresh=5;
const double alignThresh=0.2;
class Wallfollower {
IR* irlf;
IR* irlb;
IR* irr;
IR* irf;
mraa::Gpio* uirb;
//IR* irb;
Motor* left;
Motor* right;
Odometry* odo;
Location* start;
Location* current;
Motion* motion;
int mode;
bool nearWalls()
{
return (irlf->getDistance()<sideThresh)||(irlb->getDistance()<sideThresh)||(irf->getDistance()<frontThresh);
}
void parSetup()
{
mode=PAR;
}
void smcSetup()
{
mode=SMC;
motion->rotate(1.57);
}
void smcAlignSetup()
{
mode=SMC_align;
left->stop(); right->stop();
usleep(100000);
//current = odo->run();
}
void bgcAlignSetup()
{
mode=BGC_align;
}
void bgcSetup()
{
mode=BGC;
left->stop(); right->stop();
}
void lostSetup()
{
mode=LOST;
motion->straight(10000);
}
void bgcRun()
{
//motion->baseSpeed
}
void lostRun()
{
if(nearWalls()) smcSetup();
else motion->run();
}
void parRun()
{
double f=irlf->getDistance(), b=irlb->getDistance();
double e=f-b;
double ds=Kp*e;
right->setTarget(wfSpeed+ds);
left->setTarget(wfSpeed-ds);
left->run(); right->run();
if(irlf->getDistance()>20) bgcSetup();
else if(irf->getDistance()<frontThresh) smcSetup();
else if(!nearWalls()) lostSetup();
}
void smcRun()
{
if(motion->run()) smcAlignSetup();
}
void smcAlign()
{
double f=irlf->getDistance(),b=irlb->getDistance();
std::cout<<"front "<<f<<" back "<<b<<std::endl;
double e=fabs(f-b);
double speed = e*wrKp;
if(speed>0) motion->ccw();
else { motion->cw(); speed=-speed;}
left->setTarget(speed); right->setTarget(speed);
left->run(); right->run();
if(fabs(f-b)<alignThresh)
{
left->stop(); right->stop();
usleep(100000);
parSetup();
}
}
void bgcAlign()
{
}
public:
Wallfollower(Motor* _l, Motor* _r, IR* _irf, IR* _irr, IR* _irlf, IR* _irlb, mraa::Gpio* _uirb, Location* _start) {
left = _l;
right = _r;
irlf = _irlf;
irlb = _irlb;
irr = _irr;
irf = _irf;
uirb = _uirb;
//irb = _irb;
start = _start;
current = _start;
odo = new Odometry(_l, _r, _start->x(),_start->y(),_start->theta());
motion = new Motion(left,right,odo,_start);
lostSetup();
}
void parallel()
{
mode=PAR;
left->forward(); right->forward();
left->setSpeed(wfSpeed);
right->setSpeed(wfSpeed);
}
void run()
{
std::cout<<"Currently in mode "<<mode<<std::endl;
switch(mode)
{
case PAR: parRun(); break;
case SMC: smcRun(); break;
case SMC_align: smcAlign(); break;
case BGC_align: bgcAlign(); break;
case BGC: bgcRun(); break;
case LOST: lostRun(); break;
}
}
};