amiro-os / devices / DiWheelDrive / userthread.cpp @ 3c3c3bb9
History | View | Annotate | Download (36.732 KB)
1 |
#include "userthread.hpp" |
---|---|
2 |
#include "amiro/Constants.h" |
3 |
#include "amiro_map.hpp" |
4 |
#include "global.hpp" |
5 |
#include "linefollow.hpp" |
6 |
|
7 |
using namespace amiro; |
8 |
|
9 |
extern Global global;
|
10 |
|
11 |
// a buffer for the z-value of the accelerometer
|
12 |
int16_t accel_z; |
13 |
bool running = false; |
14 |
|
15 |
|
16 |
/**
|
17 |
* Set speed.
|
18 |
*
|
19 |
* @param rpmSpeed speed for left and right wheel in rounds/min
|
20 |
*/
|
21 |
void UserThread::setRpmSpeedFuzzy(const int (&rpmSpeed)[2]) { |
22 |
global.motorcontrol.setTargetRPM(rpmSpeed[constants::DiWheelDrive::LEFT_WHEEL] * 1000000, rpmSpeed[constants::DiWheelDrive::RIGHT_WHEEL] * 1000000); |
23 |
} |
24 |
|
25 |
void UserThread::setRpmSpeed(const int (&rpmSpeed)[2]) { |
26 |
global.motorcontrol.setTargetRPM(rpmSpeed[constants::DiWheelDrive::LEFT_WHEEL], rpmSpeed[constants::DiWheelDrive::RIGHT_WHEEL]); |
27 |
} |
28 |
|
29 |
void UserThread::lightOneLed(Color color, int idx){ |
30 |
global.robot.setLightColor(idx, Color(color)); |
31 |
} |
32 |
|
33 |
void UserThread::lightAllLeds(Color color){
|
34 |
int led = 0; |
35 |
for(led=0; led<8; led++){ |
36 |
lightOneLed(color, led); |
37 |
} |
38 |
} |
39 |
|
40 |
void UserThread::showChargingState(){
|
41 |
uint8_t numLeds = global.robot.getPowerStatus().state_of_charge / 12;
|
42 |
Color color = Color::GREEN; |
43 |
if (numLeds <= 2){ |
44 |
color = Color::RED; |
45 |
}else if(numLeds <= 6){ |
46 |
color = Color::YELLOW; |
47 |
} |
48 |
for (int i=0; i<numLeds; i++){ |
49 |
lightOneLed(color, i); |
50 |
this->sleep(300); |
51 |
} |
52 |
this->sleep(1000); |
53 |
lightAllLeds(Color::BLACK); |
54 |
} |
55 |
|
56 |
void UserThread::chargeAsLED(){
|
57 |
uint8_t numLeds = global.robot.getPowerStatus().state_of_charge / 12;
|
58 |
Color color = Color::GREEN; |
59 |
if (numLeds <= 2){ |
60 |
color = Color::RED; |
61 |
}else if(numLeds <= 6){ |
62 |
color = Color::YELLOW; |
63 |
} |
64 |
for (int i=0; i<numLeds; i++){ |
65 |
lightOneLed(color, i); |
66 |
// this->sleep(300);
|
67 |
} |
68 |
// this->sleep(1000);
|
69 |
// lightAllLeds(Color::BLACK);
|
70 |
} |
71 |
|
72 |
// ----------------------------------------------------------------
|
73 |
|
74 |
void UserThread::getProxySectorVals(uint16_t (&proxVals)[8], uint16_t (&sProx)[8]){ |
75 |
for (int i=0; i<8; i++){ |
76 |
sProx[i] = (proxVals[i] < proxVals[(i+1) % 8]) ? proxVals[i] : proxVals[(i+1) % 8]; |
77 |
// chprintf((BaseSequentialStream*)&global.sercanmux1, "%d: %d, ", i, sProx[i]);
|
78 |
|
79 |
} |
80 |
// chprintf((BaseSequentialStream*)&global.sercanmux1, "\n");
|
81 |
|
82 |
} |
83 |
|
84 |
|
85 |
void UserThread::getMaxFrontSectorVal(uint16_t (&sProx)[8], int32_t &sPMax){ |
86 |
for (int i=2; i<5; i++){ |
87 |
sPMax = (sPMax < sProx[i]) ? sProx[i] : sPMax; |
88 |
} |
89 |
} |
90 |
|
91 |
void UserThread::proxSectorSpeedCorrection(int (&rpmSpeed)[2], uint16_t (&proxVals)[8]){ |
92 |
int i;
|
93 |
uint16_t sProx[8];
|
94 |
int32_t sPMax = 0;
|
95 |
getProxySectorVals(proxVals, sProx); |
96 |
getMaxFrontSectorVal(sProx, sPMax); |
97 |
|
98 |
int32_t speedL = rpmSpeed[0] - (sPMax * pCtrl.pFactor);
|
99 |
int32_t speedR = rpmSpeed[1] - (sPMax * pCtrl.pFactor);
|
100 |
|
101 |
|
102 |
|
103 |
if(sPMax > pCtrl.threshMid){
|
104 |
rpmSpeed[0] = 0; |
105 |
rpmSpeed[1] = 0; |
106 |
pCtrl.staticCont++; |
107 |
}else if((speedL > 0) || (speedR > 0)){ |
108 |
pCtrl.staticCont = 0;
|
109 |
rpmSpeed[0] = speedL;
|
110 |
rpmSpeed[1] = speedR;
|
111 |
}else{
|
112 |
rpmSpeed[0] = 4000000 + (rpmSpeed[0] - global.rpmForward[0] * 1000000); |
113 |
rpmSpeed[1] = 4000000 + (rpmSpeed[1] - global.rpmForward[0] * 1000000); |
114 |
} |
115 |
|
116 |
for(i=4; i<5; i++){ |
117 |
if ((proxVals[i] > pCtrl.threshMid) && (proxVals[i+1] > pCtrl.threshLow)){ |
118 |
rpmSpeed[0] = -5000000 ; |
119 |
rpmSpeed[1] = -5000000 ; |
120 |
// pCtrl.staticCont++;
|
121 |
break;
|
122 |
} |
123 |
} |
124 |
chargeAsLED(); |
125 |
|
126 |
// chprintf((BaseSequentialStream*)&global.sercanmux1, "Max: %d factor: %d, Panel: %d SpeedL: %d SpeedR: %d ActualL: %d ActualR: %d\n",sPMax, pCtrl.pFactor, sPMax * pCtrl.pFactor, speedL, speedR, rpmSpeed[0], rpmSpeed[1]);
|
127 |
|
128 |
|
129 |
} |
130 |
// -------------------------------------------------------------------
|
131 |
|
132 |
|
133 |
void UserThread::preventCollision( int (&rpmSpeed)[2], uint16_t (&proxVals)[8]) { |
134 |
|
135 |
if((proxVals[3] > pCtrl.threshLow) || (proxVals[4] > pCtrl.threshLow)){ |
136 |
rpmSpeed[0] = rpmSpeed[0] / 2; |
137 |
rpmSpeed[1] = rpmSpeed[1] / 2; |
138 |
} |
139 |
|
140 |
if((proxVals[3] > pCtrl.threshMid) || (proxVals[4] > pCtrl.threshMid)){ |
141 |
rpmSpeed[0] = rpmSpeed[0] / 3; |
142 |
rpmSpeed[1] = rpmSpeed[1] / 3; |
143 |
} |
144 |
|
145 |
if((proxVals[3] > pCtrl.threshHigh) || (proxVals[4] > pCtrl.threshHigh)){ |
146 |
rpmSpeed[0] = 0; |
147 |
rpmSpeed[1] = 0; |
148 |
utCount.ringProxCount++; |
149 |
}else{
|
150 |
utCount.ringProxCount = 0;
|
151 |
} |
152 |
|
153 |
} |
154 |
|
155 |
|
156 |
/**
|
157 |
* Blocks as long as the position changes.
|
158 |
*/
|
159 |
void UserThread::checkForMotion(){
|
160 |
bool motion = true; |
161 |
int led = 0; |
162 |
types::position oldPos = global.odometry.getPosition(); |
163 |
while(motion){
|
164 |
this->sleep(200); |
165 |
types::position tmp = global.odometry.getPosition(); |
166 |
motion = oldPos.x != tmp.x; //abs(oldPos.x - tmp.x)+ abs(oldPos.y - tmp.y)+abs(oldPos.z - tmp.z);
|
167 |
oldPos = tmp; |
168 |
global.robot.setLightColor((led + 1) % 8, Color(Color::YELLOW)); |
169 |
global.robot.setLightColor(led % 8, Color(Color::BLACK));
|
170 |
led++; |
171 |
} |
172 |
lightAllLeds(Color::BLACK); |
173 |
} |
174 |
|
175 |
bool UserThread::checkFrontalObject(){
|
176 |
uint32_t thresh = pCtrl.threshMid; |
177 |
uint32_t prox; |
178 |
for(int i=0; i<8; i++){ |
179 |
prox = global.robot.getProximityRingValue(i); |
180 |
if((i == 3) || (i == 4)){ |
181 |
if(prox < thresh){
|
182 |
return false; |
183 |
} |
184 |
}else{
|
185 |
if(prox > thresh){
|
186 |
return false; |
187 |
} |
188 |
} |
189 |
} |
190 |
return true; |
191 |
} |
192 |
|
193 |
bool UserThread::checkPinVoltage(){
|
194 |
return global.ltc4412.isPluggedIn();
|
195 |
} |
196 |
|
197 |
bool UserThread::checkPinEnabled(){
|
198 |
return global.ltc4412.isEnabled();
|
199 |
} |
200 |
|
201 |
int UserThread::checkDockingSuccess(){
|
202 |
// setRpmSpeed(stop);
|
203 |
checkForMotion(); |
204 |
int success = 0; |
205 |
// global.odometry.resetPosition();
|
206 |
types::position start = global.startPos = global.odometry.getPosition(); |
207 |
global.motorcontrol.setMotorEnable(false);
|
208 |
this->sleep(1000); |
209 |
types::position stop_ = global.endPos = global.odometry.getPosition(); |
210 |
|
211 |
// Amiro moved, docking was not successful
|
212 |
// if ((start.x + stop_.x) || (start.y + stop_.y)){
|
213 |
if (abs(start.x - stop_.x) > 200 /* || (start.y + stop_.y) */){ |
214 |
lightAllLeds(Color::RED); |
215 |
// Enable Motor again if docking was not successful
|
216 |
global.motorcontrol.setMotorEnable(true);
|
217 |
success = 0;
|
218 |
}else{
|
219 |
lightAllLeds(Color::GREEN); |
220 |
success = 1;
|
221 |
} |
222 |
|
223 |
// this->sleep(500);
|
224 |
lightAllLeds(Color::BLACK); |
225 |
return success;
|
226 |
} |
227 |
|
228 |
int UserThread::getProxyRingSum(){
|
229 |
int prox_sum = 0; |
230 |
for(int i=0; i<8;i++){ |
231 |
prox_sum += global.robot.getProximityRingValue(i);; |
232 |
} |
233 |
return prox_sum;
|
234 |
} |
235 |
|
236 |
int32_t UserThread::meanDeviation(uint16_t a, uint16_t b){ |
237 |
int32_t diff = a - b; |
238 |
int32_t res = 0;
|
239 |
devCor.proxbuf[devCor.pCount] = (diff*100)/((a+b)/2); |
240 |
for (int i = 0; i< PROX_DEVIATION_MEAN_WINDOW; i++){ |
241 |
res += devCor.proxbuf[i]; |
242 |
} |
243 |
devCor.pCount++; |
244 |
devCor.pCount = devCor.pCount % PROX_DEVIATION_MEAN_WINDOW; |
245 |
|
246 |
devCor.currentDeviation = res / PROX_DEVIATION_MEAN_WINDOW; |
247 |
return devCor.currentDeviation;
|
248 |
} |
249 |
|
250 |
void setAttributes(uint8_t (&map)[MAX_NODES][NODE_ATTRIBUTES],
|
251 |
uint8_t id, uint8_t l, uint8_t r, uint8_t att) { |
252 |
map[id][0] = l;
|
253 |
map[id][1] = r;
|
254 |
map[id][2] = att;
|
255 |
} |
256 |
|
257 |
UserThread::UserThread() : |
258 |
chibios_rt::BaseStaticThread<USER_THREAD_STACK_SIZE>() |
259 |
{ |
260 |
} |
261 |
|
262 |
UserThread::~UserThread() |
263 |
{ |
264 |
} |
265 |
|
266 |
msg_t |
267 |
UserThread::main() |
268 |
{ |
269 |
/*
|
270 |
* SETUP
|
271 |
*/
|
272 |
// User thread state:
|
273 |
|
274 |
for (uint8_t led = 0; led < 8; ++led) { |
275 |
global.robot.setLightColor(led, Color(Color::BLACK)); |
276 |
} |
277 |
|
278 |
// State Variables
|
279 |
ut_states prevState = ut_states::UT_IDLE; |
280 |
ut_states currentState = ut_states::UT_INACTIVE; |
281 |
ut_states newState = ut_states::UT_INACTIVE; |
282 |
|
283 |
|
284 |
running = false;
|
285 |
LineFollowStrategy lStrategy = LineFollowStrategy::EDGE_RIGHT; |
286 |
LineFollow lf(&global); |
287 |
AmiroMap map(&global); |
288 |
/*
|
289 |
* LOOP
|
290 |
*/
|
291 |
while (!this->shouldTerminate()) |
292 |
{ |
293 |
/*
|
294 |
* read accelerometer z-value
|
295 |
*/
|
296 |
accel_z = global.lis331dlh.getAccelerationForce(LIS331DLH::AXIS_Z); |
297 |
|
298 |
if (accel_z < -900 /*-0.9g*/) { |
299 |
// Start line following when AMiRo is rotated
|
300 |
if(currentState == ut_states::UT_INACTIVE){
|
301 |
newState = ut_states::UT_FOLLOW_LINE; |
302 |
}else{
|
303 |
newState = ut_states::UT_IDLE; |
304 |
} |
305 |
lightAllLeds(Color::GREEN); |
306 |
this->sleep(1000); |
307 |
lightAllLeds(Color::BLACK); |
308 |
|
309 |
// If message was received handle it here:
|
310 |
} else if(global.msgReceived){ |
311 |
global.msgReceived = false;
|
312 |
// running = true;
|
313 |
switch(global.lfStrategy){
|
314 |
case msg_content::MSG_START:
|
315 |
newState = ut_states::UT_CALIBRATION_CHECK; |
316 |
break;
|
317 |
case msg_content::MSG_STOP:
|
318 |
newState = ut_states::UT_IDLE; |
319 |
break;
|
320 |
case msg_content::MSG_EDGE_RIGHT:
|
321 |
// newState = ut_states::UT_FOLLOW_LINE;
|
322 |
lStrategy = LineFollowStrategy::EDGE_RIGHT; |
323 |
break;
|
324 |
case msg_content::MSG_EDGE_LEFT:
|
325 |
// newState = ut_states::UT_FOLLOW_LINE;
|
326 |
lStrategy = LineFollowStrategy::EDGE_LEFT; |
327 |
break;
|
328 |
case msg_content::MSG_FUZZY:
|
329 |
// newState = ut_states::UT_FOLLOW_LINE;
|
330 |
lStrategy = LineFollowStrategy::FUZZY; |
331 |
break;
|
332 |
case msg_content::MSG_DOCK:
|
333 |
newState = ut_states::UT_DETECT_STATION; |
334 |
break;
|
335 |
case msg_content::MSG_UNDOCK:
|
336 |
newState = ut_states::UT_RELEASE; |
337 |
break;
|
338 |
case msg_content::MSG_CHARGE:
|
339 |
newState = ut_states::UT_CHARGING; |
340 |
break;
|
341 |
case msg_content::MSG_RESET_ODOMETRY:
|
342 |
global.odometry.resetPosition(); |
343 |
break;
|
344 |
case msg_content::MSG_CALIBRATE_BLACK:
|
345 |
proxCalib.calibrateBlack = true;
|
346 |
// global.odometry.resetPosition();
|
347 |
newState = ut_states::UT_CALIBRATION; |
348 |
break;
|
349 |
case msg_content::MSG_CALIBRATE_WHITE:
|
350 |
proxCalib.calibrateBlack = false;
|
351 |
newState = ut_states::UT_CALIBRATION; |
352 |
break;
|
353 |
case msg_content::MSG_TEST_MAP_STATE:
|
354 |
if (AMIRO_MAP_AUTO_TRACKING){
|
355 |
newState = ut_states::UT_TEST_MAP_AUTO_STATE; |
356 |
}else {
|
357 |
newState = ut_states::UT_TEST_MAP_STATE; |
358 |
} |
359 |
|
360 |
break;
|
361 |
|
362 |
default:
|
363 |
newState = ut_states::UT_IDLE; |
364 |
break;
|
365 |
} |
366 |
} |
367 |
// newState = currentState;
|
368 |
|
369 |
// Get sensor data
|
370 |
uint16_t WL = global.vcnl4020[constants::DiWheelDrive::PROX_WHEEL_LEFT].getProximityScaledWoOffset(); |
371 |
uint16_t WR = global.vcnl4020[constants::DiWheelDrive::PROX_WHEEL_RIGHT].getProximityScaledWoOffset(); |
372 |
for(int i=0; i<8;i++){ |
373 |
rProx[i] = global.robot.getProximityRingValue(i); |
374 |
} |
375 |
|
376 |
// Continously update devication values
|
377 |
meanDeviation(rProx[0] & 0xFFF0, rProx[7] & 0xFFF0); |
378 |
// int FL = global.vcnl4020[constants::DiWheelDrive::PROX_FRONT_LEF |