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 [2015/11/10 10:41] heikopikneren: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: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]]//+//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 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 90: Line 87:
  
 <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>
Line 100: Line 95:
 #include <homelab/module/lcd_gfx.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);
   
- // 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_gfx_init(); + lcd_gfx_init();  
-  + lcd_gfx_clear();  
- // Clearing the LCD + lcd_gfx_goto_char_xy(1,2);
- lcd_gfx_clear(); +
- +
- // Line selection +
- lcd_gfx_goto_char_xy(1,2); +
-  +
- // Name of the program+
  lcd_gfx_write_string("Distance sensor");  lcd_gfx_write_string("Distance sensor");
    
Line 130: 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, value);  distance = ir_distance_calculate_cm(GP2Y0A21YK, value);
 +
 +                lcd_gfx_goto_char_xy(1,3);
  
  // Was the calculation successful?  // Was the calculation successful?
Line 142: 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 on the LCD+
  lcd_gfx_goto_char_xy(1,3);  lcd_gfx_goto_char_xy(1,3);
  lcd_gfx_write_string(text);  lcd_gfx_write_string(text);
- 
- // Break 
  sw_delay_ms(500);  sw_delay_ms(500);
  }  }
 } }
 </code> </code>
 +/*
 ===== Extra materials ===== ===== Extra materials =====
  
   * {{:examples:sensor:ir_distance:ir_distance.ods|Graph of the Sharp GP2Y0A21YK sensor}}   * {{:examples:sensor:ir_distance:ir_distance.ods|Graph of the Sharp GP2Y0A21YK sensor}}
   * [[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.1447152119.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