Atm_AccelStepper.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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_MOVE 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, RUNNING, -1, -1, -1, -1, -1, -1,
  10. /* ENABLED */ ENT_ENABLED, -1, -1, DISABLE, -1, DISABLE, RUNNING, STOP, STOP, -1, -1, -1, -1,
  11. /* RUNNING */ ENT_RUNNING, LP_RUNNING, -1, DISABLE, -1, -1, RUNNING, STOP, STOP, LIMIT_LOW, LIMIT_HIGH, ENABLED, -1,
  12. /* STOP */ ENT_STOP, -1, -1, DISABLE, -1, -1, RUNNING, -1, STOP, -1, -1, -1, -1,
  13. /* HOMING_LOW */ ENT_HOMING_LOW, -1, EXT_HOMING_LOW, DISABLE, -1, -1, -1, STOP, STOP, STOP, -1, -1, -1,
  14. /* HOMING_HIGH */ ENT_HOMING_HIGH, -1, EXT_HOMING_HIGH, DISABLE, -1, -1, -1, STOP, STOP, -1, STOP, -1, -1,
  15. /* LIMIT_LOW */ ENT_LIMIT_LOW, -1, -1, -1, -1, -1, RUNNING, STOP, STOP, LIMIT_LOW, -1, -1, -1,
  16. /* LIMIT_HIGH */ ENT_LIMIT_HIGH, -1, -1, -1, -1, -1, RUNNING, 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. stepper->setMaxSpeed(1000);
  22. stepper->setAcceleration(100);
  23. idle_timer.set(ATM_TIMER_OFF);
  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_AccelStepper::event( int id ) {
  30. //updateLimitSwitch();
  31. switch ( id ) {
  32. case EVT_DISABLE:
  33. return 0;
  34. case EVT_ENABLE:
  35. return 0;
  36. case EVT_ENABLED_TIMEOUT:
  37. return 0;
  38. case EVT_MOVE:
  39. return 0;
  40. case EVT_STOP:
  41. return 0;
  42. case EVT_EMERGENCY_STOP:
  43. return 0;
  44. case EVT_ON_LIMIT_LOW:
  45. switch(_limitLow_Mode) {
  46. case 0:
  47. return 0;
  48. case 1: //digital INPUT
  49. limitLow_State = digitalRead(_limitLow_Pin);
  50. limitLow_State = _limitLow_Reversed ? !limitLow_State : limitLow_State;
  51. return limitLow_State;
  52. case 2:
  53. int analogTemp = analogRead(_limitLow_Pin);
  54. limitLow_State = _limitLow_Thresholds[0] < analogTemp && analogTemp < _limitLow_Thresholds[1];
  55. limitLow_State = _limitLow_Reversed ? !limitLow_State : limitLow_State;
  56. return limitLow_State;
  57. }
  58. case EVT_ON_LIMIT_HIGH:
  59. switch(_limitHigh_Mode) {
  60. case 0:
  61. return 0;
  62. case 1: //digital INPUT
  63. limitHigh_State = digitalRead(_limitHigh_Pin);
  64. limitHigh_State = _limitHigh_Reversed ? !limitHigh_State : limitHigh_State;
  65. return limitHigh_State;
  66. case 2:
  67. int analogTemp = analogRead(_limitHigh_Pin);
  68. limitHigh_State = _limitHigh_Thresholds[0] < analogTemp && analogTemp < _limitHigh_Thresholds[1];
  69. limitHigh_State = _limitHigh_Reversed ? !limitHigh_State : limitHigh_State;
  70. return limitHigh_State;
  71. }
  72. case EVT_ON_TARGET:
  73. return _currentStep == _targetStep;;
  74. }
  75. return 0;
  76. }
  77. /* Add C++ code for each action
  78. * This generates the 'output' for the state machine
  79. *
  80. * Available connectors:
  81. * push( connectors, ON_CHANGEPOSITION, 0, <v>, <up> );
  82. * push( connectors, ON_CHANGESTATE, 0, <v>, <up> );
  83. * push( connectors, ON_ONLIMITHIGH, 0, <v>, <up> );
  84. * push( connectors, ON_ONLIMITLOW, 0, <v>, <up> );
  85. * push( connectors, ON_ONTARGET, 0, <v>, <up> );
  86. * push( connectors, ON_STOP, 0, <v>, <up> );
  87. */
  88. void Atm_AccelStepper::action( int id ) {
  89. switch ( id ) {
  90. long int tempStep ;
  91. case ENT_DISABLED:
  92. push(connectors, ON_CHANGESTATE, 0, state(), 0);
  93. enabled = _enableReversed ? HIGH : LOW;
  94. digitalWrite(_enablePin, enabled);
  95. return;
  96. case ENT_ENABLED:
  97. push(connectors, ON_CHANGESTATE, 0, state(), 0);
  98. enabled = _enableReversed ? LOW : HIGH ;
  99. digitalWrite(_enablePin, enabled);
  100. return;
  101. case ENT_RUNNING:
  102. push(connectors, ON_CHANGESTATE, 0, state(), 0);
  103. return;
  104. case LP_RUNNING:
  105. switch (runMode) {
  106. case 0:
  107. stepper->run();
  108. break;
  109. case 1:
  110. stepper->runSpeed();
  111. break;
  112. }
  113. tempStep = stepper->currentPosition();
  114. if (tempStep != _currentStep){
  115. _currentStep = tempStep;
  116. push(connectors, ON_CHANGEPOSITION, 0, tempStep, 0);
  117. }
  118. return;
  119. case ENT_STOP:
  120. push(connectors, ON_CHANGESTATE, 0, state(), 0);
  121. stepper->stop();
  122. push( connectors, ON_STOP, 0, 0, 0 );
  123. if (last_trigger == EVT_STOP) {
  124. stepper->stop();
  125. push( connectors, ON_STOP, 0, 0, 0 );
  126. }
  127. if (last_trigger == EVT_EMERGENCY_STOP) {
  128. stepper->setSpeed(0);
  129. push( connectors, ON_STOP, 0, 1, 0 );
  130. }
  131. return;
  132. case ENT_HOMING_LOW:
  133. return;
  134. case EXT_HOMING_LOW:
  135. return;
  136. case ENT_HOMING_HIGH:
  137. return;
  138. case EXT_HOMING_HIGH:
  139. return;
  140. case ENT_LIMIT_LOW:
  141. return;
  142. case ENT_LIMIT_HIGH:
  143. return;
  144. }
  145. }
  146. /* Optionally override the default trigger() method
  147. * Control how your machine processes triggers
  148. */
  149. Atm_AccelStepper& Atm_AccelStepper::trigger( int event ) {
  150. Machine::trigger( event );
  151. return *this;
  152. }
  153. /* Optionally override the default state() method
  154. * Control what the machine returns when another process requests its state
  155. */
  156. int Atm_AccelStepper::state( void ) {
  157. return Machine::state();
  158. }
  159. /* Nothing customizable below this line
  160. ************************************************************************************************
  161. */
  162. /* Still I'll customize a little just here
  163. */
  164. Atm_AccelStepper& Atm_AccelStepper::move( long int stepRel) {
  165. _targetStep = _currentStep + stepRel;
  166. //Serial.println(_targetStep);
  167. stepper->move(_targetStep);
  168. enable();
  169. trigger( EVT_MOVE );
  170. return *this;
  171. }
  172. Atm_AccelStepper& Atm_AccelStepper::moveTo( long int stepAbs) {
  173. _targetStep = stepAbs;
  174. stepper->moveTo(_targetStep);
  175. enable();
  176. trigger( EVT_MOVE );
  177. return *this;
  178. }
  179. Atm_AccelStepper& Atm_AccelStepper::rotate( long int speed) {
  180. trigger( EVT_MOVE );
  181. return *this;
  182. }
  183. /* Public event methods
  184. *
  185. */
  186. Atm_AccelStepper& Atm_AccelStepper::disable() {
  187. trigger( EVT_DISABLE );
  188. return *this;
  189. }
  190. Atm_AccelStepper& Atm_AccelStepper::enable() {
  191. trigger( EVT_ENABLE );
  192. return *this;
  193. }
  194. // Atm_AccelStepper& Atm_AccelStepper::move() {
  195. // trigger( EVT_MOVE );
  196. // return *this;
  197. // }
  198. Atm_AccelStepper& Atm_AccelStepper::stop() {
  199. trigger( EVT_STOP );
  200. return *this;
  201. }
  202. Atm_AccelStepper& Atm_AccelStepper::emergency_stop() {
  203. trigger( EVT_EMERGENCY_STOP );
  204. return *this;
  205. }
  206. Atm_AccelStepper& Atm_AccelStepper::on_limit_low() {
  207. trigger( EVT_ON_LIMIT_LOW );
  208. return *this;
  209. }
  210. Atm_AccelStepper& Atm_AccelStepper::on_limit_high() {
  211. trigger( EVT_ON_LIMIT_HIGH );
  212. return *this;
  213. }
  214. Atm_AccelStepper& Atm_AccelStepper::on_target() {
  215. trigger( EVT_ON_TARGET );
  216. return *this;
  217. }
  218. /*
  219. * onChangeposition() push connector variants ( slots 1, autostore 0, broadcast 0 )
  220. */
  221. Atm_AccelStepper& Atm_AccelStepper::onChangeposition( Machine& machine, int event ) {
  222. onPush( connectors, ON_CHANGEPOSITION, 0, 1, 1, machine, event );
  223. return *this;
  224. }
  225. Atm_AccelStepper& Atm_AccelStepper::onChangeposition( atm_cb_push_t callback, int idx ) {
  226. onPush( connectors, ON_CHANGEPOSITION, 0, 1, 1, callback, idx );
  227. return *this;
  228. }
  229. /*
  230. * onChangestate() push connector variants ( slots 1, autostore 0, broadcast 0 )
  231. */
  232. Atm_AccelStepper& Atm_AccelStepper::onChangestate( Machine& machine, int event ) {
  233. onPush( connectors, ON_CHANGESTATE, 0, 1, 1, machine, event );
  234. return *this;
  235. }
  236. Atm_AccelStepper& Atm_AccelStepper::onChangestate( atm_cb_push_t callback, int idx ) {
  237. onPush( connectors, ON_CHANGESTATE, 0, 1, 1, callback, idx );
  238. return *this;
  239. }
  240. /*
  241. * onOnlimithigh() push connector variants ( slots 1, autostore 0, broadcast 0 )
  242. */
  243. Atm_AccelStepper& Atm_AccelStepper::onOnlimithigh( Machine& machine, int event ) {
  244. onPush( connectors, ON_ONLIMITHIGH, 0, 1, 1, machine, event );
  245. return *this;
  246. }
  247. Atm_AccelStepper& Atm_AccelStepper::onOnlimithigh( atm_cb_push_t callback, int idx ) {
  248. onPush( connectors, ON_ONLIMITHIGH, 0, 1, 1, callback, idx );
  249. return *this;
  250. }
  251. /*
  252. * onOnlimitlow() push connector variants ( slots 1, autostore 0, broadcast 0 )
  253. */
  254. Atm_AccelStepper& Atm_AccelStepper::onOnlimitlow( Machine& machine, int event ) {
  255. onPush( connectors, ON_ONLIMITLOW, 0, 1, 1, machine, event );
  256. return *this;
  257. }
  258. Atm_AccelStepper& Atm_AccelStepper::onOnlimitlow( atm_cb_push_t callback, int idx ) {
  259. onPush( connectors, ON_ONLIMITLOW, 0, 1, 1, callback, idx );
  260. return *this;
  261. }
  262. /*
  263. * onOntarget() push connector variants ( slots 1, autostore 0, broadcast 0 )
  264. */
  265. Atm_AccelStepper& Atm_AccelStepper::onOntarget( Machine& machine, int event ) {
  266. onPush( connectors, ON_ONTARGET, 0, 1, 1, machine, event );
  267. return *this;
  268. }
  269. Atm_AccelStepper& Atm_AccelStepper::onOntarget( atm_cb_push_t callback, int idx ) {
  270. onPush( connectors, ON_ONTARGET, 0, 1, 1, callback, idx );
  271. return *this;
  272. }
  273. /*
  274. * onStop() push connector variants ( slots 1, autostore 0, broadcast 0 )
  275. */
  276. Atm_AccelStepper& Atm_AccelStepper::onStop( Machine& machine, int event ) {
  277. onPush( connectors, ON_STOP, 0, 1, 1, machine, event );
  278. return *this;
  279. }
  280. Atm_AccelStepper& Atm_AccelStepper::onStop( atm_cb_push_t callback, int idx ) {
  281. onPush( connectors, ON_STOP, 0, 1, 1, callback, idx );
  282. return *this;
  283. }
  284. /* State trace method
  285. * Sets the symbol table and the default logging method for serial monitoring
  286. */
  287. Atm_AccelStepper& Atm_AccelStepper::trace( Stream & stream ) {
  288. Machine::setTrace( &stream, atm_serial_debug::trace,
  289. "ACCELSTEPPER\0EVT_DISABLE\0EVT_ENABLE\0EVT_ENABLED_TIMEOUT\0EVT_MOVE\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" );
  290. return *this;
  291. }