Advertisement
pleasedontcode

test rev_03

Dec 3rd, 2023
61
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:50:30
  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.  
  27. /********* User code review feedback **********
  28. #### Feedback 1 ####
  29. - you are not using 100km/h target to correct servo position.
  30. #### Feedback 2 ####
  31. - you have to adjust velocity speed read from potentiometer and gi
  32. ve a correction on servo base on target speed of 100km/h.
  33. ********* User code review feedback **********/
  34.  
  35. /****** DEFINITION OF LIBRARIES *****/
  36. #include <Servo.h>
  37.  
  38. /****** FUNCTION PROTOTYPES *****/
  39. void setup(void);
  40. void loop(void);
  41. void updateInputs(void);
  42. float lookupServoPosition(float velocity);
  43. float map_f(float x, float in_min, float in_max, float out_min, float out_max);
  44. void convertInputsFromRawToPhyData(void);
  45. float lookup_phyData_from_voltage(float voltage, uint8_t segment_points, const float* lookup_table);
  46.  
  47. /***** DEFINITION OF ANALOG INPUT PINS *****/
  48. const uint8_t pot_Potentiometer_Vout_PIN_A0 = A0;
  49.  
  50. /***** DEFINITION OF PWM OUTPUT PINS *****/
  51. const uint8_t actuator_Servomotor_PWMSignal_PIN_D3 = 9;
  52.  
  53. /****** DEFINITION OF ANALOG INPUTS CHARACTERISTIC CURVES *****/
  54. const uint8_t SEGMENT_POINTS_voltage_velocity_PIN_A0 = 5;
  55. const float voltage_velocity_PIN_A0_lookup[2][SEGMENT_POINTS_voltage_velocity_PIN_A0] =
  56. {
  57.   {0.2, 0.8, 2.0, 3.0, 4.0}, //Voltage [V]
  58.   {10.0, 30.0, 60.0, 100.0, 250.0} //velocity [km/h]
  59. };
  60.  
  61.  
  62. /***** DEFINITION OF INPUT RAW VARIABLES *****/
  63. /***** used to store raw data *****/
  64. unsigned int pot_Potentiometer_Vout_PIN_A0_rawData = 0; // Analog Input
  65.  
  66. /***** DEFINITION OF INPUT PHYSICAL VARIABLES *****/
  67. /***** used to store data after characteristic curve transformation *****/
  68. float pot_Potentiometer_Vout_PIN_A0_phyData = 0.0; // velocity [km/h]
  69.  
  70. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  71. Servo myservo;
  72.  
  73. void setup(void)
  74. {
  75.   // put your setup code here, to run once:
  76.   myservo.attach(actuator_Servomotor_PWMSignal_PIN_D3);
  77.  
  78.   pinMode(pot_Potentiometer_Vout_PIN_A0, INPUT);
  79. }
  80.  
  81. void loop(void)
  82. {
  83.   // put your main code here, to run repeatedly:
  84.   updateInputs(); // Refresh input data
  85.  
  86.   convertInputsFromRawToPhyData(); // after that updateInput function is called, so raw data are transformed in physical data in according to characteristic curve
  87.  
  88.   // Calculate servo position based on velocity
  89.   float targetVelocity = 100.0; // Target velocity speed in km/h
  90.   float targetPosition = lookupServoPosition(targetVelocity);
  91.  
  92.   // Write the target position to the servo
  93.   myservo.write(targetPosition);
  94.  
  95.   delay(15);
  96. }
  97.  
  98. void updateInputs()
  99. {
  100.   pot_Potentiometer_Vout_PIN_A0_rawData = analogRead(pot_Potentiometer_Vout_PIN_A0);
  101. }
  102.  
  103. /* BLOCK lookupServoPosition */
  104. float lookupServoPosition(float velocity)
  105. {
  106.   // Search table for appropriate value.
  107.   uint8_t index = 0;
  108.  
  109.   const float *velocityPointer = &voltage_velocity_PIN_A0_lookup[1][0];
  110.   const float *positionPointer = &voltage_velocity_PIN_A0_lookup[0][0];
  111.  
  112.   // Perform minimum and maximum velocity saturation based on characteristic curve
  113.   velocity = min(velocity, velocityPointer[SEGMENT_POINTS_voltage_velocity_PIN_A0-1]);
  114.   velocity = max(velocity, velocityPointer[0]);
  115.  
  116.   while( velocityPointer[index] <= velocity && index < SEGMENT_POINTS_voltage_velocity_PIN_A0 )
  117.     index++;
  118.  
  119.   // If index is zero, velocity value is smaller than our table range
  120.   if( index == 0 )
  121.   {
  122.     return map_f(velocity,
  123.       velocityPointer[0],   // X1
  124.       velocityPointer[1],   // X2
  125.       positionPointer[0],   // Y1
  126.       positionPointer[1]);  // Y2
  127.   }
  128.   // If index is maxed out, velocity value is larger than our range.
  129.   else if( index == SEGMENT_POINTS_voltage_velocity_PIN_A0 )
  130.   {
  131.     return map_f(velocity,
  132.       velocityPointer[SEGMENT_POINTS_voltage_velocity_PIN_A0-2],   // X1
  133.       velocityPointer[SEGMENT_POINTS_voltage_velocity_PIN_A0-1],   // X2
  134.       positionPointer[SEGMENT_POINTS_voltage_velocity_PIN_A0-2],   // Y1
  135.       positionPointer[SEGMENT_POINTS_voltage_velocity_PIN_A0-1]);  // Y2
  136.   }
  137.   // index is between 0 and max, just right
  138.   else
  139.   {
  140.     return map_f(velocity,
  141.       velocityPointer[index-1], // X1
  142.       velocityPointer[index],   // X2
  143.       positionPointer[index-1], // Y1
  144.       positionPointer[index]);  // Y2
  145.   }
  146. }
  147. /* END BLOCK lookupServoPosition */
  148.  
  149. /* BLOCK map_f */
  150. float map_f(float x, float in_min, float in_max, float out_min, float out_max)
  151. {
  152.     return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  153. }
  154. /* END BLOCK map_f */
  155.  
  156. /* BLOCK convertInputsFromRawToPhyData */
  157. void convertInputsFromRawToPhyData()
  158. {
  159.     float voltage = 0.0;
  160.  
  161.     voltage = pot_Potentiometer_Vout_PIN_A0_rawData * (3.3 / 1023.0);
  162.     pot_Potentiometer_Vout_PIN_A0_phyData = lookup_phyData_from_voltage(voltage, SEGMENT_POINTS_voltage_velocity_PIN_A0, &(voltage_velocity_PIN_A0_lookup[0][0]));
  163.  
  164. }
  165. /* END BLOCK convertInputsFromRawToPhyData */
  166.  
  167. float lookup_phyData_from_voltage(float voltage, uint8_t segment_points, const float* lookup_table)
  168. {
  169.   // Implement the lookup function here
  170.   // ...
  171.   return 0.0;
  172. }
  173.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement