amiro-os / devices / DiWheelDrive / userthread.hpp @ b24df8ad
History | View | Annotate | Download (5.95 KB)
1 | 58fe0e0b | Thomas Schöpping | #ifndef AMIRO_USERTHREAD_H_
|
---|---|---|---|
2 | #define AMIRO_USERTHREAD_H_
|
||
3 | |||
4 | #include <ch.hpp> |
||
5 | #include <amiroosconf.h> |
||
6 | 181f2892 | galberding | #include <amiro/Color.h> |
7 | 10bf9cc0 | galberding | // #include "global.hpp"
|
8 | // #include "linefollow.hpp"
|
||
9 | #include <cmath> |
||
10 | 181f2892 | galberding | |
11 | // Speed when driving towards the docking station
|
||
12 | #define CHARGING_SPEED 5 |
||
13 | 61544eee | galberding | #define DETECTION_SPEED 10 |
14 | #define DIST_THRESH 100 |
||
15 | #define RELEASE_COUNT 200 |
||
16 | 181f2892 | galberding | // Thresh to determain how much update steps should pass while alining
|
17 | 10bf9cc0 | galberding | // #define MAX_CORRECTION_STEPS 200
|
18 | #define DOCKING_CORRECTION_TIMEOUT 200 |
||
19 | #define REVERSE_DOCKING_TIMEOUT 2*DOCKING_CORRECTION_TIMEOUT |
||
20 | b24df8ad | galberding | #define REVERSE_ADJUSTMENT_TIMEOUT 200 |
21 | // #define MAX_RING_PROX_VALUE_DEVIATION
|
||
22 | 10bf9cc0 | galberding | |
23 | 181f2892 | galberding | // Thresh for wheel proxy sensors, when summed values fall below the state changes
|
24 | 10bf9cc0 | galberding | // #define PROXY_WHEEL_THRESH 18000
|
25 | e2002d0e | galberding | // Thresh for detecting obsticles
|
26 | 10bf9cc0 | galberding | // #define PROXY_RING_THRESH 15000
|
27 | 27d4e1fa | galberding | |
28 | 10bf9cc0 | galberding | // #define PUSH_BACK_COUNT 5
|
29 | #define PUSH_BACK_TIMEOUT 5 |
||
30 | 181f2892 | galberding | // Thresh for how long (update steps) the front sensors are allowed to detect white
|
31 | 10bf9cc0 | galberding | // #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 | 181f2892 | galberding | // Rotation around 180 degrees in microradian
|
36 | 10bf9cc0 | galberding | // #define ROTATION_180 3141592
|
37 | 181f2892 | galberding | #define ROTATION_180 3141592 |
38 | // Rotation around -20 degrees in microradian
|
||
39 | #define ROTATION_20 -349065 |
||
40 | #define ROTATION_DURATION 10000 |
||
41 | |||
42 | 61544eee | galberding | #define RING_PROX_FRONT_THRESH 18000 |
43 | b24df8ad | galberding | // #define PROX_MAX_VAL 65430
|
44 | #define PROX_MAX_VAL 0xFFF0 |
||
45 | |||
46 | 58fe0e0b | Thomas Schöpping | |
47 | 27d4e1fa | galberding | // Threshold for failing to dock
|
48 | 10bf9cc0 | galberding | #define DOCKING_ERROR_THRESH 3 |
49 | #define CAN_TRANSMIT_STATE_THRESH 50 |
||
50 | b24df8ad | galberding | #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 | 27d4e1fa | galberding | |
58 | 58fe0e0b | Thomas Schöpping | namespace amiro {
|
59 | |||
60 | 181f2892 | galberding | |
61 | 58fe0e0b | Thomas Schöpping | class UserThread : public chibios_rt::BaseStaticThread<USER_THREAD_STACK_SIZE> |
62 | { |
||
63 | 181f2892 | galberding | |
64 | // Messages which can be received and trigger state changes
|
||
65 | public:
|
||
66 | |||
67 | // States of user thread state machine
|
||
68 | 10bf9cc0 | galberding | enum states : int8_t{
|
69 | 27d4e1fa | galberding | 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 | 10bf9cc0 | galberding | TURN = 12,
|
81 | INACTIVE = 13,
|
||
82 | CALIBRATION = 14,
|
||
83 | CALIBRATION_CHECK = 15,
|
||
84 | b24df8ad | galberding | DEVIATION_CORRECTION = 16,
|
85 | 10bf9cc0 | galberding | 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 | 181f2892 | galberding | }; |
93 | |||
94 | 27d4e1fa | galberding | struct ut_counter{
|
95 | int whiteCount = 0; |
||
96 | 10bf9cc0 | galberding | int ringProxCount = 0; |
97 | fbcb25cc | galberding | // int correctionCount = 0;
|
98 | 27d4e1fa | galberding | int errorCount = 0; |
99 | 10bf9cc0 | galberding | int stateCount = 0; |
100 | fbcb25cc | galberding | int stepCount = 0; |
101 | 10bf9cc0 | galberding | 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 | 27d4e1fa | galberding | }; |
113 | 181f2892 | galberding | |
114 | 10bf9cc0 | galberding | |
115 | struct bottom_prox_calibration {
|
||
116 | bool calibrateBlack = true; |
||
117 | uint32_t buf = 0;
|
||
118 | uint8_t meanWindow = 150;
|
||
119 | }; |
||
120 | |||
121 | b24df8ad | galberding | 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 | 27d4e1fa | galberding | // static const struct ut_counter emptyUtCount;
|
129 | ut_counter utCount; |
||
130 | 10bf9cc0 | galberding | proxy_ctrl pCtrl; |
131 | bottom_prox_calibration proxCalib; |
||
132 | b24df8ad | galberding | deviation_correction devCor; |
133 | |||
134 | |||
135 | 58fe0e0b | Thomas Schöpping | explicit UserThread();
|
136 | |||
137 | virtual ~UserThread();
|
||
138 | |||
139 | virtual msg_t main();
|
||
140 | 181f2892 | galberding | |
141 | private:
|
||
142 | void setRpmSpeed(const int (&rpmSpeed)[2]); |
||
143 | c9fa414d | galberding | void setRpmSpeedFuzzy(const int (&rpmSpeed)[2]); |
144 | 181f2892 | galberding | 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 | c9fa414d | galberding | int getProxyRingSum();
|
154 | 10bf9cc0 | galberding | |
155 | /**
|
||
156 | b24df8ad | galberding | * 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 | 10bf9cc0 | galberding | * 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 | fbcb25cc | galberding | |
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 | c9fa414d | galberding | |
183 | 181f2892 | galberding | /**
|
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 | 10bf9cc0 | galberding | |
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 | 58fe0e0b | Thomas Schöpping | }; |
205 | |||
206 | } // end of namespace amiro
|
||
207 | |||
208 | #endif // AMIRO_USERTHREAD_H_ |