Revision b8b3a9c9

View differences:

devices/DiWheelDrive/amiro_map.cpp
1 1
#include "amiro_map.hpp"
2 2

  
3 3

  
4
void AmiroMap::initialize(int8_t (&config)[MAX_NODES][NODE_ATTRIBUTES]){
4
void AmiroMap::initialize(uint8_t (&config)[MAX_NODES][NODE_ATTRIBUTES]){
5 5

  
6
  uint8_t nodeCount = 0;
7
  bool end = false;
6
  // Clear old values in case the map is initialized again
7
  this->current = 0;
8
  this->next = 0;
9
  this->valid = false;
8 10

  
11
  // convert proto map to internal representation
9 12
  for (int i=0; i<MAX_NODES; i++){
10
    if(config[i][NODE_ATTRIBUTES - 1] == 255){
11
      end = true;
13
    if(config[i][2] == 0xff && i != 0){
14
      this->valid = true;
12 15
      break;
16
    } else if (config[i][2] == 0xff && i == 0) {
17
      this->valid = false;
18
      return;
13 19
    }
14
    nodeCount++;
15
  }
16
  if (!end || nodeCount == 0 ){
17

  
18
    // Failed to determine the amount of nodes
19
    return;
20
  }
21 20

  
22
  this->nodeList = (struct node*) malloc(nodeCount * sizeof(struct node)); 
21
    //look for start node (default is Node 0)
22
    if (config[i][2] == 1 ) {
23
      this->current= i;
24
    }
23 25

  
24
  
26
    this->nodeList[i].id = i;
27
    this->nodeList[i].left = config[i][0];
28
    this->nodeList[i].right = config[i][1];
29
    this->nodeList[i].flag = config[i][2];
30
    this->nodeCount++;
31
  }
25 32

  
26
}
33
  // TODO make validity check
27 34

  
28 35

  
29
void AmiroMap::update(){
30
  
31 36
}
32 37

  
38
void AmiroMap::update(bool left, bool right, LineFollowStrategy strategy) {
33 39

  
34
void AmiroMap::uninitialize(){
35
  
36 40
}
37

  
devices/DiWheelDrive/amiro_map.hpp
1 1
#ifndef AMIRO_MAP
2 2
#define AMIRO_MAP
3 3

  
4
#include <ch.hpp>
5 4
#include "global.hpp"
5
#include "linefollow.hpp"
6 6
#include <amiroosconf.h>
7
#include <ch.hpp>
8
#include <cstdint>
7 9

  
10
#define MAX_NODES 10
11
#define NODE_ATTRIBUTES 3
8 12

  
9
#define MAX_NODES 20
10
#define NODE_ATTRIBUTES 4
13
namespace amiro {
11 14

  
15
struct node {
16
  uint8_t id;
17
  uint8_t flag;
18
  uint8_t left;
19
  uint8_t right;
20
  types::position pL; // Left
21
  types::position pR; // Right
22
  types::position pB; // Back
23
};
12 24

  
13
namespace amiro {
25
class AmiroMap {
26
public:
27
  uint8_t get_next()  { return next; }
14 28

  
29
  void set_next( uint8_t next) { this->next = next; }
15 30

  
16
  struct node{
17
    uint8_t id;
18
    uint8_t flag;
19
    types::position pL; //Left
20
    types::position pR; //Right
21
    types::position pB; //Back
22
  };
23

  
24
  class AmiroMap{
25
  public:
26
    AmiroMap(Global *global):global{global}{}
27
    void initialize(int8_t (&config)[MAX_NODES][NODE_ATTRIBUTES]);
28
    void update();
29
  private:
30
    Global *global;
31
    bool valid = false;
32
    node *nodeList = NULL;
33
    node *current = NULL;
34
    node *next = NULL;
35
    void uninitialize();
36
  };
37
};
31
  uint8_t get_current()  { return current; }
32

  
33
  void set_current( uint8_t current) { this->current = current; }
34

  
35
  node* get_nodeList()  { return nodeList; }
36

  
37
  uint8_t get_nodeCount()  { return nodeCount; }
38

  
39
  void set_nodeCount( uint8_t nodeCount) { this->nodeCount = nodeCount; }
38 40

  
41
  bool get_valid()  { return valid; }
42

  
43
  void set_valid( bool valid) { this->valid = valid; }
44

  
45
  AmiroMap(Global *global) : global{global} {}
46

  
47
  /**
48
   * Initialize a new map from configuration.
49
   * @param config map configuration array
50
   *
51
   */
52
  void initialize(uint8_t (&config)[MAX_NODES][NODE_ATTRIBUTES]);
53

  
54

  
55
  /**
56
     Update the internal map state according to the detected fixpoint
57
     This function should be called for a generic update, each can cycle and in
58
     case a fixpoint on one side is detected.
59
     Internal state maschine will go into an error state in case both sensors are black.
60

  
61
     @param left fixpoint on left side detected
62
     @param right fixpoint on right side detected
63
     @param strategy current line follow strategy
64

  
65

  
66
   */
67
  void update(bool left, bool right, LineFollowStrategy strategy);
68

  
69
private:
70
  Global *global;
71
  bool valid = false;
72
  uint8_t nodeCount = 0;
73
  node nodeList[MAX_NODES];
74
  uint8_t current = 0;
75
  uint8_t next = 0;
76
};
77
}; // namespace amiro
39 78

  
40 79
#endif /* AMIRO_MAP */
devices/DiWheelDrive/main.cpp
1
#include <cstdint>
1 2
#define BL_CALLBACK_TABLE_ADDR  (0x08000000 + 0x01C0)
2 3
#define BL_MAGIC_NUMBER         ((uint32_t)0xFF669900u)
3 4

  
......
17 18
#include <chprintf.h>
18 19
#include <shell.h>
19 20

  
20
#include "linefollow.hpp" 
21

  
21
#include "linefollow.hpp"
22
#include "amiro_map.hpp"
22 23
using namespace chibios_rt;
23 24

  
24 25
Global global;
......
697 698
 * Calibrate the thresholds for left and right sensor to get the maximum threshold and to
698 699
 * be able to detect the correction direction.
699 700
 * In this case it is expected that the FL-Sensor sould be in the white part of the edge and the FR-Sensor in the black one.
700
 * 
701
 *
701 702
 * Note: invert the threshs to drive on the other edge.
702
 * 
703
 *
703 704
 * */
704 705
void shellRequestCalibrateLineSensores(BaseSequentialStream *chp, int argc, char *argv[]) {
705 706
    // int vcnl4020AmbientLight[4];
......
710 711
    int maxDelta = 0;
711 712
    int sensorL = 0;
712 713
    int sensorR = 0;
713
 
714

  
714 715
  if (argc == 1){
715 716
    chprintf(chp, "Test %i rounds \n", atoi(argv[0]));
716 717
    rounds = atoi(argv[0]);
717
    
718

  
718 719
  }else{
719 720
    chprintf(chp, "Usage: calbrate_line_sensors [1,n]\nThis will calibrate the thresholds for the left and right sensor\naccording to the maximum delta value recorded.\n");
720 721
    return;
......
748 749
    //   delta *= -1;
749 750
    // }
750 751

  
751
    chprintf(chp,"FL: 0x%x, FR: 0x%x, Delta: %d, ProxyL: %x, ProxyR: %x, MaxDelta: %d\n", 
752
    chprintf(chp,"FL: 0x%x, FR: 0x%x, Delta: %d, ProxyL: %x, ProxyR: %x, MaxDelta: %d\n",
752 753
                  vcnl4020Proximity[constants::DiWheelDrive::PROX_FRONT_LEFT],
753 754
                  vcnl4020Proximity[constants::DiWheelDrive::PROX_FRONT_RIGHT],
754 755
                  delta,
......
758 759
    // sleep(CAN::UPDATE_PERIOD);
759 760
    BaseThread::sleep(CAN::UPDATE_PERIOD);
760 761
  }
761
  
762

  
762 763

  
763 764
  global.linePID.threshProxyL = sensorL / rounds;
764 765
  global.linePID.threshProxyR = sensorR / rounds;
......
776 777
  if (argc == 1){
777 778
    chprintf(chp, "Test %i rounds \n", atoi(argv[0]));
778 779
    rounds = atoi(argv[0]);
779
    
780

  
780 781
  } else {
781 782
    chprintf(chp, "Usage: dev_proxi_sensor_data <rounds> \n");
782 783
  }
......
787 788
        // vcnl4020AmbientLight[i] = global.vcnl4020[i].getAmbientLight();
788 789
        vcnl4020Proximity[i] = global.vcnl4020[i].getProximityScaledWoOffset();
789 790
    }
790
    
791

  
791 792
    int32_t delta = (vcnl4020Proximity[constants::DiWheelDrive::PROX_FRONT_LEFT]
792 793
                  - vcnl4020Proximity[constants::DiWheelDrive::PROX_FRONT_RIGHT]);
793 794

  
794
    chprintf(chp,"WL:%d,FL:%d,FR:%d,WR:%d,Delta:%d\n", 
795
    chprintf(chp,"WL:%d,FL:%d,FR:%d,WR:%d,Delta:%d\n",
795 796
                  vcnl4020Proximity[constants::DiWheelDrive::PROX_WHEEL_LEFT],
796 797
                  vcnl4020Proximity[constants::DiWheelDrive::PROX_FRONT_LEFT],
797 798
                  vcnl4020Proximity[constants::DiWheelDrive::PROX_FRONT_RIGHT],
......
817 818
int32_t proxbuf[window]= { 0 };
818 819
int32_t meanDeviation(uint16_t a, uint16_t b){
819 820
  int32_t diff = a-b;
820
  int32_t res = 0; 
821
  int32_t res = 0;
821 822
  proxbuf[counter] = (diff*100)/((a+b)/2);
822 823
  for (int i = 0; i< window; i++){
823 824
    res += proxbuf[i];
......
854 855
    // int32_t deviation = meanDeviation((prox[3]+old3) / 2  , (prox[4]+old4) / 2);
855 856
    // uint16_t notouch = 100;
856 857
    // uint16_t toucht = 20031;
857
    // sign = 
858
    // sign =
858 859
    // i = 0;
859 860
    chprintf(chp, "0:%i 1:%i 2:%i 3:%i 4:%i 5:%i 6:%i 7:%i Deviation:%i \n", prox[0], prox[1], prox[2], prox[3], prox[4], prox[5], prox[6], prox[7], deviation);
860 861
    BaseThread::sleep(CAN::UPDATE_PERIOD);
......
884 885
    // }
885 886
    // uint16_t notouch = 100;
886 887
    // uint16_t toucht = 20031;
887
    // sign = 
888
    // sign =
888 889
    // i = 0;
889 890

  
890 891
    if (j < 10){
......
964 965
  for (int i=0; i<24;i++){
965 966
    global.stateTracker[i] = 0;
966 967
  }
968
}
967 969

  
970
inline void setAttributes(uint8_t (&map)[MAX_NODES][NODE_ATTRIBUTES], uint8_t id,
971
                   uint8_t l, uint8_t r, uint8_t att) {
972
  map[id][0] = l;
973
  map[id][1] = r;
974
  map[id][2] = att;
975
}
976

  
977
#define TEST_CASES 10
978
void shellRequestTestSuite(BaseSequentialStream *chp, int argc, char *argv[]){
979
  uint8_t testmap[MAX_NODES][NODE_ATTRIBUTES] = {0};
980
  bool testres[TEST_CASES];
981
  setAttributes(testmap, 0, 1, 2, 1);
982
  setAttributes(testmap, 1, 2, 2, 0);
983
  setAttributes(testmap, 2, 1, 0, 0);
984
  setAttributes(testmap, 3, 0, 0, 0xff);
985
  AmiroMap map = AmiroMap(&global);
986

  
987
  // --------------------------------------------------
988

  
989
  int tcase = 0;
990
  map.initialize(testmap);
991
  testres[tcase] = map.get_valid();
992

  
993
  tcase++; // 1
994
  setAttributes(testmap, 0, 1, 2, 0xff);
995
  map.initialize(testmap);
996
  testres[tcase] = !map.get_valid();
997

  
998
  tcase++; // 2
999
  setAttributes(testmap, 0, 1, 2, 0);
1000
  setAttributes(testmap, 2, 1, 0, 1);
1001
  map.initialize(testmap);
1002
  testres[tcase] = map.get_current() == 2;
1003

  
1004
  // --------------------------------------------------
1005

  
1006
  int failed = 0;
1007
  int passed = 0;
1008
  for (int i=0; i<=tcase; i++) {
1009
    if (testres[i]){
1010
      passed++;
1011
    }else{
1012
      failed++;
1013
      chprintf(chp, "Test %d Failed\n", i);
1014
    }
1015
  }
1016
  chprintf(chp, "Total: %d, Passed: %d, Failed: %d\n", tcase + 1, passed, failed);
1017
}
1018

  
1019
void shellRequestMapTest(BaseSequentialStream *chp, int argc, char *argv[]) {
1020

  
1021
  // TODO:
1022
  // For now see everything fixed. Create array with pointer to all node structs. Determine
1023

  
1024
  // chprintf(chp, "  +-------------------+\n");
1025
  // chprintf(chp, "  |                   |\n");
1026
  // chprintf(chp, "  |                   |\n");
1027
  // chprintf(chp, "+-v--+                |\n");
1028
  // chprintf(chp, "|  0 |              +-++\n");
1029
  // chprintf(chp, "+-+--+     +--------+2 | <-------+\n");
1030
  // chprintf(chp, "  |        |        +--+         |\n");
1031
  // chprintf(chp, "  |        |        ^            |\n");
1032
  // chprintf(chp, "  |        |        |            |\n");
1033
  // chprintf(chp, "  |        v        |            |\n");
1034
  // chprintf(chp, "  |      +-+-+------+            |\n");
1035
  // chprintf(chp, "  +------> 1 |                   |\n");
1036
  // chprintf(chp, "         +---+-------------------+\n");
1037

  
1038
  uint8_t testmap[MAX_NODES][NODE_ATTRIBUTES] = {0};
1039
  setAttributes(testmap, 0, 1, 2, 1);
1040
  setAttributes(testmap, 1, 2, 2, 0);
1041
  setAttributes(testmap, 2, 1, 0, 0);
1042
  setAttributes(testmap, 3, 0, 0, 0xff);
1043
  AmiroMap map = AmiroMap(&global);
1044
  map.initialize(testmap);
968 1045
}
969 1046

  
970 1047
static const ShellCommand commands[] = {
971
  {"shutdown", shellRequestShutdown},
972
  {"wakeup", shellRequestWakeup},
973
  {"check", shellRequestCheck},
974
  {"reset_memory", shellRequestResetMemory},
975
  {"get_board_id", shellRequestGetBoardId},
976
  {"set_board_id", shellRequestSetBoardId},
977
  {"get_memory_data", shellRequestGetMemoryData},
978
  {"get_vcnl", shellRequestGetVcnl},
979
  {"calib_vcnl_offset", shellRequestCalib},
980
  {"set_vcnl_offset", shellRequestSetVcnlOffset},
981
  {"reset_vcnl_offset", shellRequestResetVcnlOffset},
982
  {"get_vcnl_offset", shellRequestGetVcnlOffset},
983
  {"reset_Ed_Eb", shellRequestResetCalibrationConstants},
984
  {"get_Ed_Eb", shellRequestGetCalibrationConstants},
985
  {"set_Ed_Eb", shellRequestSetCalibrationConstants},
986
  {"get_robot_id", shellRequestGetRobotId},
987
  {"get_system_load", shellRequestGetSystemLoad},
988
  {"set_lights", shellRequestSetLights},
989
  {"shell_board", shellSwitchBoardCmd},
990
  {"get_bootloader_info", shellRequestGetBootloaderInfo},
991
  {"motor_drive", shellRequestMotorDrive},
992
  {"motor_stop", shellRequestMotorStop},
993
  {"motor_calibrate", shellRequestMotorCalibrate},
994
  {"motor_getGains", shellRequestMotorGetGains},
995
  {"motor_resetGains", shellRequestMotorResetGains},
996
  {"calibrate_line_sensors", shellRequestCalibrateLineSensores},
997
  {"printProxyBottom", sellRequestgetBottomSensorData},
998
  {"printProxyRing", shellRequestProxyRingValues},
999
  {"printMagnetometer", shellRequestMagnetoMeter},
1000
  {"printMagnetometerRes", shellRequestMagnetoMeterPrint},
1001
  {"printLocation", shellRequestPrintCoordinate},
1002
  {"checkPowerPins", shellRequestCheckPower},
1003
  {"infos", shellRequestErrorInfo},
1004
  {NULL, NULL}
1005
};
1048
    {"shutdown", shellRequestShutdown},
1049
    {"wakeup", shellRequestWakeup},
1050
    {"check", shellRequestCheck},
1051
    {"reset_memory", shellRequestResetMemory},
1052
    {"get_board_id", shellRequestGetBoardId},
1053
    {"set_board_id", shellRequestSetBoardId},
1054
    {"get_memory_data", shellRequestGetMemoryData},
1055
    {"get_vcnl", shellRequestGetVcnl},
1056
    {"calib_vcnl_offset", shellRequestCalib},
1057
    {"set_vcnl_offset", shellRequestSetVcnlOffset},
1058
    {"reset_vcnl_offset", shellRequestResetVcnlOffset},
1059
    {"get_vcnl_offset", shellRequestGetVcnlOffset},
1060
    {"reset_Ed_Eb", shellRequestResetCalibrationConstants},
1061
    {"get_Ed_Eb", shellRequestGetCalibrationConstants},
1062
    {"set_Ed_Eb", shellRequestSetCalibrationConstants},
1063
    {"get_robot_id", shellRequestGetRobotId},
1064
    {"get_system_load", shellRequestGetSystemLoad},
1065
    {"set_lights", shellRequestSetLights},
1066
    {"shell_board", shellSwitchBoardCmd},
1067
    {"get_bootloader_info", shellRequestGetBootloaderInfo},
1068
    {"motor_drive", shellRequestMotorDrive},
1069
    {"motor_stop", shellRequestMotorStop},
1070
    {"motor_calibrate", shellRequestMotorCalibrate},
1071
    {"motor_getGains", shellRequestMotorGetGains},
1072
    {"motor_resetGains", shellRequestMotorResetGains},
1073
    {"calibrate_line_sensors", shellRequestCalibrateLineSensores},
1074
    {"printProxyBottom", sellRequestgetBottomSensorData},
1075
    {"printProxyRing", shellRequestProxyRingValues},
1076
    {"printMagnetometer", shellRequestMagnetoMeter},
1077
    {"printMagnetometerRes", shellRequestMagnetoMeterPrint},
1078
    {"printLocation", shellRequestPrintCoordinate},
1079
    {"checkPowerPins", shellRequestCheckPower},
1080
    {"stateInfos", shellRequestErrorInfo},
1081
    {"testMap", shellRequestMapTest},
1082
    {"test", shellRequestTestSuite },
1083
    {NULL, NULL}};
1006 1084

  
1007 1085
static const ShellConfig shell_cfg1 = {
1008 1086
  (BaseSequentialStream *) &global.sercanmux1,

Also available in: Unified diff