C++ class for controlling DC motor with encoder feedback. Dependencies include LS7366LIB, MotCon, and PID.
Dependencies: PID LS7366LIB MotCon2
Diff: Axis.h
- Revision:
- 12:7a7fe3baf733
- Parent:
- 11:93d924320ddc
diff -r 93d924320ddc -r 7a7fe3baf733 Axis.h --- a/Axis.h Tue Nov 15 15:41:33 2016 +0000 +++ b/Axis.h Wed Jan 09 13:35:44 2019 +0000 @@ -1,4 +1,6 @@ - +// Updated Axis Class on 20190108 to include mode change (Position, Velocity, Acceleration) +// all in radians, rad/sec, rad/sec/sec +// J. Bradshaw #ifndef MBED_AXIS_H #define MBED_AXIS_H @@ -10,17 +12,39 @@ class Axis{ public: + /** Create a closed loop controller connected to the specified pins + * + * @param _spi address of the spi object for LS7366 encoder IC communication + * @param _cs chip select signal used for the LS7366 encoder IC spi addressing + * @param _pwm pulse width modulation output pin for motor control signal + * @param _dir DigitalOut pin to control the motor direction pin1 + * @param _analog analog input pin for monitoring current + * @param _limit pointer to integer object for limit switch detection + */ Axis(SPI& _spi, PinName _cs, PinName _pwm, PinName _dir, PinName _analog, int* limit); + /** Create a closed loop controller connected to the specified pins + * + * @param _spi address of the spi object for LS7366 encoder IC communication + * @param _cs chip select signal used for the LS7366 encoder IC spi addressing + * @param _pwm pulse width modulation output pin for motor control signal + * @param _dir DigitalOut pin to control the motor direction pin 1 + * @param _dir2 DigitalOut pin to control the motor direction pin 2 + * @param _analog analog input pin for monitoring current + * @param _limit pointer to integer object for limit switch detection + */ + Axis(SPI& _spi, PinName _cs, PinName _pwm, PinName _dir, PinName _dir2, PinName _analog, int* limit); void paramUpdate(void); - void center(void); - void init(void); + void init(float encCountsPerRev); void moveTrapezoid(float position, float time); - void moveUpdate(void); + void moveScurve(float position, float time); + void moveUpdateTrapezoid(void); float readCurrent(void); void axisOff(void); void axisOn(void); void zero(void); void writeEncoderValue(long value); + void updatePIDgains(float P, float I, float D); + void changeMoveMode(int mode); long enc; //used to return the data from the LS7366 encoder chip float co; // = 0.0; @@ -42,8 +66,11 @@ int moveState; int debug; int *ptr_limit; - float motCurrent; //motor current read from readCurrent() function + float motI; //motor current read from readCurrent() function + volatile float motI_last; float mot_I_lim; //max current limit + float dIdT; + float mot_I_max, mot_I_max_last; int axisState; int motInvert; char dataFormat; //'r'=radians (default), 'd'=degrees, 'e'=encoder counts @@ -51,6 +78,8 @@ float pos_deg, vel_deg; //current position measurement in degrees float ctsPerDeg; int busyflag; + int moveMode; + float countsPerRev; Ticker update; Ticker moveProfile; @@ -65,6 +94,7 @@ DigitalOut _cs; PwmOut _pwm; DigitalOut _dir; + DigitalOut _dir2; AnalogIn _analog; };