Statistics
| Branch: | Tag: | Revision:

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_LEFT].getProximityScaledWoOffset();
379
    // int FR = global.vcnl4020[constants::DiWheelDrive::PROX_FRONT_RIGHT].getProximityScaledWoOffset();
380
    switch(currentState){
381
      case ut_states::UT_INACTIVE:
382
        // Dummy state to deactivate every interaction
383
      break;
384
      // ---------------------------------------
385
      case ut_states::UT_CALIBRATION_CHECK:
386
        // global.robot.calibrate();
387
        if(global.linePID.BThresh >= global.linePID.WThresh){
388
          newState = ut_states::UT_CALIBRATION_ERROR;
389
        }else{
390
          newState = ut_states::UT_FOLLOW_LINE;
391
        }
392
      break;
393
      // ---------------------------------------
394
      case ut_states::UT_CALIBRATION:
395
        /* Calibrate the global thresholds for black or white.
396
            This values will be used by the line follow object
397
        */
398

    
399
        proxCalib.buf = 0;
400
        if(proxCalib.calibrateBlack){
401
          chprintf((BaseSequentialStream*)&global.sercanmux1, "Black Calibration, Place AMiRo on black Surface!\n");
402
          global.robot.calibrate();
403
        }
404
        for(int i=0; i <= proxCalib.meanWindow; i++){
405
          proxCalib.buf += global.vcnl4020[constants::DiWheelDrive::PROX_FRONT_LEFT].getProximityScaledWoOffset()
406
                          + global.vcnl4020[constants::DiWheelDrive::PROX_FRONT_RIGHT].getProximityScaledWoOffset();
407
          this->sleep(CAN::UPDATE_PERIOD);
408
        }
409
        proxCalib.buf = proxCalib.buf / (2*proxCalib.meanWindow);
410

    
411
        if(proxCalib.calibrateBlack){
412
          global.linePID.BThresh = proxCalib.buf;
413
        }else  {
414
          global.linePID.WThresh  = proxCalib.buf;
415
        }
416
          chprintf((BaseSequentialStream*)&global.sercanmux1, "Black: %d, White: %d!\n", global.linePID.BThresh, global.linePID.WThresh);
417

    
418
        newState = ut_states::UT_IDLE;
419
      break;
420
      // ---------------------------------------
421
      case ut_states::UT_IDLE:
422
        global.motorcontrol.setMotorEnable(true);
423
        setRpmSpeed(stop);
424
        if(/* checkPinVoltage() && */ checkPinEnabled()){
425
          global.robot.requestCharging(0);
426
        }
427
        // pCtrl.pFactor = 0;
428
        pCtrl.staticCont = 0;
429
        utCount.whiteCount = 0;
430
        utCount.ringProxCount = 0;
431
        utCount.errorCount = 0;
432
        newState = ut_states::UT_INACTIVE;
433
      break;
434
      // ---------------------------------------
435
      case ut_states::UT_FOLLOW_LINE:
436
      // Set correct forward speed to every strategy
437
        if (global.forwardSpeed != global.rpmForward[0]){
438
          global.forwardSpeed = global.rpmForward[0];
439
        }
440

    
441
        if(lf.getStrategy() != lStrategy){
442
          lf.setStrategy(lStrategy);
443
        }
444

    
445
        if(lf.followLine(rpmSpeed)){
446
          utCount.whiteCount++;
447
          if(utCount.whiteCount >= WHITE_DETETION_TIMEOUT){
448
            setRpmSpeed(stop);
449
            utCount.whiteCount = 0;
450
            newState = ut_states::UT_WHITE_DETECTION_ERROR;
451
          }
452
        }else{
453
          utCount.whiteCount = 0;
454
        }
455

    
456
        preventCollision(rpmSpeed, rProx);
457
        // proxSectorSpeedCorrection(rpmSpeed, rProx);
458

    
459
        if(utCount.ringProxCount > RING_PROX_DETECTION_TIMEOUT){
460
          utCount.ringProxCount = 0;
461

    
462

    
463
          checkForMotion();
464
          // Check if only front sensors are active
465
          if (checkFrontalObject()) {
466
            // global.distcontrol.setTargetPosition(0, 2792526, ROTATION_DURATION);
467
            // // BaseThread::sleep(8000);
468
            // checkForMotion();
469
            this->utCount.whiteCount = 0;
470
            newState = ut_states::UT_TURN;
471
            // lf.promptStrategyChange(LineFollowStrategy::EDGE_LEFT);
472
          } else {
473
            newState = ut_states::UT_PROXY_DETECTION_ERROR;
474
          }
475
        }
476

    
477
        if (lf.getStrategy() == LineFollowStrategy::FUZZY){
478
          setRpmSpeedFuzzy(rpmSpeed);
479
        }else{
480

    
481
          setRpmSpeed(rpmSpeed);
482
        }
483

    
484
      break;
485
      // ---------------------------------------
486
    case ut_states::UT_TURN:{
487
        // Check the line strategy in order to continue driving on the right side
488
      int factor = SPEED_CONVERSION_FACTOR;
489
      int frontL = global.vcnl4020[constants::DiWheelDrive::PROX_FRONT_LEFT].getProximityScaledWoOffset();
490
      int frontR = global.vcnl4020[constants::DiWheelDrive::PROX_FRONT_RIGHT].getProximityScaledWoOffset();
491
      int blackSensor = 0;
492
      if (lf.getStrategy() == LineFollowStrategy::EDGE_RIGHT) {
493
        factor = -factor;
494
        blackSensor = frontL;
495
      }else{
496
        blackSensor = frontR;
497
      }
498

    
499
      rpmSpeed[0] = factor * CHARGING_SPEED;
500
      rpmSpeed[1] = -factor * CHARGING_SPEED;
501
      setRpmSpeed(rpmSpeed);
502

    
503
      if ((blackSensor >= global.linePID.WThresh )){
504
        utCount.whiteCount = 1;
505
      }else {
506
        if((utCount.whiteCount == 1) && (blackSensor <= global.linePID.BThresh)){
507
          utCount.whiteCount = 0;
508
          newState = ut_states::UT_FOLLOW_LINE;
509
          setRpmSpeed(stop);
510
        }
511
      }
512
      break;
513
    }
514
      // ---------------------------------------
515
    case ut_states::UT_DETECT_STATION:
516

    
517
        if (global.forwardSpeed != DETECTION_SPEED){
518
          global.forwardSpeed = DETECTION_SPEED;
519
        }
520
        if(lf.getStrategy() != LineFollowStrategy::EDGE_RIGHT){
521
          lf.setStrategy(LineFollowStrategy::EDGE_RIGHT);
522
        }
523

    
524
        lf.followLine(rpmSpeed);
525
        setRpmSpeed(rpmSpeed);
526
        // // Detect marker before docking station
527
        // if ((WL+WR) < PROXY_WHEEL_THRESH){
528
        // Use proxy ring
529
        if ((rProx[3]+rProx[4]) > RING_PROX_FRONT_THRESH){
530

    
531
          setRpmSpeed(stop);
532
          checkForMotion();
533
          // 180° Rotation
534
          global.distcontrol.setTargetPosition(0, ROTATION_180, ROTATION_DURATION);
535
          // BaseThread::sleep(8000);
536
          checkForMotion();
537
          newState = ut_states::UT_CORRECT_POSITIONING;
538
        }
539
      break;
540
      // ---------------------------------------
541
    case ut_states::UT_CORRECT_POSITIONING:
542
        if (global.forwardSpeed != CHARGING_SPEED){
543
          global.forwardSpeed = CHARGING_SPEED;
544
        }
545
        if(lf.getStrategy() != LineFollowStrategy::EDGE_LEFT){
546
          lf.promptStrategyChange(LineFollowStrategy::EDGE_LEFT);
547
        }
548
        lf.followLine(rpmSpeed);
549
        setRpmSpeed(rpmSpeed);
550

    
551
        utCount.stateTime++;
552
        if (utCount.stateTime >= DOCKING_CORRECTION_TIMEOUT){
553
          utCount.stateTime = 0;
554
          newState = ut_states::UT_REVERSE;
555
          setRpmSpeed(stop);
556
          checkForMotion();
557
        }
558
      break;
559
      // ---------------------------------------
560
    case ut_states::UT_REVERSE:
561
        if(lf.getStrategy() != LineFollowStrategy::REVERSE){
562
          lf.setStrategy(LineFollowStrategy::REVERSE);
563
        }
564
        lf.followLine(rpmSpeed);
565
        setRpmSpeed(rpmSpeed);
566
        // utCount.stateTime++;
567

    
568
        // Docking is only successful if Deviation is in range and sensors are at their max values.
569
        if((rProx[0] >= PROX_MAX_VAL)
570
           && (rProx[7] >= PROX_MAX_VAL)
571
           && ((devCor.currentDeviation > -MAX_DEVIATION_FACTOR) && (devCor.currentDeviation < MAX_DEVIATION_FACTOR) )){
572
          // setRpmSpeed(stop);
573
          // checkForMotion();
574
          utCount.stateTime = 0;
575
          newState = ut_states::UT_PUSH_BACK;
576
        }else if ((devCor.currentDeviation <= -MAX_DEVIATION_FACTOR) && ((rProx[0] > DEVIATION_DIST_THRESH) || (rProx[7] > DEVIATION_DIST_THRESH))){
577
          // Case R
578
          utCount.stateTime = 0;
579
          setRpmSpeed(stop);
580
          devCor.RCase = true;
581
          lightAllLeds(Color::YELLOW);
582
          newState = ut_states::UT_DEVIATION_CORRECTION;
583
        }else if ((devCor.currentDeviation >= MAX_DEVIATION_FACTOR) && ((rProx[0] > DEVIATION_DIST_THRESH) || (rProx[7] > DEVIATION_DIST_THRESH))){
584
          // Case L
585
          utCount.stateTime = 0;
586
          setRpmSpeed(stop);
587
          devCor.RCase = false;
588
          lightAllLeds(Color::WHITE);
589
          newState = ut_states::UT_DEVIATION_CORRECTION;
590
        }else if (utCount.stateTime >= REVERSE_DOCKING_TIMEOUT){
591
          setRpmSpeed(stop);
592
          utCount.stateTime = 0;
593
          utCount.errorCount++;
594
          if (utCount.errorCount >= DOCKING_ERROR_THRESH){
595
            newState = ut_states::UT_DOCKING_ERROR;
596
          }else{
597
            newState = ut_states::UT_CORRECT_POSITIONING;
598
          }
599
        }
600

    
601
        // if((devCor.currentDeviation <= -10)){
602
        //   rpmSpeed[0] -= 2000000;
603
        // }else if(devCor.currentDeviation >= 10){
604
        //   rpmSpeed[1] -= 2000000;
605
        // }
606
        // setRpmSpeed(rpmSpeed);
607
      break;
608
      // ---------------------------------------
609
    case ut_states::UT_DEVIATION_CORRECTION:
610
        // if(lf.getStrategy() != LineFollowStrategy::REVERSE){
611
        //   lf.setStrategy(LineFollowStrategy::REVERSE);
612
        // }
613
        // lf.followLine(rpmSpeed);
614
        // setRpmSpeed(rpmSpeed);
615
        if(utCount.stateTime < DEVIATION_CORRECTION_DURATION / 2 ){
616
          if(devCor.RCase){
617
            rpmSpeed[0] = 0;
618
            rpmSpeed[1] = DEVIATION_CORRECTION_SPEED;
619
          }else {
620
            rpmSpeed[0] = DEVIATION_CORRECTION_SPEED;
621
            rpmSpeed[1] = 0;
622
          }
623
          setRpmSpeed(rpmSpeed);
624
        }else if (((utCount.stateTime >= DEVIATION_CORRECTION_DURATION / 2) && (utCount.stateTime < DEVIATION_CORRECTION_DURATION +10)) ){
625
          if(devCor.RCase){
626
            rpmSpeed[0] = DEVIATION_CORRECTION_SPEED;
627
            rpmSpeed[1] = 0;
628
          }else {
629
            rpmSpeed[0] = 0;
630
            rpmSpeed[1] = DEVIATION_CORRECTION_SPEED;
631
          }
632
          setRpmSpeed(rpmSpeed);
633
          if(((devCor.currentDeviation >= -5) && (devCor.currentDeviation <= 5))){
634
            utCount.stateTime = 0;
635
            newState = ut_states::UT_REVERSE;
636
            setRpmSpeed(stop);
637
          }
638
        }else{
639
          utCount.stateTime = 0;
640
          newState = ut_states::UT_REVERSE;
641
          setRpmSpeed(stop);
642
        }
643

    
644
        utCount.stateTime++;
645

    
646

    
647
        // if (utCount.stateTime > PUSH_BACK_TIMEOUT){
648
        //   utCount.stateTime = 0;
649
        //   newState = ut_states::UT_CHECK_POSITIONING;
650
        // }
651
      break;
652
      // ---------------------------------------
653
    case ut_states::UT_PUSH_BACK:
654
        if(lf.getStrategy() != LineFollowStrategy::REVERSE){
655
          lf.setStrategy(LineFollowStrategy::REVERSE);
656
        }
657
        lf.followLine(rpmSpeed);
658
        setRpmSpeed(rpmSpeed);
659

    
660
        utCount.stateTime++;
661
        if (utCount.stateTime > PUSH_BACK_TIMEOUT){
662
          utCount.stateTime = 0;
663
          newState = ut_states::UT_CHECK_POSITIONING;
664
        }
665
      break;
666
      // ---------------------------------------
667
    case ut_states::UT_CHECK_POSITIONING:
668
        setRpmSpeed(stop);
669
        checkForMotion();
670
        if(checkDockingSuccess()){
671
          newState = ut_states::UT_CHECK_VOLTAGE;
672
        }else{
673
          utCount.errorCount++;
674
          newState = ut_states::UT_CORRECT_POSITIONING;
675
          if (utCount.errorCount >= DOCKING_ERROR_THRESH){
676
              newState = ut_states::UT_DOCKING_ERROR;
677
            }
678
        }
679
      break;
680
      // ---------------------------------------
681
    case ut_states::UT_CHECK_VOLTAGE:
682
        if(!checkPinEnabled()){
683
          global.robot.requestCharging(1);
684
        } else {
685
          if(checkPinVoltage()){
686
            // Pins are under voltage -> correctly docked
687

    
688
            newState = ut_states::UT_CHARGING;
689
          }else{
690
            utCount.errorCount++;
691
            // No voltage on pins -> falsely docked
692
            // deactivate pins
693
            global.motorcontrol.setMotorEnable(true);
694
            global.robot.requestCharging(0);
695
            // TODO: Soft release when docking falsely
696
            if((rProx[0] >= PROX_MAX_VAL) && (rProx[7] >= PROX_MAX_VAL)){
697
              newState = ut_states::UT_RELEASE_TO_CORRECT;
698
            } else {
699
              newState = ut_states::UT_RELEASE_TO_CORRECT; //ut_states::UT_CORRECT_POSITIONING;
700
            }
701

    
702
            if (utCount.errorCount > DOCKING_ERROR_THRESH){
703
              newState = ut_states::UT_DOCKING_ERROR;
704
            }
705
          }
706
        }
707
      break;
708
      // ---------------------------------------
709
    case ut_states::UT_RELEASE_TO_CORRECT:
710

    
711
        global.distcontrol.setTargetPosition(0, ROTATION_20, ROTATION_DURATION);
712
        checkForMotion();
713
        // move 1cm forward
714
        global.distcontrol.setTargetPosition(5000, 0, ROTATION_DURATION);
715
        checkForMotion();
716
        // rotate back
717
        global.distcontrol.setTargetPosition(0, -2*ROTATION_20, ROTATION_DURATION);
718
        checkForMotion();
719

    
720
        global.distcontrol.setTargetPosition(1500, 0, ROTATION_DURATION);
721
        checkForMotion();
722
        newState = ut_states::UT_CORRECT_POSITIONING;
723
      break;
724
      // ---------------------------------------
725
    case ut_states::UT_CHARGING:
726
        global.motorcontrol.setMotorEnable(false);
727
        utCount.errorCount = 0;
728
        // Formulate Request to enable charging
729
        if(/* checkPinVoltage() && */ !checkPinEnabled()){
730
          global.robot.requestCharging(1);
731
        }
732
        if(checkPinEnabled()){
733
          showChargingState();
734
        }
735
      break;
736
      // ---------------------------------------
737
    case ut_states::UT_RELEASE:
738

    
739
      if (global.forwardSpeed != DETECTION_SPEED){
740
          global.rpmForward[0] = DETECTION_SPEED;
741
        }
742
        if(/* checkPinVoltage() && */ checkPinEnabled()){
743
          global.robot.requestCharging(0);
744
        }else{
745
          global.motorcontrol.setMotorEnable(true);
746
          // TODO: Use controlled
747
          //Rotate -20° to free from magnet
748
          global.distcontrol.setTargetPosition(0, ROTATION_20, ROTATION_DURATION);
749
          checkForMotion();
750
          // move 1cm forward
751
          global.distcontrol.setTargetPosition(5000, 0, ROTATION_DURATION);
752
          checkForMotion();
753
          // rotate back
754
          // global.distcontrol.setTargetPosition(0, -ROTATION_20, ROTATION_DURATION);
755
          // checkForMotion();
756

    
757
          // global.distcontrol.setTargetPosition(5000, 0, ROTATION_DURATION);
758
          // checkForMotion();
759
          lStrategy = LineFollowStrategy::EDGE_RIGHT;
760
          newState = ut_states::UT_FOLLOW_LINE;
761
            // whiteBuf = -100;
762
          // lf.followLine(rpmSpeed);
763
          // setRpmSpeed(rpmSpeed);
764
        }
765
        // lightAllLeds(Color::BLACK);
766
      break;
767
      // ---------------------------------------
768
    case ut_states::UT_DOCKING_ERROR:
769
      newState = ut_states::UT_RELEASE;
770
      break;
771
      // ---------------------------------------
772
    case ut_states::UT_REVERSE_TIMEOUT_ERROR:
773
      newState = ut_states::UT_IDLE;
774
      break;
775
      // ---------------------------------------
776
    case ut_states::UT_CALIBRATION_ERROR:
777
      newState = ut_states::UT_IDLE;
778
      break;
779
      // ---------------------------------------
780
    case ut_states::UT_WHITE_DETECTION_ERROR:
781
      newState = ut_states::UT_IDLE;
782
      break;
783
      // ---------------------------------------
784
    case ut_states::UT_PROXY_DETECTION_ERROR:
785
      newState = ut_states::UT_IDLE;
786
      break;
787
      // ---------------------------------------
788
    case ut_states::UT_NO_CHARGING_POWER_ERROR:
789
      newState = ut_states::UT_IDLE;
790
      break;
791
      // ---------------------------------------
792
    case ut_states::UT_UNKNOWN_STATE_ERROR:
793
        newState = ut_states::UT_IDLE;
794
      break;
795
      // ---------------------------------------
796
    case ut_states::UT_TEST_MAP_AUTO_STATE:
797
      chprintf((BaseSequentialStream *)&global.sercanmux1,
798
               "Start autotracking: \n");
799

    
800
      chprintf((BaseSequentialStream *)&global.sercanmux1,
801
               "NodeCount: %d\n", map.getNodeCount());
802
      for (int i=0; i<map.getNodeCount(); i++) {
803
        chprintf((BaseSequentialStream *)&global.sercanmux1,
804
                 "ID: %d, l: %d, r: %d \n", map.getNodeList()[i].id,
805
                 map.getNodeList()[i].left, map.getNodeList()[i].right);
806
      }
807
      newState = UT_IDLE;
808
      break;
809
    case ut_states::UT_TEST_MAP_STATE:{
810
      // Test suit for amiro map
811

    
812

    
813

    
814
      // AmiroMap map = AmiroMap(&global);
815

    
816
      // --------------------------------------------------
817

    
818
      global.tcase = 0;
819
      // Set basic valid map configuration
820
      setAttributes(global.testmap, 0, 1, 2, 1);
821
      setAttributes(global.testmap, 1, 2, 2, 0);
822
      setAttributes(global.testmap, 2, 1, 0, 0);
823
      setAttributes(global.testmap, 3, 0, 0, 0xff);
824
      chprintf((BaseSequentialStream *)&global.sercanmux1, "Init Case: %d, res: %d\n",global.tcase, map.initialize());
825
      global.testres[global.tcase] = map.get_state()->valid;
826

    
827
      global.tcase++; // 1
828
      // Test map fail if first node is flagged with end
829
      setAttributes(global.testmap, 0, 1, 2, 0xff);
830
      map.initialize();
831
      global.testres[global.tcase] = !map.get_state()->valid;
832

    
833
      global.tcase++; // 2
834
      // Test if node 2 is set as start node
835
      setAttributes(global.testmap, 0, 1, 2, 0);
836
      setAttributes(global.testmap, 2, 1, 0, 1);
837
      map.initialize();
838
      global.testres[global.tcase] = map.get_state()->current == 2;
839

    
840
      global.tcase++; // 3
841
      // Test if non reachable nodes will trigger invalid map
842
      setAttributes(global.testmap, 3, 0, 0, 0);
843
      setAttributes(global.testmap, 4, 0, 0, 0xff);
844
      map.initialize();
845
      global.testres[global.tcase] = !map.get_state()->valid;
846

    
847
      global.tcase++; // 4
848
      // Test Reinitialization
849
      setAttributes(global.testmap, 0, 1, 2, 1);
850
      setAttributes(global.testmap, 1, 2, 2, 0);
851
      setAttributes(global.testmap, 2, 1, 0, 0);
852
      setAttributes(global.testmap, 3, 0, 0, 0xff);
853
      map.initialize();
854
      global.testres[global.tcase] = map.get_state()->valid;
855

    
856
      global.odometry.resetPosition();
857
      uint8_t ret = 0;
858
      global.tcase++; // 5
859
      // Test update under normal linefollowing without fixpoint
860
      ret = map.update(20000, 20000, LineFollowStrategy::EDGE_RIGHT);
861
      chprintf((BaseSequentialStream *)&global.sercanmux1,
862
               "Update test %d: Ret %d, cur %d, nex %d\n", global.tcase, ret,
863
               map.get_state()->current, map.get_state()->next);
864
      // No case should be true because neither was a node visited nor
865
      // was a fixpoint detected.
866
      global.testres[global.tcase] = (ret == 0x4);
867

    
868

    
869
      global.odometry.setPosition(1.0, 0.0, 0.0);
870
      chprintf((BaseSequentialStream *)&global.sercanmux1, "Current Point: %d\n", global.odometry.getPosition().x);
871
      global.tcase++; // 6
872
      // Fixpoint on left side
873
      ret = map.update(0, 20000, LineFollowStrategy::EDGE_RIGHT);
874
      chprintf((BaseSequentialStream *)&global.sercanmux1,
875
               "Update test %d: Ret %d, cur %d, nex %d\n", global.tcase, ret,
876
               map.get_state()->current, map.get_state()->next);
877
      // No case should be true because neither was a node visited nor
878
      // was a fixpoint detected.
879
      // global.odometry
880
      global.testres[global.tcase] = (ret == 0x1)
881
        && (map.get_state()->strategy == 0x01)
882
        && (map.get_state()->dist == 0)
883
        && (map.get_state()->current == 2);
884

    
885

    
886
      global.odometry.setPosition(1.5, 0.0, 0.0);
887
      chprintf((BaseSequentialStream *)&global.sercanmux1,
888
               "Current Point: %d\n", global.odometry.getPosition().x);
889
      global.tcase++; // 7
890
      // Fixpoint on left side, no update should appear because fixpoint already
891
      // marked
892
      ret = map.update(0, 20000, LineFollowStrategy::EDGE_RIGHT);
893
      chprintf((BaseSequentialStream *)&global.sercanmux1,
894
               "Update test %d: Ret %d, cur %d, nex %d\n", global.tcase, ret,
895
               map.get_state()->current, map.get_state()->next);
896
      // No case should be true because neither was a node visited nor
897
      // was a fixpoint detected.
898
      global.testres[global.tcase] = (ret == 0x00)
899
        && (map.get_state()->strategy == 0x01);
900
        // && (map.get_state()->dist == 0);
901

    
902
      global.odometry.setPosition(1.2, 0.0, 0.0);
903
      global.tcase++; // 8
904
      // Fixpoint on left side, no update should appear because fixpoint already
905
      // marked
906
      ret = map.update(20000, 20000, LineFollowStrategy::EDGE_RIGHT);
907
      chprintf((BaseSequentialStream *)&global.sercanmux1,
908
               "Update test %d: Ret %d, cur %d, nex %d, dist %d, len %d\n", global.tcase, ret,
909
               map.get_state()->current, map.get_state()->next, map.get_state()->dist, map.get_state()->eLength);
910
      // No case should be true because neither was a node visited nor
911
      // was a fixpoint detected.
912
      global.testres[global.tcase] =
913
          (ret == 0x04) && (map.get_state()->strategy == 0x01)
914
        && (map.get_state()->dist == 0);
915

    
916
      global.odometry.setPosition(.5, 0.0, 0.0);
917
      chprintf((BaseSequentialStream *)&global.sercanmux1,
918
               "Current Point: %d\n", global.odometry.getPosition().x);
919
      global.tcase++; // 9
920
      // Fixpoint on left side, no update should appear because fixpoint already
921
      // marked
922
      ret = map.update(0, 20000, LineFollowStrategy::EDGE_RIGHT);
923
      chprintf((BaseSequentialStream *)&global.sercanmux1,
924
               "Update test %d: Ret %d, cur %d, nex %d, dist %d, len %d\n",
925
               global.tcase, ret, map.get_state()->current,
926
               map.get_state()->next, map.get_state()->dist,
927
               map.get_state()->eLength);
928
      // No case should be true because neither was a node visited nor
929
      // was a fixpoint detected.
930
      global.testres[global.tcase] =
931
        (ret == 9) &&
932
        (map.get_state()->strategy == 1) &&
933
        (map.get_state()->dist == 0) &&
934
        (map.get_state()->eLength == 50);
935

    
936
      global.odometry.setPosition(.75, 0.0, 0.0);
937
      chprintf((BaseSequentialStream *)&global.sercanmux1,
938
               "Current Point: %d\n", global.odometry.getPosition().x);
939
      global.tcase++; // 10
940
      // Fixpoint on left side, no update should appear because fixpoint already
941
      // marked
942
      ret = map.update(20000, 20000, LineFollowStrategy::EDGE_RIGHT);
943
      chprintf((BaseSequentialStream *)&global.sercanmux1,
944
               "Update test %d: Ret %d, cur %d, nex %d, dist %d, len %d\n",
945
               global.tcase, ret, map.get_state()->current,
946
               map.get_state()->next, map.get_state()->dist,
947
               map.get_state()->eLength);
948
      // No case should be true because neither was a node visited nor
949
      // was a fixpoint detected.
950
      global.testres[global.tcase] =
951
          (ret == 12) && (map.get_state()->strategy == 1) &&
952
          (map.get_state()->dist == 50) && (map.get_state()->eLength == 50);
953

    
954
      int failed = 0;
955
      int passed = 0;
956
      for (int i = 0; i <= global.tcase; i++) {
957
        if (global.testres[i]) {
958
          passed++;
959
          chprintf((BaseSequentialStream *)&global.sercanmux1,
960
                   "Test %d Passed!\n", i);
961
        } else {
962
          failed++;
963
          chprintf((BaseSequentialStream *)&global.sercanmux1,
964
                   "Test %d Failed\n", i);
965
        }
966
      }
967
      chprintf((BaseSequentialStream *)&global.sercanmux1,
968
               "Total: %d, Passed: %d, Failed: %d\n", global.tcase + 1, passed,
969
               failed);
970

    
971
      newState = ut_states::UT_IDLE;
972
      break;
973
    }
974
      // --------------------------------------------------
975
    default:
976
      newState = ut_states::UT_UNKNOWN_STATE_ERROR;
977
      break;
978
    }
