Atm_AccelStepper.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 = 5000;
  42. long int acceleration = 1000;
  43. Atm_AccelStepper& setMaxSpeed( long int maxSpeed = 5000);
  44. Atm_AccelStepper& setHomingSpeed( long int homingSpeed = 400);
  45. Atm_AccelStepper& setAcceleration( long int acc = 1000);
  46. Atm_AccelStepper& move(long int stepRel, long int maxSpeed = 5000, long int acc = 1000 );
  47. Atm_AccelStepper& moveTo(long int stepAbs, long int maxSpeed = 5000, long int acc = 1000 );
  48. Atm_AccelStepper& rotate(long int speed );
  49. Atm_AccelStepper& homing( bool direction, long int speed);
  50. int runMode = 0; // 0 uses run() for positioning, 1 uses runSpeed() for constant speed
  51. Atm_AccelStepper& position_refresh( long int refresh_ms = 1000);
  52. // Atm_AccelStepper& rotationReversed(bool reversed);
  53. Atm_AccelStepper& setEnablePin( int enablePin );
  54. Atm_AccelStepper& pinReversed( bool directionInvert=false, bool stepInvert=false, bool enableInvert=false );
  55. bool enabled ;
  56. Atm_AccelStepper& limitLow_set(int mode = 0, int pin = -1, int reversed=0);
  57. Atm_AccelStepper& limitLow_isHard(bool hardlimit = 1);
  58. Atm_AccelStepper& limitLow_setThresholds (int threshold_low=510, int threshold_high = 1024);
  59. Atm_AccelStepper& limitHigh_set(int mode = 0, int pin = -1, int reversed=0);
  60. Atm_AccelStepper& limitHigh_isHard(bool hardlimit = 1);
  61. Atm_AccelStepper& limitHigh_setThresholds (int threshold_low=510, int threshold_high = 1024);
  62. bool limitLow_State;
  63. bool limitHigh_State;
  64. private:
  65. enum { ENT_DISABLED, ENT_ENABLED,ENT_RUNNING, LP_RUNNING, ENT_STOP, LP_STOP, ENT_HOMING_LOW, LP_HOMING_LOW, EXT_HOMING_LOW, ENT_HOMING_HIGH, LP_HOMING_HIGH, EXT_HOMING_HIGH, ENT_LIMIT_LOW, ENT_LIMIT_HIGH }; // ACTIONS
  66. enum { ON_CHANGEPOSITION, ON_CHANGESTATE, ON_ONLIMITHIGH, ON_ONLIMITLOW, ON_ONTARGET, ON_STOP, CONN_MAX }; // CONNECTORS
  67. atm_connector connectors[CONN_MAX];
  68. int event( int id );
  69. void action( int id );
  70. void stepper_update(void);
  71. long int _currentStep = 0;
  72. long int _targetStep = 0;
  73. long int _maxStep ;
  74. atm_timer_millis position_timer ;
  75. int POSITION_SEND_TIMER = 100 ;
  76. // bool _rotationReversed = 0 ;
  77. int _enablePin = -1;
  78. bool _enableReversed = 0 ;
  79. atm_timer_millis idle_timer ;
  80. int IDLE_TIMEOUT_DURATION = 500000 ;
  81. int _limitLow_Pin;
  82. int _limitLow_Mode = 0; //0 no limit, 1 digital, 2 analog with thresholds
  83. bool _limitLow_Reversed ; //invert logic of limit switches
  84. int _limitLow_Thresholds[2] ; //analog value range for two analog limits
  85. bool _limitLow_Hard = 1;
  86. int _limitHigh_Pin;
  87. int _limitHigh_Mode; //0 no limit, 1 digital, 2 analog with thresholds
  88. bool _limitHigh_Reversed ; //invert logic of limit switches
  89. int _limitHigh_Thresholds[2] ; //analog value range for two analog limits
  90. bool _limitHigh_Hard = 1 ;
  91. int _isHoming = 0 ;
  92. void updateLimitSwitch();
  93. };
  94. /*
  95. Automaton::ATML::begin - Automaton Markup Language
  96. <?xml version="1.0" encoding="UTF-8"?>
  97. <machines>
  98. <machine name="Atm_AccelStepper">
  99. <states>
  100. <DISABLED index="0" on_enter="ENT_DISABLED">
  101. <EVT_ENABLE>ENABLED</EVT_ENABLE>
  102. </DISABLED>
  103. <ENABLED index="1" on_enter="ENT_ENABLED">
  104. <EVT_DISABLE>DISABLED</EVT_DISABLE>
  105. <EVT_ENABLED_TIMEOUT>DISABLED</EVT_ENABLED_TIMEOUT>
  106. <EVT_STOP>STOP</EVT_STOP>
  107. <EVT_EMERGENCY_STOP>STOP</EVT_EMERGENCY_STOP>
  108. </ENABLED>
  109. <RUNNING index="2" on_loop="LP_RUNNING">
  110. <EVT_DISABLE>DISABLED</EVT_DISABLE>
  111. <EVT_STOP>STOP</EVT_STOP>
  112. <EVT_EMERGENCY_STOP>STOP</EVT_EMERGENCY_STOP>
  113. <EVT_ON_LIMIT_LOW>LIMIT_LOW</EVT_ON_LIMIT_LOW>
  114. <EVT_ON_LIMIT_HIGH>LIMIT_HIGH</EVT_ON_LIMIT_HIGH>
  115. <EVT_ON_TARGET>ENABLED</EVT_ON_TARGET>
  116. </RUNNING>
  117. <STOP index="3">
  118. <EVT_DISABLE>DISABLED</EVT_DISABLE>
  119. <EVT_STOP>STOP</EVT_STOP>
  120. <EVT_EMERGENCY_STOP>STOP</EVT_EMERGENCY_STOP>
  121. </STOP>
  122. <HOMING_LOW index="4" on_enter="ENT_HOMING_LOW" on_exit="EXT_HOMING_LOW">
  123. <EVT_DISABLE>DISABLED</EVT_DISABLE>
  124. <EVT_STOP>STOP</EVT_STOP>
  125. <EVT_EMERGENCY_STOP>STOP</EVT_EMERGENCY_STOP>
  126. <EVT_ON_LIMIT_LOW>ENABLED</EVT_ON_LIMIT_LOW>
  127. </HOMING_LOW>
  128. <HOMING_HIGH index="5" on_enter="ENT_HOMING_HIGH" on_exit="EXT_HOMING_HIGH">
  129. <EVT_DISABLE>DISABLED</EVT_DISABLE>
  130. <EVT_STOP>STOP</EVT_STOP>
  131. <EVT_EMERGENCY_STOP>STOP</EVT_EMERGENCY_STOP>
  132. <EVT_ON_LIMIT_HIGH>ENABLED</EVT_ON_LIMIT_HIGH>
  133. </HOMING_HIGH>
  134. <LIMIT_LOW index="6" on_enter="ENT_LIMIT_LOW">
  135. <EVT_STOP>STOP</EVT_STOP>
  136. <EVT_EMERGENCY_STOP>STOP</EVT_EMERGENCY_STOP>
  137. <EVT_ON_LIMIT_LOW>LIMIT_LOW</EVT_ON_LIMIT_LOW>
  138. </LIMIT_LOW>
  139. <LIMIT_HIGH index="7" on_enter="ENT_LIMIT_HIGH">
  140. <EVT_STOP>STOP</EVT_STOP>
  141. <EVT_EMERGENCY_STOP>STOP</EVT_EMERGENCY_STOP>
  142. <EVT_ON_LIMIT_HIGH>LIMIT_HIGH</EVT_ON_LIMIT_HIGH>
  143. </LIMIT_HIGH>
  144. </states>
  145. <events>
  146. <EVT_DISABLE index="0" access="MIXED"/>
  147. <EVT_ENABLE index="1" access="MIXED"/>
  148. <EVT_ENABLED_TIMEOUT index="2" access="PRIVATE"/>
  149. <EVT_STOP index="3" access="MIXED"/>
  150. <EVT_EMERGENCY_STOP index="4" access="MIXED"/>
  151. <EVT_ON_LIMIT_LOW index="5" access="MIXED"/>
  152. <EVT_ON_LIMIT_HIGH index="6" access="MIXED"/>
  153. <EVT_ON_TARGET index="7" access="MIXED"/>
  154. </events>
  155. <connectors>
  156. <CHANGEPOSITION autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  157. <CHANGESTATE autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  158. <ONLIMITHIGH autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  159. <ONLIMITLOW autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  160. <ONTARGET autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  161. <STOP autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  162. </connectors>
  163. <methods>
  164. </methods>
  165. </machine>
  166. </machines>
  167. Automaton::ATML::end
  168. */