Revision 3c3c3bb9

View differences:

devices/DiWheelDrive/userthread.cpp
1 1
#include "userthread.hpp"
2
#include "amiro/Constants.h"
2 3
#include "amiro_map.hpp"
3 4
#include "global.hpp"
4 5
#include "linefollow.hpp"
......
273 274
  for (uint8_t led = 0; led < 8; ++led) {
274 275
    global.robot.setLightColor(led, Color(Color::BLACK));
275 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

  
276 284
  running = false;
277 285
  LineFollowStrategy lStrategy = LineFollowStrategy::EDGE_RIGHT;
278 286
  LineFollow lf(&global);
......
289 297

  
290 298
    if (accel_z < -900 /*-0.9g*/) {
291 299
      // Start line following when AMiRo is rotated
292
      if(currentState == states::INACTIVE){
293
        newState = states::FOLLOW_LINE;
300
      if(currentState == ut_states::UT_INACTIVE){
301
        newState = ut_states::UT_FOLLOW_LINE;
294 302
      }else{
295
        newState = states::IDLE;
303
        newState = ut_states::UT_IDLE;
296 304
      }
297 305
      lightAllLeds(Color::GREEN);
298 306
      this->sleep(1000);
......
304 312
      // running = true;
305 313
      switch(global.lfStrategy){
306 314
      case msg_content::MSG_START:
307
        newState = states::CALIBRATION_CHECK;
315
        newState = ut_states::UT_CALIBRATION_CHECK;
308 316
        break;
309 317
      case msg_content::MSG_STOP:
310
        newState = states::IDLE;
318
        newState = ut_states::UT_IDLE;
311 319
        break;
312 320
      case msg_content::MSG_EDGE_RIGHT:
313
        // newState = states::FOLLOW_LINE;
321
        // newState = ut_states::UT_FOLLOW_LINE;
314 322
        lStrategy = LineFollowStrategy::EDGE_RIGHT;
315 323
        break;
316 324
      case  msg_content::MSG_EDGE_LEFT:
317
        // newState = states::FOLLOW_LINE;
325
        // newState = ut_states::UT_FOLLOW_LINE;
318 326
        lStrategy = LineFollowStrategy::EDGE_LEFT;
319 327
        break;
320 328
      case msg_content::MSG_FUZZY:
321
        // newState = states::FOLLOW_LINE;
329
        // newState = ut_states::UT_FOLLOW_LINE;
322 330
        lStrategy = LineFollowStrategy::FUZZY;
323 331
        break;
324 332
      case msg_content::MSG_DOCK:
325
        newState = states::DETECT_STATION;
333
        newState = ut_states::UT_DETECT_STATION;
326 334
        break;
327 335
      case msg_content::MSG_UNDOCK:
328
        newState = states::RELEASE;
336
        newState = ut_states::UT_RELEASE;
329 337
        break;
330 338
      case msg_content::MSG_CHARGE:
331
        newState = states::CHARGING;
339
        newState = ut_states::UT_CHARGING;
332 340
        break;
333 341
      case msg_content::MSG_RESET_ODOMETRY:
334 342
        global.odometry.resetPosition();
......
336 344
      case msg_content::MSG_CALIBRATE_BLACK:
337 345
        proxCalib.calibrateBlack = true;
338 346
        // global.odometry.resetPosition();
339
        newState = states::CALIBRATION;
347
        newState = ut_states::UT_CALIBRATION;
340 348
        break;
341 349
      case msg_content::MSG_CALIBRATE_WHITE:
342 350
        proxCalib.calibrateBlack = false;
343
        newState = states::CALIBRATION;
351
        newState = ut_states::UT_CALIBRATION;
344 352
        break;
345 353
      case msg_content::MSG_TEST_MAP_STATE:
346
        newState = states::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

  
347 360
        break;
348 361

  
349 362
      default:
350
        newState = states::IDLE;
363
        newState = ut_states::UT_IDLE;
351 364
        break;
352 365
      }
353 366
    }
354 367
    // newState = currentState;
355 368

  
356 369
    // Get sensor data
357
    // uint16_t WL = global.vcnl4020[constants::DiWheelDrive::PROX_WHEEL_LEFT].getProximityScaledWoOffset();
358
    // uint16_t WR = global.vcnl4020[constants::DiWheelDrive::PROX_WHEEL_RIGHT].getProximityScaledWoOffset();
370
    uint16_t WL = global.vcnl4020[constants::DiWheelDrive::PROX_WHEEL_LEFT].getProximityScaledWoOffset();
371
    uint16_t WR = global.vcnl4020[constants::DiWheelDrive::PROX_WHEEL_RIGHT].getProximityScaledWoOffset();
359 372
    for(int i=0; i<8;i++){
360 373
      rProx[i] = global.robot.getProximityRingValue(i);
361 374
    }
......
365 378
    // int FL = global.vcnl4020[constants::DiWheelDrive::PROX_FRONT_LEFT].getProximityScaledWoOffset();
366 379
    // int FR = global.vcnl4020[constants::DiWheelDrive::PROX_FRONT_RIGHT].getProximityScaledWoOffset();
367 380
    switch(currentState){
368
      case states::INACTIVE:
381
      case ut_states::UT_INACTIVE:
369 382
        // Dummy state to deactivate every interaction
370 383
      break;
371 384
      // ---------------------------------------
372
      case states::CALIBRATION_CHECK:
385
      case ut_states::UT_CALIBRATION_CHECK:
373 386
        // global.robot.calibrate();
374 387
        if(global.linePID.BThresh >= global.linePID.WThresh){
375
          newState = states::CALIBRATION_ERROR;
388
          newState = ut_states::UT_CALIBRATION_ERROR;
376 389
        }else{
377
          newState = states::FOLLOW_LINE;
390
          newState = ut_states::UT_FOLLOW_LINE;
378 391
        }
379 392
      break;
380 393
      // ---------------------------------------
381
      case states::CALIBRATION:
394
      case ut_states::UT_CALIBRATION:
382 395
        /* Calibrate the global thresholds for black or white.
383 396
            This values will be used by the line follow object
384 397
        */
......
402 415
        }
403 416
          chprintf((BaseSequentialStream*)&global.sercanmux1, "Black: %d, White: %d!\n", global.linePID.BThresh, global.linePID.WThresh);
404 417

  
405
        newState = states::IDLE;
418
        newState = ut_states::UT_IDLE;
406 419
      break;
407 420
      // ---------------------------------------
408
      case states::IDLE:
421
      case ut_states::UT_IDLE:
409 422
        global.motorcontrol.setMotorEnable(true);
410 423
        setRpmSpeed(stop);
411 424
        if(/* checkPinVoltage() && */ checkPinEnabled()){
......
416 429
        utCount.whiteCount = 0;
417 430
        utCount.ringProxCount = 0;
418 431
        utCount.errorCount = 0;
419
        newState = states::INACTIVE;
432
        newState = ut_states::UT_INACTIVE;
420 433
      break;
421 434
      // ---------------------------------------
422
      case states::FOLLOW_LINE:
435
      case ut_states::UT_FOLLOW_LINE:
423 436
      // Set correct forward speed to every strategy
424 437
        if (global.forwardSpeed != global.rpmForward[0]){
425 438
          global.forwardSpeed = global.rpmForward[0];
......
434 447
          if(utCount.whiteCount >= WHITE_DETETION_TIMEOUT){
435 448
            setRpmSpeed(stop);
436 449
            utCount.whiteCount = 0;
437
            newState = states::WHITE_DETECTION_ERROR;
450
            newState = ut_states::UT_WHITE_DETECTION_ERROR;
438 451
          }
439 452
        }else{
440 453
          utCount.whiteCount = 0;
......
454 467
            // // BaseThread::sleep(8000);
455 468
            // checkForMotion();
456 469
            this->utCount.whiteCount = 0;
457
            newState = states::TURN;
470
            newState = ut_states::UT_TURN;
458 471
            // lf.promptStrategyChange(LineFollowStrategy::EDGE_LEFT);
459 472
          } else {
460
            newState = states::PROXY_DETECTION_ERROR;
473
            newState = ut_states::UT_PROXY_DETECTION_ERROR;
461 474
          }
462 475
        }
463 476

  
......
470 483

  
471 484
      break;
472 485
      // ---------------------------------------
473
    case states::TURN:{
486
    case ut_states::UT_TURN:{
474 487
        // Check the line strategy in order to continue driving on the right side
475 488
      int factor = SPEED_CONVERSION_FACTOR;
476 489
      int frontL = global.vcnl4020[constants::DiWheelDrive::PROX_FRONT_LEFT].getProximityScaledWoOffset();
......
492 505
      }else {
493 506
        if((utCount.whiteCount == 1) && (blackSensor <= global.linePID.BThresh)){
494 507
          utCount.whiteCount = 0;
495
          newState = states::FOLLOW_LINE;
508
          newState = ut_states::UT_FOLLOW_LINE;
496 509
          setRpmSpeed(stop);
497 510
        }
498 511
      }
499 512
      break;
500 513
    }
501 514
      // ---------------------------------------
502
    case states::DETECT_STATION:
515
    case ut_states::UT_DETECT_STATION:
503 516

  
504 517
        if (global.forwardSpeed != DETECTION_SPEED){
505 518
          global.forwardSpeed = DETECTION_SPEED;
......
521 534
          global.distcontrol.setTargetPosition(0, ROTATION_180, ROTATION_DURATION);
522 535
          // BaseThread::sleep(8000);
523 536
          checkForMotion();
524
          newState = states::CORRECT_POSITIONING;
537
          newState = ut_states::UT_CORRECT_POSITIONING;
525 538
        }
526 539
      break;
527 540
      // ---------------------------------------
528
    case states::CORRECT_POSITIONING:
541
    case ut_states::UT_CORRECT_POSITIONING:
529 542
        if (global.forwardSpeed != CHARGING_SPEED){
530 543
          global.forwardSpeed = CHARGING_SPEED;
531 544
        }
......
538 551
        utCount.stateTime++;
539 552
        if (utCount.stateTime >= DOCKING_CORRECTION_TIMEOUT){
540 553
          utCount.stateTime = 0;
541
          newState = states::REVERSE;
554
          newState = ut_states::UT_REVERSE;
542 555
          setRpmSpeed(stop);
543 556
          checkForMotion();
544 557
        }
545 558
      break;
546 559
      // ---------------------------------------
547
    case states::REVERSE:
560
    case ut_states::UT_REVERSE:
548 561
        if(lf.getStrategy() != LineFollowStrategy::REVERSE){
549 562
          lf.setStrategy(LineFollowStrategy::REVERSE);
550 563
        }
......
559 572
          // setRpmSpeed(stop);
560 573
          // checkForMotion();
561 574
          utCount.stateTime = 0;
562
          newState = states::PUSH_BACK;
575
          newState = ut_states::UT_PUSH_BACK;
563 576
        }else if ((devCor.currentDeviation <= -MAX_DEVIATION_FACTOR) && ((rProx[0] > DEVIATION_DIST_THRESH) || (rProx[7] > DEVIATION_DIST_THRESH))){
564 577
          // Case R
565 578
          utCount.stateTime = 0;
566 579
          setRpmSpeed(stop);
567 580
          devCor.RCase = true;
568 581
          lightAllLeds(Color::YELLOW);
569
          newState = states::DEVIATION_CORRECTION;
582
          newState = ut_states::UT_DEVIATION_CORRECTION;
570 583
        }else if ((devCor.currentDeviation >= MAX_DEVIATION_FACTOR) && ((rProx[0] > DEVIATION_DIST_THRESH) || (rProx[7] > DEVIATION_DIST_THRESH))){
571 584
          // Case L
572 585
          utCount.stateTime = 0;
573 586
          setRpmSpeed(stop);
574 587
          devCor.RCase = false;
575 588
          lightAllLeds(Color::WHITE);
576
          newState = states::DEVIATION_CORRECTION;
589
          newState = ut_states::UT_DEVIATION_CORRECTION;
577 590
        }else if (utCount.stateTime >= REVERSE_DOCKING_TIMEOUT){
578 591
          setRpmSpeed(stop);
579 592
          utCount.stateTime = 0;
580 593
          utCount.errorCount++;
581 594
          if (utCount.errorCount >= DOCKING_ERROR_THRESH){
582
            newState = states::DOCKING_ERROR;
595
            newState = ut_states::UT_DOCKING_ERROR;
583 596
          }else{
584
            newState = states::CORRECT_POSITIONING;
597
            newState = ut_states::UT_CORRECT_POSITIONING;
585 598
          }
586 599
        }
587 600

  
......
593 606
        // setRpmSpeed(rpmSpeed);
594 607
      break;
595 608
      // ---------------------------------------
596
    case states::DEVIATION_CORRECTION:
609
    case ut_states::UT_DEVIATION_CORRECTION:
597 610
        // if(lf.getStrategy() != LineFollowStrategy::REVERSE){
598 611
        //   lf.setStrategy(LineFollowStrategy::REVERSE);
599 612
        // }
......
619 632
          setRpmSpeed(rpmSpeed);
620 633
          if(((devCor.currentDeviation >= -5) && (devCor.currentDeviation <= 5))){
621 634
            utCount.stateTime = 0;
622
            newState = states::REVERSE;
635
            newState = ut_states::UT_REVERSE;
623 636
            setRpmSpeed(stop);
624 637
          }
625 638
        }else{
626 639
          utCount.stateTime = 0;
627
          newState = states::REVERSE;
640
          newState = ut_states::UT_REVERSE;
628 641
          setRpmSpeed(stop);
629 642
        }
