Advertisement
pleasedontcode

checkObstacle rev_01

Oct 18th, 2023
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: checkObstacle
  13.     - Source Code compiled for: Arduino Nano
  14.     - Source Code created on: 2023-10-18 13:08:42
  15.     - Source Code generated by: Francesco
  16.  
  17. ********* Pleasedontcode.com **********/
  18. /****** DEFINITION OF LIBRARIES *****/
  19. #include <Arduino.h>
  20.  
  21. /****** SYSTEM REQUIREMENT 1 *****/
  22. /* If the sensor identifies an object closer than 10cm, */
  23. /* activate the relay; if the sensor is at least 20cm away, */
  24. /* deactivate the relay. Use a hysteresis of 10 and 20 cm. */
  25.  
  26. /****** FUNCTION PROTOTYPES *****/
  27. void setup(void);
  28. void loop(void);
  29. void updateInputs(void);
  30. void convertInputsFromRawToPhyData(void);
  31.  
  32. /***** DEFINITION OF ANALOG INPUT PINS *****/
  33. const uint8_t obstacleSensor_PIN_A0 = A0;
  34.  
  35. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  36. const uint8_t relay_PIN_D2 = 2;
  37.  
  38. /****** DEFINITION OF ANALOG INPUTS CHARACTERISTIC CURVES *****/
  39. const uint8_t SEGMENT_POINTS_voltage_distance_PIN_A0 = 4;
  40. const float voltage_distance_PIN_A0_lookup[2][SEGMENT_POINTS_voltage_distance_PIN_A0] PROGMEM =
  41. {
  42.     {4.5, 3.8, 2.4, 1.0},   // Voltage [V]
  43.     {10.0, 30.0, 50.0, 200.0}   // distance [cm]
  44. };
  45.  
  46. /***** DEFINITION OF INPUT RAW VARIABLES *****/
  47. /***** used to store raw data *****/
  48. unsigned int obstacleSensor_PIN_A0_rawData = 0; // Analog Input
  49.  
  50. /***** DEFINITION OF INPUT PHYSICAL VARIABLES *****/
  51. /***** used to store data after characteristic curve transformation *****/
  52. float obstacleSensor_PIN_A0_phyData = 0.0; // distance [cm]
  53.  
  54. void setup(void)
  55. {
  56.     // put your setup code here, to run once:
  57.     pinMode(obstacleSensor_PIN_A0, INPUT);
  58.     pinMode(relay_PIN_D2, OUTPUT);
  59. }
  60.  
  61. void loop(void)
  62. {
  63.     // put your main code here, to run repeatedly:
  64.     updateInputs(); // Refresh input data
  65.     convertInputsFromRawToPhyData(); // Transform raw data to physical data
  66.  
  67.     // Check if the obstacle is closer than 10cm
  68.     if (obstacleSensor_PIN_A0_phyData < 10.0)
  69.     {
  70.         digitalWrite(relay_PIN_D2, HIGH); // Activate the relay
  71.     }
  72.     // Check if the obstacle is at least 20cm away
  73.     else if (obstacleSensor_PIN_A0_phyData >= 20.0)
  74.     {
  75.         digitalWrite(relay_PIN_D2, LOW); // Deactivate the relay
  76.     }
  77.  
  78.     delay(100); // Delay for stability
  79. }
  80.  
  81. void updateInputs()
  82. {
  83.     obstacleSensor_PIN_A0_rawData = analogRead(obstacleSensor_PIN_A0);
  84. }
  85.  
  86. float lookup_phyData_from_voltage(float voltage, int segment_points, const float* voltage_phyData_lookup)
  87. {
  88.     // Search table for appropriate value.
  89.     uint8_t index = 0;
  90.  
  91.     const float *voltagePointer = &voltage_phyData_lookup[0];
  92.     const float *phyDataPointer = &voltage_phyData_lookup[segment_points];
  93.  
  94.     // Perform minimum and maximum voltage saturation based on characteristic curve
  95.     voltage = min(voltage, voltagePointer[segment_points - 1]);
  96.     voltage = max(voltage, voltagePointer[0]);
  97.  
  98.     while (pgm_read_float(voltagePointer[index]) <= voltage && index < segment_points)
  99.         index++;
  100.  
  101.     // If index is zero, physical value is smaller than our table range
  102.     if (index == 0)
  103.     {
  104.         return map_f(voltage,
  105.                      pgm_read_float(voltagePointer[0]),   // X1
  106.                      pgm_read_float(voltagePointer[1]),   // X2
  107.                      pgm_read_float(phyDataPointer[0]),   // Y1
  108.                      pgm_read_float(phyDataPointer[1])); // Y2
  109.     }
  110.     // If index is maxed out, physical value is larger than our range.
  111.     else if (index == segment_points)
  112.     {
  113.         return map_f(voltage,
  114.                      pgm_read_float(voltagePointer[segment_points - 2]),   // X1
  115.                      pgm_read_float(voltagePointer[segment_points - 1]),   // X2
  116.                      pgm_read_float(phyDataPointer[segment_points - 2]),   // Y1
  117.                      pgm_read_float(phyDataPointer[segment_points - 1])); // Y2
  118.     }
  119.     // index is between 0 and max, just right
  120.     else
  121.     {
  122.         return map_f(voltage,
  123.                      pgm_read_float(voltagePointer[index - 1]), // X1
  124.                      pgm_read_float(voltagePointer[index]),    // X2
  125.                      pgm_read_float(phyDataPointer[index - 1]), // Y1
  126.                      pgm_read_float(phyDataPointer[index]));  // Y2
  127.     }
  128. }
  129.  
  130. float map_f(float x, float in_min, float in_max, float out_min, float out_max)
  131. {
  132.     return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  133. }
  134.  
  135. void convertInputsFromRawToPhyData()
  136. {
  137.     float voltage = 0.0;
  138.  
  139.     voltage = obstacleSensor_PIN_A0_rawData * (3.3 / 1023.0);
  140.     obstacleSensor_PIN_A0_phyData = lookup_phyData_from_voltage(voltage, SEGMENT_POINTS_voltage_distance_PIN_A0, &(voltage_distance_PIN_A0_lookup[0][0]));
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement