Advertisement
pleasedontcode

"Servo Control" rev_01

Nov 29th, 2024
69
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: "Servo Control"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-11-29 09:29:57
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Fix the code */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /* START CODE */
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <Servo.h>  //https://github.com/arduino-libraries/Servo
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31.  
  32. /***** DEFINITION OF PWM OUTPUT PINS *****/
  33. const uint8_t Base_Servomotor_PWMSignal_PIN_D3      = 3;
  34. const uint8_t Braccio1_Servomotor_PWMSignal_PIN_D5      = 5;
  35. const uint8_t Pinza_Servomotor_PWMSignal_PIN_D6     = 6;
  36. const uint8_t Polso_Servomotor_PWMSignal_PIN_D9     = 9;
  37. const uint8_t Rotazione_Servomotor_PWMSignal_PIN_D10        = 10;
  38.  
  39. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  40. uint8_t Base_Servomotor_PWMSignal_PIN_D3_rawData        = 0;
  41. uint8_t Braccio1_Servomotor_PWMSignal_PIN_D5_rawData        = 0;
  42. uint8_t Pinza_Servomotor_PWMSignal_PIN_D6_rawData       = 0;
  43. uint8_t Polso_Servomotor_PWMSignal_PIN_D9_rawData       = 0;
  44. uint8_t Rotazione_Servomotor_PWMSignal_PIN_D10_rawData      = 0;
  45.  
  46. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  47. float   Base_Servomotor_PWMSignal_PIN_D3_phyData        = 0.0;
  48. float   Braccio1_Servomotor_PWMSignal_PIN_D5_phyData        = 0.0;
  49. float   Pinza_Servomotor_PWMSignal_PIN_D6_phyData       = 0.0;
  50. float   Polso_Servomotor_PWMSignal_PIN_D9_phyData       = 0.0;
  51. float   Rotazione_Servomotor_PWMSignal_PIN_D10_phyData      = 0.0;
  52.  
  53. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  54.  
  55. // Creating servo objects
  56. Servo servoBase;
  57. Servo servoBraccio1;
  58. Servo servoPolso;
  59. Servo servoPinza;
  60.  
  61. // Defining PWM pins for the servos
  62. #define PIN_SERVO_BASE 3
  63. #define PIN_SERVO_BRACCIO1 5
  64. #define PIN_SERVO_POLSO 6
  65. #define PIN_SERVO_PINZA 10
  66.  
  67. // Initial positions of the servos (in degrees)
  68. int posizioneBase = 90;
  69. int posizioneBraccio1 = 120;
  70. int posizionePolso = 115;
  71. int posizionePinza = 145;
  72.  
  73. // Function to constrain angle values
  74. int constrainAngle(int angolo, int minimo, int massimo) {
  75.   return constrain(angolo, minimo, massimo);
  76. }
  77.  
  78. void setup(void)
  79. {
  80.     // put your setup code here, to run once:
  81.  
  82.     pinMode(Base_Servomotor_PWMSignal_PIN_D3,    OUTPUT);
  83.     pinMode(Braccio1_Servomotor_PWMSignal_PIN_D5,    OUTPUT);
  84.     pinMode(Pinza_Servomotor_PWMSignal_PIN_D6,   OUTPUT);
  85.     pinMode(Polso_Servomotor_PWMSignal_PIN_D9,   OUTPUT);
  86.     pinMode(Rotazione_Servomotor_PWMSignal_PIN_D10,  OUTPUT);
  87.  
  88.     Serial.begin(9600);
  89.  
  90.     // Associating servos to their respective pins
  91.     servoBase.attach(PIN_SERVO_BASE);
  92.     servoBraccio1.attach(PIN_SERVO_BRACCIO1);
  93.     servoPolso.attach(PIN_SERVO_POLSO);
  94.     servoPinza.attach(PIN_SERVO_PINZA);
  95.  
  96.     // Initial positioning of the servos
  97.     servoBase.write(posizioneBase);
  98.     servoBraccio1.write(posizioneBraccio1);
  99.     servoPolso.write(posizionePolso);
  100.     servoPinza.write(posizionePinza);
  101.  
  102.     Serial.println("Sistema pronto. Usa i tasti per controllare i servo:");
  103.     Serial.println("q/a - Base");
  104.     Serial.println("w/s - Braccio 1");
  105.     Serial.println("e/d - Polso");
  106.     Serial.println("t/g - Pinza");
  107. }
  108.  
  109. void loop(void)
  110. {
  111.     // put your main code here, to run repeatedly:
  112.     updateOutputs(); // Refresh output data
  113.  
  114.     // Check if a command has been received via serial
  115.     if (Serial.available() > 0) {
  116.         char comando = Serial.read();
  117.  
  118.         // Update the servo position based on the received command
  119.         switch (comando) {
  120.             case 'q':
  121.                 posizioneBase = constrainAngle(posizioneBase + 2, 0, 180);
  122.                 break;
  123.             case 'a':
  124.                 posizioneBase = constrainAngle(posizioneBase - 2, 0, 180);
  125.                 break;
  126.             case 'w':
  127.                 posizioneBraccio1 = constrainAngle(posizioneBraccio1 + 2, 100, 140);
  128.                 break;
  129.             case 's':
  130.                 posizioneBraccio1 = constrainAngle(posizioneBraccio1 - 2, 100, 140);
  131.                 break;
  132.             case 'e':
  133.                 posizionePolso = constrainAngle(posizionePolso + 2, 100, 130);
  134.                 break;
  135.             case 'd':
  136.                 posizionePolso = constrainAngle(posizionePolso - 2, 100, 130);
  137.                 break;
  138.             case 't':
  139.                 posizionePinza = constrainAngle(posizionePinza + 2, 120, 175);
  140.                 break;
  141.             case 'g':
  142.                 posizionePinza = constrainAngle(posizionePinza - 2, 120, 175);
  143.                 break;
  144.             default:
  145.                 Serial.println("Comando non valido. Usa i tasti indicati.");
  146.                 break;
  147.         }
  148.  
  149.         // Update the servos to the new positions
  150.         servoBase.write(posizioneBase);
  151.         servoBraccio1.write(posizioneBraccio1);
  152.         servoPolso.write(posizionePolso);
  153.         servoPinza.write(posizionePinza);
  154.  
  155.         // Print the new servo position for monitoring
  156.         Serial.print("Base: ");
  157.         Serial.print(posizioneBase);
  158.         Serial.print(" | Braccio1: ");
  159.         Serial.print(posizioneBraccio1);
  160.         Serial.print(" | Polso: ");
  161.         Serial.print(posizionePolso);
  162.         Serial.print(" | Pinza: ");
  163.         Serial.println(posizionePinza);
  164.     }
  165. }
  166.  
  167. void updateOutputs()
  168. {
  169.     analogWrite(Base_Servomotor_PWMSignal_PIN_D3, Base_Servomotor_PWMSignal_PIN_D3_rawData);
  170.     analogWrite(Braccio1_Servomotor_PWMSignal_PIN_D5, Braccio1_Servomotor_PWMSignal_PIN_D5_rawData);
  171.     analogWrite(Pinza_Servomotor_PWMSignal_PIN_D6, Pinza_Servomotor_PWMSignal_PIN_D6_rawData);
  172.     analogWrite(Polso_Servomotor_PWMSignal_PIN_D9, Polso_Servomotor_PWMSignal_PIN_D9_rawData);
  173.     analogWrite(Rotazione_Servomotor_PWMSignal_PIN_D10, Rotazione_Servomotor_PWMSignal_PIN_D10_rawData);
  174. }
  175.  
  176. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement