Atm_AccelStepper.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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_STOP, EVT_EMERGENCY_STOP, EVT_ON_LIMIT_LOW, EVT_ON_LIMIT_HIGH, EVT_ON_TARGET, ELSE }; // EVENTS
  8. Atm_AccelStepper( void ) : Machine() {};
  9. Atm_AccelStepper& begin( int step_pin, int dir_pin );
  10. Atm_AccelStepper& trace( Stream & stream );
  11. Atm_AccelStepper& trigger( int event );
  12. int state( void );
  13. Atm_AccelStepper& onChangeposition( Machine& machine, int event = 0 );
  14. Atm_AccelStepper& onChangeposition( atm_cb_push_t callback, int idx = 0 );
  15. Atm_AccelStepper& onChangestate( Machine& machine, int event = 0 );
  16. Atm_AccelStepper& onChangestate( atm_cb_push_t callback, int idx = 0 );
  17. Atm_AccelStepper& onOnlimithigh( Machine& machine, int event = 0 );
  18. Atm_AccelStepper& onOnlimithigh( atm_cb_push_t callback, int idx = 0 );
  19. Atm_AccelStepper& onOnlimitlow( Machine& machine, int event = 0 );
  20. Atm_AccelStepper& onOnlimitlow( atm_cb_push_t callback, int idx = 0 );
  21. Atm_AccelStepper& onOntarget( Machine& machine, int event = 0 );
  22. Atm_AccelStepper& onOntarget( atm_cb_push_t callback, int idx = 0 );
  23. Atm_AccelStepper& onStop( Machine& machine, int event = 0 );
  24. Atm_AccelStepper& onStop( atm_cb_push_t callback, int idx = 0 );
  25. Atm_AccelStepper& disable( void );
  26. Atm_AccelStepper& enable( void );
  27. Atm_AccelStepper& stop( void );
  28. Atm_AccelStepper& emergency_stop( void );
  29. Atm_AccelStepper& on_limit_low( void );
  30. Atm_AccelStepper& on_limit_high( void );
  31. Atm_AccelStepper& on_target( void );
  32. AccelStepper *stepper;
  33. Atm_AccelStepper& move( long int stepRel );
  34. Atm_AccelStepper& moveTo( long int stepAbs );
  35. Atm_AccelStepper& rotate( long int Speed );
  36. Atm_AccelStepper& homing( bool direction );
  37. bool runMode = 0; // 0 uses run() for positioning, 1 uses runSpeed() for constant speed
  38. Atm_AccelStepper& setEnablePin( int enablePin );
  39. Atm_AccelStepper& enableReversed( bool reverse );
  40. bool enabled ;
  41. Atm_AccelStepper& limitLow_set(int mode = 0, int pin = -1, int reversed=0);
  42. Atm_AccelStepper& limitLow_setThresholds (int threshold_low=510, int threshold_high = 1024);
  43. Atm_AccelStepper& limitHigh_set(int mode = 0, int pin = -1, int reversed=0);
  44. Atm_AccelStepper& limitHigh_setThresholds (int threshold_low=510, int threshold_high = 1024);
  45. bool limitLow_State;
  46. bool limitHigh_State;
  47. private:
  48. enum { ENT_DISABLED, ENT_ENABLED,ENT_RUNNING, LP_RUNNING, ENT_HOMING_LOW, EXT_HOMING_LOW, ENT_HOMING_HIGH, EXT_HOMING_HIGH, ENT_LIMIT_LOW, ENT_LIMIT_HIGH }; // ACTIONS
  49. enum { ON_CHANGEPOSITION, ON_CHANGESTATE, ON_ONLIMITHIGH, ON_ONLIMITLOW, ON_ONTARGET, ON_STOP, CONN_MAX }; // CONNECTORS
  50. atm_connector connectors[CONN_MAX];
  51. int event( int id );
  52. void action( int id );
  53. long int _currentStep = 0;
  54. long int _targetStep = 0;
  55. long int _maxStep ;
  56. int _enablePin = -1;
  57. bool _enableReversed = 0 ;
  58. atm_timer_millis idle_timer ;
  59. int IDLE_TIMEOUT_DURATION = 500000 ;
  60. int _limitLow_Pin;
  61. int _limitLow_Mode; //0 no limit, 1 digital, 2 analog with thresholds
  62. bool _limitLow_Reversed ; //invert logic of limit switches
  63. int _limitLow_Thresholds[2] ; //analog value range for two analog limits
  64. int _limitHigh_Pin;
  65. int _limitHigh_Mode; //0 no limit, 1 digital, 2 analog with thresholds
  66. bool _limitHigh_Reversed ; //invert logic of limit switches
  67. int _limitHigh_Thresholds[2] ; //analog value range for two analog limits
  68. void updateLimitSwitch();
  69. };
  70. /*
  71. Automaton::ATML::begin - Automaton Markup Language
  72. <?xml version="1.0" encoding="UTF-8"?>
  73. <machines>
  74. <machine name="Atm_AccelStepper">
  75. <states>
  76. <DISABLED index="0" on_enter="ENT_DISABLED">
  77. <EVT_ENABLE>ENABLED</EVT_ENABLE>
  78. </DISABLED>
  79. <ENABLED index="1" on_enter="ENT_ENABLED">
  80. <EVT_DISABLE>DISABLED</EVT_DISABLE>
  81. <EVT_ENABLED_TIMEOUT>DISABLED</EVT_ENABLED_TIMEOUT>
  82. <EVT_STOP>STOP</EVT_STOP>
  83. <EVT_EMERGENCY_STOP>STOP</EVT_EMERGENCY_STOP>
  84. </ENABLED>
  85. <RUNNING index="2" on_loop="LP_RUNNING">
  86. <EVT_DISABLE>DISABLED</EVT_DISABLE>
  87. <EVT_STOP>STOP</EVT_STOP>
  88. <EVT_EMERGENCY_STOP>STOP</EVT_EMERGENCY_STOP>
  89. <EVT_ON_LIMIT_LOW>LIMIT_LOW</EVT_ON_LIMIT_LOW>
  90. <EVT_ON_LIMIT_HIGH>LIMIT_HIGH</EVT_ON_LIMIT_HIGH>
  91. <EVT_ON_TARGET>ENABLED</EVT_ON_TARGET>
  92. </RUNNING>
  93. <STOP index="3">
  94. <EVT_DISABLE>DISABLED</EVT_DISABLE>
  95. <EVT_STOP>STOP</EVT_STOP>
  96. <EVT_EMERGENCY_STOP>STOP</EVT_EMERGENCY_STOP>
  97. </STOP>
  98. <HOMING_LOW index="4" on_enter="ENT_HOMING_LOW" on_exit="EXT_HOMING_LOW">
  99. <EVT_DISABLE>DISABLED</EVT_DISABLE>
  100. <EVT_STOP>STOP</EVT_STOP>
  101. <EVT_EMERGENCY_STOP>STOP</EVT_EMERGENCY_STOP>
  102. <EVT_ON_LIMIT_LOW>ENABLED</EVT_ON_LIMIT_LOW>
  103. </HOMING_LOW>
  104. <HOMING_HIGH index="5" on_enter="ENT_HOMING_HIGH" on_exit="EXT_HOMING_HIGH">
  105. <EVT_DISABLE>DISABLED</EVT_DISABLE>
  106. <EVT_STOP>STOP</EVT_STOP>
  107. <EVT_EMERGENCY_STOP>STOP</EVT_EMERGENCY_STOP>
  108. <EVT_ON_LIMIT_HIGH>ENABLED</EVT_ON_LIMIT_HIGH>
  109. </HOMING_HIGH>
  110. <LIMIT_LOW index="6" on_enter="ENT_LIMIT_LOW">
  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. </LIMIT_LOW>
  115. <LIMIT_HIGH index="7" on_enter="ENT_LIMIT_HIGH">
  116. <EVT_STOP>STOP</EVT_STOP>
  117. <EVT_EMERGENCY_STOP>STOP</EVT_EMERGENCY_STOP>
  118. <EVT_ON_LIMIT_HIGH>LIMIT_HIGH</EVT_ON_LIMIT_HIGH>
  119. </LIMIT_HIGH>
  120. </states>
  121. <events>
  122. <EVT_DISABLE index="0" access="MIXED"/>
  123. <EVT_ENABLE index="1" access="MIXED"/>
  124. <EVT_ENABLED_TIMEOUT index="2" access="PRIVATE"/>
  125. <EVT_STOP index="3" access="MIXED"/>
  126. <EVT_EMERGENCY_STOP index="4" access="MIXED"/>
  127. <EVT_ON_LIMIT_LOW index="5" access="MIXED"/>
  128. <EVT_ON_LIMIT_HIGH index="6" access="MIXED"/>
  129. <EVT_ON_TARGET index="7" access="MIXED"/>
  130. </events>
  131. <connectors>
  132. <CHANGEPOSITION autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  133. <CHANGESTATE autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  134. <ONLIMITHIGH autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  135. <ONLIMITLOW autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  136. <ONTARGET autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  137. <STOP autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  138. </connectors>
  139. <methods>
  140. </methods>
  141. </machine>
  142. </machines>
  143. Automaton::ATML::end
  144. */