Statistics
| Branch: | Tag: | Revision:

amiro-os / devices / DiWheelDrive / amiro_map.cpp @ 6acaea07

History | View | Annotate | Download (4.4 KB)

1 e1f1c4b5 galberding
#include "amiro_map.hpp"
2
3 7520e117 galberding
// #include <cstdint>
4
// #include <math.h>
5 e1f1c4b5 galberding
6 7520e117 galberding
7
uint8_t AmiroMap::initialize(){
8 e1f1c4b5 galberding
9 b8b3a9c9 galberding
  // Clear old values in case the map is initialized again
10 7520e117 galberding
  this->state.current = 0;
11
  this->state.next = 0;
12
  this->state.valid = false;
13 6acaea07 galberding
  this->nodeCount = 0;
14 e1f1c4b5 galberding
15 b8b3a9c9 galberding
  // convert proto map to internal representation
16 e1f1c4b5 galberding
  for (int i=0; i<MAX_NODES; i++){
17 7520e117 galberding
    if(global->testmap[i][2] == 0xff && i != 0){
18 e1f1c4b5 galberding
      break;
19 7520e117 galberding
    } else if (global->testmap[i][2] == 0xff && i == 0) {
20
      this->state.valid = false;
21 d2230e6e galberding
      return 255;
22 e1f1c4b5 galberding
    }
23
24 b8b3a9c9 galberding
    //look for start node (default is Node 0)
25 7520e117 galberding
    if (global->testmap[i][2] == 1 ) {
26
      this->state.current = i;
27 b8b3a9c9 galberding
    }
28 e1f1c4b5 galberding
29 b8b3a9c9 galberding
    this->nodeList[i].id = i;
30 7520e117 galberding
    this->nodeList[i].left = global->testmap[i][0];
31
    this->nodeList[i].right = global->testmap[i][1];
32
    this->nodeList[i].flag = global->testmap[i][2];
33 b8b3a9c9 galberding
    this->nodeCount++;
34
  }
35 e1f1c4b5 galberding
36 b8b3a9c9 galberding
  // TODO make validity check
37 d2230e6e galberding
38 a07a7a1c galberding
  for (int j=0; j<nodeCount; j++) {
39 d2230e6e galberding
    this->nodeList[j].visited = 0;
40 a07a7a1c galberding
    visitNode(j);
41
    for (int k = 0; k < nodeCount; k++) {
42 d2230e6e galberding
      if (this->nodeList[k].visited == 1) {
43
        this->nodeList[k].visited = 0;
44 a07a7a1c galberding
      } else {
45 7520e117 galberding
        this->state.valid = false;
46 d2230e6e galberding
        return k;
47 a07a7a1c galberding
      }
48
    }
49
  }
50 e1f1c4b5 galberding
51 7520e117 galberding
  this->state.valid = true;
52 d2230e6e galberding
  return 42;
53 a07a7a1c galberding
}
54 e1f1c4b5 galberding
55 a07a7a1c galberding
void AmiroMap::visitNode(uint8_t id){
56 d2230e6e galberding
  if (this->nodeList[id].visited == 1){
57 a07a7a1c galberding
    return;
58
  }else{
59 d2230e6e galberding
    nodeList[id].visited = 1;
60 a07a7a1c galberding
    visitNode(this->nodeList[id].left);
61
    visitNode(this->nodeList[id].right);
62
  }
63 e1f1c4b5 galberding
}
64
65 7520e117 galberding
void AmiroMap::update(LineFollowStrategy strategy) {
66
  // Each time at the end of the user thread state maschine, when the strategy
67
  // is changed or a fixpoint is detected, this method gets called.
68
  // The strategy change only has an effect on the fixpoint or in particular if
69
  // a fixpoint is detected.
70
71
  // set the strategy directly, actually there is no need to store that variable in the class
72
  // but we will go with it for now to initialize everything properly.
73
  this->lfStrategy = strategy;
74
  uint16_t WL = global->vcnl4020[constants::DiWheelDrive::PROX_WHEEL_LEFT].getProximityScaledWoOffset();
75
  uint16_t WR = global->vcnl4020[constants::DiWheelDrive::PROX_WHEEL_RIGHT].getProximityScaledWoOffset();
76
77
  // Check the wheel sensors
78
  bool left = global->linePID.BThresh >= WL;
79
  bool right = global->linePID.BThresh >= WR;
80
  types::position currentPos = global->odometry.getPosition();
81
82
  if (left && right) {
83
    // TODO A dangerous case -> amiro could be lifted
84
  }
85
  else if (left && !leftDetected) {
86
    // The sensor on the left side of the Amiro is driving on black
87
    // To prevent continous fixpoint detection a point needs to be marked as currently detected
88
    // and released.
89
    leftDetected = true;
90
    nodeList[state.next].pR.f_x = currentPos.f_x;
91
    nodeList[state.next].pR.f_y = currentPos.f_y;
92
    nodeList[state.next].pR.f_z = currentPos.f_z;
93
    nodeList[state.next].pR.x = currentPos.x;
94
    nodeList[state.next].pR.y = currentPos.y;
95
    nodeList[state.next].pR.z = currentPos.z;
96
    nodeList[state.next].visited |= 0x01;
97
    state.current = state.next;
98
    state.next = nodeList[state.current].right;
99
    state.strategy = 0x01;
100
  }
101
  else if (right && !rightDetected) {
102
    // Same as left only for the right sensor.
103
    rightDetected = true;
104
    nodeList[state.next].pL.f_x = currentPos.f_x;
105
    nodeList[state.next].pL.f_y = currentPos.f_y;
106
    nodeList[state.next].pL.f_z = currentPos.f_z;
107
    nodeList[state.next].pL.x = currentPos.x;
108
    nodeList[state.next].pL.y = currentPos.y;
109
    nodeList[state.next].pL.z = currentPos.z;
110
    nodeList[state.next].visited |= 0x02;
111
    state.current = state.next;
112
    state.next = nodeList[state.current].left;
113
    state.strategy = 0x02;
114
  }
115
  else if (!left && !right) {
116
    // in case the fixpoint is not detected anymore
117
    leftDetected = false;
118
    rightDetected = false;
119
  }
120
121 a07a7a1c galberding
122 7520e117 galberding
  // update internal map_state
123
  // Update travel distance
124
  // check if the nodes of the specific strategy where visited
125
  // if (state.strategy
126
  //     == (nodeList[state.current].visited & nodeList[state.next].visited)) {
127
  //   // only update distance if both nodes were visited
128
  //   if (state.strategy == 0x01) {
129
  //     // Amiro is driving on the right edge
130
  //     state.dist = (uint8_t) sqrt(
131
  //                                 pow(nodeList[state.next].pR.x - nodeList[state.current].pR.x, 2)
132
  //                                 + pow(nodeList[state.next].pR.y - nodeList[state.current].pR.y, 2));
133
  //   } else {
134
  //     // Driving on the left edge
135 e1f1c4b5 galberding
136 7520e117 galberding
  //   }
137
  // }
138 e1f1c4b5 galberding
}