Atm_AccelStepper.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #pragma once
  2. //#ifdef AccelStepper_h
  3. #include <Automaton.h>
  4. #include "AccelStepper.h"
  5. class Atm_AccelStepper: public Machine {
  6. public:
  7. enum { DISABLE, ENABLED, RUNNING, STOP, HOMING_LOW, HOMING_HIGH, LIMIT_LOW, LIMIT_HIGH }; // STATES
  8. enum { EVT_DISABLE, EVT_ENABLE, EVT_ENABLED_TIMEOUT, EVT_MOVE, EVT_STOP,
  9. EVT_EMERGENCY_STOP, EVT_ON_LIMIT_LOW, EVT_ON_LIMIT_HIGH, EVT_ON_TARGET,
  10. EVT_HOMING_LOW, EVT_HOMING_HIGH, ELSE }; // EVENTS
  11. Atm_AccelStepper( void ) : Machine() {};
  12. Atm_AccelStepper& begin( int step_pin, int dir_pin );
  13. Atm_AccelStepper& trace( Stream & stream );
  14. Atm_AccelStepper& trigger( int event );
  15. int state( void );
  16. Atm_AccelStepper& onChangeposition( Machine& machine, int event = 0 );
  17. Atm_AccelStepper& onChangeposition( atm_cb_push_t callback, int idx = 0 );
  18. Atm_AccelStepper& onChangestate( Machine& machine, int event = 0 );
  19. Atm_AccelStepper& onChangestate( atm_cb_push_t callback, int idx = 0 );
  20. Atm_AccelStepper& onOnlimithigh( Machine& machine, int event = 0 );
  21. Atm_AccelStepper& onOnlimithigh( atm_cb_push_t callback, int idx = 0 );
  22. Atm_AccelStepper& onOnlimitlow( Machine& machine, int event = 0 );
  23. Atm_AccelStepper& onOnlimitlow( atm_cb_push_t callback, int idx = 0 );
  24. Atm_AccelStepper& onOntarget( Machine& machine, int event = 0 );
  25. Atm_AccelStepper& onOntarget( atm_cb_push_t callback, int idx = 0 );
  26. Atm_AccelStepper& onStop( Machine& machine, int event = 0 );
  27. Atm_AccelStepper& onStop( atm_cb_push_t callback, int idx = 0 );
  28. Atm_AccelStepper& onOnhominglow( Machine& machine, int event = 0 );
  29. Atm_AccelStepper& onOnhominglow( atm_cb_push_t callback, int idx = 0 );
  30. Atm_AccelStepper& onOnhominghigh( Machine& machine, int event = 0 );
  31. Atm_AccelStepper& onOnhominghigh( atm_cb_push_t callback, int idx = 0 );
  32. Atm_AccelStepper& disable( void );
  33. Atm_AccelStepper& enable( void );
  34. //Atm_AccelStepper& move( void );
  35. Atm_AccelStepper& stop( void );
  36. Atm_AccelStepper& emergency_stop( void );
  37. Atm_AccelStepper& on_limit_low( void );
  38. Atm_AccelStepper& on_limit_high( void );
  39. Atm_AccelStepper& on_target( void );
  40. AccelStepper *stepper;
  41. long int _maxStep = 0; //0 is undefined, can be set using setMaxstep or homingHigh
  42. long int homing_speed = 1000;
  43. bool homingLow_done = 0 ;
  44. bool homingHigh_done = 0 ;
  45. long int max_speed = 1000;
  46. bool direction ; //0 is towards negative, 1 towards positive
  47. long int acceleration = 100;
  48. long int playSteps = 0; //steps of play to compensate when moving. Added to targetposition when moving towards positive
  49. Atm_AccelStepper& setMaxStep( long int maxStep );
  50. Atm_AccelStepper& setMaxSpeed( long int maxSpeed );
  51. Atm_AccelStepper& setHomingSpeed( long int homingSpeed);
  52. Atm_AccelStepper& setAcceleration( long int acc);
  53. Atm_AccelStepper& setPosition( long int position);
  54. Atm_AccelStepper& setPlayCompensation(long int steps);
  55. bool compensatePlay(int speed);
  56. long int getPosition();
  57. long int distanceToGo();
  58. bool isRunning();
  59. float getSpeed();
  60. Atm_AccelStepper& move(long int stepRel);
  61. Atm_AccelStepper& moveTo(long int stepAbs);
  62. Atm_AccelStepper& movePercent(float percent);
  63. Atm_AccelStepper& moveToPercent(float percent);
  64. Atm_AccelStepper& rotate(long int speed );
  65. Atm_AccelStepper& homing( bool direction );
  66. int runMode = 0; // 0 uses run() for positioning, 1 uses runSpeed() for constant speed
  67. Atm_AccelStepper& position_refresh( long int refresh_ms = 1000);
  68. // Atm_AccelStepper& rotationReversed(bool reversed);
  69. Atm_AccelStepper& setEnablePin( int enablePin );
  70. Atm_AccelStepper& pinReversed( bool directionInvert=false, bool stepInvert=false, bool enableInvert=false );
  71. bool enabled ;
  72. //limits public methods and variables
  73. Atm_AccelStepper& limitLow_set(int mode = 0, int pin = -1, int reversed=0);
  74. Atm_AccelStepper& limitLow_isHard(bool hardlimit = 1);
  75. Atm_AccelStepper& limitLow_setThresholds (int threshold_low=510, int threshold_high = 1024);
  76. Atm_AccelStepper& limitHigh_set(int mode = 0, int pin = -1, int reversed=0);
  77. Atm_AccelStepper& limitHigh_isHard(bool hardlimit = 1);
  78. Atm_AccelStepper& limitHigh_setThresholds (int threshold_low=510, int threshold_high = 1024);
  79. bool limitLow_State_raw;
  80. bool limitLow_State = 0;
  81. bool limitHigh_State = 0;
  82. bool limitHigh_State_raw;
  83. bool limitLow_State_prev;
  84. bool limitHigh_State_prev;
  85. const static int limit_buf_size = 25 ;
  86. bool limitLow_state_buf [limit_buf_size] ;
  87. int limitLow_state_total = 0 ;
  88. int limitLow_buf_head ;
  89. int limitLow_avg(bool limitState);
  90. bool limitHigh_state_buf [limit_buf_size] ;
  91. int limitHigh_state_total = 0 ;
  92. int limitHigh_buf_head ;
  93. int limitHigh_avg(bool limitState);
  94. bool changed = 0 ; //temp container to test if some value changed
  95. private:
  96. // ACTIONS
  97. enum { ENT_DISABLED, ENT_ENABLED,ENT_RUNNING, LP_RUNNING, ENT_STOP, LP_STOP,
  98. ENT_HOMING_LOW, LP_HOMING_LOW, EXT_HOMING_LOW,
  99. ENT_HOMING_HIGH, LP_HOMING_HIGH, EXT_HOMING_HIGH,
  100. ENT_LIMIT_LOW, LP_LIMIT_LOW, ENT_LIMIT_HIGH, LP_LIMIT_HIGH };
  101. // CONNECTORS
  102. enum { ON_CHANGEPOSITION, ON_CHANGESTATE, ON_ONLIMITHIGH,
  103. ON_ONLIMITLOW, ON_ONTARGET, ON_STOP, ON_ONHOMINGLOW, ON_ONHOMINGHIGH, CONN_MAX };
  104. atm_connector connectors[CONN_MAX];
  105. int event( int id );
  106. void action( int id );
  107. //positionning private variables
  108. long int _currentStep = 0;
  109. long int _targetStep = 0;
  110. long int _currentSpeed;
  111. atm_timer_millis position_timer ;
  112. int POSITION_SEND_TIMER = 50 ;
  113. void stepper_update(void);
  114. // bool _rotationReversed = 0 ;
  115. //enable private variables
  116. int _enablePin = -1;
  117. bool _enableReversed = 0 ;
  118. //enable timeout not implemented yet
  119. // atm_timer_millis idle_timer ;
  120. // int IDLE_TIMEOUT_DURATION = 500000 ;
  121. //Limits private variables
  122. int _limitLow_Pin;
  123. int _limitLow_Mode = 0; //0 no limit, 1 digital, 2 analog with thresholds
  124. bool _limitLow_Reversed ; //invert logic of limit switches
  125. int _limitLow_Thresholds[2] ; //analog value range for two analog limits
  126. bool _limitLow_Hard = 0;
  127. int _limitHigh_Pin;
  128. int _limitHigh_Mode=0; //0 no limit, 1 digital, 2 analog with thresholds
  129. bool _limitHigh_Reversed ; //invert logic of limit switches
  130. int _limitHigh_Thresholds[2] ; //analog value range for two analog limits
  131. bool _limitHigh_Hard = 0 ;
  132. // int _isHoming = 0 ;
  133. atm_timer_millis limits_timer ;
  134. int LIMIT_UPDATE_RATE = 25 ;
  135. unsigned long limit_update_offset;
  136. // void updateLimitSwitch();
  137. };
  138. //#endif
  139. /*
  140. Automaton::ATML::begin - Automaton Markup Language
  141. <?xml version="1.0" encoding="UTF-8"?>
  142. <machines>
  143. <machine name="Atm_AccelStepper">
  144. <states>
  145. <DISABLED index="0" on_enter="ENT_DISABLED">
  146. <EVT_ENABLE>ENABLED</EVT_ENABLE>
  147. </DISABLED>
  148. <ENABLED index="1" on_enter="ENT_ENABLED">
  149. <EVT_DISABLE>DISABLED</EVT_DISABLE>
  150. <EVT_ENABLED_TIMEOUT>DISABLED</EVT_ENABLED_TIMEOUT>
  151. <EVT_STOP>STOP</EVT_STOP>
  152. <EVT_EMERGENCY_STOP>STOP</EVT_EMERGENCY_STOP>
  153. </ENABLED>
  154. <RUNNING index="2" on_loop="LP_RUNNING">
  155. <EVT_DISABLE>DISABLED</EVT_DISABLE>
  156. <EVT_STOP>STOP</EVT_STOP>
  157. <EVT_EMERGENCY_STOP>STOP</EVT_EMERGENCY_STOP>
  158. <EVT_ON_LIMIT_LOW>LIMIT_LOW</EVT_ON_LIMIT_LOW>
  159. <EVT_ON_LIMIT_HIGH>LIMIT_HIGH</EVT_ON_LIMIT_HIGH>
  160. <EVT_ON_TARGET>ENABLED</EVT_ON_TARGET>
  161. </RUNNING>
  162. <STOP index="3">
  163. <EVT_DISABLE>DISABLED</EVT_DISABLE>
  164. <EVT_STOP>STOP</EVT_STOP>
  165. <EVT_EMERGENCY_STOP>STOP</EVT_EMERGENCY_STOP>
  166. </STOP>
  167. <HOMING_LOW index="4" on_enter="ENT_HOMING_LOW" on_exit="EXT_HOMING_LOW">
  168. <EVT_DISABLE>DISABLED</EVT_DISABLE>
  169. <EVT_STOP>STOP</EVT_STOP>
  170. <EVT_EMERGENCY_STOP>STOP</EVT_EMERGENCY_STOP>
  171. <EVT_ON_LIMIT_LOW>ENABLED</EVT_ON_LIMIT_LOW>
  172. </HOMING_LOW>
  173. <HOMING_HIGH index="5" on_enter="ENT_HOMING_HIGH" on_exit="EXT_HOMING_HIGH">
  174. <EVT_DISABLE>DISABLED</EVT_DISABLE>
  175. <EVT_STOP>STOP</EVT_STOP>
  176. <EVT_EMERGENCY_STOP>STOP</EVT_EMERGENCY_STOP>
  177. <EVT_ON_LIMIT_HIGH>ENABLED</EVT_ON_LIMIT_HIGH>
  178. </HOMING_HIGH>
  179. <LIMIT_LOW index="6" on_enter="ENT_LIMIT_LOW">
  180. <EVT_STOP>STOP</EVT_STOP>
  181. <EVT_EMERGENCY_STOP>STOP</EVT_EMERGENCY_STOP>
  182. <EVT_ON_LIMIT_LOW>LIMIT_LOW</EVT_ON_LIMIT_LOW>
  183. </LIMIT_LOW>
  184. <LIMIT_HIGH index="7" on_enter="ENT_LIMIT_HIGH">
  185. <EVT_STOP>STOP</EVT_STOP>
  186. <EVT_EMERGENCY_STOP>STOP</EVT_EMERGENCY_STOP>
  187. <EVT_ON_LIMIT_HIGH>LIMIT_HIGH</EVT_ON_LIMIT_HIGH>
  188. </LIMIT_HIGH>
  189. </states>
  190. <events>
  191. <EVT_DISABLE index="0" access="MIXED"/>
  192. <EVT_ENABLE index="1" access="MIXED"/>
  193. <EVT_ENABLED_TIMEOUT index="2" access="PRIVATE"/>
  194. <EVT_STOP index="3" access="MIXED"/>
  195. <EVT_EMERGENCY_STOP index="4" access="MIXED"/>
  196. <EVT_ON_LIMIT_LOW index="5" access="MIXED"/>
  197. <EVT_ON_LIMIT_HIGH index="6" access="MIXED"/>
  198. <EVT_ON_TARGET index="7" access="MIXED"/>
  199. </events>
  200. <connectors>
  201. <CHANGEPOSITION autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  202. <CHANGESTATE autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  203. <ONLIMITHIGH autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  204. <ONLIMITLOW autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  205. <ONTARGET autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  206. <STOP autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  207. </connectors>
  208. <methods>
  209. </methods>
  210. </machine>
  211. </machines>
  212. Automaton::ATML::end
  213. */