|
3 | 3 | #define SIDE_WALL_DETECTION (CELL_DIMENSION * 0.90) |
4 | 4 | #define FRONT_WALL_DETECTION (CELL_DIMENSION * 1.5) |
5 | 5 | #define SIDE_CALIBRATION_READINGS 20 |
6 | | -#define SENSORS_SM_TICKS 4 |
7 | 6 | #define LOG_CONVERSION_TABLE_STEP 4 |
8 | 7 | #define LOG_CONVERSION_TABLE_SIZE (ADC_RESOLUTION / LOG_CONVERSION_TABLE_STEP) |
9 | 8 |
|
@@ -190,72 +189,47 @@ static void set_emitter_off(uint8_t emitter) |
190 | 189 | } |
191 | 190 |
|
192 | 191 | /** |
193 | | - * @brief State machine to manage the sensors activation and deactivation |
194 | | - * states and readings. |
195 | | - * |
196 | | - * In order to get accurate distance values, the phototransistor's output |
197 | | - * will be read with the infrared emitter sensors powered on and powered |
198 | | - * off. Besides, to avoid undesired interactions between different emitters and |
199 | | - * phototranistors, the reads will be done one by one. |
200 | | - * |
201 | | - * The battery voltage is also read on the state 1. |
202 | | - * |
203 | | - * - State 1 (first because the emitter is OFF on start): |
204 | | - * -# Save phototranistors sensors (ADC1) from emitter OFF and |
205 | | - * power ON the emitter. |
206 | | - * - State 2: |
207 | | - * -# Start the phototranistors sensors (ADC1) read. |
208 | | - * - State 3: |
209 | | - * -# Save phototranistors sensors (ADC1) from emitter ON and |
210 | | - * power OFF the emitter. |
211 | | - * - State 4: |
212 | | - * -# Start the phototranistors sensors (ADC1) read. |
| 192 | + * @brief Get sensors values with emitter on and off. |
213 | 193 | */ |
214 | | -static void sm_emitter_adc(void) |
| 194 | +void get_sensors_raw(uint16_t *off, uint16_t *on) |
215 | 195 | { |
216 | | - static uint8_t emitter_status = 1; |
217 | | - static uint8_t sensor_index = SENSOR_SIDE_LEFT_ID; |
| 196 | + uint8_t i = 0; |
218 | 197 |
|
219 | | - switch (emitter_status) { |
220 | | - case 1: |
221 | | - sensors_off[sensor_index] = |
222 | | - adc_read_injected(ADC1, (sensor_index + 1)); |
223 | | - set_emitter_on(sensor_index); |
224 | | - emitter_status = 2; |
225 | | - break; |
226 | | - case 2: |
227 | | - adc_start_conversion_injected(ADC1); |
228 | | - emitter_status = 3; |
229 | | - break; |
230 | | - case 3: |
231 | | - sensors_on[sensor_index] = |
232 | | - adc_read_injected(ADC1, (sensor_index + 1)); |
233 | | - set_emitter_off(sensor_index); |
234 | | - emitter_status = 4; |
235 | | - break; |
236 | | - case 4: |
237 | | - adc_start_conversion_injected(ADC1); |
238 | | - emitter_status = 1; |
239 | | - if (sensor_index == (NUM_SENSOR - 1)) |
240 | | - sensor_index = 0; |
241 | | - else |
242 | | - sensor_index++; |
243 | | - break; |
244 | | - default: |
245 | | - break; |
| 198 | + for (i = 0; i < NUM_SENSOR; i++) { |
| 199 | + off[i] = sensors_off[i]; |
| 200 | + on[i] = sensors_on[i]; |
246 | 201 | } |
247 | 202 | } |
248 | 203 |
|
249 | 204 | /** |
250 | | - * @brief Get sensors values with emitter on and off. |
| 205 | + * @brief Update the sensors raw readings. |
251 | 206 | */ |
252 | | -void get_sensors_raw(uint16_t *off, uint16_t *on) |
| 207 | +static void inject_readings(void) |
| 208 | +{ |
| 209 | + adc_start_conversion_injected(ADC1); |
| 210 | + while (!(adc_eoc_injected(ADC1))) |
| 211 | + ; |
| 212 | + // Clear injected end of conversion |
| 213 | + ADC_SR(ADC1) &= ~ADC_SR_JEOC; |
| 214 | +} |
| 215 | + |
| 216 | +/** |
| 217 | + * @brief Update the sensors raw readings. |
| 218 | + */ |
| 219 | +static void update_raw_readings(void) |
253 | 220 | { |
254 | 221 | uint8_t i = 0; |
255 | 222 |
|
| 223 | + inject_readings(); |
| 224 | + for (i = 0; i < NUM_SENSOR; i++) |
| 225 | + sensors_off[i] = adc_read_injected(ADC1, (i + 1)); |
| 226 | + |
256 | 227 | for (i = 0; i < NUM_SENSOR; i++) { |
257 | | - off[i] = sensors_off[i]; |
258 | | - on[i] = sensors_on[i]; |
| 228 | + set_emitter_on(i); |
| 229 | + sleep_us(10); |
| 230 | + inject_readings(); |
| 231 | + sensors_on[i] = adc_read_injected(ADC1, (i + 1)); |
| 232 | + set_emitter_off(i); |
259 | 233 | } |
260 | 234 | } |
261 | 235 |
|
@@ -291,6 +265,7 @@ void update_distance_readings(void) |
291 | 265 | { |
292 | 266 | uint8_t i = 0; |
293 | 267 |
|
| 268 | + update_raw_readings(); |
294 | 269 | for (i = 0; i < NUM_SENSOR; i++) { |
295 | 270 | distance[i] = (sensors_calibration_a[i] / |
296 | 271 | raw_log(sensors_on[i], sensors_off[i]) - |
@@ -433,7 +408,7 @@ void side_sensors_calibration(void) |
433 | 408 | for (i = 0; i < SIDE_CALIBRATION_READINGS; i++) { |
434 | 409 | left_temp += distance[SENSOR_SIDE_LEFT_ID]; |
435 | 410 | right_temp += distance[SENSOR_SIDE_RIGHT_ID]; |
436 | | - sleep_ticks(SENSORS_SM_TICKS); |
| 411 | + sleep_ticks(2); |
437 | 412 | } |
438 | 413 | calibration_factor[SENSOR_SIDE_LEFT_ID] += |
439 | 414 | (left_temp / SIDE_CALIBRATION_READINGS) - MIDDLE_MAZE_DISTANCE; |
|
0 commit comments