Atm_AccelStepper.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 */ ENT_RUNNING, 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. push(connectors, ON_CHANGE, 0, state(), 0);
  88. enabled = _enableReversed ? HIGH : LOW;
  89. digitalWrite(_enablePin, enabled);
  90. return;
  91. case ENT_ENABLED:
  92. push(connectors, ON_CHANGE, 0, state(), 0);
  93. enabled = _enableReversed ? LOW : HIGH ;
  94. digitalWrite(_enablePin, enabled);
  95. sendOSC();
  96. return;
  97. case ENT_RUNNING:
  98. push(connectors, ON_CHANGE, 0, state(), 0);
  99. push(connectors, ON_CHANGE, 0, 2, 0);
  100. return;
  101. case LP_RUNNING:
  102. tempStep = motor->getPosition();
  103. if (tempStep != _currentStep){
  104. _currentStep = tempStep;
  105. push(connectors, ON_CHANGEPOSITION, 0, _currentStep, 0);
  106. }
  107. case ENT_HOMING_LOW:
  108. return;
  109. case EXT_HOMING_LOW:
  110. return;
  111. case ENT_HOMING_HIGH:
  112. return;
  113. case EXT_HOMING_HIGH:
  114. return;
  115. case ENT_LIMIT_LOW:
  116. return;
  117. case ENT_LIMIT_HIGH:
  118. return;
  119. }
  120. }
  121. /* Optionally override the default trigger() method
  122. * Control how your machine processes triggers
  123. */
  124. Atm_AccelStepper& Atm_AccelStepper::trigger( int event ) {
  125. Machine::trigger( event );
  126. return *this;
  127. }
  128. /* Optionally override the default state() method
  129. * Control what the machine returns when another process requests its state
  130. */
  131. int Atm_AccelStepper::state( void ) {
  132. return Machine::state();
  133. }
  134. /* Nothing customizable below this line
  135. ************************************************************************************************
  136. */
  137. /* Public event methods
  138. *
  139. */
  140. Atm_AccelStepper& Atm_AccelStepper::disable() {
  141. trigger( EVT_DISABLE );
  142. return *this;
  143. }
  144. Atm_AccelStepper& Atm_AccelStepper::enable() {
  145. trigger( EVT_ENABLE );
  146. return *this;
  147. }
  148. Atm_AccelStepper& Atm_AccelStepper::stop() {
  149. trigger( EVT_STOP );
  150. return *this;
  151. }
  152. Atm_AccelStepper& Atm_AccelStepper::emergency_stop() {
  153. trigger( EVT_EMERGENCY_STOP );
  154. return *this;
  155. }
  156. Atm_AccelStepper& Atm_AccelStepper::on_limit_low() {
  157. trigger( EVT_ON_LIMIT_LOW );
  158. return *this;
  159. }
  160. Atm_AccelStepper& Atm_AccelStepper::on_limit_high() {
  161. trigger( EVT_ON_LIMIT_HIGH );
  162. return *this;
  163. }
  164. Atm_AccelStepper& Atm_AccelStepper::on_target() {
  165. trigger( EVT_ON_TARGET );
  166. return *this;
  167. }
  168. /*
  169. * onChangeposition() push connector variants ( slots 1, autostore 0, broadcast 0 )
  170. */
  171. Atm_AccelStepper& Atm_AccelStepper::onChangeposition( Machine& machine, int event ) {
  172. onPush( connectors, ON_CHANGEPOSITION, 0, 1, 1, machine, event );
  173. return *this;
  174. }
  175. Atm_AccelStepper& Atm_AccelStepper::onChangeposition( atm_cb_push_t callback, int idx ) {
  176. onPush( connectors, ON_CHANGEPOSITION, 0, 1, 1, callback, idx );
  177. return *this;
  178. }
  179. /*
  180. * onChangestate() push connector variants ( slots 1, autostore 0, broadcast 0 )
  181. */
  182. Atm_AccelStepper& Atm_AccelStepper::onChangestate( Machine& machine, int event ) {
  183. onPush( connectors, ON_CHANGESTATE, 0, 1, 1, machine, event );
  184. return *this;
  185. }
  186. Atm_AccelStepper& Atm_AccelStepper::onChangestate( atm_cb_push_t callback, int idx ) {
  187. onPush( connectors, ON_CHANGESTATE, 0, 1, 1, callback, idx );
  188. return *this;
  189. }
  190. /*
  191. * onOnlimithigh() push connector variants ( slots 1, autostore 0, broadcast 0 )
  192. */
  193. Atm_AccelStepper& Atm_AccelStepper::onOnlimithigh( Machine& machine, int event ) {
  194. onPush( connectors, ON_ONLIMITHIGH, 0, 1, 1, machine, event );
  195. return *this;
  196. }
  197. Atm_AccelStepper& Atm_AccelStepper::onOnlimithigh( atm_cb_push_t callback, int idx ) {
  198. onPush( connectors, ON_ONLIMITHIGH, 0, 1, 1, callback, idx );
  199. return *this;
  200. }
  201. /*
  202. * onOnlimitlow() push connector variants ( slots 1, autostore 0, broadcast 0 )
  203. */
  204. Atm_AccelStepper& Atm_AccelStepper::onOnlimitlow( Machine& machine, int event ) {
  205. onPush( connectors, ON_ONLIMITLOW, 0, 1, 1, machine, event );
  206. return *this;
  207. }
  208. Atm_AccelStepper& Atm_AccelStepper::onOnlimitlow( atm_cb_push_t callback, int idx ) {
  209. onPush( connectors, ON_ONLIMITLOW, 0, 1, 1, callback, idx );
  210. return *this;
  211. }
  212. /*
  213. * onOntarget() push connector variants ( slots 1, autostore 0, broadcast 0 )
  214. */
  215. Atm_AccelStepper& Atm_AccelStepper::onOntarget( Machine& machine, int event ) {
  216. onPush( connectors, ON_ONTARGET, 0, 1, 1, machine, event );
  217. return *this;
  218. }
  219. Atm_AccelStepper& Atm_AccelStepper::onOntarget( atm_cb_push_t callback, int idx ) {
  220. onPush( connectors, ON_ONTARGET, 0, 1, 1, callback, idx );
  221. return *this;
  222. }
  223. /*
  224. * onStop() push connector variants ( slots 1, autostore 0, broadcast 0 )
  225. */
  226. Atm_AccelStepper& Atm_AccelStepper::onStop( Machine& machine, int event ) {
  227. onPush( connectors, ON_STOP, 0, 1, 1, machine, event );
  228. return *this;
  229. }
  230. Atm_AccelStepper& Atm_AccelStepper::onStop( atm_cb_push_t callback, int idx ) {
  231. onPush( connectors, ON_STOP, 0, 1, 1, callback, idx );
  232. return *this;
  233. }
  234. /* State trace method
  235. * Sets the symbol table and the default logging method for serial monitoring
  236. */
  237. Atm_AccelStepper& Atm_AccelStepper::trace( Stream & stream ) {
  238. Machine::setTrace( &stream, atm_serial_debug::trace,
  239. "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" );
  240. return *this;
  241. }