Advertisement
pleasedontcode

test rev_02

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