servo_align.ino 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. Servo Alignment Script
  3. This script set the servo to installation positions
  4. (as well as help debugging servo wires)
  5. */
  6. #include <ESP32Servo.h> //Require ESP32Servo
  7. #define STP_DATA_PIN 4 // Stepper Shift Register DS
  8. #define STP_CLOCK_PIN 16 // Stepper Shift Register SH_CP
  9. #define STP_LATCH_PIN 17 // Stepper Shift Register ST_CP
  10. #define SERVO_SWITCH 27 //Servo to push the switch
  11. #define SERVO_COVER 14 //Servo to push the cover
  12. #define TOGGLE_SWITCH 13 //Switch on top of the matrix display
  13. #define SERVO_ALIGNMENT_OFFSET 3
  14. /* Hardware Type Definations */
  15. Servo servoSwitchPusher;
  16. Servo servoCoverPusher;
  17. //Get the current state of the switch
  18. // true = After human pushed
  19. // false = After robot pushed
  20. bool getSwitchState() {
  21. int switchState = digitalRead(TOGGLE_SWITCH);
  22. return (switchState == 1);
  23. }
  24. //Push switch back with delay
  25. void pushSwitchDelayed(int coverDelay, int pusherDelay) {
  26. servoCoverPusher.write(90);
  27. delay(coverDelay);
  28. servoSwitchPusher.write(130 + SERVO_ALIGNMENT_OFFSET);
  29. delay(pusherDelay);
  30. servoCoverPusher.write(0);
  31. servoSwitchPusher.write(0);
  32. }
  33. //writeStep write the step given the byte representation of a step on both stepper
  34. //e.g. writeStep(0b00110011);
  35. void writeStep(byte stepByte) {
  36. shiftOut(STP_DATA_PIN, STP_CLOCK_PIN, MSBFIRST, stepByte);
  37. digitalWrite(STP_LATCH_PIN, HIGH);
  38. digitalWrite(STP_LATCH_PIN, LOW);
  39. delayMicroseconds(840); //780 - 800 ms minimum, recommend 840 to 860ms
  40. }
  41. //Set all coils to off and enter standby mode
  42. //to save power and prevent overheating
  43. void standbySteppers() {
  44. writeStep(0b00000000);
  45. }
  46. void setup() {
  47. Serial.begin(115200);
  48. // Allow allocation of all timers
  49. ESP32PWM::allocateTimer(0);
  50. ESP32PWM::allocateTimer(1);
  51. ESP32PWM::allocateTimer(2);
  52. ESP32PWM::allocateTimer(3);
  53. // put your setup code here, to run once:
  54. /* Servo IO */
  55. servoSwitchPusher.setPeriodHertz(50);
  56. servoCoverPusher.setPeriodHertz(50);
  57. servoSwitchPusher.attach(SERVO_SWITCH);
  58. servoCoverPusher.attach(SERVO_COVER);
  59. servoCoverPusher.write(0);
  60. servoSwitchPusher.write(0);
  61. //Set servo pusher to vertical install position
  62. delay(1000);
  63. servoSwitchPusher.write(125);
  64. /* Stepper IO */
  65. pinMode(STP_DATA_PIN, OUTPUT);
  66. pinMode(STP_CLOCK_PIN, OUTPUT);
  67. pinMode(STP_LATCH_PIN, OUTPUT);
  68. standbySteppers();
  69. }
  70. void loop() {
  71. // put your main code here, to run repeatedly:
  72. //Switch test
  73. bool switchPushed = getSwitchState();
  74. if (switchPushed) {
  75. servoSwitchPusher.write(0);
  76. Serial.println("Switch pushed");
  77. delay(1000);
  78. pushSwitchDelayed(300,300);
  79. delay(3000);
  80. servoSwitchPusher.write(125);
  81. } else {
  82. Serial.println("Switch idle");
  83. }
  84. delay(500);
  85. }