Atm_AccelStepper.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. #ifdef AccelStepper_h
  2. #include "Atm_AccelStepper.h"
  3. /* Add optional parameters for the state machine to begin()
  4. * Add extra initialization code
  5. */
  6. Atm_AccelStepper& Atm_AccelStepper::begin(int step_pin, int dir_pin) {
  7. // clang-format off
  8. const static state_t state_table[] PROGMEM = {
  9. /* 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 EVT_HOMING_LOW EVT_HOMING_HIGH ELSE */
  10. /* DISABLE */ ENT_DISABLED, -1, -1, -1, ENABLED, -1, RUNNING, -1, -1, DISABLE, DISABLE, -1, HOMING_LOW, HOMING_HIGH, -1,
  11. /* ENABLED */ ENT_ENABLED, -1, -1, DISABLE, -1, DISABLE, RUNNING, -1, -1, ENABLED, ENABLED, -1, HOMING_LOW, HOMING_HIGH, -1,
  12. /* RUNNING */ ENT_RUNNING, LP_RUNNING, -1, DISABLE, -1, -1, RUNNING, STOP, STOP, RUNNING, RUNNING, ENABLED, -1, -1, -1,
  13. /* STOP */ ENT_STOP, LP_STOP, -1, DISABLE, ENABLED, -1, RUNNING, STOP, STOP, STOP, STOP, ENABLED, -1, -1, -1,
  14. /* HOMING_LOW */ ENT_HOMING_LOW, LP_HOMING_LOW, EXT_HOMING_LOW, DISABLE, -1, -1, -1, STOP, STOP, HOMING_LOW, HOMING_LOW, -1, -1, -1, -1,
  15. /* HOMING_HIGH */ ENT_HOMING_HIGH, LP_HOMING_HIGH, EXT_HOMING_HIGH, DISABLE, -1, -1, -1, STOP, STOP, HOMING_HIGH, HOMING_HIGH, -1, -1, -1, -1,
  16. /* LIMIT_LOW */ ENT_LIMIT_LOW, LP_LIMIT_LOW, -1, -1, -1, -1, RUNNING, STOP, STOP, -1, -1, -1, -1, -1, -1,
  17. /* LIMIT_HIGH */ ENT_LIMIT_HIGH, LP_LIMIT_HIGH, -1, -1, -1, -1, RUNNING, STOP, STOP, -1, -1, -1, -1, -1, -1
  18. };
  19. // clang-format on
  20. Machine::begin( state_table, ELSE );
  21. stepper = new AccelStepper(1, step_pin, dir_pin);
  22. stepper->setMaxSpeed(max_speed);
  23. stepper->setAcceleration(acceleration);
  24. // idle_timer.set(ATM_TIMER_OFF);
  25. position_timer.set(POSITION_SEND_TIMER);
  26. limits_timer.set(LIMIT_UPDATE_RATE);
  27. limits_timer.setFromNow(this, LIMIT_UPDATE_RATE);
  28. return *this;
  29. }
  30. /* Add C++ code for each internally handled event (input)
  31. * The code must return 1 to trigger the event
  32. */
  33. int Atm_AccelStepper::event( int id ) {
  34. //bool tempState ;
  35. switch ( id ) {
  36. case EVT_DISABLE:
  37. return 0;
  38. case EVT_ENABLE:
  39. return 0;
  40. case EVT_ENABLED_TIMEOUT:
  41. return 0;
  42. case EVT_MOVE:
  43. return 0;
  44. case EVT_STOP:
  45. return 0;
  46. case EVT_EMERGENCY_STOP:
  47. return 0;
  48. case EVT_ON_LIMIT_LOW:
  49. // if (limits_timer.expired(this)){
  50. if (millis()-limit_update_offset > LIMIT_UPDATE_RATE){
  51. switch(_limitLow_Mode) {
  52. case 0:
  53. break;
  54. case 1: //digital INPUT
  55. // Serial.println("digital");
  56. limitLow_State_raw = digitalRead(_limitLow_Pin);
  57. limitLow_State_raw = _limitLow_Reversed ? !limitLow_State_raw : limitLow_State_raw;
  58. break;
  59. case 2:
  60. int analogTemp = analogRead(_limitLow_Pin);
  61. limitLow_State_raw = (_limitLow_Thresholds[0] < analogTemp) && (analogTemp < _limitLow_Thresholds[1]);
  62. limitLow_State_raw = _limitLow_Reversed ? !limitLow_State_raw : limitLow_State_raw;
  63. // if(limitLow_State){
  64. // delay(3);
  65. // analogTemp = analogRead(_limitLow_Pin);
  66. // limitLow_State = (_limitLow_Thresholds[0] < analogTemp) && (analogTemp < _limitLow_Thresholds[1]);
  67. // limitLow_State = _limitLow_Reversed ? !limitLow_State : limitLow_State;
  68. // }
  69. break;
  70. }
  71. limitLow_State = limitLow_avg(limitLow_State_raw);
  72. changed = limitLow_State != limitLow_State_prev ? 1 : 0 ;
  73. limitLow_State_prev = limitLow_State ;
  74. //Serial.println(limitLow_State);
  75. if (changed){push( connectors, ON_ONLIMITLOW, 0, limitLow_State, 0 );}
  76. // Serial.println("in limit");
  77. return changed ;
  78. }
  79. else{return 0;}
  80. case EVT_ON_LIMIT_HIGH:
  81. // if (limits_timer.expired(this)){
  82. if (millis()-limit_update_offset > LIMIT_UPDATE_RATE){
  83. switch(_limitHigh_Mode) {
  84. case 0:
  85. break;
  86. case 1: //digital INPUT
  87. limitHigh_State_raw = digitalRead(_limitHigh_Pin);
  88. limitHigh_State_raw = _limitHigh_Reversed ? !limitHigh_State_raw : limitHigh_State_raw;
  89. break;
  90. case 2:
  91. //Serial.println("analog");
  92. int analogTemp = analogRead(_limitHigh_Pin);
  93. limitHigh_State_raw = (_limitHigh_Thresholds[0] < analogTemp) && (analogTemp < _limitHigh_Thresholds[1]);
  94. limitHigh_State_raw = _limitHigh_Reversed ? !limitHigh_State_raw : limitHigh_State_raw;
  95. // if(limitHigh_State){
  96. // delay(3);
  97. // analogTemp = analogRead(_limitHigh_Pin);
  98. // limitHigh_State = (_limitHigh_Thresholds[0] < analogTemp) && (analogTemp < _limitHigh_Thresholds[1]);
  99. // limitHigh_State = _limitHigh_Reversed ? !limitHigh_State : limitHigh_State;
  100. // }
  101. break;
  102. }
  103. limitHigh_State = limitHigh_avg(limitHigh_State_raw);
  104. changed = limitHigh_State != limitHigh_State_prev ? 1 : 0;
  105. limitHigh_State_prev = limitHigh_State;
  106. if (changed){push( connectors, ON_ONLIMITHIGH, 0, limitHigh_State, 0 );}
  107. // limits_timer.setFromNow(this, LIMIT_UPDATE_RATE);
  108. limit_update_offset = millis();
  109. return changed ;
  110. }
  111. else{return 0;}
  112. case EVT_ON_TARGET:
  113. return runMode ? 0 : _currentStep == _targetStep; //only send when in positionning mode
  114. case EVT_HOMING_LOW:
  115. return 0;
  116. case EVT_HOMING_HIGH:
  117. return 0;
  118. }
  119. return 0;
  120. }
  121. /* Add C++ code for each action
  122. * This generates the 'output' for the state machine
  123. *
  124. * Available connectors:
  125. * push( connectors, ON_CHANGEPOSITION, 0, <v>, <up> );
  126. * push( connectors, ON_CHANGESTATE, 0, <v>, <up> );
  127. * push( connectors, ON_ONLIMITHIGH, 0, <v>, <up> );
  128. * push( connectors, ON_ONLIMITLOW, 0, <v>, <up> );
  129. * push( connectors, ON_ONTARGET, 0, <v>, <up> );
  130. * push( connectors, ON_STOP, 0, <v>, <up> );
  131. */
  132. void Atm_AccelStepper::action( int id ) {
  133. switch ( id ) {
  134. case ENT_DISABLED:
  135. push(connectors, ON_CHANGESTATE, 0, state(), 0);
  136. enabled = _enableReversed ? HIGH : LOW;
  137. digitalWrite(_enablePin, enabled);
  138. limits_timer.setFromNow(this, LIMIT_UPDATE_RATE);
  139. return;
  140. case ENT_ENABLED:
  141. // _isHoming = 0 ;
  142. stepper_update();
  143. if(last_trigger == EVT_ON_TARGET){push( connectors, ON_ONTARGET, 0, _currentStep, 0 );};
  144. push(connectors, ON_CHANGESTATE, 0, state(), 0);
  145. push(connectors, ON_CHANGEPOSITION, 0, stepper->currentPosition(), _currentSpeed);
  146. enabled = _enableReversed ? LOW : HIGH ;
  147. //reset limit state so that they trigger again if we're stopped on it
  148. limitLow_State = 0;
  149. limitHigh_State = 0;
  150. digitalWrite(_enablePin, enabled);
  151. limits_timer.setFromNow(this, LIMIT_UPDATE_RATE);
  152. return;
  153. case ENT_RUNNING:
  154. push(connectors, ON_CHANGESTATE, 0, state(), 0);
  155. if (last_trigger!=EVT_ON_LIMIT_LOW && last_trigger!=EVT_ON_LIMIT_HIGH){compensatePlay(_currentSpeed);break;}
  156. if (runMode){stepper->setSpeed( _currentSpeed);}
  157. else{stepper->moveTo(_targetStep);}
  158. position_timer.setFromNow(this, POSITION_SEND_TIMER);
  159. limits_timer.setFromNow(this, LIMIT_UPDATE_RATE);
  160. return;
  161. case LP_RUNNING:
  162. //Serial.println(millis()-LIMIT_UPDATE_OFFSET);
  163. // if on limits and limits are hard, stop moving in one direction
  164. if(limitLow_State && _limitLow_Hard && (_currentSpeed<0.)) {
  165. trigger(EVT_EMERGENCY_STOP);
  166. break;
  167. }
  168. if( limitHigh_State && _limitHigh_Hard && (_currentSpeed>0.)) {
  169. trigger(EVT_EMERGENCY_STOP);
  170. break;
  171. }
  172. // if hard limits and homing is done, check current position is within limits
  173. if (_limitLow_Hard && homingLow_done) {
  174. if( (_currentStep<0) && (_currentSpeed<0.)) {trigger(EVT_EMERGENCY_STOP);break;}
  175. }
  176. if (_limitHigh_Hard && _maxStep) { //maxstep is 0 if not defined
  177. if( (_currentStep>_maxStep) && (_currentSpeed>0.)) {trigger(EVT_EMERGENCY_STOP);break;}
  178. }
  179. stepper_update();
  180. if(runMode && _currentSpeed == 0.) {trigger(EVT_EMERGENCY_STOP);break;}//trigger(EVT_ON_TARGET);
  181. if(!runMode && (_currentStep==_targetStep)){push( connectors, ON_ONTARGET, 0, _currentStep, 0 );trigger(EVT_EMERGENCY_STOP);break;}
  182. return;
  183. case ENT_STOP:
  184. push(connectors, ON_CHANGESTATE, 0, state(), 0);
  185. if (last_trigger == EVT_STOP) {
  186. runMode = 0 ;
  187. // stepper->setSpeed(stepper->speed());
  188. // stepper_update();
  189. stepper->stop();
  190. _targetStep = stepper->targetPosition();
  191. break;
  192. // push( connectors, ON_STOP, 0, 0, 0 );
  193. }
  194. if (last_trigger == EVT_EMERGENCY_STOP) {
  195. runMode=0 ;
  196. stepper->setSpeed(0);
  197. _currentStep = stepper->currentPosition();
  198. _targetStep = _currentStep ;
  199. stepper->setCurrentPosition(_currentStep);
  200. break;
  201. // stepper->moveTo(_targetStep);
  202. // push(connectors, ON_CHANGEPOSITION, 0, _currentStep, stepper->speed());
  203. // push( connectors, ON_STOP, 0, 1, 0 );
  204. }
  205. // stepper_update();
  206. // push(connectors, ON_CHANGEPOSITION, 0, _currentStep, stepper->speed());
  207. return;
  208. case LP_STOP:
  209. if(_currentSpeed == 0.) {trigger(EVT_ENABLE);break;}
  210. stepper_update();
  211. return;
  212. case ENT_HOMING_LOW:
  213. homingLow_done = 0;
  214. push(connectors, ON_CHANGESTATE, 0, state(), 0);
  215. push(connectors, ON_CHANGEPOSITION, 0, _currentStep, _currentSpeed);
  216. runMode = 1;
  217. _currentSpeed = -1*homing_speed;
  218. if (last_trigger!=EVT_ON_LIMIT_LOW && last_trigger!=EVT_ON_LIMIT_HIGH){compensatePlay(_currentSpeed);}
  219. stepper->setSpeed(-1*homing_speed);
  220. position_timer.setFromNow(this, POSITION_SEND_TIMER);
  221. limits_timer.setFromNow(this, LIMIT_UPDATE_RATE);
  222. return;
  223. case LP_HOMING_LOW:
  224. stepper_update();
  225. if(limitLow_State) {
  226. stepper->setCurrentPosition(0);
  227. _currentStep = 0;
  228. homingLow_done = 1 ;
  229. runMode = 0;
  230. trigger(EVT_EMERGENCY_STOP);
  231. push(connectors, ON_ONHOMINGLOW, 0, _currentStep, homingLow_done);
  232. }
  233. else if (changed && limitHigh_State && _limitHigh_Hard ){ //hit high limit and high is hard, stop
  234. homingLow_done = 0 ;
  235. runMode = 0;
  236. trigger(EVT_EMERGENCY_STOP);
  237. push(connectors, ON_ONHOMINGLOW, 0, _currentStep, homingLow_done);
  238. }
  239. return;
  240. case EXT_HOMING_LOW:
  241. return;
  242. case ENT_HOMING_HIGH:
  243. push(connectors, ON_CHANGESTATE, 0, state(), 0);
  244. push(connectors, ON_CHANGEPOSITION, 0, _currentStep, _currentSpeed);
  245. runMode = 1;
  246. _currentSpeed = homing_speed;
  247. if (last_trigger!=EVT_ON_LIMIT_LOW && last_trigger!=EVT_ON_LIMIT_HIGH){compensatePlay(_currentSpeed);}
  248. stepper->setSpeed(homing_speed);
  249. position_timer.setFromNow(this, POSITION_SEND_TIMER);
  250. limits_timer.setFromNow(this, LIMIT_UPDATE_RATE);
  251. return;
  252. case LP_HOMING_HIGH:
  253. stepper_update();
  254. if(limitHigh_State) {
  255. _maxStep = stepper->currentPosition();
  256. _currentStep = _maxStep;
  257. homingHigh_done = 1;
  258. runMode = 0 ;
  259. trigger(EVT_EMERGENCY_STOP);
  260. push(connectors, ON_ONHOMINGHIGH, 0, _currentStep, homingHigh_done);
  261. }
  262. else if (changed && limitLow_State &&_limitLow_Hard ){ //if hit low limit and low is hard, stop
  263. homingHigh_done = 0;
  264. runMode = 0 ;
  265. trigger(EVT_EMERGENCY_STOP);
  266. push(connectors, ON_ONHOMINGHIGH, 0, _currentStep, homingHigh_done);
  267. }
  268. return;
  269. case EXT_HOMING_HIGH:
  270. return;
  271. case ENT_LIMIT_LOW:
  272. /*triggered by a change in limit state
  273. if state is 0, we may leave this state for running
  274. if state is 1 we stay in limit state loop, where moves are allowed only in
  275. the free direction, until a trigger comes with state 0
  276. */
  277. push( connectors, ON_ONLIMITLOW, 0, limitLow_State, 0 );
  278. push(connectors, ON_CHANGEPOSITION, 0, _currentStep, _currentSpeed);
  279. if (!limitLow_State){trigger(EVT_MOVE);}
  280. return;
  281. case LP_LIMIT_LOW:
  282. //stop motor if going down, allow going up
  283. // if(_limitLow_Hard && (_targetStep < _currentStep)) {
  284. if(_limitLow_Hard && (stepper->speed()<0.)) {
  285. // Serial.println("youpi");
  286. // _currentStep = stepper->currentPosition();
  287. stepper->moveTo(_currentStep);
  288. _targetStep = _currentStep;
  289. stepper->setSpeed(0);
  290. }
  291. stepper_update();
  292. //else{} // _isHoming ? trigger(EVT_STOP):
  293. return;
  294. case ENT_LIMIT_HIGH:
  295. push( connectors, ON_ONLIMITHIGH, 0, limitHigh_State, 0 );
  296. push(connectors, ON_CHANGEPOSITION, 0, _currentStep, _currentSpeed);
  297. if (!limitHigh_State){trigger(EVT_MOVE);};
  298. return;
  299. case LP_LIMIT_HIGH:
  300. //stop motor if going down, allow going up
  301. if(_limitHigh_Hard && (_currentSpeed>0.)) {
  302. // Serial.println("youpi");
  303. // _currentStep = stepper->currentPosition();
  304. stepper->moveTo(_currentStep);
  305. _targetStep = _currentStep;
  306. stepper->setSpeed(0);
  307. }
  308. stepper_update();
  309. // else{}
  310. return;
  311. }
  312. }
  313. /* Optionally override the default trigger() method
  314. * Control how your machine processes triggers
  315. */
  316. Atm_AccelStepper& Atm_AccelStepper::trigger( int event ) {
  317. Machine::trigger( event );
  318. return *this;
  319. }
  320. /* Optionally override the default state() method
  321. * Control what the machine returns when another process requests its state
  322. */
  323. int Atm_AccelStepper::state( void ) {
  324. return Machine::state();
  325. }
  326. /* Nothing customizable below this line
  327. ************************************************************************************************
  328. */
  329. /* Still I'll customize a little just here
  330. */
  331. void Atm_AccelStepper::stepper_update(void) {
  332. switch (runMode) {
  333. case 0: //positional modae
  334. stepper->run();
  335. break;
  336. case 1: // speed mode
  337. stepper->runSpeed();
  338. break;
  339. }
  340. _currentSpeed = stepper->speed();
  341. if (_currentSpeed) {direction = _currentSpeed>0 ? 1 : 0;}
  342. long int tempStep = stepper->currentPosition();
  343. if (tempStep != _currentStep){
  344. _currentStep = tempStep;
  345. //Serial.println(stepper->currentPosition());
  346. if (position_timer.expired(this)){
  347. push(connectors, ON_CHANGEPOSITION, 0, _currentStep, _currentSpeed);
  348. position_timer.setFromNow(this, POSITION_SEND_TIMER);
  349. }
  350. }
  351. }
  352. Atm_AccelStepper& Atm_AccelStepper::setMaxStep( long int maxStep ) {
  353. _maxStep = maxStep;
  354. return *this ;
  355. }
  356. Atm_AccelStepper& Atm_AccelStepper::setMaxSpeed( long int maxSpeed){
  357. max_speed = maxSpeed ;
  358. stepper->setMaxSpeed(max_speed);
  359. return *this ;
  360. }
  361. Atm_AccelStepper& Atm_AccelStepper::setHomingSpeed(long int homingSpeed){
  362. homing_speed = homingSpeed ;
  363. return *this ;
  364. }
  365. Atm_AccelStepper& Atm_AccelStepper::setAcceleration(long int acc){
  366. acceleration = acc ;
  367. stepper->setAcceleration(acceleration);
  368. return *this ;
  369. }
  370. Atm_AccelStepper& Atm_AccelStepper::setPosition(long int position){
  371. stepper->setCurrentPosition(position);
  372. _currentStep = position ;
  373. return *this ;
  374. }
  375. Atm_AccelStepper& Atm_AccelStepper::setPlayCompensation(long int steps){
  376. playSteps = steps ;
  377. }
  378. bool Atm_AccelStepper::compensatePlay(int speed){ //check if there is a change in direction,
  379. //use just after _targetStep update and before moving
  380. if(playSteps)
  381. { bool newDirection ;
  382. switch (runMode) {
  383. case 0: //positional modae
  384. newDirection = _targetStep > _currentStep;
  385. break;
  386. case 1: // speed mode
  387. newDirection = speed>0;
  388. break;
  389. }
  390. if (direction != newDirection){
  391. long int compensation = newDirection ? -playSteps : playSteps ;
  392. setPosition(_currentStep + compensation);
  393. Serial.print("play compensation ");
  394. Serial.println(compensation);
  395. return 1;
  396. }
  397. else{return 0;}}
  398. }
  399. long int Atm_AccelStepper::getPosition(){
  400. return stepper->currentPosition();;
  401. }
  402. long int Atm_AccelStepper::distanceToGo(){
  403. return stepper->distanceToGo();;
  404. }
  405. bool Atm_AccelStepper::isRunning(){
  406. return stepper->isRunning();
  407. }
  408. float Atm_AccelStepper::getSpeed(){
  409. return stepper->speed();
  410. }
  411. Atm_AccelStepper& Atm_AccelStepper::position_refresh(long int refresh_ms){
  412. POSITION_SEND_TIMER = refresh_ms ;
  413. return *this ;
  414. }
  415. Atm_AccelStepper& Atm_AccelStepper::move( long int stepRel) {
  416. _targetStep = _currentStep + stepRel;
  417. runMode = 0;
  418. stepper->moveTo(_targetStep);
  419. // stepper_update();
  420. _currentSpeed = stepper->speed();
  421. enable();
  422. trigger( EVT_MOVE );
  423. return *this;
  424. }
  425. Atm_AccelStepper& Atm_AccelStepper::moveTo( long int stepAbs) {
  426. _targetStep = stepAbs;
  427. runMode = 0;
  428. stepper->moveTo(_targetStep);
  429. _currentSpeed = stepper->speed();
  430. enable();
  431. trigger( EVT_MOVE );
  432. return *this;
  433. }
  434. Atm_AccelStepper& Atm_AccelStepper::movePercent( float percent) {
  435. _targetStep = (float)_currentStep + percent/100.*float(_maxStep);
  436. runMode = 0;
  437. // Serial.println(_targetStep);
  438. stepper->moveTo(_targetStep);
  439. _currentSpeed = stepper->speed();
  440. enable();
  441. trigger( EVT_MOVE );
  442. return *this;
  443. }
  444. Atm_AccelStepper& Atm_AccelStepper::moveToPercent( float percent) {
  445. _targetStep = (float) percent/100*float(_maxStep) ;
  446. runMode = 0;
  447. stepper->moveTo(_targetStep);
  448. _currentSpeed = stepper->speed();
  449. enable();
  450. trigger( EVT_MOVE );
  451. return *this;
  452. }
  453. Atm_AccelStepper& Atm_AccelStepper::rotate( long int speed) {
  454. runMode = 1;
  455. stepper->setSpeed( speed);
  456. _currentSpeed = stepper->speed();
  457. // stepper_update();
  458. enable();
  459. trigger( EVT_MOVE );
  460. return *this;
  461. }
  462. Atm_AccelStepper& Atm_AccelStepper::homing( bool homingDirection ){
  463. enable();
  464. homingDirection == 1 ? this->trigger(EVT_HOMING_HIGH) : this->trigger(EVT_HOMING_LOW);
  465. return *this;
  466. }
  467. int Atm_AccelStepper::limitLow_avg(bool limistate){
  468. limitLow_state_total = limitLow_state_total + limistate - limitLow_state_buf[limitLow_buf_head];
  469. limitLow_state_buf[limitLow_buf_head] = limistate;
  470. if ( limitLow_buf_head + 1 >= limit_buf_size ) {
  471. limitLow_buf_head = 0;
  472. } else {
  473. limitLow_buf_head++;
  474. }
  475. return limitLow_state_total > limit_buf_size / 2; //all values should agree
  476. }
  477. int Atm_AccelStepper::limitHigh_avg(bool limistate){
  478. limitHigh_state_total = limitHigh_state_total + limistate - limitHigh_state_buf[limitHigh_buf_head];
  479. limitHigh_state_buf[limitHigh_buf_head] = limistate;
  480. if ( limitHigh_buf_head + 1 >= limit_buf_size ) {
  481. limitHigh_buf_head = 0;
  482. } else {
  483. limitHigh_buf_head++;
  484. }
  485. return limitHigh_state_total > limit_buf_size / 2; //all values should agree
  486. }
  487. Atm_AccelStepper& Atm_AccelStepper::setEnablePin( int enablePin ){
  488. _enablePin = enablePin ;
  489. pinMode(_enablePin, OUTPUT);
  490. stepper->setEnablePin(_enablePin);
  491. return *this;
  492. }
  493. Atm_AccelStepper& Atm_AccelStepper::pinReversed( bool directionInvert,
  494. bool stepInvert, bool enableInvert){
  495. _enableReversed = enableInvert ;
  496. stepper->setPinsInverted(directionInvert, stepInvert, enableInvert);
  497. return *this;
  498. }
  499. Atm_AccelStepper& Atm_AccelStepper::limitLow_set(int mode, int pin, int reversed){
  500. _limitLow_Mode = mode ;
  501. _limitLow_Pin = pin ;
  502. _limitLow_Reversed = reversed ;
  503. if (_limitLow_Mode==1) {pinMode(_limitLow_Pin, INPUT_PULLUP);}
  504. if (_limitLow_Mode==2) {pinMode(_limitLow_Pin, INPUT);}
  505. return *this;
  506. }
  507. Atm_AccelStepper& Atm_AccelStepper::limitLow_isHard(bool hardlimit){
  508. _limitLow_Hard = hardlimit;
  509. return *this;
  510. }
  511. Atm_AccelStepper& Atm_AccelStepper::limitLow_setThresholds (int threshold_low, int threshold_high){
  512. _limitLow_Thresholds[0] = threshold_low ;
  513. _limitLow_Thresholds[1] = threshold_high ;
  514. return *this;
  515. }
  516. Atm_AccelStepper& Atm_AccelStepper::limitHigh_set(int mode, int pin, int reversed){
  517. _limitHigh_Mode = mode ;
  518. _limitHigh_Pin = pin ;
  519. _limitHigh_Reversed = reversed ;
  520. if (_limitHigh_Mode==1) {pinMode(_limitHigh_Pin, INPUT_PULLUP);}
  521. if (_limitHigh_Mode==2) {pinMode(_limitHigh_Pin, INPUT);}
  522. return *this;
  523. }
  524. Atm_AccelStepper& Atm_AccelStepper::limitHigh_isHard(bool hardlimit){
  525. _limitHigh_Hard = hardlimit;
  526. return *this;
  527. }
  528. Atm_AccelStepper& Atm_AccelStepper::limitHigh_setThresholds (int threshold_low, int threshold_high){
  529. _limitHigh_Thresholds[0] = threshold_low ;
  530. _limitHigh_Thresholds[1] = threshold_high ;
  531. return *this;
  532. }
  533. /* Public event methods
  534. *
  535. */
  536. Atm_AccelStepper& Atm_AccelStepper::disable() {
  537. trigger( EVT_DISABLE );
  538. return *this;
  539. }
  540. Atm_AccelStepper& Atm_AccelStepper::enable() {
  541. trigger( EVT_ENABLE );
  542. return *this;
  543. }
  544. // Atm_AccelStepper& Atm_AccelStepper::move() {
  545. // trigger( EVT_MOVE );
  546. // return *this;
  547. // }
  548. Atm_AccelStepper& Atm_AccelStepper::stop() {
  549. trigger( EVT_STOP );
  550. return *this;
  551. }
  552. Atm_AccelStepper& Atm_AccelStepper::emergency_stop() {
  553. trigger( EVT_EMERGENCY_STOP );
  554. return *this;
  555. }
  556. Atm_AccelStepper& Atm_AccelStepper::on_limit_low() {
  557. trigger( EVT_ON_LIMIT_LOW );
  558. return *this;
  559. }
  560. Atm_AccelStepper& Atm_AccelStepper::on_limit_high() {
  561. trigger( EVT_ON_LIMIT_HIGH );
  562. return *this;
  563. }
  564. Atm_AccelStepper& Atm_AccelStepper::on_target() {
  565. trigger( EVT_ON_TARGET );
  566. return *this;
  567. }
  568. /*
  569. * onChangeposition() push connector variants ( slots 1, autostore 0, broadcast 0 )
  570. */
  571. Atm_AccelStepper& Atm_AccelStepper::onChangeposition( Machine& machine, int event ) {
  572. onPush( connectors, ON_CHANGEPOSITION, 0, 1, 1, machine, event );
  573. return *this;
  574. }
  575. Atm_AccelStepper& Atm_AccelStepper::onChangeposition( atm_cb_push_t callback, int idx ) {
  576. onPush( connectors, ON_CHANGEPOSITION, 0, 1, 1, callback, idx );
  577. return *this;
  578. }
  579. /*
  580. * onChangestate() push connector variants ( slots 1, autostore 0, broadcast 0 )
  581. */
  582. Atm_AccelStepper& Atm_AccelStepper::onChangestate( Machine& machine, int event ) {
  583. onPush( connectors, ON_CHANGESTATE, 0, 1, 1, machine, event );
  584. return *this;
  585. }
  586. Atm_AccelStepper& Atm_AccelStepper::onChangestate( atm_cb_push_t callback, int idx ) {
  587. onPush( connectors, ON_CHANGESTATE, 0, 1, 1, callback, idx );
  588. return *this;
  589. }
  590. /*
  591. * onOnlimithigh() push connector variants ( slots 1, autostore 0, broadcast 0 )
  592. */
  593. Atm_AccelStepper& Atm_AccelStepper::onOnlimithigh( Machine& machine, int event ) {
  594. onPush( connectors, ON_ONLIMITHIGH, 0, 1, 1, machine, event );
  595. return *this;
  596. }
  597. Atm_AccelStepper& Atm_AccelStepper::onOnlimithigh( atm_cb_push_t callback, int idx ) {
  598. onPush( connectors, ON_ONLIMITHIGH, 0, 1, 1, callback, idx );
  599. return *this;
  600. }
  601. /*
  602. * onOnlimitlow() push connector variants ( slots 1, autostore 0, broadcast 0 )
  603. */
  604. Atm_AccelStepper& Atm_AccelStepper::onOnlimitlow( Machine& machine, int event ) {
  605. onPush( connectors, ON_ONLIMITLOW, 0, 1, 1, machine, event );
  606. return *this;
  607. }
  608. Atm_AccelStepper& Atm_AccelStepper::onOnlimitlow( atm_cb_push_t callback, int idx ) {
  609. onPush( connectors, ON_ONLIMITLOW, 0, 1, 1, callback, idx );
  610. return *this;
  611. }
  612. /*
  613. * onOntarget() push connector variants ( slots 1, autostore 0, broadcast 0 )
  614. */
  615. Atm_AccelStepper& Atm_AccelStepper::onOntarget( Machine& machine, int event ) {
  616. onPush( connectors, ON_ONTARGET, 0, 1, 1, machine, event );
  617. return *this;
  618. }
  619. Atm_AccelStepper& Atm_AccelStepper::onOntarget( atm_cb_push_t callback, int idx ) {
  620. onPush( connectors, ON_ONTARGET, 0, 1, 1, callback, idx );
  621. return *this;
  622. }
  623. /*
  624. * onStop() push connector variants ( slots 1, autostore 0, broadcast 0 )
  625. */
  626. Atm_AccelStepper& Atm_AccelStepper::onStop( Machine& machine, int event ) {
  627. onPush( connectors, ON_STOP, 0, 1, 1, machine, event );
  628. return *this;
  629. }
  630. Atm_AccelStepper& Atm_AccelStepper::onStop( atm_cb_push_t callback, int idx ) {
  631. onPush( connectors, ON_STOP, 0, 1, 1, callback, idx );
  632. return *this;
  633. }
  634. Atm_AccelStepper& Atm_AccelStepper::onOnhominglow( Machine& machine, int event ) {
  635. onPush( connectors, ON_ONHOMINGLOW, 0, 1, 1, machine, event );
  636. return *this;
  637. }
  638. Atm_AccelStepper& Atm_AccelStepper::onOnhominglow( atm_cb_push_t callback, int idx ) {
  639. onPush( connectors, ON_ONHOMINGLOW, 0, 1, 1, callback, idx );
  640. return *this;
  641. }
  642. Atm_AccelStepper& Atm_AccelStepper::onOnhominghigh( Machine& machine, int event ) {
  643. onPush( connectors, ON_ONHOMINGHIGH, 0, 1, 1, machine, event );
  644. return *this;
  645. }
  646. Atm_AccelStepper& Atm_AccelStepper::onOnhominghigh( atm_cb_push_t callback, int idx ) {
  647. onPush( connectors, ON_ONHOMINGHIGH, 0, 1, 1, callback, idx );
  648. return *this;
  649. }
  650. /* State trace method
  651. * Sets the symbol table and the default logging method for serial monitoring
  652. */
  653. Atm_AccelStepper& Atm_AccelStepper::trace( Stream & stream ) {
  654. Machine::setTrace( &stream, atm_serial_debug::trace,
  655. "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\0EVT_HOMING_LOW\0EVT_HOMING_HIGH\0ELSE\0DISABLED\0ENABLED\0RUNNING\0STOP\0HOMING_LOW\0HOMING_HIGH\0LIMIT_LOW\0LIMIT_HIGH" );
  656. return *this;
  657. }
  658. #endif