630 643

  
......
633 646

  
634 647
        // if (utCount.stateTime > PUSH_BACK_TIMEOUT){
635 648
        //   utCount.stateTime = 0;
636
        //   newState = states::CHECK_POSITIONING;
649
        //   newState = ut_states::UT_CHECK_POSITIONING;
637 650
        // }
638 651
      break;
639 652
      // ---------------------------------------
640
    case states::PUSH_BACK:
653
    case ut_states::UT_PUSH_BACK:
641 654
        if(lf.getStrategy() != LineFollowStrategy::REVERSE){
642 655
          lf.setStrategy(LineFollowStrategy::REVERSE);
643 656
        }
......
647 660
        utCount.stateTime++;
648 661
        if (utCount.stateTime > PUSH_BACK_TIMEOUT){
649 662
          utCount.stateTime = 0;
650
          newState = states::CHECK_POSITIONING;
663
          newState = ut_states::UT_CHECK_POSITIONING;
651 664
        }
652 665
      break;
653 666
      // ---------------------------------------
654
    case states::CHECK_POSITIONING:
667
    case ut_states::UT_CHECK_POSITIONING:
655 668
        setRpmSpeed(stop);
656 669
        checkForMotion();
657 670
        if(checkDockingSuccess()){
658
          newState = states::CHECK_VOLTAGE;
671
          newState = ut_states::UT_CHECK_VOLTAGE;
659 672
        }else{
660 673
          utCount.errorCount++;
661
          newState = states::CORRECT_POSITIONING;
674
          newState = ut_states::UT_CORRECT_POSITIONING;
662 675
          if (utCount.errorCount >= DOCKING_ERROR_THRESH){
663
              newState = states::DOCKING_ERROR;
676
              newState = ut_states::UT_DOCKING_ERROR;
664 677
            }
665 678
        }
