Advertisement
pleasedontcode

"PID Positioning" rev_03

Jun 24th, 2024
442
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: "PID Positioning"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-06-24 14:34:47
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Implement Arduino PID control algorithm to */
  21.     /* maintain ball position within 32 cm beam using */
  22.     /* Ultrasonic sensor and 360-degree Servo motor. */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <Ultrasonic.h>  //https://github.com/ErickSimoes/Ultrasonic
  27. #include <Servo.h>       //https://github.com/arduino-libraries/Servo
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32. void updateOutputs(void);
  33. void computePID(void);
  34.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. const uint8_t Ultrasonic_Sensor_HC_SR04_Echo_PIN_D3 = 3;
  37.  
  38. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  39. const uint8_t Ultrasonic_Sensor_HC_SR04_Trigger_PIN_D2 = 2;
  40.  
  41. /***** DEFINITION OF PWM OUTPUT PINS *****/
  42. const uint8_t servo_Servomotor_PWMSignal_PIN_D5 = 5;
  43.  
  44. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  45. /***** used to store raw data *****/
  46. bool Ultrasonic_Sensor_HC_SR04_Trigger_PIN_D2_rawData = 0;
  47. uint8_t servo_Servomotor_PWMSignal_PIN_D5_rawData = 0;
  48.  
  49. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  50. /***** used to store data after characteristic curve transformation *****/
  51. float Ultrasonic_Sensor_HC_SR04_Trigger_PIN_D2_phyData = 0.0;
  52. float servo_Servomotor_PWMSignal_PIN_D5_phyData = 0.0;
  53.  
  54. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  55. // Initialize the Ultrasonic sensor object with trigger and echo pins
  56. Ultrasonic ultrasonic(Ultrasonic_Sensor_HC_SR04_Trigger_PIN_D2, Ultrasonic_Sensor_HC_SR04_Echo_PIN_D3);
  57.  
  58. // Initialize the Servo object
  59. Servo myservo;
  60.  
  61. /***** PID CONTROL VARIABLES *****/
  62. float setPoint = 32.0; // Desired position in cm
  63. float input, output;
  64. float Kp = 2.0, Ki = 5.0, Kd = 1.0; // PID constants
  65. float previousError = 0, integral = 0;
  66. unsigned long lastTime;
  67.  
  68. void setup(void)
  69. {
  70.     // put your setup code here, to run once:
  71.     Serial.begin(9600);  // Initialize serial communication for debugging
  72.  
  73.     pinMode(Ultrasonic_Sensor_HC_SR04_Echo_PIN_D3, INPUT);
  74.     pinMode(Ultrasonic_Sensor_HC_SR04_Trigger_PIN_D2, OUTPUT);
  75.     pinMode(servo_Servomotor_PWMSignal_PIN_D5, OUTPUT);
  76.  
  77.     // Attach the servo to the PWM pin
  78.     myservo.attach(servo_Servomotor_PWMSignal_PIN_D5);
  79.  
  80.     lastTime = millis();
  81. }
  82.  
  83. void loop(void)
  84. {
  85.     // put your main code here, to run repeatedly:
  86.     updateOutputs(); // Refresh output data
  87.  
  88.     // Read distance from the ultrasonic sensor
  89.     input = ultrasonic.read();
  90.     Serial.print("Distance in CM: ");
  91.     Serial.println(input);
  92.  
  93.     // Compute PID control
  94.     computePID();
  95.  
  96.     // Map the PID output to a servo position (0 to 180 degrees)
  97.     int servoPosition = map(output, 0, 100, 0, 180); // Assuming max distance is 100 cm
  98.     myservo.write(servoPosition); // Set the servo position
  99.  
  100.     delay(100); // Delay for 100 milliseconds
  101. }
  102.  
  103. void updateOutputs()
  104. {
  105.     digitalWrite(Ultrasonic_Sensor_HC_SR04_Trigger_PIN_D2, Ultrasonic_Sensor_HC_SR04_Trigger_PIN_D2_rawData);
  106.     analogWrite(servo_Servomotor_PWMSignal_PIN_D5, servo_Servomotor_PWMSignal_PIN_D5_rawData);
  107. }
  108.  
  109. void computePID()
  110. {
  111.     unsigned long now = millis();
  112.     double timeChange = (double)(now - lastTime);
  113.  
  114.     // Calculate error
  115.     float error = setPoint - input;
  116.  
  117.     // Integral term
  118.     integral += (error * timeChange);
  119.  
  120.     // Derivative term
  121.     float derivative = (error - previousError) / timeChange;
  122.  
  123.     // PID output
  124.     output = Kp * error + Ki * integral + Kd * derivative;
  125.  
  126.     // Remember variables for next loop
  127.     previousError = error;
  128.     lastTime = now;
  129. }
  130.  
  131. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement