Atm_Teenstep.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. #include "Atm_Teenstep.h"
  2. /* Add optional parameters for the state machine to begin()
  3. * Add extra initialization code
  4. */
  5. Atm_Teenstep& Atm_Teenstep::begin(Stepper & motorRef, StepControl & stepControlRef) {
  6. // clang-format off
  7. const static state_t state_table[] PROGMEM = {
  8. /* ON_ENTER ON_LOOP ON_EXIT EVT_MOVE_TIMEOUT EVT_LIMIT_HIGH EVT_LIMIT_LOW EVT_EMERGENCYSTOP EVT_STOP EVT_ONTARGET EVT_MOVE EVT_DISABLE EVT_ENABLE ELSE */
  9. /* DISABLED */ ENT_DISABLED, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, ENABLED, -1,
  10. /* ENABLED */ ENT_ENABLED, -1, -1, -1, -1, -1, -1, -1, -1, RUNNING, DISABLED, -1, -1,
  11. /* RUNNING */ ENT_RUNNING, LP_RUNNING, -1, -1, EMERGENCY_STOP, EMERGENCY_STOP, EMERGENCY_STOP, STOPPING, ENABLED, RUNNING, -1, -1, -1,
  12. /* STOPPING */ ENT_STOPPING, -1, -1, -1, EMERGENCY_STOP, EMERGENCY_STOP, EMERGENCY_STOP, -1, -1, RUNNING, -1, -1, -1,
  13. /* EMERGENCY_STOP */ ENT_EMERGENCY_STOP, -1, -1, -1, -1, -1, -1, -1, -1, RUNNING, -1, ENABLED, -1,
  14. /* HOMING_HIGH */ ENT_HOMING_HIGH, -1, EXT_HOMING_HIGH, -1, ENABLED, EMERGENCY_STOP, EMERGENCY_STOP, STOPPING, -1, -1, -1, -1, -1,
  15. /* HOMING_LOW */ ENT_HOMING_LOW, -1, EXT_HOMING_LOW, -1, EMERGENCY_STOP, ENABLED, EMERGENCY_STOP, STOPPING, -1, -1, -1, -1, -1,
  16. };
  17. // clang-format on
  18. Machine::begin( state_table, ELSE );
  19. this-> motor = &motorRef;
  20. this-> controller = &stepControlRef;
  21. return *this;
  22. }
  23. /* Add C++ code for each internally handled event (input)
  24. * The code must return 1 to trigger the event
  25. */
  26. int Atm_Teenstep::event( int id ) {
  27. switch ( id ) {
  28. case EVT_MOVE_TIMEOUT:
  29. return 0;
  30. case EVT_LIMIT_HIGH:
  31. return 0;
  32. case EVT_LIMIT_LOW:
  33. return limitState[0];
  34. case EVT_EMERGENCYSTOP:
  35. return limitState[1];
  36. case EVT_STOP:
  37. return 0;
  38. case EVT_ONTARGET:
  39. return _currentStep == _targetStep;
  40. case EVT_MOVE:
  41. return 0;
  42. case EVT_DISABLE:
  43. return 0;
  44. case EVT_ENABLE:
  45. return 0;
  46. }
  47. return 0;
  48. }
  49. /* Add C++ code for each action
  50. * This generates the 'output' for the state machine
  51. *
  52. * Available connectors:
  53. * push( connectors, ON_CHANGE, 0, <v>, <up> );
  54. * push( connectors, ON_CHANGEPOSITION, 0, <v>, <up> );
  55. * push( connectors, ON_LIMITHIGH, 0, <v>, <up> );
  56. * push( connectors, ON_LIMITLOW, 0, <v>, <up> );
  57. */
  58. void Atm_Teenstep::action( int id ) {
  59. switch ( id ) {
  60. long int tempStep ;
  61. case ENT_DISABLED:
  62. enabled = _enableReversed ? HIGH : LOW;
  63. digitalWrite(_enablePin, enabled);
  64. sendOSC();
  65. return;
  66. case ENT_ENABLED:
  67. enabled = _enableReversed ? LOW : HIGH ;
  68. digitalWrite(_enablePin, enabled);
  69. sendOSC();
  70. return;
  71. case ENT_RUNNING:
  72. return;
  73. case LP_RUNNING:
  74. tempStep = motor->getPosition();
  75. //connectors[ON_ONCHANGEPOSITION].push(_currentStep);
  76. // if (tempStep != _currentStep){push( connectors, ON_ONCHANGEPOSITION, 0, _currentStep, 0 );} ;
  77. _currentStep = tempStep;
  78. updateLimitSwitch();
  79. sendOSC();
  80. return;
  81. case ENT_STOPPING:
  82. sendOSC();
  83. return;
  84. case ENT_EMERGENCY_STOP:
  85. controller->emergencyStop();
  86. sendOSC();
  87. return;
  88. case ENT_HOMING_HIGH:
  89. _limitType ? move(2147483647) : trigger(EVT_ENABLE) ;
  90. sendOSC();
  91. return;
  92. case EXT_HOMING_HIGH:
  93. _maxStep = motor->getPosition();
  94. return;
  95. case ENT_HOMING_LOW:
  96. _limitType ? move(-2147483647) : trigger(EVT_STOP) ;
  97. sendOSC();
  98. return;
  99. case EXT_HOMING_LOW:
  100. motor->setPosition(0);
  101. return;
  102. }
  103. }
  104. /* Optionally override the default trigger() method
  105. * Control how your machine processes triggers
  106. */
  107. Atm_Teenstep& Atm_Teenstep::trigger( int event ) {
  108. Machine::trigger( event );
  109. return *this;
  110. }
  111. /* Optionally override the default state() method
  112. * Control what the machine returns when another process requests its state
  113. */
  114. int Atm_Teenstep::state( void ) {
  115. return Machine::state();
  116. }
  117. /* CUSTOM METHODS
  118. ********************************************************************************************************
  119. */
  120. // Atm_TeensyStep& Atm_TeensyStep::enable( bool enable ){
  121. //
  122. // return *this;
  123. // }
  124. ///////// ENABLE/DISABLE ////////
  125. Atm_Teenstep& Atm_Teenstep::setEnablePin( int enablePin ){
  126. _enablePin = enablePin ;
  127. pinMode(_enablePin, OUTPUT);
  128. return *this;
  129. }
  130. Atm_Teenstep& Atm_Teenstep::enableReversed( bool reverse ){
  131. _enableReversed = reverse ;
  132. return *this;
  133. }
  134. ///////// LIMITS ////////
  135. Atm_Teenstep& Atm_Teenstep::setLimitPins( int limitPinLow){
  136. _limitPin[0] = limitPinLow;
  137. pinMode(_limitPin[0], INPUT);
  138. return *this;
  139. }
  140. Atm_Teenstep& Atm_Teenstep::setLimitPins( int limitPinLow, int limitPinHigh){
  141. _limitPin[0] = limitPinLow;
  142. _limitPin[1] = limitPinHigh;
  143. pinMode(_limitPin[0], INPUT);
  144. pinMode(_limitPin[1], INPUT);
  145. return *this;
  146. }
  147. Atm_Teenstep& Atm_Teenstep::setLimitType( int limitType){
  148. _limitType = limitType;
  149. return *this;
  150. }
  151. Atm_Teenstep& Atm_Teenstep::limitReversed( bool reversed){
  152. _limitReversed = reversed;
  153. return *this;
  154. }
  155. Atm_Teenstep& Atm_Teenstep::limitThresholds( int limitThreshold0,
  156. int limitThreshold1, int limitThreshold2, int limitThreshold3){
  157. _limitThresholds[0] = limitThreshold0;
  158. _limitThresholds[1] = limitThreshold1;
  159. _limitThresholds[2] = limitThreshold2;
  160. _limitThresholds[3] = limitThreshold3;
  161. return *this;
  162. }
  163. void Atm_Teenstep::updateLimitSwitch(){
  164. switch (_limitType) { // limitType!=0 means there is limit to check
  165. case NONE:
  166. return ;
  167. case DIGITAL_1:
  168. limitState[0] = digitalRead(_limitPin[0]);
  169. limitState[0] = _limitReversed ? !limitState[0] : limitState[0];
  170. return;
  171. case DIGITAL_2:
  172. limitState[0] = digitalRead(_limitPin[0]);
  173. limitState[1] = digitalRead(_limitPin[1]);
  174. limitState[0] = _limitReversed ? !limitState[0] : limitState[0];
  175. limitState[1] = _limitReversed ? !limitState[1] : limitState[1];
  176. return;
  177. case ANALOG_1:
  178. int read = analogRead(_limitPin[0]) ;
  179. limitState[0] = _limitThresholds[0] < read && read < _limitThresholds[1] ;
  180. limitState[1] = _limitThresholds[2] < read && read < _limitThresholds[3] ;
  181. return;
  182. }
  183. }
  184. Atm_Teenstep& Atm_Teenstep::onOSC( void ){
  185. // _enableReversed = reverse ;
  186. return *this;
  187. }
  188. Atm_Teenstep& Atm_Teenstep::sendOSC( void ){
  189. // _enableReversed = reverse ;
  190. return *this;
  191. }
  192. /* Nothing customizable below this line
  193. ************************************************************************************************
  194. */
  195. /* Public event methods
  196. *
  197. */
  198. Atm_Teenstep& Atm_Teenstep::move_timeout() {
  199. trigger( EVT_MOVE_TIMEOUT );
  200. return *this;
  201. }
  202. Atm_Teenstep& Atm_Teenstep::limit_high() {
  203. trigger( EVT_LIMIT_HIGH );
  204. return *this;
  205. }
  206. Atm_Teenstep& Atm_Teenstep::limit_low() {
  207. trigger( EVT_LIMIT_LOW );
  208. return *this;
  209. }
  210. Atm_Teenstep& Atm_Teenstep::emergencystop() {
  211. trigger( EVT_EMERGENCYSTOP );
  212. return *this;
  213. }
  214. Atm_Teenstep& Atm_Teenstep::stop() {
  215. trigger( EVT_STOP );
  216. return *this;
  217. }
  218. Atm_Teenstep& Atm_Teenstep::ontarget() {
  219. trigger( EVT_ONTARGET );
  220. return *this;
  221. }
  222. Atm_Teenstep& Atm_Teenstep::move(long int stepRel) {
  223. _targetStep = _currentStep + stepRel;
  224. Serial.println(_targetStep);
  225. motor->setTargetAbs(_targetStep);
  226. controller->moveAsync(*motor);
  227. enable();
  228. trigger( EVT_MOVE );
  229. return *this;
  230. }
  231. Atm_Teenstep& Atm_Teenstep::moveTo(long int stepAbs) {
  232. _targetStep = stepAbs;
  233. motor->setTargetAbs(_targetStep);
  234. controller->moveAsync(*motor);
  235. enable();
  236. trigger( EVT_MOVE );
  237. return *this;
  238. }
  239. Atm_Teenstep& Atm_Teenstep::disable() {
  240. trigger( EVT_DISABLE );
  241. return *this;
  242. }
  243. Atm_Teenstep& Atm_Teenstep::enable() {
  244. trigger( EVT_ENABLE );
  245. return *this;
  246. }
  247. /*
  248. * onChange() push connector variants ( slots 1, autostore 0, broadcast 0 )
  249. */
  250. Atm_Teenstep& Atm_Teenstep::onChange( Machine& machine, int event ) {
  251. onPush( connectors, ON_CHANGE, 0, 1, 1, machine, event );
  252. return *this;
  253. }
  254. Atm_Teenstep& Atm_Teenstep::onChange( atm_cb_push_t callback, int idx ) {
  255. onPush( connectors, ON_CHANGE, 0, 1, 1, callback, idx );
  256. return *this;
  257. }
  258. /*
  259. * onChangeposition() push connector variants ( slots 1, autostore 0, broadcast 0 )
  260. */
  261. Atm_Teenstep& Atm_Teenstep::onChangeposition( Machine& machine, int event ) {
  262. onPush( connectors, ON_CHANGEPOSITION, 0, 1, 1, machine, event );
  263. return *this;
  264. }
  265. Atm_Teenstep& Atm_Teenstep::onChangeposition( atm_cb_push_t callback, int idx ) {
  266. onPush( connectors, ON_CHANGEPOSITION, 0, 1, 1, callback, idx );
  267. return *this;
  268. }
  269. /*
  270. * onLimithigh() push connector variants ( slots 1, autostore 0, broadcast 0 )
  271. */
  272. Atm_Teenstep& Atm_Teenstep::onLimithigh( Machine& machine, int event ) {
  273. onPush( connectors, ON_LIMITHIGH, 0, 1, 1, machine, event );
  274. return *this;
  275. }
  276. Atm_Teenstep& Atm_Teenstep::onLimithigh( atm_cb_push_t callback, int idx ) {
  277. onPush( connectors, ON_LIMITHIGH, 0, 1, 1, callback, idx );
  278. return *this;
  279. }
  280. /*
  281. * onLimitlow() push connector variants ( slots 1, autostore 0, broadcast 0 )
  282. */
  283. Atm_Teenstep& Atm_Teenstep::onLimitlow( Machine& machine, int event ) {
  284. onPush( connectors, ON_LIMITLOW, 0, 1, 1, machine, event );
  285. return *this;
  286. }
  287. Atm_Teenstep& Atm_Teenstep::onLimitlow( atm_cb_push_t callback, int idx ) {
  288. onPush( connectors, ON_LIMITLOW, 0, 1, 1, callback, idx );
  289. return *this;
  290. }
  291. /* State trace method
  292. * Sets the symbol table and the default logging method for serial monitoring
  293. */
  294. Atm_Teenstep& Atm_Teenstep::trace( Stream & stream ) {
  295. Machine::setTrace( &stream, atm_serial_debug::trace,
  296. "TEENSTEP\0EVT_MOVE_TIMEOUT\0EVT_LIMIT_HIGH\0EVT_LIMIT_LOW\0EVT_EMERGENCYSTOP\0EVT_STOP\0EVT_ONTARGET\0EVT_MOVE\0EVT_DISABLE\0EVT_ENABLE\0ELSE\0DISABLED\0ENABLED\0RUNNING\0STOPPING\0EMERGENCY_STOP\0HOMING_HIGH\0HOMING_LOW" );
  297. return *this;
  298. }