Atm_stepper.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "Atm_stepper.h"
  2. /* Add optional parameters for the state machine to begin()
  3. * Add extra initialization code
  4. */
  5. Atm_stepper& Atm_stepper::begin(int stepPin, int dirPin) {
  6. // clang-format off
  7. const static state_t state_table[] PROGMEM = {
  8. /* ON_ENTER ON_LOOP ON_EXIT EVT_IDLE_TIMER EVT_ON_TARGET EVT_GOTO ELSE */
  9. /* IDLE */ ENT_IDLE, -1, -1, -1, -1, RUNNING, -1,
  10. /* READY */ ENT_READY, -1, -1, IDLE, -1, RUNNING, -1,
  11. /* RUNNING */ ENT_RUNNING, LP_RUNNING, -1, -1, STOPPING, RUNNING, -1,
  12. /* STOPPING */ ENT_STOPPING, -1, EXT_STOPPING, -1, READY, -1, -1,
  13. };
  14. // clang-format on
  15. Machine::begin( state_table, ELSE );
  16. AccelStepper _motor (1, stepPin, dirPin);
  17. // Stepper _motor(stepPin, dirPin );
  18. // StepControl _controller;
  19. // _motor.setMaxSpeed(20000);
  20. // _motor.setAcceleration(1000);
  21. //_controller = new StepControl;
  22. pinMode(0, OUTPUT);
  23. digitalWrite(0, HIGH);
  24. return *this;
  25. }
  26. /* Add C++ code for each internally handled event (input)
  27. * The code must return 1 to trigger the event
  28. */
  29. int Atm_stepper::event( int id ) {
  30. switch ( id ) {
  31. case EVT_IDLE_TIMER:
  32. return 0;
  33. case EVT_ON_TARGET:
  34. return _motor.distanceToGo()== 0 ;
  35. }
  36. return 0;
  37. }
  38. /* Add C++ code for each action
  39. * This generates the 'output' for the state machine
  40. *
  41. * Available connectors:
  42. * push( connectors, ON_ONCHANGE, 0, <v>, <up> );
  43. */
  44. void Atm_stepper::action( int id ) {
  45. switch ( id ) {
  46. case ENT_IDLE:
  47. digitalWrite(0, LOW);
  48. return;
  49. case ENT_READY:
  50. digitalWrite(0, HIGH);
  51. return;
  52. case ENT_RUNNING:
  53. digitalWrite(0, HIGH);
  54. // _controller.moveAsync(*_motor);
  55. return;
  56. case LP_RUNNING:
  57. _motor.run();
  58. Serial.println(_motor.currentPosition());
  59. return;
  60. case ENT_STOPPING:
  61. return;
  62. case EXT_STOPPING:
  63. return;
  64. }
  65. }
  66. /* Optionally override the default trigger() method
  67. * Control how your machine processes triggers
  68. */
  69. Atm_stepper& Atm_stepper::trigger( int event ) {
  70. Machine::trigger( event );
  71. return *this;
  72. }
  73. /* Optionally override the default state() method
  74. * Control what the machine returns when another process requests its state
  75. */
  76. int Atm_stepper::state( void ) {
  77. return Machine::state();
  78. }
  79. /* Nothing customizable below this line
  80. ************************************************************************************************
  81. */
  82. /* Public event methods
  83. *
  84. */
  85. Atm_stepper& Atm_stepper::gotoStep(long int targetStep ) {
  86. // _motor->setTargetRel(targetStep);
  87. _motor.move(targetStep);
  88. _motor.run();
  89. Serial.println(_motor.distanceToGo());
  90. _motor.setMaxSpeed(5000);
  91. _motor.setAcceleration(6000);
  92. trigger( EVT_GOTO );
  93. return *this;
  94. }
  95. /*
  96. * onOnchange() push connector variants ( slots 1, autostore 0, broadcast 0 )
  97. */
  98. Atm_stepper& Atm_stepper::onOnchange( Machine& machine, int event ) {
  99. onPush( connectors, ON_ONCHANGE, 0, 1, 1, machine, event );
  100. return *this;
  101. }
  102. Atm_stepper& Atm_stepper::onOnchange( atm_cb_push_t callback, int idx ) {
  103. onPush( connectors, ON_ONCHANGE, 0, 1, 1, callback, idx );
  104. return *this;
  105. }
  106. /* State trace method
  107. * Sets the symbol table and the default logging method for serial monitoring
  108. */
  109. Atm_stepper& Atm_stepper::trace( Stream & stream ) {
  110. Machine::setTrace( &stream, atm_serial_debug::trace,
  111. "STEPPER\0EVT_IDLE_TIMER\0EVT_ON_TARGET\0EVT_GOTO\0ELSE\0IDLE\0READY\0RUNNING\0STOPPING" );
  112. return *this;
  113. }