Skip to content

Commit 0183353

Browse files
committed
Read sensors with systick and just before control
1 parent 2629408 commit 0183353

File tree

2 files changed

+32
-57
lines changed

2 files changed

+32
-57
lines changed

src/setup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,10 +370,10 @@ void setup(void)
370370
setup_clock();
371371
setup_exceptions();
372372
setup_gpio();
373+
setup_adc1();
373374
setup_usart();
374375
setup_encoders();
375376
setup_pwm();
376377
setup_mpu();
377378
setup_systick();
378-
setup_adc1();
379379
}

src/target/detection.c

Lines changed: 31 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#define SIDE_WALL_DETECTION (CELL_DIMENSION * 0.90)
44
#define FRONT_WALL_DETECTION (CELL_DIMENSION * 1.5)
55
#define SIDE_CALIBRATION_READINGS 20
6-
#define SENSORS_SM_TICKS 4
76
#define LOG_CONVERSION_TABLE_STEP 4
87
#define LOG_CONVERSION_TABLE_SIZE (ADC_RESOLUTION / LOG_CONVERSION_TABLE_STEP)
98

@@ -190,72 +189,47 @@ static void set_emitter_off(uint8_t emitter)
190189
}
191190

192191
/**
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.
213193
*/
214-
static void sm_emitter_adc(void)
194+
void get_sensors_raw(uint16_t *off, uint16_t *on)
215195
{
216-
static uint8_t emitter_status = 1;
217-
static uint8_t sensor_index = SENSOR_SIDE_LEFT_ID;
196+
uint8_t i = 0;
218197

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];
246201
}
247202
}
248203

249204
/**
250-
* @brief Get sensors values with emitter on and off.
205+
* @brief Update the sensors raw readings.
251206
*/
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)
253220
{
254221
uint8_t i = 0;
255222

223+
inject_readings();
224+
for (i = 0; i < NUM_SENSOR; i++)
225+
sensors_off[i] = adc_read_injected(ADC1, (i + 1));
226+
256227
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);
259233
}
260234
}
261235

@@ -291,6 +265,7 @@ void update_distance_readings(void)
291265
{
292266
uint8_t i = 0;
293267

268+
update_raw_readings();
294269
for (i = 0; i < NUM_SENSOR; i++) {
295270
distance[i] = (sensors_calibration_a[i] /
296271
raw_log(sensors_on[i], sensors_off[i]) -
@@ -433,7 +408,7 @@ void side_sensors_calibration(void)
433408
for (i = 0; i < SIDE_CALIBRATION_READINGS; i++) {
434409
left_temp += distance[SENSOR_SIDE_LEFT_ID];
435410
right_temp += distance[SENSOR_SIDE_RIGHT_ID];
436-
sleep_ticks(SENSORS_SM_TICKS);
411+
sleep_ticks(2);
437412
}
438413
calibration_factor[SENSOR_SIDE_LEFT_ID] +=
439414
(left_temp / SIDE_CALIBRATION_READINGS) - MIDDLE_MAZE_DISTANCE;

0 commit comments

Comments
 (0)