Atm_AccelStepper.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #include "Atm_AccelStepper.h"
  2. /* Add optional parameters for the state machine to begin()
  3. * Add extra initialization code
  4. */
  5. Atm_AccelStepper& Atm_AccelStepper::begin(int step_pin, int dir_pin) {
  6. // clang-format off
  7. const static state_t state_table[] PROGMEM = {
  8. /* ON_ENTER ON_LOOP ON_EXIT EVT_DISABLE EVT_ENABLE EVT_ENABLED_TIMEOUT EVT_STOP EVT_EMERGENCY_STOP EVT_ON_LIMIT_LOW EVT_ON_LIMIT_HIGH EVT_ON_TARGET ELSE */
  9. /* DISABLED */ ENT_DISABLED, -1, -1, -1, ENABLED, -1, -1, -1, -1, -1, -1, -1,
  10. /* ENABLED */ ENT_ENABLED, -1, -1, DISABLE, -1, DISABLED, STOP, STOP, -1, -1, -1, -1,
  11. /* RUNNING */ -1, LP_RUNNING, -1, DISABLE, -1, -1, STOP, STOP, LIMIT_LOW, LIMIT_HIGH, ENABLED, -1,
  12. /* STOP */ -1, -1, -1, DISABLE, -1, -1, STOP, STOP, -1, -1, -1, -1,
  13. /* HOMING_LOW */ ENT_HOMING_LOW, -1, EXT_HOMING_LOW, DISABLE, -1, -1, STOP, STOP, ENABLED, -1, -1, -1,
  14. /* HOMING_HIGH */ ENT_HOMING_HIGH, -1, EXT_HOMING_HIGH, DISABLE, -1, -1, STOP, STOP, -1, ENABLED, -1, -1,
  15. /* LIMIT_LOW */ ENT_LIMIT_LOW, -1, -1, -1, -1, -1, STOP, STOP, LIMIT_LOW, -1, -1, -1,
  16. /* LIMIT_HIGH */ ENT_LIMIT_HIGH, -1, -1, -1, -1, -1, STOP, STOP, -1, LIMIT_HIGH, -1, -1,
  17. };
  18. // clang-format on
  19. Machine::begin( state_table, ELSE );
  20. stepper = new AccelStepper(1, step_pin, dir_pin);
  21. idle_timer.set(ATM_TIMER_OFF);
  22. return *this;
  23. }
  24. /* Add C++ code for each internally handled event (input)
  25. * The code must return 1 to trigger the event
  26. */
  27. int Atm_AccelStepper::event( int id ) {
  28. //updateLimitSwitch();
  29. switch ( id ) {
  30. case EVT_DISABLE:
  31. return 0;
  32. case EVT_ENABLE:
  33. return 0;
  34. case EVT_ENABLED_TIMEOUT:
  35. return 0;
  36. case EVT_STOP:
  37. return 0;
  38. case EVT_EMERGENCY_STOP:
  39. return 0;
  40. case EVT_ON_LIMIT_LOW:
  41. switch(_limitLow_Mode) {
  42. case 0:
  43. return 0;
  44. case 1: //digital INPUT
  45. limitLow_State = digitalRead(_limitLow_Pin);
  46. limitLow_State = _limitLow_Reversed ? !limitLow_State : limitLow_State;
  47. return limitLow_State;
  48. case 2:
  49. int analogTemp = analogRead(_limitLow_Pin);
  50. limitLow_State = _limitLow_Thresholds[0] < analogTemp && analogTemp < _limitLow_Thresholds[1];
  51. limitLow_State = _limitLow_Reversed ? !limitLow_State : limitLow_State;
  52. return limitLow_State;
  53. }
  54. case EVT_ON_LIMIT_HIGH:
  55. switch(_limitHigh_Mode) {
  56. case 0:
  57. return 0;
  58. case 1: //digital INPUT
  59. limitHigh_State = digitalRead(_limitHigh_Pin);
  60. limitHigh_State = _limitHigh_Reversed ? !limitHigh_State : limitHigh_State;
  61. return limitHigh_State;
  62. case 2:
  63. int analogTemp = analogRead(_limitHigh_Pin);
  64. limitHigh_State = _limitHigh_Thresholds[0] < analogTemp && analogTemp < _limitHigh_Thresholds[1];
  65. limitHigh_State = _limitHigh_Reversed ? !limitHigh_State : limitHigh_State;
  66. return limitHigh_State;
  67. }
  68. case EVT_ON_TARGET:
  69. return _currentStep == _targetStep;;
  70. }
  71. return 0;
  72. }
  73. /* Add C++ code for each action
  74. * This generates the 'output' for the state machine
  75. *
  76. * Available connectors:
  77. * push( connectors, ON_CHANGEPOSITION, 0, <v>, <up> );
  78. * push( connectors, ON_CHANGESTATE, 0, <v>, <up> );
  79. * push( connectors, ON_ONLIMITHIGH, 0, <v>, <up> );
  80. * push( connectors, ON_ONLIMITLOW, 0, <v>, <up> );
  81. * push( connectors, ON_ONTARGET, 0, <v>, <up> );
  82. * push( connectors, ON_STOP, 0, <v>, <up> );
  83. */
  84. void Atm_AccelStepper::action( int id ) {
  85. switch ( id ) {
  86. case ENT_DISABLED:
  87. return;
  88. case ENT_ENABLED:
  89. return;
  90. case LP_RUNNING:
  91. return;
  92. case ENT_HOMING_LOW:
  93. return;
  94. case EXT_HOMING_LOW:
  95. return;
  96. case ENT_HOMING_HIGH:
  97. return;
  98. case EXT_HOMING_HIGH:
  99. return;
  100. case ENT_LIMIT_LOW:
  101. return;
  102. case ENT_LIMIT_HIGH:
  103. return;
  104. }
  105. }
  106. /* Optionally override the default trigger() method
  107. * Control how your machine processes triggers
  108. */
  109. Atm_AccelStepper& Atm_AccelStepper::trigger( int event ) {
  110. Machine::trigger( event );
  111. return *this;
  112. }
  113. /* Optionally override the default state() method
  114. * Control what the machine returns when another process requests its state
  115. */
  116. int Atm_AccelStepper::state( void ) {
  117. return Machine::state();
  118. }
  119. /* Nothing customizable below this line
  120. ************************************************************************************************
  121. */
  122. /* Public event methods
  123. *
  124. */
  125. Atm_AccelStepper& Atm_AccelStepper::disable() {
  126. trigger( EVT_DISABLE );
  127. return *this;
  128. }
  129. Atm_AccelStepper& Atm_AccelStepper::enable() {
  130. trigger( EVT_ENABLE );
  131. return *this;
  132. }
  133. Atm_AccelStepper& Atm_AccelStepper::stop() {
  134. trigger( EVT_STOP );
  135. return *this;
  136. }
  137. Atm_AccelStepper& Atm_AccelStepper::emergency_stop() {
  138. trigger( EVT_EMERGENCY_STOP );
  139. return *this;
  140. }
  141. Atm_AccelStepper& Atm_AccelStepper::on_limit_low() {
  142. trigger( EVT_ON_LIMIT_LOW );
  143. return *this;
  144. }
  145. Atm_AccelStepper& Atm_AccelStepper::on_limit_high() {
  146. trigger( EVT_ON_LIMIT_HIGH );
  147. return *this;
  148. }
  149. Atm_AccelStepper& Atm_AccelStepper::on_target() {
  150. trigger( EVT_ON_TARGET );
  151. return *this;
  152. }
  153. /*
  154. * onChangeposition() push connector variants ( slots 1, autostore 0, broadcast 0 )
  155. */
  156. Atm_AccelStepper& Atm_AccelStepper::onChangeposition( Machine& machine, int event ) {
  157. onPush( connectors, ON_CHANGEPOSITION, 0, 1, 1, machine, event );
  158. return *this;
  159. }
  160. Atm_AccelStepper& Atm_AccelStepper::onChangeposition( atm_cb_push_t callback, int idx ) {
  161. onPush( connectors, ON_CHANGEPOSITION, 0, 1, 1, callback, idx );
  162. return *this;
  163. }
  164. /*
  165. * onChangestate() push connector variants ( slots 1, autostore 0, broadcast 0 )
  166. */
  167. Atm_AccelStepper& Atm_AccelStepper::onChangestate( Machine& machine, int event ) {
  168. onPush( connectors, ON_CHANGESTATE, 0, 1, 1, machine, event );
  169. return *this;
  170. }
  171. Atm_AccelStepper& Atm_AccelStepper::onChangestate( atm_cb_push_t callback, int idx ) {
  172. onPush( connectors, ON_CHANGESTATE, 0, 1, 1, callback, idx );
  173. return *this;
  174. }
  175. /*
  176. * onOnlimithigh() push connector variants ( slots 1, autostore 0, broadcast 0 )
  177. */
  178. Atm_AccelStepper& Atm_AccelStepper::onOnlimithigh( Machine& machine, int event ) {
  179. onPush( connectors, ON_ONLIMITHIGH, 0, 1, 1, machine, event );
  180. return *this;
  181. }
  182. Atm_AccelStepper& Atm_AccelStepper::onOnlimithigh( atm_cb_push_t callback, int idx ) {
  183. onPush( connectors, ON_ONLIMITHIGH, 0, 1, 1, callback, idx );
  184. return *this;
  185. }
  186. /*
  187. * onOnlimitlow() push connector variants ( slots 1, autostore 0, broadcast 0 )
  188. */
  189. Atm_AccelStepper& Atm_AccelStepper::onOnlimitlow( Machine& machine, int event ) {
  190. onPush( connectors, ON_ONLIMITLOW, 0, 1, 1, machine, event );
  191. return *this;
  192. }
  193. Atm_AccelStepper& Atm_AccelStepper::onOnlimitlow( atm_cb_push_t callback, int idx ) {
  194. onPush( connectors, ON_ONLIMITLOW, 0, 1, 1, callback, idx );
  195. return *this;
  196. }
  197. /*
  198. * onOntarget() push connector variants ( slots 1, autostore 0, broadcast 0 )
  199. */
  200. Atm_AccelStepper& Atm_AccelStepper::onOntarget( Machine& machine, int event ) {
  201. onPush( connectors, ON_ONTARGET, 0, 1, 1, machine, event );
  202. return *this;
  203. }
  204. Atm_AccelStepper& Atm_AccelStepper::onOntarget( atm_cb_push_t callback, int idx ) {
  205. onPush( connectors, ON_ONTARGET, 0, 1, 1, callback, idx );
  206. return *this;
  207. }
  208. /*
  209. * onStop() push connector variants ( slots 1, autostore 0, broadcast 0 )
  210. */
  211. Atm_AccelStepper& Atm_AccelStepper::onStop( Machine& machine, int event ) {
  212. onPush( connectors, ON_STOP, 0, 1, 1, machine, event );
  213. return *this;
  214. }
  215. Atm_AccelStepper& Atm_AccelStepper::onStop( atm_cb_push_t callback, int idx ) {
  216. onPush( connectors, ON_STOP, 0, 1, 1, callback, idx );
  217. return *this;
  218. }
  219. /* State trace method
  220. * Sets the symbol table and the default logging method for serial monitoring
  221. */
  222. Atm_AccelStepper& Atm_AccelStepper::trace( Stream & stream ) {
  223. Machine::setTrace( &stream, atm_serial_debug::trace,
  224. "ACCELSTEPPER\0EVT_DISABLE\0EVT_ENABLE\0EVT_ENABLED_TIMEOUT\0EVT_STOP\0EVT_EMERGENCY_STOP\0EVT_ON_LIMIT_LOW\0EVT_ON_LIMIT_HIGH\0EVT_ON_TARGET\0ELSE\0DISABLED\0ENABLED\0RUNNING\0STOP\0HOMING_LOW\0HOMING_HIGH\0LIMIT_LOW\0LIMIT_HIGH" );
  225. return *this;
  226. }