-
Notifications
You must be signed in to change notification settings - Fork 25
Read sensors in the systick #263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,6 @@ | |
| #define SIDE_WALL_DETECTION (CELL_DIMENSION * 0.90) | ||
| #define FRONT_WALL_DETECTION (CELL_DIMENSION * 1.5) | ||
| #define SIDE_CALIBRATION_READINGS 20 | ||
| #define SENSORS_SM_TICKS 4 | ||
| #define LOG_CONVERSION_TABLE_STEP 4 | ||
| #define LOG_CONVERSION_TABLE_SIZE (ADC_RESOLUTION / LOG_CONVERSION_TABLE_STEP) | ||
|
|
||
|
|
@@ -190,86 +189,52 @@ static void set_emitter_off(uint8_t emitter) | |
| } | ||
|
|
||
| /** | ||
| * @brief State machine to manage the sensors activation and deactivation | ||
| * states and readings. | ||
| * | ||
| * In order to get accurate distance values, the phototransistor's output | ||
| * will be read with the infrared emitter sensors powered on and powered | ||
| * off. Besides, to avoid undesired interactions between different emitters and | ||
| * phototranistors, the reads will be done one by one. | ||
| * | ||
| * The battery voltage is also read on the state 1. | ||
| * | ||
| * - State 1 (first because the emitter is OFF on start): | ||
| * -# Save phototranistors sensors (ADC1) from emitter OFF and | ||
| * power ON the emitter. | ||
| * - State 2: | ||
| * -# Start the phototranistors sensors (ADC1) read. | ||
| * - State 3: | ||
| * -# Save phototranistors sensors (ADC1) from emitter ON and | ||
| * power OFF the emitter. | ||
| * - State 4: | ||
| * -# Start the phototranistors sensors (ADC1) read. | ||
| * @brief Get sensors values with emitter on and off. | ||
| */ | ||
| static void sm_emitter_adc(void) | ||
| void get_sensors_raw(uint16_t *off, uint16_t *on) | ||
| { | ||
| static uint8_t emitter_status = 1; | ||
| static uint8_t sensor_index = SENSOR_SIDE_LEFT_ID; | ||
| uint8_t i = 0; | ||
|
|
||
| switch (emitter_status) { | ||
| case 1: | ||
| sensors_off[sensor_index] = | ||
| adc_read_injected(ADC1, (sensor_index + 1)); | ||
| set_emitter_on(sensor_index); | ||
| emitter_status = 2; | ||
| break; | ||
| case 2: | ||
| adc_start_conversion_injected(ADC1); | ||
| emitter_status = 3; | ||
| break; | ||
| case 3: | ||
| sensors_on[sensor_index] = | ||
| adc_read_injected(ADC1, (sensor_index + 1)); | ||
| set_emitter_off(sensor_index); | ||
| emitter_status = 4; | ||
| break; | ||
| case 4: | ||
| adc_start_conversion_injected(ADC1); | ||
| emitter_status = 1; | ||
| if (sensor_index == (NUM_SENSOR - 1)) | ||
| sensor_index = 0; | ||
| else | ||
| sensor_index++; | ||
| break; | ||
| default: | ||
| break; | ||
| for (i = 0; i < NUM_SENSOR; i++) { | ||
| off[i] = sensors_off[i]; | ||
| on[i] = sensors_on[i]; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @brief TIM1 interruption routine. | ||
| * | ||
| * - Manage the update event interruption flag. | ||
| * - Trigger state machine to manage sensors. | ||
| * @brief Start and wait for complete injection of sensor readings. | ||
| */ | ||
| void tim1_up_isr(void) | ||
| static void inject_readings(void) | ||
| { | ||
| if (timer_get_flag(TIM1, TIM_SR_UIF)) { | ||
| timer_clear_flag(TIM1, TIM_SR_UIF); | ||
| sm_emitter_adc(); | ||
| } | ||
| adc_start_conversion_injected(ADC1); | ||
| while (!(adc_eoc_injected(ADC1))) | ||
| ; | ||
| // Clear injected end of conversion | ||
| ADC_SR(ADC1) &= ~ADC_SR_JEOC; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Get sensors values with emitter on and off. | ||
| * @brief Update the sensors raw readings. | ||
| */ | ||
| void get_sensors_raw(uint16_t *off, uint16_t *on) | ||
| static void update_raw_readings(void) | ||
| { | ||
| uint8_t i = 0; | ||
|
|
||
| for (i = 0; i < NUM_SENSOR; i++) { | ||
| off[i] = sensors_off[i]; | ||
| on[i] = sensors_on[i]; | ||
| inject_readings(); | ||
| sensors_off[i] = adc_read_injected(ADC1, (i + 1)); | ||
| } | ||
| for (i = 0; i < NUM_SENSOR; i++) { | ||
| inject_readings(); | ||
| sensors_off[i] = adc_read_injected(ADC1, (i + 1)); | ||
| } | ||
|
|
||
| for (i = 0; i < NUM_SENSOR; i++) { | ||
| set_emitter_on(i); | ||
| sleep_us(10); | ||
| inject_readings(); | ||
| sensors_on[i] = adc_read_injected(ADC1, (i + 1)); | ||
| set_emitter_off(i); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -305,6 +270,7 @@ void update_distance_readings(void) | |
| { | ||
| uint8_t i = 0; | ||
|
|
||
| update_raw_readings(); | ||
| for (i = 0; i < NUM_SENSOR; i++) { | ||
| distance[i] = (sensors_calibration_a[i] / | ||
| raw_log(sensors_on[i], sensors_off[i]) - | ||
|
|
@@ -447,7 +413,7 @@ void side_sensors_calibration(void) | |
| for (i = 0; i < SIDE_CALIBRATION_READINGS; i++) { | ||
| left_temp += distance[SENSOR_SIDE_LEFT_ID]; | ||
| right_temp += distance[SENSOR_SIDE_RIGHT_ID]; | ||
| sleep_ticks(SENSORS_SM_TICKS); | ||
| sleep_ticks(2); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why did you change it to 2? Coudl you add an explicative macro?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How would you call it? It is just some small delay in between readings, it could be any other number really. I could create a |
||
| } | ||
| calibration_factor[SENSOR_SIDE_LEFT_ID] += | ||
| (left_temp / SIDE_CALIBRATION_READINGS) - MIDDLE_MAZE_DISTANCE; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Expand the description