666 679
      break;
667 680
      // ---------------------------------------
668
    case states::CHECK_VOLTAGE:
681
    case ut_states::UT_CHECK_VOLTAGE:
669 682
        if(!checkPinEnabled()){
670 683
          global.robot.requestCharging(1);
671 684
        } else {
672 685
          if(checkPinVoltage()){
673 686
            // Pins are under voltage -> correctly docked
674 687

  
675
            newState = states::CHARGING;
688
            newState = ut_states::UT_CHARGING;
676 689
          }else{
677 690
            utCount.errorCount++;
678 691
            // No voltage on pins -> falsely docked
......
681 694
            global.robot.requestCharging(0);
682 695
            // TODO: Soft release when docking falsely
683 696
            if((rProx[0] >= PROX_MAX_VAL) && (rProx[7] >= PROX_MAX_VAL)){
684
              newState = states::RELEASE_TO_CORRECT;
697
              newState = ut_states::UT_RELEASE_TO_CORRECT;
685 698
            } else {
686
              newState = states::RELEASE_TO_CORRECT; //states::CORRECT_POSITIONING;
699
              newState = ut_states::UT_RELEASE_TO_CORRECT; //ut_states::UT_CORRECT_POSITIONING;
687 700
            }
688 701

  
689 702
            if (utCount.errorCount > DOCKING_ERROR_THRESH){
690
              newState = states::DOCKING_ERROR;
703
              newState = ut_states::UT_DOCKING_ERROR;
691 704
            }
692 705
          }
693 706
        }
694 707
      break;
695 708
      // ---------------------------------------
696
    case states::RELEASE_TO_CORRECT:
709
    case ut_states::UT_RELEASE_TO_CORRECT:
697 710

  
698 711
        global.distcontrol.setTargetPosition(0, ROTATION_20, ROTATION_DURATION);
699 712
        checkForMotion();
......
706 719

  
707 720
        global.distcontrol.setTargetPosition(1500, 0, ROTATION_DURATION);
708 721
        checkForMotion();
709
        newState = states::CORRECT_POSITIONING;
722
        newState = ut_states::UT_CORRECT_POSITIONING;
710 723
      break;
711 724
      // ---------------------------------------
712
    case states::CHARGING:
725
    case ut_states::UT_CHARGING:
713 726
        global.motorcontrol.setMotorEnable(false);
714 727
        utCount.errorCount = 0;
715 728
        // Formulate Request to enable charging
......
721 734
        }
722 735
      break;
723 736
      // ---------------------------------------
724
    case states::RELEASE:
737
    case ut_states::UT_RELEASE:
725 738

  
726 739
      if (global.forwardSpeed != DETECTION_SPEED){
727 740
          global.rpmForward[0] = DETECTION_SPEED;
......
744 757
          // global.distcontrol.setTargetPosition(5000, 0, ROTATION_DURATION);
745 758
          // checkForMotion();
746 759
          lStrategy = LineFollowStrategy::EDGE_RIGHT;
747
          newState = states::FOLLOW_LINE;
760
          newState = ut_states::UT_FOLLOW_LINE;
748 761
            // whiteBuf = -100;
749 762
          // lf.followLine(rpmSpeed);
750 763
          // setRpmSpeed(rpmSpeed);
......
752 765
        // lightAllLeds(Color::BLACK);
753 766
      break;
754 767
      // ---------------------------------------
755
    case states::DOCKING_ERROR:
756
      newState = states::RELEASE;
768
    case ut_states::UT_DOCKING_ERROR:
769
      newState = ut_states::UT_RELEASE;
757 770
      break;
758 771
      // ---------------------------------------
759
    case states::REVERSE_TIMEOUT_ERROR:
760
      newState = states::IDLE;
772
    case ut_states::UT_REVERSE_TIMEOUT_ERROR:
773
      newState = ut_states::UT_IDLE;
761 774
      break;
762 775
      // ---------------------------------------
763
    case states::CALIBRATION_ERROR:
764
      newState = states::IDLE;
776
    case ut_states::UT_CALIBRATION_ERROR:
777
      newState = ut_states::UT_IDLE;
765 778
      break;
766 779
      // ---------------------------------------
767
    case states::WHITE_DETECTION_ERROR:
768
      newState = states::IDLE;
780
    case ut_states::UT_WHITE_DETECTION_ERROR:
781
      newState = ut_states::UT_IDLE;
769 782
      break;
770 783
      // ---------------------------------------
771
    case states::PROXY_DETECTION_ERROR:
772
      newState = states::IDLE;
784
    case ut_states::UT_PROXY_DETECTION_ERROR:
785
      newState = ut_states::UT_IDLE;
773 786
      break;
774 787
      // ---------------------------------------
775
    case states::NO_CHARGING_POWER_ERROR:
776
      newState = states::IDLE;
788
    case ut_states::UT_NO_CHARGING_POWER_ERROR:
789
      newState = ut_states::UT_IDLE;
777 790
      break;
778 791
      // ---------------------------------------
779
    case states::UNKNOWN_STATE_ERROR:
780
        newState = states::IDLE;
792
    case ut_states::UT_UNKNOWN_STATE_ERROR:
793
        newState = ut_states::UT_IDLE;
781 794
      break;
782 795
      // ---------------------------------------
783
    case states::TEST_MAP_STATE:{
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:{
784 810
      // Test suit for amiro map
785 811

  
786 812

  
......
942 968
               "Total: %d, Passed: %d, Failed: %d\n", global.tcase + 1, passed,
943 969
               failed);
944 970

  
945
      newState = states::IDLE;
971
      newState = ut_states::UT_IDLE;
946 972
      break;
947 973
    }
948 974
      // --------------------------------------------------
949 975
    default:
950
      newState = states::UNKNOWN_STATE_ERROR;
976
      newState = ut_states::UT_UNKNOWN_STATE_ERROR;
951 977
      break;
952 978
    }