979

    
980
    // In case a new state is set:
981
    // 1. Record the state transition
982
    if (AMIRO_MAP_AUTO_TRACKING) {
983
      // map.trackUpdate(WL, WR, lStrategy, currentState);
984
    }
985

    
986
    if (currentState != newState){
987

    
988
      global.stateTransitionCounter++;
989
      // Clear all state transitions to prevent overflow
990
      if (global.stateTransitionCounter >= 255) {
991
        global.stateTransitionCounter = 0;
992
        for (int i = 0; i < 24; i++) {
993
          global.stateTracker[i] = 0;
994
        }
995
}
996
      // Transmit the new state over can
997
      chprintf((BaseSequentialStream*)&global.sercanmux1, "Transmit state %d\n", newState);
998
      global.robot.transmitState(newState);
999

    
1000
      // Increase state count for specific state
1001
      // TODO: Improve with dictionary or other than switch case
1002
      if (newState == ut_states::UT_IDLE)
1003
        {global.stateTracker[ut_states::UT_IDLE] += 1;}
1004
      else if (newState == ut_states::UT_FOLLOW_LINE)
1005
        {global.stateTracker[ut_states::UT_FOLLOW_LINE] += 1;}
1006
      else if (newState == ut_states::UT_DETECT_STATION)
1007
        {global.stateTracker[ut_states::UT_DETECT_STATION] += 1;}
1008
      else if (newState == ut_states::UT_REVERSE)
1009
        {global.stateTracker[ut_states::UT_REVERSE] += 1;}
1010
      else if (newState == ut_states::UT_PUSH_BACK)
1011
        {global.stateTracker[ut_states::UT_PUSH_BACK] += 1;}
1012
      else if (newState == ut_states::UT_CHECK_POSITIONING)
1013
        {global.stateTracker[ut_states::UT_CHECK_POSITIONING] += 1;}
1014
      else if (newState == ut_states::UT_CHECK_VOLTAGE)
1015
        {global.stateTracker[ut_states::UT_CHECK_VOLTAGE] += 1;}
1016
      else if (newState == ut_states::UT_CHARGING)
1017
        {global.stateTracker[ut_states::UT_CHARGING] += 1;}
1018
      else if (newState == ut_states::UT_RELEASE)
1019
        {global.stateTracker[ut_states::UT_RELEASE] += 1;}
1020
      else if (newState == ut_states::UT_RELEASE_TO_CORRECT)
1021
        {global.stateTracker[ut_states::UT_RELEASE_TO_CORRECT] += 1;}
1022
      else if (newState == ut_states::UT_CORRECT_POSITIONING)
1023
        {global.stateTracker[ut_states::UT_CORRECT_POSITIONING] += 1;}
1024
      else if (newState == ut_states::UT_TURN)
1025
        {global.stateTracker[ut_states::UT_TURN] += 1;}
1026
      else if (newState == ut_states::UT_INACTIVE)
1027
        {global.stateTracker[ut_states::UT_INACTIVE] += 1;}
1028
      else if (newState == ut_states::UT_CALIBRATION)
1029
        {global.stateTracker[ut_states::UT_CALIBRATION] += 1;}
1030
      else if (newState == ut_states::UT_CALIBRATION_CHECK)
1031
        {global.stateTracker[ut_states::UT_CALIBRATION_CHECK] += 1;}
1032
      else if (newState == ut_states::UT_DEVIATION_CORRECTION)
1033
        {global.stateTracker[ut_states::UT_DEVIATION_CORRECTION] += 1;}
1034
      else if (newState == ut_states::UT_DOCKING_ERROR)
1035
        {global.stateTracker[16+(-ut_states::UT_DOCKING_ERROR)] += 1;}
1036
      else if (newState == ut_states::UT_REVERSE_TIMEOUT_ERROR)
1037
        {global.stateTracker[16+(-ut_states::UT_REVERSE_TIMEOUT_ERROR)] += 1;}
1038
      else if (newState == ut_states::UT_CALIBRATION_ERROR)
1039
        {global.stateTracker[16+(-ut_states::UT_CALIBRATION_ERROR)] += 1;}
1040
      else if (newState == ut_states::UT_WHITE_DETECTION_ERROR)
1041
        {global.stateTracker[16+(-ut_states::UT_WHITE_DETECTION_ERROR)] += 1;}
1042
      else if (newState == ut_states::UT_PROXY_DETECTION_ERROR)
1043
        {global.stateTracker[16+(-ut_states::UT_PROXY_DETECTION_ERROR)] += 1;}
1044
      else if (newState == ut_states::UT_NO_CHARGING_POWER_ERROR)
1045
        {global.stateTracker[16+(-ut_states::UT_NO_CHARGING_POWER_ERROR)] += 1;}
1046
      else if (newState == ut_states::UT_UNKNOWN_STATE_ERROR)
1047
        {global.stateTracker[16+(-ut_states::UT_UNKNOWN_STATE_ERROR)] += 1;}
1048
    }
1049

    
1050
    // Keep track of last state and set the new state for next iteration
1051
    prevState = currentState;
1052
    currentState = newState;
1053

    
1054
    this->sleep(CAN::UPDATE_PERIOD);
1055
  }
1056

    
1057
  return RDY_OK;
1058
}