Atm_AccelStepper.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #pragma once
  2. #include <Automaton.h>
  3. #include <AccelStepper.h>
  4. class Atm_AccelStepper: public Machine {
  5. public:
  6. enum { DISABLE, ENABLED, RUNNING, STOP, HOMING_LOW, HOMING_HIGH, LIMIT_LOW, LIMIT_HIGH }; // STATES
  7. enum { EVT_DISABLE, EVT_ENABLE, EVT_ENABLED_TIMEOUT, EVT_MOVE, EVT_STOP,
  8. EVT_EMERGENCY_STOP, EVT_ON_LIMIT_LOW, EVT_ON_LIMIT_HIGH, EVT_ON_TARGET,
  9. EVT_HOMING_LOW, EVT_HOMING_HIGH, ELSE }; // EVENTS
  10. Atm_AccelStepper( void ) : Machine() {};
  11. Atm_AccelStepper& begin( int step_pin, int dir_pin );
  12. Atm_AccelStepper& trace( Stream & stream );
  13. Atm_AccelStepper& trigger( int event );
  14. int state( void );
  15. Atm_AccelStepper& onChangeposition( Machine& machine, int event = 0 );
  16. Atm_AccelStepper& onChangeposition( atm_cb_push_t callback, int idx = 0 );
  17. Atm_AccelStepper& onChangestate( Machine& machine, int event = 0 );
  18. Atm_AccelStepper& onChangestate( atm_cb_push_t callback, int idx = 0 );
  19. Atm_AccelStepper& onOnlimithigh( Machine& machine, int event = 0 );
  20. Atm_AccelStepper& onOnlimithigh( atm_cb_push_t callback, int idx = 0 );
  21. Atm_AccelStepper& onOnlimitlow( Machine& machine, int event = 0 );
  22. Atm_AccelStepper& onOnlimitlow( atm_cb_push_t callback, int idx = 0 );
  23. Atm_AccelStepper& onOntarget( Machine& machine, int event = 0 );
  24. Atm_AccelStepper& onOntarget( atm_cb_push_t callback, int idx = 0 );
  25. Atm_AccelStepper& onStop( Machine& machine, int event = 0 );
  26. Atm_AccelStepper& onStop( atm_cb_push_t callback, int idx = 0 );
  27. Atm_AccelStepper& onOnhominglow( Machine& machine, int event = 0 );
  28. Atm_AccelStepper& onOnhominglow( atm_cb_push_t callback, int idx = 0 );
  29. Atm_AccelStepper& onOnhominghigh( Machine& machine, int event = 0 );
  30. Atm_AccelStepper& onOnhominghigh( atm_cb_push_t callback, int idx = 0 );
  31. Atm_AccelStepper& disable( void );
  32. Atm_AccelStepper& enable( void );
  33. //Atm_AccelStepper& move( void );
  34. Atm_AccelStepper& stop( void );
  35. Atm_AccelStepper& emergency_stop( void );
  36. Atm_AccelStepper& on_limit_low( void );
  37. Atm_AccelStepper& on_limit_high( void );
  38. Atm_AccelStepper& on_target( void );
  39. AccelStepper *stepper;
  40. long int homing_speed = 100;
  41. long int max_speed = 10000;
  42. long int acceleration = 1000;
  43. Atm_AccelStepper& setMaxSpeed( long int maxSpeed );
  44. Atm_AccelStepper& setHomingSpeed( long int homingSpeed);
  45. Atm_AccelStepper& setAcceleration( long int acc);
  46. Atm_AccelStepper& setPosition( long int position);
  47. long int getPosition();
  48. long int distanceToGo();
  49. bool isRunning();
  50. float getSpeed();
  51. Atm_AccelStepper& move(long int stepRel );
  52. Atm_AccelStepper& moveTo(long int stepAbs );
  53. Atm_AccelStepper& rotate(long int speed );
  54. Atm_AccelStepper& homing( bool direction );
  55. int runMode = 0; // 0 uses run() for positioning, 1 uses runSpeed() for constant speed
  56. Atm_AccelStepper& position_refresh( long int refresh_ms = 1000);
  57. // Atm_AccelStepper& rotationReversed(bool reversed);
  58. Atm_AccelStepper& setEnablePin( int enablePin );
  59. Atm_AccelStepper& pinReversed( bool directionInvert=false, bool stepInvert=false, bool enableInvert=false );
  60. bool enabled ;
  61. Atm_AccelStepper& limitLow_set(int mode = 0, int pin = -1, int reversed=0);
  62. Atm_AccelStepper& limitLow_isHard(bool hardlimit = 1);
  63. Atm_AccelStepper& limitLow_setThresholds (int threshold_low=510, int threshold_high = 1024);
  64. Atm_AccelStepper& limitHigh_set(int mode = 0, int pin = -1, int reversed=0);
  65. Atm_AccelStepper& limitHigh_isHard(bool hardlimit = 1);
  66. Atm_AccelStepper& limitHigh_setThresholds (int threshold_low=510, int threshold_high = 1024);
  67. bool limitLow_State;
  68. bool limitHigh_State;
  69. bool previous_state = limitLow_State;
  70. private:
  71. // ACTIONS
  72. enum { ENT_DISABLED, ENT_ENABLED,ENT_RUNNING, LP_RUNNING, ENT_STOP, LP_STOP,
  73. ENT_HOMING_LOW, LP_HOMING_LOW, EXT_HOMING_LOW,
  74. ENT_HOMING_HIGH, LP_HOMING_HIGH, EXT_HOMING_HIGH,
  75. ENT_LIMIT_LOW, LP_LIMIT_LOW, ENT_LIMIT_HIGH, LP_LIMIT_HIGH };
  76. // CONNECTORS
  77. enum { ON_CHANGEPOSITION, ON_CHANGESTATE, ON_ONLIMITHIGH,
  78. ON_ONLIMITLOW, ON_ONTARGET, ON_STOP, CONN_MAX };
  79. atm_connector connectors[CONN_MAX];
  80. int event( int id );
  81. void action( int id );
  82. void stepper_update(void);
  83. long int _currentStep = 0;
  84. long int _targetStep = 0;
  85. long int _maxStep ;
  86. atm_timer_millis position_timer ;
  87. int POSITION_SEND_TIMER = 50 ;
  88. // bool _rotationReversed = 0 ;
  89. int _enablePin = -1;
  90. bool _enableReversed = 0 ;
  91. atm_timer_millis idle_timer ;
  92. int IDLE_TIMEOUT_DURATION = 500000 ;
  93. int _limitLow_Pin;
  94. int _limitLow_Mode = 0; //0 no limit, 1 digital, 2 analog with thresholds
  95. bool _limitLow_Reversed ; //invert logic of limit switches
  96. int _limitLow_Thresholds[2] ; //analog value range for two analog limits
  97. bool _limitLow_Hard = 0;
  98. int _limitHigh_Pin;
  99. int _limitHigh_Mode=0; //0 no limit, 1 digital, 2 analog with thresholds
  100. bool _limitHigh_Reversed ; //invert logic of limit switches
  101. int _limitHigh_Thresholds[2] ; //analog value range for two analog limits
  102. bool _limitHigh_Hard = 0 ;
  103. int _isHoming = 0 ;
  104. void updateLimitSwitch();
  105. };
  106. /*
  107. Automaton::ATML::begin - Automaton Markup Language
  108. <?xml version="1.0" encoding="UTF-8"?>
  109. <machines>
  110. <machine name="Atm_AccelStepper">
  111. <states>
  112. <DISABLED index="0" on_enter="ENT_DISABLED">
  113. <EVT_ENABLE>ENABLED</EVT_ENABLE>
  114. </DISABLED>
  115. <ENABLED index="1" on_enter="ENT_ENABLED">
  116. <EVT_DISABLE>DISABLED</EVT_DISABLE>
  117. <EVT_ENABLED_TIMEOUT>DISABLED</EVT_ENABLED_TIMEOUT>
  118. <EVT_STOP>STOP</EVT_STOP>
  119. <EVT_EMERGENCY_STOP>STOP</EVT_EMERGENCY_STOP>
  120. </ENABLED>
  121. <RUNNING index="2" on_loop="LP_RUNNING">
  122. <EVT_DISABLE>DISABLED</EVT_DISABLE>
  123. <EVT_STOP>STOP</EVT_STOP>
  124. <EVT_EMERGENCY_STOP>STOP</EVT_EMERGENCY_STOP>
  125. <EVT_ON_LIMIT_LOW>LIMIT_LOW</EVT_ON_LIMIT_LOW>
  126. <EVT_ON_LIMIT_HIGH>LIMIT_HIGH</EVT_ON_LIMIT_HIGH>
  127. <EVT_ON_TARGET>ENABLED</EVT_ON_TARGET>
  128. </RUNNING>
  129. <STOP index="3">
  130. <EVT_DISABLE>DISABLED</EVT_DISABLE>
  131. <EVT_STOP>STOP</EVT_STOP>
  132. <EVT_EMERGENCY_STOP>STOP</EVT_EMERGENCY_STOP>
  133. </STOP>
  134. <HOMING_LOW index="4" on_enter="ENT_HOMING_LOW" on_exit="EXT_HOMING_LOW">
  135. <EVT_DISABLE>DISABLED</EVT_DISABLE>
  136. <EVT_STOP>STOP</EVT_STOP>
  137. <EVT_EMERGENCY_STOP>STOP</EVT_EMERGENCY_STOP>
  138. <EVT_ON_LIMIT_LOW>ENABLED</EVT_ON_LIMIT_LOW>
  139. </HOMING_LOW>
  140. <HOMING_HIGH index="5" on_enter="ENT_HOMING_HIGH" on_exit="EXT_HOMING_HIGH">
  141. <EVT_DISABLE>DISABLED</EVT_DISABLE>
  142. <EVT_STOP>STOP</EVT_STOP>
  143. <EVT_EMERGENCY_STOP>STOP</EVT_EMERGENCY_STOP>
  144. <EVT_ON_LIMIT_HIGH>ENABLED</EVT_ON_LIMIT_HIGH>
  145. </HOMING_HIGH>
  146. <LIMIT_LOW index="6" on_enter="ENT_LIMIT_LOW">
  147. <EVT_STOP>STOP</EVT_STOP>
  148. <EVT_EMERGENCY_STOP>STOP</EVT_EMERGENCY_STOP>
  149. <EVT_ON_LIMIT_LOW>LIMIT_LOW</EVT_ON_LIMIT_LOW>
  150. </LIMIT_LOW>
  151. <LIMIT_HIGH index="7" on_enter="ENT_LIMIT_HIGH">
  152. <EVT_STOP>STOP</EVT_STOP>
  153. <EVT_EMERGENCY_STOP>STOP</EVT_EMERGENCY_STOP>
  154. <EVT_ON_LIMIT_HIGH>LIMIT_HIGH</EVT_ON_LIMIT_HIGH>
  155. </LIMIT_HIGH>
  156. </states>
  157. <events>
  158. <EVT_DISABLE index="0" access="MIXED"/>
  159. <EVT_ENABLE index="1" access="MIXED"/>
  160. <EVT_ENABLED_TIMEOUT index="2" access="PRIVATE"/>
  161. <EVT_STOP index="3" access="MIXED"/>
  162. <EVT_EMERGENCY_STOP index="4" access="MIXED"/>
  163. <EVT_ON_LIMIT_LOW index="5" access="MIXED"/>
  164. <EVT_ON_LIMIT_HIGH index="6" access="MIXED"/>
  165. <EVT_ON_TARGET index="7" access="MIXED"/>
  166. </events>
  167. <connectors>
  168. <CHANGEPOSITION autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  169. <CHANGESTATE autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  170. <ONLIMITHIGH autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  171. <ONLIMITLOW autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  172. <ONTARGET autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  173. <STOP autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  174. </connectors>
  175. <methods>
  176. </methods>
  177. </machine>
  178. </machines>
  179. Automaton::ATML::end
  180. */