123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- #pragma once
- #include <Automaton.h>
- #include "AccelStepper.h"
- class Atm_AccelStepper: public Machine {
- public:
- enum { DISABLE, ENABLED, RUNNING, STOP, HOMING_LOW, HOMING_HIGH, LIMIT_LOW, LIMIT_HIGH };
- enum { EVT_DISABLE, EVT_ENABLE, EVT_ENABLED_TIMEOUT, EVT_MOVE, EVT_STOP,
- EVT_EMERGENCY_STOP, EVT_ON_LIMIT_LOW, EVT_ON_LIMIT_HIGH, EVT_ON_TARGET,
- EVT_HOMING_LOW, EVT_HOMING_HIGH, ELSE };
- Atm_AccelStepper( void ) : Machine() {};
- Atm_AccelStepper& begin( int step_pin, int dir_pin );
- Atm_AccelStepper& trace( Stream & stream );
- Atm_AccelStepper& trigger( int event );
- int state( void );
- Atm_AccelStepper& onChangeposition( Machine& machine, int event = 0 );
- Atm_AccelStepper& onChangeposition( atm_cb_push_t callback, int idx = 0 );
- Atm_AccelStepper& onChangestate( Machine& machine, int event = 0 );
- Atm_AccelStepper& onChangestate( atm_cb_push_t callback, int idx = 0 );
- Atm_AccelStepper& onOnlimithigh( Machine& machine, int event = 0 );
- Atm_AccelStepper& onOnlimithigh( atm_cb_push_t callback, int idx = 0 );
- Atm_AccelStepper& onOnlimitlow( Machine& machine, int event = 0 );
- Atm_AccelStepper& onOnlimitlow( atm_cb_push_t callback, int idx = 0 );
- Atm_AccelStepper& onOntarget( Machine& machine, int event = 0 );
- Atm_AccelStepper& onOntarget( atm_cb_push_t callback, int idx = 0 );
- Atm_AccelStepper& onStop( Machine& machine, int event = 0 );
- Atm_AccelStepper& onStop( atm_cb_push_t callback, int idx = 0 );
- Atm_AccelStepper& onOnhominglow( Machine& machine, int event = 0 );
- Atm_AccelStepper& onOnhominglow( atm_cb_push_t callback, int idx = 0 );
- Atm_AccelStepper& onOnhominghigh( Machine& machine, int event = 0 );
- Atm_AccelStepper& onOnhominghigh( atm_cb_push_t callback, int idx = 0 );
- Atm_AccelStepper& disable( void );
- Atm_AccelStepper& enable( void );
-
- Atm_AccelStepper& stop( void );
- Atm_AccelStepper& emergency_stop( void );
- Atm_AccelStepper& on_limit_low( void );
- Atm_AccelStepper& on_limit_high( void );
- Atm_AccelStepper& on_target( void );
- AccelStepper *stepper;
- long int homing_speed = 1000;
- bool homingLow_done = 0 ;
- bool homingHigh_done = 0 ;
- long int max_speed = 1000;
- long int acceleration = 100;
- Atm_AccelStepper& setMaxSpeed( long int maxSpeed );
- Atm_AccelStepper& setHomingSpeed( long int homingSpeed);
- Atm_AccelStepper& setAcceleration( long int acc);
- Atm_AccelStepper& setPosition( long int position);
- long int getPosition();
- long int distanceToGo();
- bool isRunning();
- float getSpeed();
- Atm_AccelStepper& move(long int stepRel );
- Atm_AccelStepper& moveTo(long int stepAbs );
- Atm_AccelStepper& rotate(long int speed );
- Atm_AccelStepper& homing( bool direction );
- int runMode = 0;
- Atm_AccelStepper& position_refresh( long int refresh_ms = 1000);
-
- Atm_AccelStepper& setEnablePin( int enablePin );
- Atm_AccelStepper& pinReversed( bool directionInvert=false, bool stepInvert=false, bool enableInvert=false );
- bool enabled ;
-
- Atm_AccelStepper& limitLow_set(int mode = 0, int pin = -1, int reversed=0);
- Atm_AccelStepper& limitLow_isHard(bool hardlimit = 1);
- Atm_AccelStepper& limitLow_setThresholds (int threshold_low=510, int threshold_high = 1024);
- Atm_AccelStepper& limitHigh_set(int mode = 0, int pin = -1, int reversed=0);
- Atm_AccelStepper& limitHigh_isHard(bool hardlimit = 1);
- Atm_AccelStepper& limitHigh_setThresholds (int threshold_low=510, int threshold_high = 1024);
- bool limitLow_State_raw;
- bool limitLow_State = 0;
- bool limitHigh_State = 0;
- bool limitHigh_State_raw;
- bool limitLow_State_prev;
- bool limitHigh_State_prev;
- const static int limit_buf_size = 20 ;
- bool limitLow_state_buf [limit_buf_size] ;
- int limitLow_state_total = 0 ;
- int limitLow_buf_head ;
- int limitLow_avg(bool limitState);
- bool limitHigh_state_buf [limit_buf_size] ;
- int limitHigh_state_total = 0 ;
- int limitHigh_buf_head ;
- int limitHigh_avg(bool limitState);
- bool changed = 0 ;
- private:
-
- 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, LP_LIMIT_LOW, ENT_LIMIT_HIGH, LP_LIMIT_HIGH };
-
- enum { ON_CHANGEPOSITION, ON_CHANGESTATE, ON_ONLIMITHIGH,
- ON_ONLIMITLOW, ON_ONTARGET, ON_STOP, ON_ONHOMINGLOW, ON_ONHOMINGHIGH, CONN_MAX };
- atm_connector connectors[CONN_MAX];
- int event( int id );
- void action( int id );
-
- long int _currentStep = 0;
- long int _targetStep = 0;
- long int _maxStep = -1;
- atm_timer_millis position_timer ;
- int POSITION_SEND_TIMER = 50 ;
- void stepper_update(void);
-
-
- int _enablePin = -1;
- bool _enableReversed = 0 ;
-
-
-
-
- int _limitLow_Pin;
- int _limitLow_Mode = 0;
- bool _limitLow_Reversed ;
- int _limitLow_Thresholds[2] ;
- bool _limitLow_Hard = 0;
- int _limitHigh_Pin;
- int _limitHigh_Mode=0;
- bool _limitHigh_Reversed ;
- int _limitHigh_Thresholds[2] ;
- bool _limitHigh_Hard = 0 ;
-
- atm_timer_millis limits_timer ;
- int LIMIT_UPDATE_RATE = 5 ;
-
- };
|