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