C++ class for controlling DC motor with encoder feedback. Dependencies include LS7366LIB, MotCon, and PID.
Dependencies: LS7366LIB MotCon2 PID
Diff: Axis.cpp
- Revision:
- 10:32faca5a2577
- Parent:
- 9:7bc59203ce98
- Child:
- 11:93d924320ddc
diff -r 7bc59203ce98 -r 32faca5a2577 Axis.cpp --- a/Axis.cpp Mon Aug 29 19:42:07 2016 +0000 +++ b/Axis.cpp Wed Aug 31 21:46:27 2016 +0000 @@ -4,16 +4,14 @@ #include "MotCon.h" #include "PID.h" -#define RETRIES_MAX 10000 - -Axis::Axis(SPI& spi, PinName cs, PinName pwm, PinName dir, PinName analog, int* limit, int *busy): _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 = 75.0; //35.0; + this->Pk = 140.0; //120.0; //rough gains, seem to work well but could use tuning + this->Ik = 95.0; //75.0; this->Dk = 0.0; this->set_point = 0.0; this->set_point_last = 0.0; @@ -41,10 +39,7 @@ this->motInvert = 0; this->dataFormat = 'r'; //default is radians // this->ctsPerDeg = cpd; //update counts per degree passed from constructor - this->ptr_busy = busy; - while(*this->ptr_busy); - *this->ptr_busy=1; 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 this->motcon = new MotCon(pwm, dir); @@ -58,21 +53,9 @@ this->set_point = 0.0; this->pid->setSetPoint(this->set_point); this->enc = this->ls7366->LS7366_read_counter(); //update class variable - *this->ptr_busy=0; } -void Axis::init(void){ - int retries=0; - DigitalOut led3(P1_20); - while(*this->ptr_busy==1){ - if(retries>=RETRIES_MAX){ - led3=1; - } - wait_us(5); - retries++; - } - //now set the busy flag - *this->ptr_busy=1; +void Axis::init(void){ //resets the controllers internals this->pid->reset(); @@ -102,22 +85,9 @@ this->pid->setSetPoint(0); this->pid->setTunings(this->Pk, this->Ik, this->Dk); //turns on controller - - *this->ptr_busy=0; //clear the busy flag } void Axis::paramUpdate(void){ - int retries=0; - DigitalOut led3(P1_20); - while(*this->ptr_busy==1){ - if(retries>=RETRIES_MAX){ - led3=1; - } - wait_us(5); - retries++; - } - - *this->ptr_busy=1; //set the busy flag //testOut = 1; this->enc = this->ls7366->LS7366_read_counter(); this->pos = (float)this->enc; // * this->countsPerDeg * PI/180.0; //times counts/degree and convert to radians @@ -148,9 +118,6 @@ this->pos_last = this->pos; this->vel_last = this->vel; this->set_point_last = this->set_point; - //testOut = 0; - - *this->ptr_busy=0; //clear the busy flag } void Axis::center(void){ @@ -167,7 +134,6 @@ if(this->debug) printf("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()); } - while(*this->ptr_busy==1); this->zero(); //zero channel // this->set_point = -(totalCounts/2.0); @@ -311,18 +277,7 @@ this->update.attach(this, &Axis::paramUpdate, this->Tdelay); } -void Axis::zero(void){ - int retries=0; - DigitalOut led3(P1_20); - while(*this->ptr_busy==1){ - if(retries>=RETRIES_MAX){ - led3=!led3; - retries=0; - } - wait_us(5); - retries++; - } - *this->ptr_busy=1; //set the busy flag +void Axis::zero(void){ this->ls7366->LS7366_reset_counter(); this->ls7366->LS7366_quad_mode_x4(); @@ -330,8 +285,6 @@ this->enc = this->ls7366->LS7366_read_counter(); - *this->ptr_busy=0; //clear the busy flag - this->pos = 0.0; this->set_point = 0.0; this->pid->setSetPoint(0); @@ -339,20 +292,8 @@ } void Axis::writeEncoderValue(long value){ - int retries=0; - DigitalOut led3(P1_20); - while(*this->ptr_busy==1){ - if(retries>=RETRIES_MAX){ - led3=1; - } - wait_us(5); - retries++; - } - *this->ptr_busy=1; //set the busy flag this->ls7366->LS7366_write_DTR(value); - - *this->ptr_busy=0; //clear the busy flag this->set_point = (float)value; this->pid->setSetPoint(this->set_point);