Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:examples:sensor:ir_distance [2010/04/13 06:38] raivo.sellen: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: [HW] [[en:hardware:homelab:sensor]], [HW] [[en:hardware:homelab:lcd]], [AVR] [[en:avr:adc]], [LIB] [[en:software:homelab:library:adc]], [LIB] [[en:software:homelab:library:module:lcd_alphanumeric]], [LIB] [[en:software:homelab:library:module:sensor]]//+//Necessary knowledge:  
 +[HW] [[en:hardware:homelab:digi]],  
 +[AVR] [[en:avr:adc]], \\ 
 +[LIB] [[en:software:homelab:library:adc]], [LIB] [[en:software:homelab:library:module:lcd_graphic]], [LIB] [[en:software:homelab:library:module:sensor]]//
  
 ===== Theory ===== ===== Theory =====
Line 10: Line 13:
  
 [{{  :examples:sensor:ir_distance:sensor_ir_distance_principle.png?240|The route of the light beam from an IR distance sensor}}] [{{  :examples:sensor:ir_distance:sensor_ir_distance_principle.png?240|The route of the light beam from an IR distance sensor}}]
- 
-[{{  :examples:sensor:ir_distance:sensor_ir_distance_graph.png?240|Diagram of voltage-distance of IR distance sensor}}] 
  
 The output of distance sensors by "Sharp" is inversely proportional, this means that when the distance is growing the output is decreasing (decreasing is gradually slowing). Exact graph of the relation between distance and output is usually on the data-sheet of the sensor. All sensors have their specific measuring range where the measured results are creditable and this range depends on the type of the sensor. Maximum distance measured is restricted by two aspects: the amount of reflected light is decreasing and inability of the PSD registering the small changes of the location of the reflected ray. When measuring objects which are too far, the output remains approximately the same as it is when measuring the objects at the maximum distance. Minimum distance is restricted due to peculiarity of Sharp sensors, meaning the output starts to decrease (again) sharply as the distance is at certain point (depending on the model 4-20 cm). This means that to one value of the output corresponds two values of distance. This problem can be avoided by noticing that the object is not too close to the sensor. The output of distance sensors by "Sharp" is inversely proportional, this means that when the distance is growing the output is decreasing (decreasing is gradually slowing). Exact graph of the relation between distance and output is usually on the data-sheet of the sensor. All sensors have their specific measuring range where the measured results are creditable and this range depends on the type of the sensor. Maximum distance measured is restricted by two aspects: the amount of reflected light is decreasing and inability of the PSD registering the small changes of the location of the reflected ray. When measuring objects which are too far, the output remains approximately the same as it is when measuring the objects at the maximum distance. Minimum distance is restricted due to peculiarity of Sharp sensors, meaning the output starts to decrease (again) sharply as the distance is at certain point (depending on the model 4-20 cm). This means that to one value of the output corresponds two values of distance. This problem can be avoided by noticing that the object is not too close to the sensor.
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 is connected to the Sensor module. Its output voltage is sent to the channel 0 of the analogue-digital converter of the AVR. On the basis of previous exercises of sensors, it is easy to write a program which measures the output voltage of the distance sensors, but in addition, this exercise includes converting this output voltage to distance.  +[{{  :examples:sensor:ir_distance:sensor_ir_distance_graph.png?240|Diagram of voltage-distance of IR 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 can be connected to any ADC (the analogue-digital converter) channel of the HomeLab module. On the basis of previous exercises of sensors, it is easy to write a program which measures the output voltage of the distance sensors, but in addition, this exercise includes converting this output voltage to distance.  
  
 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.  
  
-[{{  :examples:sensor:ir_distance:sensor_ir_distance_gp2y0a21yk_graph_calculation.png?580  |The graph of linearizing ADC value and distance}}]+[{{  :examples:sensor:ir_distance:sensor_ir_distance_gp2y0a21yk_graph_calculation.png?400|The graph of linearizing ADC value and distance}}]
  
 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:
 </code> </code>
  
-To make the conversion the function //ir_distance_calculate_cm// must be engaged. The first parameter of this function is the object of the parameters of the IR distance sensor, second is the value of the ADC. The function returns the calculated distance in centimeters. If the operation is wrong (unnatural value of the ADC) the returned value is -1.  Following program demonstrates the use of IR distance sensor and conversion function. Alphanumeric LCD is used, where measured results are displayed. If the distance is unnatural “?” is displayed.   +To make the conversion the function //ir_distance_calculate_cm// must be engaged. The first parameter of this function is the object of the parameters of the IR distance sensor, second is the value of the ADC. The function returns the calculated distance in centimeters. If the operation is wrong (unnatural value of the ADC) the returned value is -1.  Following program demonstrates the use of IR distance sensor and conversion function. Graphical LCD is used, where measured results are displayed. If the distance is unnatural “?” is displayed.   
  
 <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 <stdio.h> #include <stdio.h>
 #include <homelab/adc.h> #include <homelab/adc.h>
 #include <homelab/delay.h> #include <homelab/delay.h>
 #include <homelab/module/sensors.h> #include <homelab/module/sensors.h>
-#include <homelab/module/lcd_alpha.h>+#include <homelab/module/lcd_gfx.h> 
 + 
 +#define ADC_CHANNEL 0
  
-// 
 // Main program // Main program
-// 
 int main(void) int main(void)
 {  {
- unsigned short value+ signed short valuedistance;
- signed short distance; +
  char text[16];  char text[16];
- + 
 + // Robotic HomeLab II external sensors pin of Sensor module  
 + //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,2);  
- lcd_alpha_clear(); + lcd_gfx_write_string("Distance sensor");
-  +
- // Name of the program +
- lcd_alpha_write_string("Distance sensor");+
    
  // Setup of the ADC  // Setup of the ADC
Line 122: Line 118:
    
  // Endless loop  // Endless loop
- while (true)+ while (1)
  {  {
- // Reading the 4 times rounded value of the output voltage of the sensor + // 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, value);  distance = ir_distance_calculate_cm(GP2Y0A21YK, value);
 +
 +                lcd_gfx_goto_char_xy(1,3);
  
  // Was the calculation successful?  // Was the calculation successful?
Line 134: Line 132:
  {   {
  // Conversing distance to text  // Conversing distance to text
- sprintf(text, "%cm   ", distance);+ sprintf(text, "%3d cm   ", distance);
  }  }
  else  else
  {   {
  // Creating the text for unknown distance  // Creating the text for unknown distance
- sprintf(text, "? cm   ");+ sprintf(text, "Error   ");
  }  }
- +  
- // Displaying the text in the beginning of the second row on the LCD + lcd_gfx_goto_char_xy(1,3); 
- lcd_alpha_goto_xy(01); + lcd_gfx_write_string(text);
- lcd_alpha_write_string(text); +
- +
- // Break+
  sw_delay_ms(500);  sw_delay_ms(500);
  }  }
 } }
 </code> </code>
 +/*
 ===== Extra materials ===== ===== Extra materials =====
  
Line 157: Line 152:
   * [[http://www.acroname.com/robotics/info/articles/irlinear/irlinear.html|Linearizing Sharp Ranger Data]]   * [[http://www.acroname.com/robotics/info/articles/irlinear/irlinear.html|Linearizing Sharp Ranger Data]]
  
 +*/
en/examples/sensor/ir_distance.1271140729.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0