amiro-os / devices / DiWheelDrive / userthread.hpp @ 9090cc7e
History | View | Annotate | Download (6.138 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 | 8dced1c9 | galberding | // #include "linefollow.hpp"
|
9 | 10bf9cc0 | galberding | #include <cmath> |
10 | 181f2892 | galberding | |
11 | d4c6efa9 | galberding | |
12 | |||
13 | // TODO: Clean up and sort defines!
|
||
14 | 181f2892 | galberding | // Speed when driving towards the docking station
|
15 | #define CHARGING_SPEED 5 |
||
16 | 61544eee | galberding | #define DETECTION_SPEED 10 |
17 | #define DIST_THRESH 100 |
||
18 | #define RELEASE_COUNT 200 |
||
19 | d4c6efa9 | galberding | #define SPEED_CONVERSION_FACTOR 1000000 //Convert value to um/s |
20 | 181f2892 | galberding | // Thresh to determain how much update steps should pass while alining
|
21 | 10bf9cc0 | galberding | // #define MAX_CORRECTION_STEPS 200
|
22 | #define DOCKING_CORRECTION_TIMEOUT 200 |
||
23 | #define REVERSE_DOCKING_TIMEOUT 2*DOCKING_CORRECTION_TIMEOUT |
||
24 | b24df8ad | galberding | #define REVERSE_ADJUSTMENT_TIMEOUT 200 |
25 | 8dced1c9 | galberding | // #define MAX_RING_PROX_VALUE_DEVIATION
|
26 | 10bf9cc0 | galberding | |
27 | 181f2892 | galberding | // Thresh for wheel proxy sensors, when summed values fall below the state changes
|
28 | 10bf9cc0 | galberding | // #define PROXY_WHEEL_THRESH 18000
|
29 | e2002d0e | galberding | // Thresh for detecting obsticles
|
30 | 10bf9cc0 | galberding | // #define PROXY_RING_THRESH 15000
|
31 | 27d4e1fa | galberding | |
32 | 10bf9cc0 | galberding | // #define PUSH_BACK_COUNT 5
|
33 | #define PUSH_BACK_TIMEOUT 5 |
||
34 | 181f2892 | galberding | // Thresh for how long (update steps) the front sensors are allowed to detect white
|
35 | 10bf9cc0 | galberding | // #define WHITE_COUNT_THRESH 150
|
36 | #define WHITE_DETETION_TIMEOUT 150 |
||
37 | // #define RING_PROX_COUNT_THRESH 1000
|
||
38 | 8dced1c9 | galberding | #define RING_PROX_DETECTION_TIMEOUT 400 |
39 | 181f2892 | galberding | // Rotation around 180 degrees in microradian
|
40 | 10bf9cc0 | galberding | // #define ROTATION_180 3141592
|
41 | 181f2892 | galberding | #define ROTATION_180 3141592 |
42 | // Rotation around -20 degrees in microradian
|
||
43 | #define ROTATION_20 -349065 |
||
44 | #define ROTATION_DURATION 10000 |
||
45 | |||
46 | 61544eee | galberding | #define RING_PROX_FRONT_THRESH 18000 |
47 | b24df8ad | galberding | // #define PROX_MAX_VAL 65430
|
48 | #define PROX_MAX_VAL 0xFFF0 |
||
49 | |||
50 | 58fe0e0b | Thomas Schöpping | |
51 | 27d4e1fa | galberding | // Threshold for failing to dock
|
52 | 10bf9cc0 | galberding | #define DOCKING_ERROR_THRESH 3 |
53 | #define CAN_TRANSMIT_STATE_THRESH 50 |
||
54 | 84b4c632 | galberding | #define PROX_DEVIATION_MEAN_WINDOW 3 |
55 | b24df8ad | galberding | #define MAX_DEVIATION_CORRECTIONS 4 |
56 | 84b4c632 | galberding | #define MAX_DEVIATION_FACTOR 35 |
57 | #define DEVIATION_CORRECTION_DURATION 900 |
||
58 | b24df8ad | galberding | #define DEVIATION_CORRECTION_SPEED 2000000 |
59 | #define DEVIATION_CORRECTION_VALUE (DEVIATION_CORRECTION_SPEED / 2) |
||
60 | 84b4c632 | galberding | #define DEVIATION_DIST_THRESH 25000 |
61 | 27d4e1fa | galberding | |
62 | 58fe0e0b | Thomas Schöpping | namespace amiro {
|
63 | |||
64 | 181f2892 | galberding | |
65 | 58fe0e0b | Thomas Schöpping | class UserThread : public chibios_rt::BaseStaticThread<USER_THREAD_STACK_SIZE> |
66 | { |
||
67 | 181f2892 | galberding | |
68 | // Messages which can be received and trigger state changes
|
||
69 | public:
|
||
70 | |||
71 | // States of user thread state machine
|
||
72 | 10bf9cc0 | galberding | enum states : int8_t{
|
73 | e404e6c0 | galberding | 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 | 10bf9cc0 | galberding | REVERSE_TIMEOUT_ERROR = -2,
|
92 | e404e6c0 | galberding | CALIBRATION_ERROR = -3,
|
93 | 10bf9cc0 | galberding | WHITE_DETECTION_ERROR = -4,
|
94 | PROXY_DETECTION_ERROR = -5,
|
||
95 | e404e6c0 | galberding | NO_CHARGING_POWER_ERROR = -6,
|
96 | UNKNOWN_STATE_ERROR = -7
|
||
97 | 181f2892 | galberding | }; |
98 | |||
99 | 27d4e1fa | galberding | struct ut_counter{
|
100 | int whiteCount = 0; |
||
101 | 10bf9cc0 | galberding | int ringProxCount = 0; |
102 | fbcb25cc | galberding | // int correctionCount = 0;
|
103 | 27d4e1fa | galberding | int errorCount = 0; |
104 | 10bf9cc0 | galberding | int stateCount = 0; |
105 | fbcb25cc | galberding | int stepCount = 0; |
106 | 10bf9cc0 | galberding | 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 | 27d4e1fa | galberding | }; |
118 | 181f2892 | galberding | |
119 | 10bf9cc0 | galberding | |
120 | struct bottom_prox_calibration {
|
||
121 | bool calibrateBlack = true; |
||
122 | uint32_t buf = 0;
|
||
123 | uint8_t meanWindow = 150;
|
||
124 | }; |
||
125 | 8dced1c9 | galberding | |
126 | b24df8ad | galberding | 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 | 27d4e1fa | galberding | // static const struct ut_counter emptyUtCount;
|
134 | ut_counter utCount; |
||
135 | 10bf9cc0 | galberding | proxy_ctrl pCtrl; |
136 | 8dced1c9 | galberding | bottom_prox_calibration proxCalib; |
137 | b24df8ad | galberding | deviation_correction devCor; |
138 | |||
139 | |||
140 | 58fe0e0b | Thomas Schöpping | explicit UserThread();
|
141 | |||
142 | virtual ~UserThread();
|
||
143 | |||
144 | virtual msg_t main();
|
||
145 | 181f2892 | galberding | |
146 | private:
|
||
147 | void setRpmSpeed(const int (&rpmSpeed)[2]); |
||
148 | c9fa414d | galberding | void setRpmSpeedFuzzy(const int (&rpmSpeed)[2]); |
149 | 181f2892 | galberding | 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 | c9fa414d | galberding | int getProxyRingSum();
|
159 | 10bf9cc0 | galberding | |
160 | /**
|
||
161 | b24df8ad | galberding | * 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 | 8dced1c9 | galberding | * Keep in mind that initial results are wrong.
|
165 | b24df8ad | galberding | * */
|
166 | int32_t meanDeviation(uint16_t a, uint16_t b); |
||
167 | |||
168 | /**
|
||
169 | 10bf9cc0 | galberding | * 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 | 8dced1c9 | galberding | |
173 | 10bf9cc0 | galberding | /**
|
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 | 8dced1c9 | galberding | |
181 | fbcb25cc | galberding | /**
|
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 | c9fa414d | galberding | |
188 | 181f2892 | galberding | /**
|
189 | * Check if current position changes when the wheel are deactivated.
|
||
190 | 8dced1c9 | galberding | *
|
191 | 181f2892 | galberding | * 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 | 8dced1c9 | galberding | * was successful (return 1) otherwise a correction is needed (return 0).
|
195 | 181f2892 | galberding | */
|
196 | int checkDockingSuccess();
|
||
197 | 10bf9cc0 | galberding | |
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 | 58fe0e0b | Thomas Schöpping | }; |
210 | |||
211 | } // end of namespace amiro
|
||
212 | |||
213 | #endif // AMIRO_USERTHREAD_H_ |