Atm_Tstepper.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #pragma once
  2. #include <Automaton.h>
  3. //#include <Atm_timer.h>
  4. #include <TeensyStep.h>
  5. class Atm_Tstepper: public Machine {
  6. public:
  7. enum { DISABLED, ENABLED, RUNNING, STOPPING, EMERGENCY, HOMING, MOVE_TIMEOUT, HOMING_COUNTEROUT }; // STATES
  8. enum { EVT_HOMING_COUNTER, EVT_IDLE_TIMEOUT, EVT_MOVE_TIMEOUT, EVT_LIMIT_LOW, EVT_LIMIT_HIGH, EVT_EMERGENCY, EVT_STOP, EVT_ONTARGET, EVT_MOVE, EVT_DISABLE, EVT_ENABLE, EVT_HOMING, ELSE }; // EVENTS
  9. Atm_Tstepper( void ) : Machine() {};
  10. Atm_Tstepper& begin( Stepper & motorRef, StepControl & stepControlRef );
  11. Atm_Tstepper& trace( Stream & stream );
  12. Atm_Tstepper& trigger( int event );
  13. int state( void );
  14. Atm_Tstepper& onOnchange( Machine& machine, int event = 0 );
  15. Atm_Tstepper& onOnchange( atm_cb_push_t callback, int idx = 0 );
  16. Atm_Tstepper& onOnchangeacceleration( Machine& machine, int event = 0 );
  17. Atm_Tstepper& onOnchangeacceleration( atm_cb_push_t callback, int idx = 0 );
  18. Atm_Tstepper& onOnchangeposition( Machine& machine, int event = 0 );
  19. Atm_Tstepper& onOnchangeposition( atm_cb_push_t callback, int idx = 0 );
  20. Atm_Tstepper& onOnchangestate( Machine& machine, int event = 0 );
  21. Atm_Tstepper& onOnchangestate( atm_cb_push_t callback, int idx = 0 );
  22. Atm_Tstepper& onOnhighlimit( Machine& machine, int event = 0 );
  23. Atm_Tstepper& onOnhighlimit( atm_cb_push_t callback, int idx = 0 );
  24. Atm_Tstepper& onOnhomingcounterout( Machine& machine, int event = 0 );
  25. Atm_Tstepper& onOnhomingcounterout( atm_cb_push_t callback, int idx = 0 );
  26. Atm_Tstepper& onOnhomingtimeout( Machine& machine, int event = 0 );
  27. Atm_Tstepper& onOnhomingtimeout( atm_cb_push_t callback, int idx = 0 );
  28. Atm_Tstepper& onOnidletimeout( Machine& machine, int event = 0 );
  29. Atm_Tstepper& onOnidletimeout( atm_cb_push_t callback, int idx = 0 );
  30. Atm_Tstepper& onOnlowlimit( Machine& machine, int event = 0 );
  31. Atm_Tstepper& onOnlowlimit( atm_cb_push_t callback, int idx = 0 );
  32. Atm_Tstepper& onOnmovetimeout( Machine& machine, int event = 0 );
  33. Atm_Tstepper& onOnmovetimeout( atm_cb_push_t callback, int idx = 0 );
  34. Atm_Tstepper& onSpeed( Machine& machine, int event = 0 );
  35. Atm_Tstepper& onSpeed( atm_cb_push_t callback, int idx = 0 );
  36. Atm_Tstepper& homing_counter( void );
  37. Atm_Tstepper& idle_timeout( void );
  38. Atm_Tstepper& move_timeout( void );
  39. Atm_Tstepper& limit_low( void );
  40. Atm_Tstepper& limit_high( void );
  41. Atm_Tstepper& emergency( void );
  42. Atm_Tstepper& stop( void );
  43. Atm_Tstepper& ontarget( void );
  44. Atm_Tstepper& move( long int stepRel );
  45. Atm_Tstepper& moveTo( long int stepAbs );
  46. Atm_Tstepper& disable( void );
  47. Atm_Tstepper& enable( void );
  48. Atm_Tstepper& homing( int direction );
  49. Atm_Tstepper& setLimitType( int limitType = 0);
  50. Atm_Tstepper& setLimitPins( int limitPinLow);
  51. Atm_Tstepper& setLimitPins( int limitPinLow, int limitPinHigh);
  52. Atm_Tstepper& limitReversed( bool reversed );
  53. Atm_Tstepper& limitThresholds( int limitThreshold0, int limitThreshold1,
  54. int limitThreshold2, int limitThreshold3);
  55. bool limitState[2] ; // up to two limits, at least one for homing
  56. virtual Atm_Tstepper& onOSC( void );
  57. virtual Atm_Tstepper& sendOSC( void );
  58. Stepper * motor;
  59. StepControl * controller;
  60. Atm_Tstepper& setEnablePin( int enablePin );
  61. Atm_Tstepper& enableReversed( bool reverse );
  62. bool enabled ;
  63. // Atm_TeensyStep& enable( bool enable );
  64. int printPosition();
  65. private:
  66. enum { ENT_DISABLED, ENT_ENABLED, ENT_RUNNING, LP_RUNNING, ENT_STOPPING, LP_STOPPING, ENT_EMERGENCY, ENT_HOMING, LP_HOMING, EXT_HOMING, ENT_MOVE_TIMEOUT }; // ACTIONS
  67. enum { ON_ONCHANGE, ON_ONCHANGEACCELERATION, ON_ONCHANGEPOSITION, ON_ONCHANGESTATE, ON_ONHIGHLIMIT, ON_ONHOMINGCOUNTEROUT, ON_ONHOMINGTIMEOUT, ON_ONIDLETIMEOUT, ON_ONLOWLIMIT, ON_ONMOVETIMEOUT, ON_SPEED, CONN_MAX }; // CONNECTORS
  68. atm_connector connectors[CONN_MAX];
  69. int event( int id );
  70. void action( int id );
  71. atm_timer_millis idle_timer ;
  72. int IDLE_TIMEOUT_DURATION = 500000 ;
  73. atm_timer_millis moving_timer ;
  74. int MOVING_TIMEOUT_DURATION = 12000000 ;
  75. atm_counter homing_counterout ;
  76. long int _currentStep ;
  77. long int _targetStep ;
  78. int _enablePin = -1;
  79. bool _enableReversed = 0 ;
  80. int _limitPin[2];
  81. enum { NONE, DIGITAL_1, DIGITAL_2, ANALOG_1 };
  82. int _limitType; //type of limit switch, from list over
  83. bool _limitReversed ; //invert logic of limit switches
  84. int _limitThresholds[4] ; //analog value range for two analog limits
  85. void updateLimitSwitch();
  86. };
  87. /*
  88. Automaton::ATML::begin - Automaton Markup Language
  89. <?xml version="1.0" encoding="UTF-8"?>
  90. <machines>
  91. <machine name="Atm_Tstepper">
  92. <states>
  93. <DISABLED index="0" on_enter="ENT_DISABLED">
  94. <EVT_ENABLE>ENABLED</EVT_ENABLE>
  95. </DISABLED>
  96. <ENABLED index="1" on_enter="ENT_ENABLED">
  97. <EVT_IDLE_TIMEOUT>DISABLED</EVT_IDLE_TIMEOUT>
  98. <EVT_MOVE>RUNNING</EVT_MOVE>
  99. <EVT_DISABLE>DISABLED</EVT_DISABLE>
  100. </ENABLED>
  101. <RUNNING index="2" on_enter="ENT_RUNNING" on_loop="LP_RUNNING">
  102. <EVT_MOVE_TIMEOUT>MOVE_TIMEOUT</EVT_MOVE_TIMEOUT>
  103. <EVT_LIMIT_LOW>EMERGENCY</EVT_LIMIT_LOW>
  104. <EVT_LIMIT_HIGH>EMERGENCY</EVT_LIMIT_HIGH>
  105. <EVT_EMERGENCY>EMERGENCY</EVT_EMERGENCY>
  106. <EVT_STOP>STOPPING</EVT_STOP>
  107. <EVT_ONTARGET>ENABLED</EVT_ONTARGET>
  108. <EVT_MOVE>RUNNING</EVT_MOVE>
  109. </RUNNING>
  110. <STOPPING index="3" on_enter="ENT_STOPPING" on_loop="LP_STOPPING">
  111. <EVT_LIMIT_LOW>EMERGENCY</EVT_LIMIT_LOW>
  112. <EVT_LIMIT_HIGH>EMERGENCY</EVT_LIMIT_HIGH>
  113. <EVT_EMERGENCY>EMERGENCY</EVT_EMERGENCY>
  114. <EVT_MOVE>RUNNING</EVT_MOVE>
  115. </STOPPING>
  116. <EMERGENCY index="4" on_enter="ENT_EMERGENCY">
  117. </EMERGENCY>
  118. <HOMING index="5" on_enter="ENT_HOMING" on_loop="LP_HOMING" on_exit="EXT_HOMING">
  119. <EVT_MOVE_TIMEOUT>MOVE_TIMEOUT</EVT_MOVE_TIMEOUT>
  120. <EVT_LIMIT_LOW>ENABLED</EVT_LIMIT_LOW>
  121. <EVT_LIMIT_HIGH>ENABLED</EVT_LIMIT_HIGH>
  122. <EVT_EMERGENCY>EMERGENCY</EVT_EMERGENCY>
  123. <EVT_STOP>STOPPING</EVT_STOP>
  124. <EVT_ONTARGET>MOVE_TIMEOUT</EVT_ONTARGET>
  125. </HOMING>
  126. <MOVE_TIMEOUT index="6" on_enter="ENT_MOVE_TIMEOUT">
  127. <EVT_MOVE_TIMEOUT>MOVE_TIMEOUT</EVT_MOVE_TIMEOUT>
  128. </MOVE_TIMEOUT>
  129. <HOMING_COUNTEROUT index="7">
  130. </HOMING_COUNTEROUT>
  131. </states>
  132. <events>
  133. <EVT_HOMING_COUNTER index="0" access="MIXED"/>
  134. <EVT_IDLE_TIMEOUT index="1" access="MIXED"/>
  135. <EVT_MOVE_TIMEOUT index="2" access="MIXED"/>
  136. <EVT_LIMIT_LOW index="3" access="MIXED"/>
  137. <EVT_LIMIT_HIGH index="4" access="MIXED"/>
  138. <EVT_EMERGENCY index="5" access="MIXED"/>
  139. <EVT_STOP index="6" access="MIXED"/>
  140. <EVT_ONTARGET index="7" access="MIXED"/>
  141. <EVT_MOVE index="8" access="MIXED"/>
  142. <EVT_DISABLE index="9" access="MIXED"/>
  143. <EVT_ENABLE index="10" access="MIXED"/>
  144. </events>
  145. <connectors>
  146. <ONCHANGE autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  147. <ONCHANGEACCELERATION autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  148. <ONCHANGEPOSITION autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  149. <ONCHANGESTATE autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  150. <ONHIGHLIMIT autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  151. <ONHOMINGCOUNTEROUT autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  152. <ONHOMINGTIMEOUT autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  153. <ONIDLETIMEOUT autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  154. <ONLOWLIMIT autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  155. <ONMOVETIMEOUT autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  156. <SPEED autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  157. </connectors>
  158. <methods>
  159. </methods>
  160. </machine>
  161. </machines>
  162. Automaton::ATML::end
  163. */