amiro-os / devices / DiWheelDrive / amiro_map.hpp @ 64cba697
History | View | Annotate | Download (3.678 KB)
1 |
#ifndef AMIRO_MAP
|
---|---|
2 |
#define AMIRO_MAP
|
3 |
|
4 |
#include "global.hpp" |
5 |
#include "linefollow.hpp" |
6 |
|
7 |
|
8 |
namespace amiro {
|
9 |
|
10 |
struct node {
|
11 |
uint8_t id; |
12 |
uint8_t flag; |
13 |
uint8_t left; |
14 |
uint8_t right; |
15 |
union edge {
|
16 |
struct edge_id{
|
17 |
uint8_t left; |
18 |
uint8_t right; |
19 |
} edge_id; |
20 |
uint8_t arr[2];
|
21 |
} edge; |
22 |
uint8_t visited; |
23 |
types::position pL; // Left
|
24 |
types::position pR; // Right
|
25 |
types::position pB; // Back
|
26 |
union p{
|
27 |
struct{types::position pL, pR, pB;} points;
|
28 |
types::position arr[3];
|
29 |
} p; |
30 |
}; |
31 |
|
32 |
struct map_state {
|
33 |
// 0 - left, 1- right
|
34 |
uint8_t strategy; |
35 |
// Node ID of last detected fixpoint
|
36 |
uint8_t current; |
37 |
// Next node ID
|
38 |
uint8_t next; |
39 |
//Traveled Distance between current and next in %
|
40 |
uint32_t dist; |
41 |
// True if the current loaded map is valid
|
42 |
bool valid;
|
43 |
// Length of the currently traveled edge
|
44 |
uint32_t eLength; |
45 |
}; |
46 |
|
47 |
class AmiroMap { |
48 |
public:
|
49 |
map_state * get_state() { return &state; }
|
50 |
uint8_t getNodeCount(){ return nodeCount; }
|
51 |
node * getNodeList(){return nodeList; }
|
52 |
|
53 |
|
54 |
AmiroMap(Global *global) : global{global} {} |
55 |
|
56 |
/**
|
57 |
* Initialize a new map from configuration.
|
58 |
* @param config map configuration array
|
59 |
*
|
60 |
*/
|
61 |
uint8_t initialize(); |
62 |
|
63 |
|
64 |
/**
|
65 |
* Update the internal map state according to the detected fixpoint
|
66 |
* This function should be called for a generic update, each can cycle and in
|
67 |
* case a fixpoint on one side is detected.
|
68 |
* Internal state maschine will go into an error state in case both sensors are black.
|
69 |
*
|
70 |
* @param left fixpoint on left side detected
|
71 |
* @param right fixpoint on right side detected
|
72 |
* @param strategy current line follow strategy
|
73 |
*
|
74 |
*
|
75 |
*/
|
76 |
uint8_t update(uint16_t WL, uint16_t WR, LineFollowStrategy strategy); |
77 |
|
78 |
|
79 |
/**
|
80 |
If this is called instead of update new fixpoints can automativally
|
81 |
get detected and will be added to the current nodeList.
|
82 |
Internally the update will be called.
|
83 |
If there are no nodes in the node list they will be created.
|
84 |
*/
|
85 |
uint8_t trackUpdate(uint16_t WL, uint16_t WR, LineFollowStrategy strategy, ut_states ut_state); |
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
private:
|
92 |
Global *global; |
93 |
LineFollowStrategy lfStrategy = LineFollowStrategy::EDGE_RIGHT; |
94 |
// Keeps track of the internal map state
|
95 |
map_state state; |
96 |
// Holds the amount of detected node of the map
|
97 |
uint8_t nodeCount = 0;
|
98 |
// Stores all nodes and the corresponding coordinates of last detected fixpoints
|
99 |
node nodeList[MAX_NODES]; |
100 |
// If driving over fixpoint prevent continuous updating
|
101 |
// True if left sensor is driving over black
|
102 |
bool fxpDetected = false; |
103 |
|
104 |
/**
|
105 |
* Recursively search through all nodes in the node list and
|
106 |
* mark all reached nodes as visited.
|
107 |
*
|
108 |
* @param id node from where to start visiting the other nodes
|
109 |
*
|
110 |
*/
|
111 |
void visitNode(uint8_t id);
|
112 |
|
113 |
/**
|
114 |
* Calculate the distance between two given points
|
115 |
*
|
116 |
* @param p1 point 1
|
117 |
* @param p2 point 2
|
118 |
*
|
119 |
*/
|
120 |
uint32_t calculateDist(types::position *p1, types::position *p2); |
121 |
|
122 |
// add node/fxp to the nodeList and return its id
|
123 |
uint8_t addNode(uint8_t l, uint8_t r, uint8_t flags); |
124 |
|
125 |
// Calculate dist and elength if possibel
|
126 |
void calTravelState(types::position *p1);
|
127 |
|
128 |
// Check if all nodes have followers
|
129 |
// Calculate validity check
|
130 |
void checkMap();
|
131 |
|
132 |
void switchToNext(types::position *p1);
|
133 |
|
134 |
// Copy contents of point from to point to (x, y, f_x)
|
135 |
void copyPoint(types::position *from, types::position *to);
|
136 |
|
137 |
// Create the first fxp with flag 1
|
138 |
void createInitNode();
|
139 |
|
140 |
uint8_t getNearest(types::position *p1); |
141 |
|
142 |
// Either create new fxpoint or assign point to existing one
|
143 |
uint8_t assignFxp(types::position *p1); |
144 |
}; |
145 |
|
146 |
}; // namespace amiro
|
147 |
|
148 |
#endif /* AMIRO_MAP */ |