Interfacing.ino 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*----------------------------------------------------------
  2. This example demonstrates how to use the serial interface and
  3. digital IO while the controller moves a motor in
  4. the background.
  5. The following serial commands are implemented
  6. m: move motor
  7. s: start stop sequence
  8. e: emergency stop
  9. h: help
  10. Additionally a pin can be used to stop the motor
  11. ------------------------------------------------------------*/
  12. #include "TeensyStep.h"
  13. // stepper and controller
  14. constexpr int stpPin = 0, dirPin = 1;
  15. Stepper motor(stpPin, dirPin);
  16. StepControl controller;
  17. // pin to stop the motor, connect a push button to this pin
  18. constexpr int stopPin = 2;
  19. // stopwatches
  20. elapsedMillis displayStopwatch = 0; // timing the display of the current position
  21. elapsedMillis blinkStopwatch = 0; // timing the heartbeat LED
  22. elapsedMillis debounceTimer = 0; // debouncing input pins
  23. int lastPos = 0;
  24. void handlePins();
  25. void handleCommands();
  26. void setup()
  27. {
  28. while (!Serial);
  29. Serial.println("Simple Serial Stepper Example");
  30. Serial.println("(type h for help)");
  31. motor.setMaxSpeed(5000);
  32. motor.setAcceleration(50000);
  33. pinMode(LED_BUILTIN, OUTPUT);
  34. pinMode(stopPin, INPUT_PULLUP); // touch the pin with GND to stop the motor
  35. }
  36. void loop()
  37. {
  38. // handle incomming commands on the serial interface ------------------
  39. handleCommands();
  40. // handle input from pins ---------------------------------------------
  41. handlePins();
  42. // display the current motor position every 20ms ----------------------
  43. if (displayStopwatch > 20)
  44. {
  45. displayStopwatch = 0;
  46. int currentPos = motor.getPosition();
  47. if (currentPos != lastPos) // only display if it changed
  48. {
  49. lastPos = currentPos;
  50. Serial.println(currentPos);
  51. }
  52. }
  53. // the usual heartbeat ------------------------------------------------
  54. if (blinkStopwatch > 250)
  55. {
  56. blinkStopwatch = 0;
  57. digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN)); // toggle LED
  58. }
  59. }
  60. //------------------------------------------------
  61. // Very simple command interface on USB-Serial:
  62. // m : starts the motor
  63. // s : starts stop sequence
  64. // e : emergency stop
  65. void handleCommands()
  66. {
  67. if (Serial.available() > 0) // skip if the serial buffer is empty
  68. {
  69. char cmd = Serial.read(); // get one char from the buffer...
  70. switch (cmd) // ... and analyze it
  71. {
  72. case 'm': // move command
  73. if (!controller.isRunning()) // skip move command if motor is running already
  74. {
  75. motor.setTargetRel(20000);
  76. controller.moveAsync(motor);
  77. Serial.println("Started motor movement");
  78. }
  79. else
  80. {
  81. Serial.println("Ignored, motor is already running");
  82. }
  83. break;
  84. case 's': // stop command
  85. controller.stopAsync(); // initiate stopping procedure
  86. Serial.println("Stopping motor");
  87. break;
  88. case 'e': // emergency stop command
  89. controller.emergencyStop();
  90. Serial.println("Emergency Stop");
  91. break;
  92. case 'h': // help / usage command
  93. case 'u':
  94. Serial.println("\nUsage:");
  95. Serial.println(" m: move motor");
  96. Serial.println(" s: start stop sequence");
  97. Serial.println(" e: emergency stop");
  98. Serial.println(" h: display this help");
  99. break;
  100. default:
  101. break;
  102. }
  103. }
  104. }
  105. //------------------------------------------------
  106. // Connect a pushbutton to the stop pin.
  107. // Stop command will be issued when pin is
  108. // pulled to GND.
  109. // Only very simple debouncing implemented. Use "debounce2.h" or
  110. // similar for a real application
  111. void handlePins()
  112. {
  113. if (controller.isRunning() && !digitalReadFast(stopPin) && debounceTimer > 200)
  114. {
  115. debounceTimer = 0;
  116. controller.stopAsync(); // initiate stopping procedure
  117. Serial.println("Stopping motor");
  118. }
  119. }