Statistics
| Branch: | Tag: | Revision:

amiro-os / devices / DiWheelDrive / userthread.cpp @ 4d54a507

History | View | Annotate | Download (36.949 KB)

1
#include "userthread.hpp"
2
#include "amiro_map.hpp"
3
#include "global.hpp"
4
#include "linefollow.hpp"
5

    
6
using namespace amiro;
7

    
8
extern Global global;
9

    
10
// a buffer for the z-value of the accelerometer
11
int16_t accel_z;
12
bool running = false;
13

    
14

    
15
/**
16
 * Set speed.
17
 *
18
 * @param rpmSpeed speed for left and right wheel in rounds/min
19
 */
20
void UserThread::setRpmSpeedFuzzy(const int (&rpmSpeed)[2]) {
21
  global.motorcontrol.setTargetRPM(rpmSpeed[constants::DiWheelDrive::LEFT_WHEEL] * 1000000, rpmSpeed[constants::DiWheelDrive::RIGHT_WHEEL] * 1000000);
22
}
23

    
24
void UserThread::setRpmSpeed(const int (&rpmSpeed)[2]) {
25
  global.motorcontrol.setTargetRPM(rpmSpeed[constants::DiWheelDrive::LEFT_WHEEL], rpmSpeed[constants::DiWheelDrive::RIGHT_WHEEL]);
26
}
27

    
28
void UserThread::lightOneLed(Color color, int idx){
29
  global.robot.setLightColor(idx, Color(color));
30
}
31

    
32
void UserThread::lightAllLeds(Color color){
33
  int led = 0;
34
  for(led=0; led<8; led++){
35
        lightOneLed(color, led);
36
      }
37
}
38

    
39
void UserThread::showChargingState(){
40
  uint8_t numLeds = global.robot.getPowerStatus().state_of_charge / 12;
41
  Color color = Color::GREEN;
42
  if (numLeds <= 2){
43
    color = Color::RED;
44
  }else if(numLeds <= 6){
45
    color = Color::YELLOW;
46
  }
47
  for (int i=0; i<numLeds; i++){
48
    lightOneLed(color, i);
49
    this->sleep(300);
50
  }
51
  this->sleep(1000);
52
  lightAllLeds(Color::BLACK);
53
}
54

    
55
void UserThread::chargeAsLED(){
56
  uint8_t numLeds = global.robot.getPowerStatus().state_of_charge / 12;
57
  Color color = Color::GREEN;
58
  if (numLeds <= 2){
59
    color = Color::RED;
60
  }else if(numLeds <= 6){
61
    color = Color::YELLOW;
62
  }
63
  for (int i=0; i<numLeds; i++){
64
    lightOneLed(color, i);
65
    // this->sleep(300);
66
  }
67
  // this->sleep(1000);
68
  // lightAllLeds(Color::BLACK);
69
}
70

    
71
// ----------------------------------------------------------------
72

    
73
void UserThread::getProxySectorVals(uint16_t (&proxVals)[8], uint16_t (&sProx)[8]){
74
  for (int i=0; i<8; i++){
75
    sProx[i] = (proxVals[i] < proxVals[(i+1) % 8]) ? proxVals[i] : proxVals[(i+1) % 8];
76
    // chprintf((BaseSequentialStream*)&global.sercanmux1, "%d: %d, ", i, sProx[i]);
77

    
78
  }
79
    // chprintf((BaseSequentialStream*)&global.sercanmux1, "\n");
80

    
81
}
82

    
83

    
84
void UserThread::getMaxFrontSectorVal(uint16_t (&sProx)[8], int32_t &sPMax){
85
  for (int i=2; i<5; i++){
86
    sPMax = (sPMax < sProx[i]) ? sProx[i] : sPMax;
87
  }
88
}
89

    
90
void UserThread::proxSectorSpeedCorrection(int (&rpmSpeed)[2], uint16_t (&proxVals)[8]){
91
  int i;
92
  uint16_t sProx[8];
93
  int32_t sPMax = 0;
94
  getProxySectorVals(proxVals, sProx);
95
  getMaxFrontSectorVal(sProx, sPMax);
96

    
97
  int32_t speedL = rpmSpeed[0] - (sPMax * pCtrl.pFactor);
98
  int32_t speedR = rpmSpeed[1] - (sPMax * pCtrl.pFactor);
99

    
100

    
101

    
102
  if(sPMax > pCtrl.threshMid){
103
      rpmSpeed[0] = 0;
104
      rpmSpeed[1] = 0;
105
      pCtrl.staticCont++;
106
  }else if((speedL > 0) || (speedR > 0)){
107
    pCtrl.staticCont = 0;
108
    rpmSpeed[0] = speedL;
109
    rpmSpeed[1] = speedR;
110
  }else{
111
    rpmSpeed[0] = 4000000 + (rpmSpeed[0] - global.rpmForward[0] * 1000000);
112
    rpmSpeed[1] = 4000000 + (rpmSpeed[1] - global.rpmForward[0] * 1000000);
113
  }
114

    
115
  for(i=4; i<5; i++){
116
    if ((proxVals[i] > pCtrl.threshMid) && (proxVals[i+1] > pCtrl.threshLow)){
117
      rpmSpeed[0] = -5000000 ;
118
      rpmSpeed[1] = -5000000 ;
119
      // pCtrl.staticCont++;
120
      break;
121
    }
122
  }
123
  chargeAsLED();
124

    
125
  // 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]);
126

    
127

    
128
}
129
// -------------------------------------------------------------------
130

    
131

    
132
void UserThread::preventCollision( int (&rpmSpeed)[2], uint16_t (&proxVals)[8]) {
133

    
134
  if((proxVals[3] > pCtrl.threshLow) || (proxVals[4] > pCtrl.threshLow)){
135
      rpmSpeed[0] = rpmSpeed[0] / 2;
136
      rpmSpeed[1] = rpmSpeed[1] / 2;
137
  }
138

    
139
  if((proxVals[3] > pCtrl.threshMid) || (proxVals[4] > pCtrl.threshMid)){
140
      rpmSpeed[0] = rpmSpeed[0] / 3;
141
      rpmSpeed[1] = rpmSpeed[1] / 3;
142
  }
143

    
144
  if((proxVals[3] > pCtrl.threshHigh) || (proxVals[4] > pCtrl.threshHigh)){
145
      rpmSpeed[0] = 0;
146
      rpmSpeed[1] = 0;
147
      utCount.ringProxCount++;
148
  }else{
149
    utCount.ringProxCount = 0;
150
  }
151

    
152
}
153

    
154

    
155
/**
156
 * Blocks as long as the position changes.
157
 */
158
void UserThread::checkForMotion(){
159
  bool motion = true;
160
  int led = 0;
161
  types::position oldPos = global.odometry.getPosition();
162
  while(motion){
163
    this->sleep(200);
164
    types::position tmp = global.odometry.getPosition();
165
    motion = oldPos.x != tmp.x; //abs(oldPos.x - tmp.x)+ abs(oldPos.y - tmp.y)+abs(oldPos.z - tmp.z);
166
    oldPos = tmp;
167
    global.robot.setLightColor((led + 1) % 8, Color(Color::YELLOW));
168
    global.robot.setLightColor(led % 8, Color(Color::BLACK));
169
    led++;
170
  }
171
  lightAllLeds(Color::BLACK);
172
}
173

    
174
bool UserThread::checkFrontalObject(){
175
  uint32_t thresh = pCtrl.threshMid;
176
  uint32_t prox;
177
  for(int i=0; i<8; i++){
178
    prox = global.robot.getProximityRingValue(i);
179
    if((i == 3) || (i == 4)){
180
      if(prox < thresh){
181
        return false;
182
      }
183
    }else{
184
      if(prox > thresh){
185
        return false;
186
      }
187
    }
188
  }
189
  return true;
190
}
191

    
192
bool UserThread::checkPinVoltage(){
193
  return global.ltc4412.isPluggedIn();
194
}
195

    
196
bool UserThread::checkPinEnabled(){
197
  return global.ltc4412.isEnabled();
198
}
199

    
200
int UserThread::checkDockingSuccess(){
201
  // setRpmSpeed(stop);
202
  checkForMotion();
203
  int success = 0;
204
  // global.odometry.resetPosition();
205
  types::position start = global.startPos = global.odometry.getPosition();
206
  global.motorcontrol.setMotorEnable(false);
207
  this->sleep(1000);
208
  types::position stop_ = global.endPos = global.odometry.getPosition();
209

    
210
  // Amiro moved, docking was not successful
211
  // if ((start.x + stop_.x)  || (start.y + stop_.y)){
212
  if (abs(start.x - stop_.x) > 200 /* || (start.y + stop_.y) */){
213
    lightAllLeds(Color::RED);
214
    // Enable Motor again if docking was not successful
215
    global.motorcontrol.setMotorEnable(true);
216
    success = 0;
217
  }else{
218
    lightAllLeds(Color::GREEN);
219
    success = 1;
220
  }
221

    
222
  // this->sleep(500);
223
  lightAllLeds(Color::BLACK);
224
  return success;
225
}
226

    
227
int UserThread::getProxyRingSum(){
228
  int prox_sum = 0;
229
  for(int i=0; i<8;i++){
230
    prox_sum += global.robot.getProximityRingValue(i);;
231
  }
232
  return prox_sum;
233
}
234

    
235
int32_t UserThread::meanDeviation(uint16_t a, uint16_t b){
236
  int32_t diff = a - b;
237
  int32_t res = 0;
238
  devCor.proxbuf[devCor.pCount] = (diff*100)/((a+b)/2);
239
  for (int i = 0; i< PROX_DEVIATION_MEAN_WINDOW; i++){
240
    res += devCor.proxbuf[i];
241
  }
242
  devCor.pCount++;
243
  devCor.pCount = devCor.pCount % PROX_DEVIATION_MEAN_WINDOW;
244

    
245
  devCor.currentDeviation =  res / PROX_DEVIATION_MEAN_WINDOW;
246
  return devCor.currentDeviation;
247
}
248

    
249
void setAttributes(uint8_t (&map)[MAX_NODES][NODE_ATTRIBUTES],
250
                          uint8_t id, uint8_t l, uint8_t r, uint8_t att) {
251
  map[id][0] = l;
252
  map[id][1] = r;
253
  map[id][2] = att;
254
}
255

    
256
UserThread::UserThread() :
257
  chibios_rt::BaseStaticThread<USER_THREAD_STACK_SIZE>()
258
{
259
}
260

    
261
UserThread::~UserThread()
262
{
263
}
264

    
265
msg_t
266
UserThread::main()
267
{
268
  /*
269
   * SETUP
270
   */
271
  // User thread state:
272

    
273
  for (uint8_t led = 0; led < 8; ++led) {
274
    global.robot.setLightColor(led, Color(Color::BLACK));
275
  }
276

    
277
  // State Variables
278
  ut_states prevState = ut_states::UT_IDLE;
279
  ut_states currentState = ut_states::UT_INACTIVE;
280
  ut_states newState = ut_states::UT_INACTIVE;
281

    
282

    
283
  running = false;
284
  LineFollowStrategy lStrategy = LineFollowStrategy::EDGE_RIGHT;
285
  LineFollow lf(&global);
286
  AmiroMap map(&global, lStrategy);
287
  /*
288
   * LOOP
289
   */
290
  while (!this->shouldTerminate())
291
  {
292
    /*
293
    * read accelerometer z-value
294
    */
295
    accel_z = global.lis331dlh.getAccelerationForce(LIS331DLH::AXIS_Z);
296

    
297
    if (accel_z < -900 /*-0.9g*/) {
298
      // Start line following when AMiRo is rotated
299
      if(currentState == ut_states::UT_INACTIVE){
300
        newState = ut_states::UT_FOLLOW_LINE;
301
      }else{
302
        newState = ut_states::UT_IDLE;
303
      }
304
      lightAllLeds(Color::GREEN);
305
      this->sleep(1000);
306
      lightAllLeds(Color::BLACK);
307

    
308
    // If message was received handle it here:
309
    } else if(global.msgReceived){
310
      global.msgReceived = false;
311
      // running = true;
312
      switch(global.lfStrategy){
313
      case msg_content::MSG_START:
314
        newState = ut_states::UT_CALIBRATION_CHECK;
315
        break;
316
      case msg_content::MSG_STOP:
317
        newState = ut_states::UT_IDLE;
318
        break;
319
      case msg_content::MSG_EDGE_RIGHT:
320
        // newState = ut_states::UT_FOLLOW_LINE;
321
        lStrategy = LineFollowStrategy::EDGE_RIGHT;
322
        break;
323
      case  msg_content::MSG_EDGE_LEFT:
324
        // newState = ut_states::UT_FOLLOW_LINE;
325
        lStrategy = LineFollowStrategy::EDGE_LEFT;
326
        break;
327
      case msg_content::MSG_FUZZY:
328
        // newState = ut_states::UT_FOLLOW_LINE;
329
        lStrategy = LineFollowStrategy::FUZZY;
330
        break;
331
      case msg_content::MSG_DOCK:
332
        newState = ut_states::UT_DETECT_STATION;
333
        break;
334
      case msg_content::MSG_UNDOCK:
335
        newState = ut_states::UT_RELEASE;
336
        break;
337
      case msg_content::MSG_CHARGE:
338
        newState = ut_states::UT_CHARGING;
339
        break;
340
      case msg_content::MSG_RESET_ODOMETRY:
341
        global.odometry.resetPosition();
342
        break;
343
      case msg_content::MSG_CALIBRATE_BLACK:
344
        proxCalib.calibrateBlack = true;
345
        // global.odometry.resetPosition();
346
        newState = ut_states::UT_CALIBRATION;
347
        break;
348
      case msg_content::MSG_CALIBRATE_WHITE:
349
        proxCalib.calibrateBlack = false;
350
        newState = ut_states::UT_CALIBRATION;
351
        break;
352
      case msg_content::MSG_TEST_MAP_STATE:
353
        if (AMIRO_MAP_AUTO_TRACKING){
354
          newState = ut_states::UT_TEST_MAP_AUTO_STATE;
355
        }else {
356
          newState = ut_states::UT_TEST_MAP_STATE;
357
        }
358
        break;
359
      case msg_content::MSG_SET_DIST_THRESH:
360

    
361

    
362
        break;
363
      case msg_content::MSG_GET_MAP_INFO:
364
        break;
365
      default:
366
        newState = ut_states::UT_IDLE;
367
        break;
368
      }
369
    }
370
    // newState = currentState;
371

    
372
    // Get sensor data
373
    uint16_t WL = global.vcnl4020[constants::DiWheelDrive::PROX_WHEEL_LEFT].getProximityScaledWoOffset();
374
    uint16_t WR = global.vcnl4020[constants::DiWheelDrive::PROX_WHEEL_RIGHT].getProximityScaledWoOffset();
375
    for(int i=0; i<8;i++){
376
      rProx[i] = global.robot.getProximityRingValue(i);
377
    }