953 979

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

  
956 986
    if (currentState != newState){
957 987

  
958 988
      global.stateTransitionCounter++;
......
969 999

  
970 1000
      // Increase state count for specific state
971 1001
      // TODO: Improve with dictionary or other than switch case
972
      if (newState == states::IDLE)
973
        {global.stateTracker[states::IDLE] += 1;}
974
      else if (newState == states::FOLLOW_LINE)
975
        {global.stateTracker[states::FOLLOW_LINE] += 1;}
976
      else if (newState == states::DETECT_STATION)
977
        {global.stateTracker[states::DETECT_STATION] += 1;}
978
      else if (newState == states::REVERSE)
979
        {global.stateTracker[states::REVERSE] += 1;}
980
      else if (newState == states::PUSH_BACK)
981
        {global.stateTracker[states::PUSH_BACK] += 1;}
982
      else if (newState == states::CHECK_POSITIONING)
983
        {global.stateTracker[states::CHECK_POSITIONING] += 1;}
984
      else if (newState == states::CHECK_VOLTAGE)
985
        {global.stateTracker[states::CHECK_VOLTAGE] += 1;}
986
      else if (newState == states::CHARGING)
987
        {global.stateTracker[states::CHARGING] += 1;}
988
      else if (newState == states::RELEASE)
989
        {global.stateTracker[states::RELEASE] += 1;}
990
      else if (newState == states::RELEASE_TO_CORRECT)
991
        {global.stateTracker[states::RELEASE_TO_CORRECT] += 1;}
992
      else if (newState == states::CORRECT_POSITIONING)
993
        {global.stateTracker[states::CORRECT_POSITIONING] += 1;}
994
      else if (newState == states::TURN)
995
        {global.stateTracker[states::TURN] += 1;}
996
      else if (newState == states::INACTIVE)
997
        {global.stateTracker[states::INACTIVE] += 1;}
998
      else if (newState == states::CALIBRATION)
999
        {global.stateTracker[states::CALIBRATION] += 1;}
1000
      else if (newState == states::CALIBRATION_CHECK)
1001
        {global.stateTracker[states::CALIBRATION_CHECK] += 1;}
1002
      else if (newState == states::DEVIATION_CORRECTION)
1003
        {global.stateTracker[states::DEVIATION_CORRECTION] += 1;}
1004
      else if (newState == states::DOCKING_ERROR)
1005
        {global.stateTracker[16+(-states::DOCKING_ERROR)] += 1;}
1006
      else if (newState == states::REVERSE_TIMEOUT_ERROR)
1007
        {global.stateTracker[16+(-states::REVERSE_TIMEOUT_ERROR)] += 1;}
1008
      else if (newState == states::CALIBRATION_ERROR)
1009
        {global.stateTracker[16+(-states::CALIBRATION_ERROR)] += 1;}
1010
      else if (newState == states::WHITE_DETECTION_ERROR)
1011
        {global.stateTracker[16+(-states::WHITE_DETECTION_ERROR)] += 1;}
1012
      else if (newState == states::PROXY_DETECTION_ERROR)
1013
        {global.stateTracker[16+(-states::PROXY_DETECTION_ERROR)] += 1;}
1014
      else if (newState == states::NO_CHARGING_POWER_ERROR)
1015
        {global.stateTracker[16+(-states::NO_CHARGING_POWER_ERROR)] += 1;}
1016
      else if (newState == states::UNKNOWN_STATE_ERROR)
1017
        {global.stateTracker[16+(-states::UNKNOWN_STATE_ERROR)] += 1;}
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;}
1018 1048
    }
