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

Dependencies:   LS7366LIB MotCon2 PID

Dependents:   LPC1768_6axis_Arm

Committer:
jebradshaw
Date:
Wed Jul 11 13:51:11 2018 +0000
Revision:
13:bd1cb132c51c
Parent:
12:b80cc2e27bdb
Axis - 20180706

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jebradshaw 0:cf7192f9f99a 1
jebradshaw 0:cf7192f9f99a 2
jebradshaw 11:93d924320ddc 3 #ifndef MBED_AXIS_H
jebradshaw 11:93d924320ddc 4 #define MBED_AXIS_H
jebradshaw 0:cf7192f9f99a 5
jebradshaw 0:cf7192f9f99a 6 #include "mbed.h"
jebradshaw 1:cd249816dba8 7 #include "PID.h" //library for software routine PID controller
jebradshaw 1:cd249816dba8 8 #include "LS7366.h" //library for quadrature encoder interface IC's
jebradshaw 1:cd249816dba8 9 #include "MotCon.h" //simple motor control routines
jebradshaw 0:cf7192f9f99a 10
jebradshaw 0:cf7192f9f99a 11 class Axis{
jebradshaw 0:cf7192f9f99a 12 public:
jebradshaw 10:32faca5a2577 13 Axis(SPI& _spi, PinName _cs, PinName _pwm, PinName _dir, PinName _analog, int* limit);
jebradshaw 0:cf7192f9f99a 14 void paramUpdate(void);
jebradshaw 2:653433f4ee72 15 void center(void);
jebradshaw 2:653433f4ee72 16 void init(void);
jebradshaw 0:cf7192f9f99a 17 void moveTrapezoid(float position, float time);
jebradshaw 0:cf7192f9f99a 18 void moveUpdate(void);
jebradshaw 2:653433f4ee72 19 float readCurrent(void);
jebradshaw 2:653433f4ee72 20 void axisOff(void);
jebradshaw 2:653433f4ee72 21 void axisOn(void);
jebradshaw 5:79dcaa63700c 22 void zero(void);
jebradshaw 8:7e399d7c990d 23 void writeEncoderValue(long value);
jebradshaw 12:b80cc2e27bdb 24 void updatePIDgains(float P, float I, float D);
jebradshaw 0:cf7192f9f99a 25
jebradshaw 7:d0458137d6e0 26 long enc; //used to return the data from the LS7366 encoder chip
jebradshaw 7:d0458137d6e0 27 float co; // = 0.0;
jebradshaw 7:d0458137d6e0 28 float Tdelay; // = .01;
jebradshaw 0:cf7192f9f99a 29 float Pk; // 120.0 for scorbot
jebradshaw 0:cf7192f9f99a 30 float Ik; // 55.0 for scorbot
jebradshaw 0:cf7192f9f99a 31 float Dk;
jebradshaw 0:cf7192f9f99a 32 float set_point;// = 0.0;
jebradshaw 0:cf7192f9f99a 33 float set_point_last;
jebradshaw 0:cf7192f9f99a 34 float pos, vel, acc; //calculated position, velocity, and acceleration
jebradshaw 8:7e399d7c990d 35 int stat; //overall axis status
jebradshaw 0:cf7192f9f99a 36 float pos_last, vel_last, acc_last; //history variables used to calculate motion
jebradshaw 0:cf7192f9f99a 37 float pos_cmd, vel_cmd, vel_avg_cmd, acc_cmd;
jebradshaw 0:cf7192f9f99a 38 float vel_max, acc_max;
jebradshaw 0:cf7192f9f99a 39 float vel_accum;
jebradshaw 0:cf7192f9f99a 40 float moveTime;
jebradshaw 0:cf7192f9f99a 41 float p_higher, p_lower;
jebradshaw 0:cf7192f9f99a 42 int moveStatus;
jebradshaw 0:cf7192f9f99a 43 int moveState;
jebradshaw 0:cf7192f9f99a 44 int debug;
jebradshaw 1:cd249816dba8 45 int *ptr_limit;
jebradshaw 12:b80cc2e27bdb 46 float motI; //motor current read from readCurrent() function
jebradshaw 12:b80cc2e27bdb 47 volatile float motI_last;
jebradshaw 7:d0458137d6e0 48 float mot_I_lim; //max current limit
jebradshaw 12:b80cc2e27bdb 49 float dIdT;
jebradshaw 12:b80cc2e27bdb 50 float mot_I_max, mot_I_max_last;
jebradshaw 2:653433f4ee72 51 int axisState;
jebradshaw 7:d0458137d6e0 52 int motInvert;
jebradshaw 7:d0458137d6e0 53 char dataFormat; //'r'=radians (default), 'd'=degrees, 'e'=encoder counts
jebradshaw 8:7e399d7c990d 54 float pos_rad, vel_rad; //current position measurement in radians
jebradshaw 8:7e399d7c990d 55 float pos_deg, vel_deg; //current position measurement in degrees
jebradshaw 8:7e399d7c990d 56 float ctsPerDeg;
jebradshaw 9:7bc59203ce98 57 int busyflag;
jebradshaw 0:cf7192f9f99a 58
jebradshaw 0:cf7192f9f99a 59 Ticker update;
jebradshaw 0:cf7192f9f99a 60 Ticker moveProfile;
jebradshaw 0:cf7192f9f99a 61 Timer t;
jebradshaw 0:cf7192f9f99a 62 PID *pid;
jebradshaw 0:cf7192f9f99a 63 LS7366 *ls7366;
jebradshaw 0:cf7192f9f99a 64 MotCon *motcon;
jebradshaw 2:653433f4ee72 65 //AnalogIn *motCurrent;
jebradshaw 0:cf7192f9f99a 66
jebradshaw 0:cf7192f9f99a 67 private:
jebradshaw 0:cf7192f9f99a 68 SPI _spi;
jebradshaw 0:cf7192f9f99a 69 DigitalOut _cs;
jebradshaw 0:cf7192f9f99a 70 PwmOut _pwm;
jebradshaw 2:653433f4ee72 71 DigitalOut _dir;
jebradshaw 2:653433f4ee72 72 AnalogIn _analog;
jebradshaw 0:cf7192f9f99a 73 };
jebradshaw 0:cf7192f9f99a 74
jebradshaw 0:cf7192f9f99a 75 #endif
jebradshaw 0:cf7192f9f99a 76
jebradshaw 0:cf7192f9f99a 77