C++ class for controlling DC motor with encoder feedback. Dependencies include LS7366LIB, MotCon, and PID.

Dependencies:   PID LS7366LIB MotCon2

Committer:
jebradshaw
Date:
Wed Jan 09 13:35:44 2019 +0000
Revision:
12:7a7fe3baf733
Parent:
11:93d924320ddc
position, velocity, acceleration modes all in radians; Axis_Init function takes encoder counts/revolution

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jebradshaw 12:7a7fe3baf733 1 // Updated Axis Class on 20190108 to include mode change (Position, Velocity, Acceleration)
jebradshaw 12:7a7fe3baf733 2 // all in radians, rad/sec, rad/sec/sec
jebradshaw 12:7a7fe3baf733 3 // J. Bradshaw
jebradshaw 0:cf7192f9f99a 4
jebradshaw 11:93d924320ddc 5 #ifndef MBED_AXIS_H
jebradshaw 11:93d924320ddc 6 #define MBED_AXIS_H
jebradshaw 0:cf7192f9f99a 7
jebradshaw 0:cf7192f9f99a 8 #include "mbed.h"
jebradshaw 1:cd249816dba8 9 #include "PID.h" //library for software routine PID controller
jebradshaw 1:cd249816dba8 10 #include "LS7366.h" //library for quadrature encoder interface IC's
jebradshaw 1:cd249816dba8 11 #include "MotCon.h" //simple motor control routines
jebradshaw 0:cf7192f9f99a 12
jebradshaw 0:cf7192f9f99a 13 class Axis{
jebradshaw 0:cf7192f9f99a 14 public:
jebradshaw 12:7a7fe3baf733 15 /** Create a closed loop controller connected to the specified pins
jebradshaw 12:7a7fe3baf733 16 *
jebradshaw 12:7a7fe3baf733 17 * @param _spi address of the spi object for LS7366 encoder IC communication
jebradshaw 12:7a7fe3baf733 18 * @param _cs chip select signal used for the LS7366 encoder IC spi addressing
jebradshaw 12:7a7fe3baf733 19 * @param _pwm pulse width modulation output pin for motor control signal
jebradshaw 12:7a7fe3baf733 20 * @param _dir DigitalOut pin to control the motor direction pin1
jebradshaw 12:7a7fe3baf733 21 * @param _analog analog input pin for monitoring current
jebradshaw 12:7a7fe3baf733 22 * @param _limit pointer to integer object for limit switch detection
jebradshaw 12:7a7fe3baf733 23 */
jebradshaw 10:32faca5a2577 24 Axis(SPI& _spi, PinName _cs, PinName _pwm, PinName _dir, PinName _analog, int* limit);
jebradshaw 12:7a7fe3baf733 25 /** Create a closed loop controller connected to the specified pins
jebradshaw 12:7a7fe3baf733 26 *
jebradshaw 12:7a7fe3baf733 27 * @param _spi address of the spi object for LS7366 encoder IC communication
jebradshaw 12:7a7fe3baf733 28 * @param _cs chip select signal used for the LS7366 encoder IC spi addressing
jebradshaw 12:7a7fe3baf733 29 * @param _pwm pulse width modulation output pin for motor control signal
jebradshaw 12:7a7fe3baf733 30 * @param _dir DigitalOut pin to control the motor direction pin 1
jebradshaw 12:7a7fe3baf733 31 * @param _dir2 DigitalOut pin to control the motor direction pin 2
jebradshaw 12:7a7fe3baf733 32 * @param _analog analog input pin for monitoring current
jebradshaw 12:7a7fe3baf733 33 * @param _limit pointer to integer object for limit switch detection
jebradshaw 12:7a7fe3baf733 34 */
jebradshaw 12:7a7fe3baf733 35 Axis(SPI& _spi, PinName _cs, PinName _pwm, PinName _dir, PinName _dir2, PinName _analog, int* limit);
jebradshaw 0:cf7192f9f99a 36 void paramUpdate(void);
jebradshaw 12:7a7fe3baf733 37 void init(float encCountsPerRev);
jebradshaw 0:cf7192f9f99a 38 void moveTrapezoid(float position, float time);
jebradshaw 12:7a7fe3baf733 39 void moveScurve(float position, float time);
jebradshaw 12:7a7fe3baf733 40 void moveUpdateTrapezoid(void);
jebradshaw 2:653433f4ee72 41 float readCurrent(void);
jebradshaw 2:653433f4ee72 42 void axisOff(void);
jebradshaw 2:653433f4ee72 43 void axisOn(void);
jebradshaw 5:79dcaa63700c 44 void zero(void);
jebradshaw 8:7e399d7c990d 45 void writeEncoderValue(long value);
jebradshaw 12:7a7fe3baf733 46 void updatePIDgains(float P, float I, float D);
jebradshaw 12:7a7fe3baf733 47 void changeMoveMode(int mode);
jebradshaw 0:cf7192f9f99a 48
jebradshaw 7:d0458137d6e0 49 long enc; //used to return the data from the LS7366 encoder chip
jebradshaw 7:d0458137d6e0 50 float co; // = 0.0;
jebradshaw 7:d0458137d6e0 51 float Tdelay; // = .01;
jebradshaw 0:cf7192f9f99a 52 float Pk; // 120.0 for scorbot
jebradshaw 0:cf7192f9f99a 53 float Ik; // 55.0 for scorbot
jebradshaw 0:cf7192f9f99a 54 float Dk;
jebradshaw 0:cf7192f9f99a 55 float set_point;// = 0.0;
jebradshaw 0:cf7192f9f99a 56 float set_point_last;
jebradshaw 0:cf7192f9f99a 57 float pos, vel, acc; //calculated position, velocity, and acceleration
jebradshaw 8:7e399d7c990d 58 int stat; //overall axis status
jebradshaw 0:cf7192f9f99a 59 float pos_last, vel_last, acc_last; //history variables used to calculate motion
jebradshaw 0:cf7192f9f99a 60 float pos_cmd, vel_cmd, vel_avg_cmd, acc_cmd;
jebradshaw 0:cf7192f9f99a 61 float vel_max, acc_max;
jebradshaw 0:cf7192f9f99a 62 float vel_accum;
jebradshaw 0:cf7192f9f99a 63 float moveTime;
jebradshaw 0:cf7192f9f99a 64 float p_higher, p_lower;
jebradshaw 0:cf7192f9f99a 65 int moveStatus;
jebradshaw 0:cf7192f9f99a 66 int moveState;
jebradshaw 0:cf7192f9f99a 67 int debug;
jebradshaw 1:cd249816dba8 68 int *ptr_limit;
jebradshaw 12:7a7fe3baf733 69 float motI; //motor current read from readCurrent() function
jebradshaw 12:7a7fe3baf733 70 volatile float motI_last;
jebradshaw 7:d0458137d6e0 71 float mot_I_lim; //max current limit
jebradshaw 12:7a7fe3baf733 72 float dIdT;
jebradshaw 12:7a7fe3baf733 73 float mot_I_max, mot_I_max_last;
jebradshaw 2:653433f4ee72 74 int axisState;
jebradshaw 7:d0458137d6e0 75 int motInvert;
jebradshaw 7:d0458137d6e0 76 char dataFormat; //'r'=radians (default), 'd'=degrees, 'e'=encoder counts
jebradshaw 8:7e399d7c990d 77 float pos_rad, vel_rad; //current position measurement in radians
jebradshaw 8:7e399d7c990d 78 float pos_deg, vel_deg; //current position measurement in degrees
jebradshaw 8:7e399d7c990d 79 float ctsPerDeg;
jebradshaw 9:7bc59203ce98 80 int busyflag;
jebradshaw 12:7a7fe3baf733 81 int moveMode;
jebradshaw 12:7a7fe3baf733 82 float countsPerRev;
jebradshaw 0:cf7192f9f99a 83
jebradshaw 0:cf7192f9f99a 84 Ticker update;
jebradshaw 0:cf7192f9f99a 85 Ticker moveProfile;
jebradshaw 0:cf7192f9f99a 86 Timer t;
jebradshaw 0:cf7192f9f99a 87 PID *pid;
jebradshaw 0:cf7192f9f99a 88 LS7366 *ls7366;
jebradshaw 0:cf7192f9f99a 89 MotCon *motcon;
jebradshaw 2:653433f4ee72 90 //AnalogIn *motCurrent;
jebradshaw 0:cf7192f9f99a 91
jebradshaw 0:cf7192f9f99a 92 private:
jebradshaw 0:cf7192f9f99a 93 SPI _spi;
jebradshaw 0:cf7192f9f99a 94 DigitalOut _cs;
jebradshaw 0:cf7192f9f99a 95 PwmOut _pwm;
jebradshaw 2:653433f4ee72 96 DigitalOut _dir;
jebradshaw 12:7a7fe3baf733 97 DigitalOut _dir2;
jebradshaw 2:653433f4ee72 98 AnalogIn _analog;
jebradshaw 0:cf7192f9f99a 99 };
jebradshaw 0:cf7192f9f99a 100
jebradshaw 0:cf7192f9f99a 101 #endif
jebradshaw 0:cf7192f9f99a 102
jebradshaw 0:cf7192f9f99a 103