1019 1049

  
1020 1050
    // Keep track of last state and set the new state for next iteration
devices/DiWheelDrive/userthread.hpp
6 6
#include <amiro/Color.h>
7 7
// #include "global.hpp"
8 8
// #include "linefollow.hpp"
9
#include <cmath>
10 9

  
11 10

  
12 11

  
......
59 58
#define DEVIATION_CORRECTION_VALUE (DEVIATION_CORRECTION_SPEED / 2)
60 59
#define DEVIATION_DIST_THRESH 25000
61 60

  
61

  
62

  
63
// Map Tracking Parameters
64
// enable amiro map to continuously build internal map representation
65
#define AMIRO_MAP_AUTO_TRACKING true
66

  
67

  
68

  
62 69
namespace amiro {
63 70

  
64 71

  
......
69 76
  public:
70 77

  
71 78
    // States of user thread state machine
72
    enum states : int8_t{
73
      IDLE                    = 0,
74
      FOLLOW_LINE             = 1,
75
      DETECT_STATION          = 2,
76
      REVERSE                 = 3,
77
      PUSH_BACK               = 4,
78
      CHECK_POSITIONING       = 5,
79
      CHECK_VOLTAGE           = 6,
80
      CHARGING                = 7,
81
      RELEASE                 = 8,
82
      RELEASE_TO_CORRECT      = 9,
83
      CORRECT_POSITIONING     = 10,
84
      TURN                    = 12,
85
      INACTIVE                = 13,
86
      CALIBRATION             = 14,
87
      CALIBRATION_CHECK       = 15,
88
      DEVIATION_CORRECTION    = 16,
89
      TEST_MAP_STATE          = 17,
90
      DOCKING_ERROR           = -1,
91
      REVERSE_TIMEOUT_ERROR   = -2,
92
      CALIBRATION_ERROR       = -3,
93
      WHITE_DETECTION_ERROR   = -4,
94
      PROXY_DETECTION_ERROR   = -5,
95
      NO_CHARGING_POWER_ERROR = -6,
96
      UNKNOWN_STATE_ERROR     = -7
97
    };
98

  
99
  struct ut_counter{
79
    // enum ut_states : int8_t {
80
    //   UT_IDLE = 0,
81
    //   UT_FOLLOW_LINE = 1,
82
    //   UT_DETECT_STATION = 2,
83
    //   UT_REVERSE = 3,
84
    //   UT_PUSH_BACK = 4,
85
    //   UT_CHECK_POSITIONING = 5,
86
    //   UT_CHECK_VOLTAGE = 6,
87
    //   UT_CHARGING = 7,
88
    //   UT_RELEASE = 8,
89
    //   UT_RELEASE_TO_CORRECT = 9,
90
    //   UT_CORRECT_POSITIONING = 10,
91
    //   UT_TURN = 12,
92
    //   UT_INACTIVE = 13,
93
    //   UT_CALIBRATION = 14,
94
    //   UT_CALIBRATION_CHECK = 15,
95
    //   UT_DEVIATION_CORRECTION = 16,
96
    //   UT_TEST_MAP_STATE = 17,
97
    //   UT_TEST_MAP_AUTO_STATE = 18,
98
    //   UT_DOCKING_ERROR = -1,
99
    //   UT_REVERSE_TIMEOUT_ERROR = -2,
100
    //   UT_CALIBRATION_ERROR = -3,
101
    //   UT_WHITE_DETECTION_ERROR = -4,
102
    //   UT_PROXY_DETECTION_ERROR = -5,
103
    //   UT_NO_CHARGING_POWER_ERROR = -6,
104
    //   UT_UNKNOWN_STATE_ERROR = -7
105
    // };
106

  
107
    struct ut_counter {
100 108
      int whiteCount = 0;
101 109
      int ringProxCount = 0;
102 110
      // int correctionCount = 0;
......
195 203
   */
196 204
  int checkDockingSuccess();
197 205

  
198
  // State Variables
199
  states prevState = states::IDLE;
200
  states currentState = states::IDLE;
201
  states newState = states::IDLE;
206

  
202 207

  
203 208
  bool continue_on_obstacle = true;
204 209
  uint16_t rProx[8]; // buffer for ring proxy values
include/amiro/Constants.h
50 50
  MSG_TEST_MAP_STATE = 11
51 51
};
52 52

  
53
enum ut_states : int8_t {
54
  UT_IDLE = 0,
55
  UT_FOLLOW_LINE = 1,
56
  UT_DETECT_STATION = 2,
57
  UT_REVERSE = 3,
58
  UT_PUSH_BACK = 4,
59
  UT_CHECK_POSITIONING = 5,
60
  UT_CHECK_VOLTAGE = 6,
61
  UT_CHARGING = 7,
62
  UT_RELEASE = 8,
63
  UT_RELEASE_TO_CORRECT = 9,
64
  UT_CORRECT_POSITIONING = 10,
65
  UT_TURN = 12,
66
  UT_INACTIVE = 13,
67
  UT_CALIBRATION = 14,
68
  UT_CALIBRATION_CHECK = 15,
69
  UT_DEVIATION_CORRECTION = 16,
70
  UT_TEST_MAP_STATE = 17,
71
  UT_TEST_MAP_AUTO_STATE = 18,
72
  UT_DOCKING_ERROR = -1,
73
  UT_REVERSE_TIMEOUT_ERROR = -2,
74
  UT_CALIBRATION_ERROR = -3,
75
  UT_WHITE_DETECTION_ERROR = -4,
76
  UT_PROXY_DETECTION_ERROR = -5,
77
  UT_NO_CHARGING_POWER_ERROR = -6,
78
  UT_UNKNOWN_STATE_ERROR = -7
79
};
80

  
53 81
namespace CAN {
54 82

  
55 83
  const uint32_t UPDATE_PERIOD        = US2ST(10000);  // 100 Hz

Also available in: Unified diff