amiro-os / devices / DiWheelDrive / userthread.hpp @ a47d64ad
History | View | Annotate | Download (6.138 KB)
1 |
#ifndef AMIRO_USERTHREAD_H_
|
---|---|
2 |
#define AMIRO_USERTHREAD_H_
|
3 |
|
4 |
#include <ch.hpp> |
5 |
#include <amiroosconf.h> |
6 |
#include <amiro/Color.h> |
7 |
// #include "global.hpp"
|
8 |
// #include "linefollow.hpp"
|
9 |
#include <cmath> |
10 |
|
11 |
|
12 |
|
13 |
// TODO: Clean up and sort defines!
|
14 |
// Speed when driving towards the docking station
|
15 |
#define CHARGING_SPEED 5 |
16 |
#define DETECTION_SPEED 10 |
17 |
#define DIST_THRESH 100 |
18 |
#define RELEASE_COUNT 200 |
19 |
#define SPEED_CONVERSION_FACTOR 1000000 //Convert value to um/s |
20 |
// Thresh to determain how much update steps should pass while alining
|
21 |
// #define MAX_CORRECTION_STEPS 200
|
22 |
#define DOCKING_CORRECTION_TIMEOUT 200 |
23 |
#define REVERSE_DOCKING_TIMEOUT 2*DOCKING_CORRECTION_TIMEOUT |
24 |
#define REVERSE_ADJUSTMENT_TIMEOUT 200 |
25 |
// #define MAX_RING_PROX_VALUE_DEVIATION
|
26 |
|
27 |
// Thresh for wheel proxy sensors, when summed values fall below the state changes
|
28 |
// #define PROXY_WHEEL_THRESH 18000
|
29 |
// Thresh for detecting obsticles
|
30 |
// #define PROXY_RING_THRESH 15000
|
31 |
|
32 |
// #define PUSH_BACK_COUNT 5
|
33 |
#define PUSH_BACK_TIMEOUT 5 |
34 |
// Thresh for how long (update steps) the front sensors are allowed to detect white
|
35 |
// #define WHITE_COUNT_THRESH 150
|
36 |
#define WHITE_DETETION_TIMEOUT 150 |
37 |
// #define RING_PROX_COUNT_THRESH 1000
|
38 |
#define RING_PROX_DETECTION_TIMEOUT 400 |
39 |
// Rotation around 180 degrees in microradian
|
40 |
// #define ROTATION_180 3141592
|
41 |
#define ROTATION_180 3141592 |
42 |
// Rotation around -20 degrees in microradian
|
43 |
#define ROTATION_20 -349065 |
44 |
#define ROTATION_DURATION 10000 |
45 |
|
46 |
#define RING_PROX_FRONT_THRESH 18000 |
47 |
// #define PROX_MAX_VAL 65430
|
48 |
#define PROX_MAX_VAL 0xFFF0 |
49 |
|
50 |
|
51 |
// Threshold for failing to dock
|
52 |
#define DOCKING_ERROR_THRESH 3 |
53 |
#define CAN_TRANSMIT_STATE_THRESH 50 |
54 |
#define PROX_DEVIATION_MEAN_WINDOW 3 |
55 |
#define MAX_DEVIATION_CORRECTIONS 4 |
56 |
#define MAX_DEVIATION_FACTOR 35 |
57 |
#define DEVIATION_CORRECTION_DURATION 900 |
58 |
#define DEVIATION_CORRECTION_SPEED 2000000 |
59 |
#define DEVIATION_CORRECTION_VALUE (DEVIATION_CORRECTION_SPEED / 2) |
60 |
#define DEVIATION_DIST_THRESH 25000 |
61 |
|
62 |
namespace amiro {
|
63 |
|
64 |
|
65 |
class UserThread : public chibios_rt::BaseStaticThread<USER_THREAD_STACK_SIZE> |
66 |
{ |
67 |
|
68 |
// Messages which can be received and trigger state changes
|
69 |
public:
|
70 |
|
71 |
// 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{
|
100 |
int whiteCount = 0; |
101 |
int ringProxCount = 0; |
102 |
// int correctionCount = 0;
|
103 |
int errorCount = 0; |
104 |
int stateCount = 0; |
105 |
int stepCount = 0; |
106 |
uint32_t stateTime = 0;
|
107 |
}; |
108 |
|
109 |
struct proxy_ctrl {
|
110 |
int threshLow = 100; |
111 |
int threshMid = 500; |
112 |
int threshHigh = 1500; |
113 |
// int threshHigh = 1500;
|
114 |
int penalty = 100000; |
115 |
int pFactor = 310000; |
116 |
int staticCont = 0; |
117 |
}; |
118 |
|
119 |
|
120 |
struct bottom_prox_calibration {
|
121 |
bool calibrateBlack = true; |
122 |
uint32_t buf = 0;
|
123 |
uint8_t meanWindow = 150;
|
124 |
}; |
125 |
|
126 |
struct deviation_correction {
|
127 |
bool RCase = true; |
128 |
int8_t pCount = 0;
|
129 |
int32_t proxbuf[PROX_DEVIATION_MEAN_WINDOW] = { 0 };
|
130 |
int32_t currentDeviation = 0;
|
131 |
}; |
132 |
|
133 |
// static const struct ut_counter emptyUtCount;
|
134 |
ut_counter utCount; |
135 |
proxy_ctrl pCtrl; |
136 |
bottom_prox_calibration proxCalib; |
137 |
deviation_correction devCor; |
138 |
|
139 |
|
140 |
explicit UserThread();
|
141 |
|
142 |
virtual ~UserThread();
|
143 |
|
144 |
virtual msg_t main();
|
145 |
|
146 |
private:
|
147 |
void setRpmSpeed(const int (&rpmSpeed)[2]); |
148 |
void setRpmSpeedFuzzy(const int (&rpmSpeed)[2]); |
149 |
void lightOneLed(Color color, int idx); |
150 |
void lightAllLeds(Color color);
|
151 |
/**
|
152 |
* Uses light ring indicate the state of charge.
|
153 |
*/
|
154 |
void showChargingState();
|
155 |
void checkForMotion();
|
156 |
bool checkPinVoltage();
|
157 |
bool checkPinEnabled();
|
158 |
int getProxyRingSum();
|
159 |
|
160 |
/**
|
161 |
* Returns percentage of mean deviation between two given values.
|
162 |
* It is intended to calculate the mean deviation between two proxy sensor
|
163 |
* values. PROX_DEVIATION_MEAN_WINDOW determains the size of the mean window.
|
164 |
* Keep in mind that initial results are wrong.
|
165 |
* */
|
166 |
int32_t meanDeviation(uint16_t a, uint16_t b); |
167 |
|
168 |
/**
|
169 |
* Check sectors around and stop if a thresh in one sector is detected.
|
170 |
*/
|
171 |
void preventCollision(int (&rpmSpeed)[2], uint16_t (&proxVals)[8]); |
172 |
|
173 |
/**
|
174 |
* Same as prevent collision but also lowers the speed when object is detected.
|
175 |
*/
|
176 |
void proxSectorSpeedCorrection(int (&rpmSpeed)[2], uint16_t (&proxVals)[8]); |
177 |
void getProxySectorVals(uint16_t (&proxVals)[8], uint16_t (&sProx)[8]); |
178 |
void getMaxFrontSectorVal(uint16_t (&sProx)[8], int32_t &sPMax); |
179 |
void chargeAsLED();
|
180 |
|
181 |
/**
|
182 |
* Returns true when front sensors reaching high values
|
183 |
* and all others are low. This indicates that the loading station is ahead.
|
184 |
* If other sensores are blocked too, indicates that someone grabs the robot.
|
185 |
*/
|
186 |
bool checkFrontalObject();
|
187 |
|
188 |
/**
|
189 |
* Check if current position changes when the wheel are deactivated.
|
190 |
*
|
191 |
* When AMiRo drives towards the loading station, it stops when a specific marker is reached.
|
192 |
* In order to validate that the AMiRo is correctly positioned in the loading station
|
193 |
* the wheels are turned off. When the position remains the same the docking procedure
|
194 |
* was successful (return 1) otherwise a correction is needed (return 0).
|
195 |
*/
|
196 |
int checkDockingSuccess();
|
197 |
|
198 |
// State Variables
|
199 |
states prevState = states::IDLE; |
200 |
states currentState = states::IDLE; |
201 |
states newState = states::IDLE; |
202 |
|
203 |
bool continue_on_obstacle = true; |
204 |
uint16_t rProx[8]; // buffer for ring proxy values |
205 |
int rpmSpeed[2] = {0}; |
206 |
int stop[2] = {0}; |
207 |
int turn[2] = {5,-5}; |
208 |
|
209 |
}; |
210 |
|
211 |
} // end of namespace amiro
|
212 |
|
213 |
#endif // AMIRO_USERTHREAD_H_ |