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

Dependencies:   LS7366LIB MotCon2 PID

Dependents:   LPC1768_6axis_Arm

Files at this revision

API Documentation at this revision

Comitter:
jebradshaw
Date:
Mon Jul 11 18:26:26 2016 +0000
Parent:
7:d0458137d6e0
Child:
9:7bc59203ce98
Commit message:
Updated gains for DC motors

Changed in this revision

Axis.cpp Show annotated file Show diff for this revision Revisions of this file
Axis.h Show annotated file Show diff for this revision Revisions of this file
MotCon.lib Show annotated file Show diff for this revision Revisions of this file
--- a/Axis.cpp	Thu May 19 12:41:53 2016 +0000
+++ b/Axis.cpp	Mon Jul 11 18:26:26 2016 +0000
@@ -4,20 +4,21 @@
 #include "MotCon.h"
 #include "PID.h"
 
-Axis::Axis(SPI& spi, PinName cs, PinName pwm, PinName dir, PinName analog, int* limit, float totalCnts): _spi(spi), _cs(cs), _pwm(pwm), _dir(dir) , _analog(analog){
+Axis::Axis(SPI& spi, PinName cs, PinName pwm, PinName dir, PinName analog, int* limit): _spi(spi), _cs(cs), _pwm(pwm), _dir(dir) , _analog(analog){
     this->_cs = 1;           // Initialize chip select as off (high)
     this->_pwm = 0.0;
     this->_dir = 0;
     this->co = 0.0;
     this->Tdelay = .01;
     this->Pk = 120.0; //80.0;      //rough gains, seem to work well but could use tuning   
-    this->Ik = 55.0;  //35.0;
+    this->Ik = 75.0;  //35.0;
     this->Dk = 0.0;
     this->set_point = 0.0;
     this->set_point_last = 0.0;
     this->pos = 0.0;
     this->vel = 0.0;
     this->acc = 0.0;
+    this->stat = -1;
     this->pos_cmd = 0.0;
     this->vel_cmd = 0.0;
     this->vel_avg_cmd = 0;
@@ -28,7 +29,6 @@
     this->p_lower = 0.0;
     this->vel_accum = 0.0;
     this->moveTime = 0.0;
-    this->totalCounts = totalCnts;
     this->enc = 0;
     this->moveStatus = 0;     //status flag to indicate state of profile movement
     this->moveState = 0;      //used for state machine in movement profiles
@@ -38,6 +38,7 @@
     this->mot_I_lim = .35;    
     this->motInvert = 0;
     this->dataFormat = 'r'; //default is radians
+//    this->ctsPerDeg = cpd;  //update counts per degree passed from constructor
     
     this->pid = new PID(0.0,0.0,0.0,Tdelay); //Kc, Ti, Td, interval
     this->ls7366 = new LS7366(spi, cs);  //LS7366 encoder interface IC
@@ -59,7 +60,7 @@
     this->pid->reset();
 
     //Encoder counts limit
-    this->pid->setInputLimits(-this->totalCounts, this->totalCounts); 
+    this->pid->setInputLimits(-55000, 55000); 
     //Pwm output from 0.0 to 1.0
     this->pid->setOutputLimits(-1.0, 1.0);
     //If there's a bias.
@@ -101,15 +102,12 @@
     //Set the new output.
     this->co = this->pid->compute();
 
-DigitalOut led3(P1_21);
     if(this->axisState){
         if(this->motInvert==0){
             this->motcon->mot_control(this->co); //send controller output to PWM motor control command    
-            led3=1;
         }
         else{
             this->motcon->mot_control(this->co, 1); //send controller output to PWM motor control command    
-            led3=0;
         }
     }
     else{
@@ -123,9 +121,7 @@
     //testOut = 0;
 }
 
-void Axis::center(void){
-    this->pid->setInputLimits(-this->totalCounts, this->totalCounts); 
-    
+void Axis::center(void){    
     while((*this->ptr_limit == 1) && (this->readCurrent() < mot_I_lim)){ //limit switch not pressed and mot current not exceeded
        this->set_point += 100;
        wait(.05);
@@ -141,9 +137,7 @@
     }
     this->zero();   //zero channel        
     
-    this->set_point = -(totalCounts/2.0);
-    wait(5.0);
-    this->zero();
+//    this->set_point = -(totalCounts/2.0);
         
     if(this->debug)
         printf("HOME END:T=%.2f SP=%.3f co=%.3f pos=%.3f vel=%.3f acc=%.3f limit=%d motI=%.3f\r\n", t.read(), this->set_point, this->co, this->pos, this->vel, this->acc,*this->ptr_limit, this->_analog.read());
@@ -262,7 +256,7 @@
 }
 
 float Axis::readCurrent(void){    
-    motCurrent = this->_analog.read() * 3.3;
+    motCurrent = (this->_analog.read() * 3.3)/ .525;  //525mV per amp
     return motCurrent;
 }
 
@@ -289,4 +283,11 @@
     
     this->set_point = 0.0;
     this->pid->setSetPoint(0);    
+}
+
+void Axis::writeEncoderValue(long value){
+    this->ls7366->LS7366_write_DTR(value);
+    
+    this->set_point = (float)value;
+    this->pid->setSetPoint(this->set_point);    
 }
\ No newline at end of file
--- a/Axis.h	Thu May 19 12:41:53 2016 +0000
+++ b/Axis.h	Mon Jul 11 18:26:26 2016 +0000
@@ -10,7 +10,7 @@
 
 class Axis{
 public:
-    Axis(SPI& _spi, PinName _cs, PinName _pwm, PinName _dir, PinName _analog, int* limit, float totalCnts);    
+    Axis(SPI& _spi, PinName _cs, PinName _pwm, PinName _dir, PinName _analog, int* limit);    
     void paramUpdate(void);
     void center(void);
     void init(void);
@@ -20,6 +20,7 @@
     void axisOff(void);
     void axisOn(void);
     void zero(void);
+    void writeEncoderValue(long value);
     
     long enc;       //used to return the data from the LS7366 encoder chip
     float co;       // = 0.0;
@@ -30,6 +31,7 @@
     float set_point;// = 0.0;
     float set_point_last;
     float pos, vel, acc;    //calculated position, velocity, and acceleration
+    int stat;               //overall axis status
     float pos_last, vel_last, acc_last; //history variables used to calculate motion
     float pos_cmd, vel_cmd, vel_avg_cmd, acc_cmd;    
     float vel_max, acc_max;
@@ -40,12 +42,14 @@
     int moveState;
     int debug;
     int *ptr_limit;
-    float totalCounts;
     float motCurrent;   //motor current read from readCurrent() function
     float mot_I_lim;    //max current limit
     int axisState;
     int motInvert;
     char dataFormat;    //'r'=radians (default), 'd'=degrees, 'e'=encoder counts
+    float pos_rad, vel_rad;      //current position measurement in radians
+    float pos_deg, vel_deg;  //current position measurement in degrees
+    float ctsPerDeg;
 
     Ticker update;
     Ticker moveProfile;
--- a/MotCon.lib	Thu May 19 12:41:53 2016 +0000
+++ b/MotCon.lib	Mon Jul 11 18:26:26 2016 +0000
@@ -1,1 +1,1 @@
-https://developer.mbed.org/users/jebradshaw/code/MotCon/#69e79f1db999
+https://developer.mbed.org/users/jebradshaw/code/MotCon/#23cd902e1774