Advertisement
pleasedontcode

test rev_01

Dec 3rd, 2023
58
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: test
  13.     - Source Code compiled for: Arduino Nano
  14.     - Source Code created on: 2023-12-03 10:36:29
  15.     - Source Code generated by: Francesco Alessandro
  16.  
  17. ********* Pleasedontcode.com **********/
  18.  
  19. /****** SYSTEM REQUIREMENTS *****/
  20. /****** SYSTEM REQUIREMENT 1 *****/
  21.     /* read velocity speed and and adjust the servo */
  22.     /* position to maintain the same velocity speed of */
  23.     /* 100km/h. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <Arduino.h>
  28. #include <Servo.h>
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33. void updateInputs(void);
  34. float lookup_phyData_from_voltage(float voltage, int segment_points, const float* voltage_phyData_lookup);
  35. float map_f(float x, float in_min, float in_max, float out_min, float out_max);
  36. void convertInputsFromRawToPhyData(void);
  37.  
  38. /***** DEFINITION OF ANALOG INPUT PINS *****/
  39. const uint8_t pot_Potentiometer_Vout_PIN_A0 = A0;
  40.  
  41. /***** DEFINITION OF PWM OUTPUT PINS *****/
  42. const uint8_t actuator_Servomotor_PWMSignal_PIN_D3 = 3;
  43.  
  44. /****** DEFINITION OF ANALOG INPUTS CHARACTERISTIC CURVES *****/
  45. const uint8_t SEGMENT_POINTS_voltage_velocity_PIN_A0 = 5;
  46. const float voltage_velocity_PIN_A0_lookup[2][SEGMENT_POINTS_voltage_velocity_PIN_A0] =
  47. {
  48.   {0.2, 0.8, 2.0, 3.0, 4.0}, //Voltage [V]
  49.   {10.0, 30.0, 60.0, 100.0, 250.0} //velocity [km/h]
  50. };
  51.  
  52.  
  53. /***** DEFINITION OF INPUT RAW VARIABLES *****/
  54. /***** used to store raw data *****/
  55. unsigned int pot_Potentiometer_Vout_PIN_A0_rawData = 0; // Analog Input
  56.  
  57. /***** DEFINITION OF INPUT PHYSICAL VARIABLES *****/
  58. /***** used to store data after characteristic curve transformation *****/
  59. float pot_Potentiometer_Vout_PIN_A0_phyData = 0.0; // velocity [km/h]
  60.  
  61. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  62. Servo myservo;
  63.  
  64. void setup(void)
  65. {
  66.   // put your setup code here, to run once:
  67.   myservo.attach(actuator_Servomotor_PWMSignal_PIN_D3);
  68.  
  69.   pinMode(pot_Potentiometer_Vout_PIN_A0, INPUT);
  70. }
  71.  
  72. void loop(void)
  73. {
  74.   // put your main code here, to run repeatedly:
  75.   updateInputs(); // Refresh input data
  76.  
  77.   convertInputsFromRawToPhyData(); // after that updateInput function is called, so raw data are transformed in physical data in according to characteristic curve
  78.  
  79.   // Write the physical data to the servo
  80.   myservo.write(pot_Potentiometer_Vout_PIN_A0_phyData);
  81.  
  82.   delay(15);
  83. }
  84.  
  85. void updateInputs()
  86. {
  87.   pot_Potentiometer_Vout_PIN_A0_rawData = analogRead(pot_Potentiometer_Vout_PIN_A0);
  88. }
  89.  
  90. /* BLOCK lookup_phyData_from_voltage */
  91. float lookup_phyData_from_voltage(float voltage, int segment_points, const float* voltage_phyData_lookup)
  92. {
  93.     // Search table for appropriate value.
  94.     uint8_t index = 0;
  95.  
  96.     const float *voltagePointer = &voltage_phyData_lookup[0];
  97.     const float *phyDataPointer = &voltage_phyData_lookup[segment_points];
  98.  
  99.     // Perform minimum and maximum voltage saturation based on characteristic curve
  100.     voltage = min(voltage, voltagePointer[segment_points-1]);
  101.     voltage = max(voltage, voltagePointer[0]);
  102.  
  103.     while( voltagePointer[index] <= voltage && index < segment_points )
  104.         index++;
  105.  
  106.     // If index is zero, physical value is smaller than our table range
  107.     if( index==0 )
  108.     {
  109.         return map_f( voltage,
  110.             voltagePointer[0],   // X1
  111.             voltagePointer[1],   // X2
  112.             phyDataPointer[0],   // Y1
  113.             phyDataPointer[1] ); // Y2
  114.     }
  115.     // If index is maxed out, phyisical value is larger than our range.
  116.     else if( index==segment_points )
  117.     {
  118.         return map_f( voltage,
  119.             voltagePointer[segment_points-2],   // X1
  120.             voltagePointer[segment_points-1],   // X2
  121.             phyDataPointer[segment_points-2],   // Y1
  122.             phyDataPointer[segment_points-1] ); // Y2
  123.     }
  124.     // index is between 0 and max, just right
  125.     else
  126.     {
  127.         return map_f( voltage,
  128.             voltagePointer[index-1], // X1
  129.             voltagePointer[index],    // X2
  130.             phyDataPointer[index-1], // Y1
  131.             phyDataPointer[index] );  // Y2
  132.     }
  133. }
  134. /* END BLOCK lookup_phyData_from_voltage */
  135.  
  136. /* BLOCK map_f */
  137. float map_f(float x, float in_min, float in_max, float out_min, float out_max)
  138. {
  139.     return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  140. }
  141. /* END BLOCK map_f */
  142.  
  143. /* BLOCK convertInputsFromRawToPhyData */
  144. void convertInputsFromRawToPhyData()
  145. {
  146.     float voltage = 0.0;
  147.  
  148.     voltage = pot_Potentiometer_Vout_PIN_A0_rawData * (3.3 / 1023.0);
  149.     pot_Potentiometer_Vout_PIN_A0_phyData = lookup_phyData_from_voltage(voltage, SEGMENT_POINTS_voltage_velocity_PIN_A0, &(voltage_velocity_PIN_A0_lookup[0][0]));
  150.  
  151. }
  152. /* END BLOCK convertInputsFromRawToPhyData */
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement