This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:examples:sensor:ir_distance [2013/03/26 21:15] – external edit 127.0.0.1 | en:examples:sensor:ir_distance [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Infrared distance sensor ====== | ====== Infrared distance sensor ====== | ||
| - | //Necessary knowledge: | + | //Necessary knowledge: |
| + | [HW] [[en: | ||
| + | [AVR] [[en: | ||
| + | [LIB] [[en: | ||
| ===== Theory ===== | ===== Theory ===== | ||
| Line 10: | Line 13: | ||
| [{{ : | [{{ : | ||
| - | |||
| - | [{{ : | ||
| The output of distance sensors by " | The output of distance sensors by " | ||
| Line 17: | Line 18: | ||
| ===== Practice | ===== Practice | ||
| - | The HomeLab set of sensors includes IR distance sensor SHARP GP2Y0A21YK. Measuring range of the sensor is 10 cm – 80 cm. The output voltage of this sensor is, depending on the distance measured, up to 3 V. The distance sensor | + | [{{ : |
| + | |||
| + | The HomeLab set of sensors includes IR distance sensor SHARP GP2Y0A21YK. Measuring range of the sensor is 10 cm – 80 cm. The output voltage of this sensor is, depending on the distance measured, up to 3 V. The distance sensor | ||
| On the datasheet of the GP2Y0A21YK is graph of relation between its output voltage and measured distance. This graph is not a linear one, however the graph of inverse values of output voltage and distance almost is, and from that is quite easy to find the formula for converting voltage to distance. To find the formula, the points of the same graph must be inserted to any kind of spreadsheet application and then generate a new graph. In spreadsheet programs is possible to calculate automatically the trend-line. Next, the graph of GP2Y0A21YK corrected output voltage inverse value’s relation to the corrected inverse value of measured distance with linear trend-line is presented. To simplify, the output voltage is already converted to 10 bit +5 V values of analogue-digital converter with comparison voltage. | On the datasheet of the GP2Y0A21YK is graph of relation between its output voltage and measured distance. This graph is not a linear one, however the graph of inverse values of output voltage and distance almost is, and from that is quite easy to find the formula for converting voltage to distance. To find the formula, the points of the same graph must be inserted to any kind of spreadsheet application and then generate a new graph. In spreadsheet programs is possible to calculate automatically the trend-line. Next, the graph of GP2Y0A21YK corrected output voltage inverse value’s relation to the corrected inverse value of measured distance with linear trend-line is presented. To simplify, the output voltage is already converted to 10 bit +5 V values of analogue-digital converter with comparison voltage. | ||
| - | [{{ : | + | [{{ : |
| As seen on the graph, the trend-line (blue) overlaps quite precisely with the points of the graph. Such overlapping is achieved by using the help of the corrective constant. This corrective constant is discovered by using the trial-and-error method – many variables were tested until such was found which made the graph overlap the trend-line the most. This corrective constant of present graph is +2; this means that to all real distances +2 is added. This way the graph is very similar to the linear trend line and a generalization can be made and | As seen on the graph, the trend-line (blue) overlaps quite precisely with the points of the graph. Such overlapping is achieved by using the help of the corrective constant. This corrective constant is discovered by using the trial-and-error method – many variables were tested until such was found which made the graph overlap the trend-line the most. This corrective constant of present graph is +2; this means that to all real distances +2 is added. This way the graph is very similar to the linear trend line and a generalization can be made and | ||
| Line 55: | Line 58: | ||
| <code c> | <code c> | ||
| - | // | ||
| // The structure of the parameters of the IR distance sensors | // The structure of the parameters of the IR distance sensors | ||
| - | // | ||
| typedef const struct | typedef const struct | ||
| { | { | ||
| Line 66: | Line 67: | ||
| ir_distance_sensor; | ir_distance_sensor; | ||
| - | // | ||
| // The object of the parameters of GP2Y0A21YK sensor | // The object of the parameters of GP2Y0A21YK sensor | ||
| - | // | ||
| const ir_distance_sensor GP2Y0A21YK = { 5461, -17, 2 }; | const ir_distance_sensor GP2Y0A21YK = { 5461, -17, 2 }; | ||
| - | // | ||
| // Converting the values of the IR distance sensor to centimeters | // Converting the values of the IR distance sensor to centimeters | ||
| // Returns -1, if the conversion did not succeed | // Returns -1, if the conversion did not succeed | ||
| - | // | ||
| signed short ir_distance_calculate_cm(ir_distance_sensor sensor, | signed short ir_distance_calculate_cm(ir_distance_sensor sensor, | ||
| unsigned short adc_value) | unsigned short adc_value) | ||
| Line 87: | Line 84: | ||
| </ | </ | ||
| - | To make the conversion the function // | + | To make the conversion the function // |
| <code c> | <code c> | ||
| - | // | ||
| // The example program of the IR distance sensor of the HomeLab | // The example program of the IR distance sensor of the HomeLab | ||
| // Measured results in centimeters is displayed on the LCD | // Measured results in centimeters is displayed on the LCD | ||
| - | // | ||
| #include < | #include < | ||
| #include < | #include < | ||
| #include < | #include < | ||
| #include < | #include < | ||
| - | #include < | + | #include < |
| + | |||
| + | #define ADC_CHANNEL 0 | ||
| - | // | ||
| // Main program | // Main program | ||
| - | // | ||
| int main(void) | int main(void) | ||
| { | { | ||
| - | unsigned | + | signed |
| - | signed short distance; | + | |
| char text[16]; | char text[16]; | ||
| + | |||
| + | // Robotic HomeLab II external sensors pin of Sensor module | ||
| + | //pin ex_sensors = PIN(G, 0); | ||
| + | // | ||
| + | // | ||
| - | // External sensor selection | ||
| - | pin ex_sensors = PIN(G, 0); | ||
| - | pin_setup_output(ex_sensors); | ||
| - | pin_set(ex_sensors); | ||
| - | |||
| // Initialization of LCD | // Initialization of LCD | ||
| - | lcd_alpha_init(LCD_ALPHA_DISP_ON); | + | lcd_gfx_init(); |
| - | + | lcd_gfx_clear(); | |
| - | // Clearing the LCD | + | lcd_gfx_goto_char_xy(1, |
| - | lcd_alpha_clear(); | + | lcd_gfx_write_string(" |
| - | + | ||
| - | // Name of the program | + | |
| - | lcd_alpha_write_string(" | + | |
| // Setup of the ADC | // Setup of the ADC | ||
| Line 127: | Line 118: | ||
| // Endless loop | // Endless loop | ||
| - | while (true) | + | while (1) |
| { | { | ||
| // Reading the 4 times rounded value of output voltage | // Reading the 4 times rounded value of output voltage | ||
| - | value = adc_get_average_value(0, 4); | + | value = adc_get_average_value(ADC_CHANNEL, 4); |
| // Conversing ADC value to distance | // Conversing ADC value to distance | ||
| distance = ir_distance_calculate_cm(GP2Y0A21YK, | distance = ir_distance_calculate_cm(GP2Y0A21YK, | ||
| + | |||
| + | lcd_gfx_goto_char_xy(1, | ||
| // Was the calculation successful? | // Was the calculation successful? | ||
| Line 139: | Line 132: | ||
| { | { | ||
| // Conversing distance to text | // Conversing distance to text | ||
| - | sprintf(text, | + | sprintf(text, |
| } | } | ||
| else | else | ||
| { | { | ||
| // Creating the text for unknown distance | // Creating the text for unknown distance | ||
| - | sprintf(text, | + | sprintf(text, |
| } | } | ||
| - | + | ||
| - | // Displaying the text on the LCD | + | lcd_gfx_goto_char_xy(1,3); |
| - | lcd_alpha_goto_xy(0, 1); | + | lcd_gfx_write_string(text); |
| - | lcd_alpha_write_string(text); | + | |
| - | + | ||
| - | // Break | + | |
| sw_delay_ms(500); | sw_delay_ms(500); | ||
| } | } | ||
| } | } | ||
| </ | </ | ||
| + | /* | ||
| ===== Extra materials ===== | ===== Extra materials ===== | ||
| Line 162: | Line 152: | ||
| * [[http:// | * [[http:// | ||
| + | */ | ||