123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- #include "Atm_AccelStepper.h"
- /* Add optional parameters for the state machine to begin()
- * Add extra initialization code
- */
- Atm_AccelStepper& Atm_AccelStepper::begin(int step_pin, int dir_pin) {
- // clang-format off
- const static state_t state_table[] PROGMEM = {
- /* ON_ENTER ON_LOOP ON_EXIT EVT_DISABLE EVT_ENABLE EVT_ENABLED_TIMEOUT EVT_STOP EVT_EMERGENCY_STOP EVT_ON_LIMIT_LOW EVT_ON_LIMIT_HIGH EVT_ON_TARGET ELSE */
- /* DISABLED */ ENT_DISABLED, -1, -1, -1, ENABLED, -1, -1, -1, -1, -1, -1, -1,
- /* ENABLED */ ENT_ENABLED, -1, -1, DISABLE, -1, DISABLED, STOP, STOP, -1, -1, -1, -1,
- /* RUNNING */ -1, LP_RUNNING, -1, DISABLE, -1, -1, STOP, STOP, LIMIT_LOW, LIMIT_HIGH, ENABLED, -1,
- /* STOP */ -1, -1, -1, DISABLE, -1, -1, STOP, STOP, -1, -1, -1, -1,
- /* HOMING_LOW */ ENT_HOMING_LOW, -1, EXT_HOMING_LOW, DISABLE, -1, -1, STOP, STOP, ENABLED, -1, -1, -1,
- /* HOMING_HIGH */ ENT_HOMING_HIGH, -1, EXT_HOMING_HIGH, DISABLE, -1, -1, STOP, STOP, -1, ENABLED, -1, -1,
- /* LIMIT_LOW */ ENT_LIMIT_LOW, -1, -1, -1, -1, -1, STOP, STOP, LIMIT_LOW, -1, -1, -1,
- /* LIMIT_HIGH */ ENT_LIMIT_HIGH, -1, -1, -1, -1, -1, STOP, STOP, -1, LIMIT_HIGH, -1, -1,
- };
- // clang-format on
- Machine::begin( state_table, ELSE );
- stepper = new AccelStepper(1, step_pin, dir_pin);
- idle_timer.set(ATM_TIMER_OFF);
- return *this;
- }
- /* Add C++ code for each internally handled event (input)
- * The code must return 1 to trigger the event
- */
- int Atm_AccelStepper::event( int id ) {
- //updateLimitSwitch();
- switch ( id ) {
- case EVT_DISABLE:
- return 0;
- case EVT_ENABLE:
- return 0;
- case EVT_ENABLED_TIMEOUT:
- return 0;
- case EVT_STOP:
- return 0;
- case EVT_EMERGENCY_STOP:
- return 0;
- case EVT_ON_LIMIT_LOW:
- switch(_limitLow_Mode) {
- case 0:
- return 0;
- case 1: //digital INPUT
- limitLow_State = digitalRead(_limitLow_Pin);
- limitLow_State = _limitLow_Reversed ? !limitLow_State : limitLow_State;
- return limitLow_State;
- case 2:
- int analogTemp = analogRead(_limitLow_Pin);
- limitLow_State = _limitLow_Thresholds[0] < analogTemp && analogTemp < _limitLow_Thresholds[1];
- limitLow_State = _limitLow_Reversed ? !limitLow_State : limitLow_State;
- return limitLow_State;
- }
- case EVT_ON_LIMIT_HIGH:
- switch(_limitHigh_Mode) {
- case 0:
- return 0;
- case 1: //digital INPUT
- limitHigh_State = digitalRead(_limitHigh_Pin);
- limitHigh_State = _limitHigh_Reversed ? !limitHigh_State : limitHigh_State;
- return limitHigh_State;
- case 2:
- int analogTemp = analogRead(_limitHigh_Pin);
- limitHigh_State = _limitHigh_Thresholds[0] < analogTemp && analogTemp < _limitHigh_Thresholds[1];
- limitHigh_State = _limitHigh_Reversed ? !limitHigh_State : limitHigh_State;
- return limitHigh_State;
- }
- case EVT_ON_TARGET:
- return _currentStep == _targetStep;;
- }
- return 0;
- }
- /* Add C++ code for each action
- * This generates the 'output' for the state machine
- *
- * Available connectors:
- * push( connectors, ON_CHANGEPOSITION, 0, <v>, <up> );
- * push( connectors, ON_CHANGESTATE, 0, <v>, <up> );
- * push( connectors, ON_ONLIMITHIGH, 0, <v>, <up> );
- * push( connectors, ON_ONLIMITLOW, 0, <v>, <up> );
- * push( connectors, ON_ONTARGET, 0, <v>, <up> );
- * push( connectors, ON_STOP, 0, <v>, <up> );
- */
- void Atm_AccelStepper::action( int id ) {
- switch ( id ) {
- case ENT_DISABLED:
- return;
- case ENT_ENABLED:
- return;
- case LP_RUNNING:
- return;
- case ENT_HOMING_LOW:
- return;
- case EXT_HOMING_LOW:
- return;
- case ENT_HOMING_HIGH:
- return;
- case EXT_HOMING_HIGH:
- return;
- case ENT_LIMIT_LOW:
- return;
- case ENT_LIMIT_HIGH:
- return;
- }
- }
- /* Optionally override the default trigger() method
- * Control how your machine processes triggers
- */
- Atm_AccelStepper& Atm_AccelStepper::trigger( int event ) {
- Machine::trigger( event );
- return *this;
- }
- /* Optionally override the default state() method
- * Control what the machine returns when another process requests its state
- */
- int Atm_AccelStepper::state( void ) {
- return Machine::state();
- }
- /* Nothing customizable below this line
- ************************************************************************************************
- */
- /* Public event methods
- *
- */
- Atm_AccelStepper& Atm_AccelStepper::disable() {
- trigger( EVT_DISABLE );
- return *this;
- }
- Atm_AccelStepper& Atm_AccelStepper::enable() {
- trigger( EVT_ENABLE );
- return *this;
- }
- Atm_AccelStepper& Atm_AccelStepper::stop() {
- trigger( EVT_STOP );
- return *this;
- }
- Atm_AccelStepper& Atm_AccelStepper::emergency_stop() {
- trigger( EVT_EMERGENCY_STOP );
- return *this;
- }
- Atm_AccelStepper& Atm_AccelStepper::on_limit_low() {
- trigger( EVT_ON_LIMIT_LOW );
- return *this;
- }
- Atm_AccelStepper& Atm_AccelStepper::on_limit_high() {
- trigger( EVT_ON_LIMIT_HIGH );
- return *this;
- }
- Atm_AccelStepper& Atm_AccelStepper::on_target() {
- trigger( EVT_ON_TARGET );
- return *this;
- }
- /*
- * onChangeposition() push connector variants ( slots 1, autostore 0, broadcast 0 )
- */
- Atm_AccelStepper& Atm_AccelStepper::onChangeposition( Machine& machine, int event ) {
- onPush( connectors, ON_CHANGEPOSITION, 0, 1, 1, machine, event );
- return *this;
- }
- Atm_AccelStepper& Atm_AccelStepper::onChangeposition( atm_cb_push_t callback, int idx ) {
- onPush( connectors, ON_CHANGEPOSITION, 0, 1, 1, callback, idx );
- return *this;
- }
- /*
- * onChangestate() push connector variants ( slots 1, autostore 0, broadcast 0 )
- */
- Atm_AccelStepper& Atm_AccelStepper::onChangestate( Machine& machine, int event ) {
- onPush( connectors, ON_CHANGESTATE, 0, 1, 1, machine, event );
- return *this;
- }
- Atm_AccelStepper& Atm_AccelStepper::onChangestate( atm_cb_push_t callback, int idx ) {
- onPush( connectors, ON_CHANGESTATE, 0, 1, 1, callback, idx );
- return *this;
- }
- /*
- * onOnlimithigh() push connector variants ( slots 1, autostore 0, broadcast 0 )
- */
- Atm_AccelStepper& Atm_AccelStepper::onOnlimithigh( Machine& machine, int event ) {
- onPush( connectors, ON_ONLIMITHIGH, 0, 1, 1, machine, event );
- return *this;
- }
- Atm_AccelStepper& Atm_AccelStepper::onOnlimithigh( atm_cb_push_t callback, int idx ) {
- onPush( connectors, ON_ONLIMITHIGH, 0, 1, 1, callback, idx );
- return *this;
- }
- /*
- * onOnlimitlow() push connector variants ( slots 1, autostore 0, broadcast 0 )
- */
- Atm_AccelStepper& Atm_AccelStepper::onOnlimitlow( Machine& machine, int event ) {
- onPush( connectors, ON_ONLIMITLOW, 0, 1, 1, machine, event );
- return *this;
- }
- Atm_AccelStepper& Atm_AccelStepper::onOnlimitlow( atm_cb_push_t callback, int idx ) {
- onPush( connectors, ON_ONLIMITLOW, 0, 1, 1, callback, idx );
- return *this;
- }
- /*
- * onOntarget() push connector variants ( slots 1, autostore 0, broadcast 0 )
- */
- Atm_AccelStepper& Atm_AccelStepper::onOntarget( Machine& machine, int event ) {
- onPush( connectors, ON_ONTARGET, 0, 1, 1, machine, event );
- return *this;
- }
- Atm_AccelStepper& Atm_AccelStepper::onOntarget( atm_cb_push_t callback, int idx ) {
- onPush( connectors, ON_ONTARGET, 0, 1, 1, callback, idx );
- return *this;
- }
- /*
- * onStop() push connector variants ( slots 1, autostore 0, broadcast 0 )
- */
- Atm_AccelStepper& Atm_AccelStepper::onStop( Machine& machine, int event ) {
- onPush( connectors, ON_STOP, 0, 1, 1, machine, event );
- return *this;
- }
- Atm_AccelStepper& Atm_AccelStepper::onStop( atm_cb_push_t callback, int idx ) {
- onPush( connectors, ON_STOP, 0, 1, 1, callback, idx );
- return *this;
- }
- /* State trace method
- * Sets the symbol table and the default logging method for serial monitoring
- */
- Atm_AccelStepper& Atm_AccelStepper::trace( Stream & stream ) {
- Machine::setTrace( &stream, atm_serial_debug::trace,
- "ACCELSTEPPER\0EVT_DISABLE\0EVT_ENABLE\0EVT_ENABLED_TIMEOUT\0EVT_STOP\0EVT_EMERGENCY_STOP\0EVT_ON_LIMIT_LOW\0EVT_ON_LIMIT_HIGH\0EVT_ON_TARGET\0ELSE\0DISABLED\0ENABLED\0RUNNING\0STOP\0HOMING_LOW\0HOMING_HIGH\0LIMIT_LOW\0LIMIT_HIGH" );
- return *this;
- }
|