Atm_Teenstep.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #pragma once
  2. #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  3. #include <Automaton.h>
  4. #include <TeensyStep.h>
  5. class Atm_Teenstep: public Machine {
  6. public:
  7. enum { DISABLED, ENABLED, RUNNING, STOPPING, EMERGENCY_STOP, HOMING_HIGH, HOMING_LOW }; // STATES
  8. enum { EVT_HOMING_LOW, EVT_HOMING_HIGH, EVT_MOVE_TIMEOUT, EVT_LIMIT_HIGH, EVT_LIMIT_LOW, EVT_EMERGENCYSTOP, EVT_STOP, EVT_ONTARGET, EVT_MOVE, EVT_DISABLE, EVT_ENABLE, ELSE }; // EVENTS
  9. Atm_Teenstep( void ) : Machine() {};
  10. Atm_Teenstep& begin( Stepper & motorRef, StepControl & stepControlRef );
  11. Atm_Teenstep& trace( Stream & stream );
  12. Atm_Teenstep& trigger( int event );
  13. int state( void );
  14. Atm_Teenstep& onChange( Machine& machine, int event = 0 );
  15. Atm_Teenstep& onChange( atm_cb_push_t callback, int idx = 0 );
  16. Atm_Teenstep& onChangeposition( Machine& machine, int event = 0 );
  17. Atm_Teenstep& onChangeposition( atm_cb_push_t callback, int idx = 0 );
  18. Atm_Teenstep& onLimithigh( Machine& machine, int event = 0 );
  19. Atm_Teenstep& onLimithigh( atm_cb_push_t callback, int idx = 0 );
  20. Atm_Teenstep& onLimitlow( Machine& machine, int event = 0 );
  21. Atm_Teenstep& onLimitlow( atm_cb_push_t callback, int idx = 0 );
  22. Atm_Teenstep& homing_low( void );
  23. Atm_Teenstep& homing_high( void );
  24. Atm_Teenstep& move_timeout( void );
  25. Atm_Teenstep& limit_high( void );
  26. Atm_Teenstep& limit_low( void );
  27. Atm_Teenstep& emergencystop( void );
  28. Atm_Teenstep& stop( void );
  29. Atm_Teenstep& ontarget( void );
  30. Atm_Teenstep& move( void );
  31. Atm_Teenstep& disable( void );
  32. Atm_Teenstep& enable( void );
  33. Atm_Teenstep& move( long int stepRel );
  34. Atm_Teenstep& moveTo( long int stepAbs );
  35. Atm_Teenstep& homing( bool direction );
  36. Atm_Teenstep& setLimitType( int limitType = 0);
  37. Atm_Teenstep& setLimitPins( int limitPinLow);
  38. Atm_Teenstep& setLimitPins( int limitPinLow, int limitPinHigh);
  39. Atm_Teenstep& limitReversed( bool reversed );
  40. Atm_Teenstep& limitThresholds( int limitThreshold0, int limitThreshold1,
  41. int limitThreshold2, int limitThreshold3);
  42. bool limitState[2] ; // up to two limits, at least one for homing
  43. virtual Atm_Teenstep& onOSC( void );
  44. virtual Atm_Teenstep& sendOSC( void );
  45. Stepper * motor;
  46. StepControl * controller;
  47. Atm_Teenstep& setEnablePin( int enablePin );
  48. Atm_Teenstep& enableReversed( bool reverse );
  49. bool enabled ;
  50. private:
  51. enum { ENT_DISABLED, ENT_ENABLED, ENT_RUNNING, LP_RUNNING, ENT_STOPPING, ENT_EMERGENCY_STOP, ENT_HOMING_HIGH, EXT_HOMING_HIGH, ENT_HOMING_LOW, EXT_HOMING_LOW }; // ACTIONS
  52. enum { ON_CHANGE, ON_CHANGEPOSITION, ON_LIMITHIGH, ON_LIMITLOW, CONN_MAX }; // CONNECTORS
  53. atm_connector connectors[CONN_MAX];
  54. int event( int id );
  55. void action( int id );
  56. atm_timer_millis idle_timer ;
  57. int IDLE_TIMEOUT_DURATION = 500000 ;
  58. atm_timer_millis moving_timer ;
  59. int MOVING_TIMEOUT_DURATION = 12000000 ;
  60. atm_counter homing_counterout ;
  61. long int _currentStep = 0;
  62. long int _targetStep = 0;
  63. long int _maxStep ;
  64. int _enablePin = -1;
  65. bool _enableReversed = 0 ;
  66. int _limitPin[2];
  67. enum { NONE, DIGITAL_1, DIGITAL_2, ANALOG_1 };
  68. int _limitType; //type of limit switch, from list over
  69. bool _limitReversed ; //invert logic of limit switches
  70. int _limitThresholds[4] ; //analog value range for two analog limits
  71. void updateLimitSwitch();
  72. };
  73. #endif
  74. /*
  75. Automaton::ATML::begin - Automaton Markup Language
  76. <?xml version="1.0" encoding="UTF-8"?>
  77. <machines>
  78. <machine name="Atm_Teenstep">
  79. <states>
  80. <DISABLED index="0" on_enter="ENT_DISABLED">
  81. <EVT_ENABLE>ENABLED</EVT_ENABLE>
  82. </DISABLED>
  83. <ENABLED index="1" on_enter="ENT_ENABLED">
  84. <EVT_MOVE>RUNNING</EVT_MOVE>
  85. <EVT_DISABLE>DISABLED</EVT_DISABLE>
  86. </ENABLED>
  87. <RUNNING index="2" on_enter="ENT_RUNNING" on_loop="LP_RUNNING">
  88. <EVT_LIMIT_HIGH>EMERGENCY_STOP</EVT_LIMIT_HIGH>
  89. <EVT_LIMIT_LOW>EMERGENCY_STOP</EVT_LIMIT_LOW>
  90. <EVT_EMERGENCYSTOP>EMERGENCY_STOP</EVT_EMERGENCYSTOP>
  91. <EVT_STOP>STOPPING</EVT_STOP>
  92. <EVT_ONTARGET>ENABLED</EVT_ONTARGET>
  93. <EVT_MOVE>RUNNING</EVT_MOVE>
  94. </RUNNING>
  95. <STOPPING index="3" on_enter="ENT_STOPPING">
  96. <EVT_LIMIT_HIGH>EMERGENCY_STOP</EVT_LIMIT_HIGH>
  97. <EVT_LIMIT_LOW>EMERGENCY_STOP</EVT_LIMIT_LOW>
  98. <EVT_EMERGENCYSTOP>EMERGENCY_STOP</EVT_EMERGENCYSTOP>
  99. <EVT_MOVE>RUNNING</EVT_MOVE>
  100. </STOPPING>
  101. <EMERGENCY_STOP index="4" on_enter="ENT_EMERGENCY_STOP">
  102. <EVT_MOVE>RUNNING</EVT_MOVE>
  103. <EVT_ENABLE>ENABLED</EVT_ENABLE>
  104. </EMERGENCY_STOP>
  105. <HOMING_HIGH index="5" on_enter="ENT_HOMING_HIGH" on_exit="EXT_HOMING_HIGH">
  106. <EVT_LIMIT_HIGH>ENABLED</EVT_LIMIT_HIGH>
  107. <EVT_LIMIT_LOW>EMERGENCY_STOP</EVT_LIMIT_LOW>
  108. <EVT_EMERGENCYSTOP>EMERGENCY_STOP</EVT_EMERGENCYSTOP>
  109. <EVT_STOP>STOPPING</EVT_STOP>
  110. </HOMING_HIGH>
  111. <HOMING_LOW index="6" on_enter="ENT_HOMING_LOW" on_exit="EXT_HOMING_LOW">
  112. <EVT_LIMIT_HIGH>EMERGENCY_STOP</EVT_LIMIT_HIGH>
  113. <EVT_LIMIT_LOW>ENABLED</EVT_LIMIT_LOW>
  114. <EVT_EMERGENCYSTOP>EMERGENCY_STOP</EVT_EMERGENCYSTOP>
  115. <EVT_STOP>STOPPING</EVT_STOP>
  116. </HOMING_LOW>
  117. </states>
  118. <events>
  119. <EVT_MOVE_TIMEOUT index="0" access="MIXED"/>
  120. <EVT_LIMIT_HIGH index="1" access="MIXED"/>
  121. <EVT_LIMIT_LOW index="2" access="MIXED"/>
  122. <EVT_EMERGENCYSTOP index="3" access="MIXED"/>
  123. <EVT_STOP index="4" access="MIXED"/>
  124. <EVT_ONTARGET index="5" access="MIXED"/>
  125. <EVT_MOVE index="6" access="MIXED"/>
  126. <EVT_DISABLE index="7" access="MIXED"/>
  127. <EVT_ENABLE index="8" access="MIXED"/>
  128. </events>
  129. <connectors>
  130. <CHANGE autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  131. <CHANGEPOSITION autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  132. <LIMITHIGH autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  133. <LIMITLOW autostore="0" broadcast="0" dir="PUSH" slots="1"/>
  134. </connectors>
  135. <methods>
  136. </methods>
  137. </machine>
  138. </machines>
  139. Automaton::ATML::end
  140. */