C++ class for controlling DC motor with encoder feedback. Dependencies include LS7366LIB, MotCon, and PID.
Dependencies: LS7366LIB MotCon2 PID
Axis.cpp@7:d0458137d6e0, 2016-05-19 (annotated)
- Committer:
- jebradshaw
- Date:
- Thu May 19 12:41:53 2016 +0000
- Revision:
- 7:d0458137d6e0
- Parent:
- 5:79dcaa63700c
- Child:
- 8:7e399d7c990d
Updated 20160519 - Plan to add motor state and output format variable ('e'-encoder counts, 'r'-radians, 'd'-degrees)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jebradshaw | 0:cf7192f9f99a | 1 | |
jebradshaw | 0:cf7192f9f99a | 2 | #include "Axis.h" |
jebradshaw | 0:cf7192f9f99a | 3 | #include "LS7366.h" |
jebradshaw | 0:cf7192f9f99a | 4 | #include "MotCon.h" |
jebradshaw | 0:cf7192f9f99a | 5 | #include "PID.h" |
jebradshaw | 0:cf7192f9f99a | 6 | |
jebradshaw | 2:653433f4ee72 | 7 | 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){ |
jebradshaw | 4:4079f92f9c26 | 8 | this->_cs = 1; // Initialize chip select as off (high) |
jebradshaw | 4:4079f92f9c26 | 9 | this->_pwm = 0.0; |
jebradshaw | 4:4079f92f9c26 | 10 | this->_dir = 0; |
jebradshaw | 4:4079f92f9c26 | 11 | this->co = 0.0; |
jebradshaw | 4:4079f92f9c26 | 12 | this->Tdelay = .01; |
jebradshaw | 4:4079f92f9c26 | 13 | this->Pk = 120.0; //80.0; //rough gains, seem to work well but could use tuning |
jebradshaw | 4:4079f92f9c26 | 14 | this->Ik = 55.0; //35.0; |
jebradshaw | 4:4079f92f9c26 | 15 | this->Dk = 0.0; |
jebradshaw | 4:4079f92f9c26 | 16 | this->set_point = 0.0; |
jebradshaw | 4:4079f92f9c26 | 17 | this->set_point_last = 0.0; |
jebradshaw | 4:4079f92f9c26 | 18 | this->pos = 0.0; |
jebradshaw | 4:4079f92f9c26 | 19 | this->vel = 0.0; |
jebradshaw | 4:4079f92f9c26 | 20 | this->acc = 0.0; |
jebradshaw | 4:4079f92f9c26 | 21 | this->pos_cmd = 0.0; |
jebradshaw | 4:4079f92f9c26 | 22 | this->vel_cmd = 0.0; |
jebradshaw | 4:4079f92f9c26 | 23 | this->vel_avg_cmd = 0; |
jebradshaw | 4:4079f92f9c26 | 24 | this->acc_cmd = 0.0; |
jebradshaw | 4:4079f92f9c26 | 25 | this->vel_max = 2700.0 * Tdelay; //counts * Tdelay |
jebradshaw | 4:4079f92f9c26 | 26 | this->acc_max = 1200.0 * Tdelay; //counts/sec/sec * Tdelay |
jebradshaw | 4:4079f92f9c26 | 27 | this->p_higher = 0.0; |
jebradshaw | 4:4079f92f9c26 | 28 | this->p_lower = 0.0; |
jebradshaw | 4:4079f92f9c26 | 29 | this->vel_accum = 0.0; |
jebradshaw | 4:4079f92f9c26 | 30 | this->moveTime = 0.0; |
jebradshaw | 4:4079f92f9c26 | 31 | this->totalCounts = totalCnts; |
jebradshaw | 4:4079f92f9c26 | 32 | this->enc = 0; |
jebradshaw | 4:4079f92f9c26 | 33 | this->moveStatus = 0; //status flag to indicate state of profile movement |
jebradshaw | 4:4079f92f9c26 | 34 | this->moveState = 0; //used for state machine in movement profiles |
jebradshaw | 4:4079f92f9c26 | 35 | this->debug = 0; |
jebradshaw | 4:4079f92f9c26 | 36 | this->update.attach(this, &Axis::paramUpdate, Tdelay); |
jebradshaw | 5:79dcaa63700c | 37 | this->axisState = 0; |
jebradshaw | 5:79dcaa63700c | 38 | this->mot_I_lim = .35; |
jebradshaw | 7:d0458137d6e0 | 39 | this->motInvert = 0; |
jebradshaw | 7:d0458137d6e0 | 40 | this->dataFormat = 'r'; //default is radians |
jebradshaw | 0:cf7192f9f99a | 41 | |
jebradshaw | 4:4079f92f9c26 | 42 | this->pid = new PID(0.0,0.0,0.0,Tdelay); //Kc, Ti, Td, interval |
jebradshaw | 4:4079f92f9c26 | 43 | this->ls7366 = new LS7366(spi, cs); //LS7366 encoder interface IC |
jebradshaw | 4:4079f92f9c26 | 44 | this->motcon = new MotCon(pwm, dir); |
jebradshaw | 4:4079f92f9c26 | 45 | this->ptr_limit = limit; |
jebradshaw | 0:cf7192f9f99a | 46 | |
jebradshaw | 0:cf7192f9f99a | 47 | //start at 0 |
jebradshaw | 0:cf7192f9f99a | 48 | this->ls7366->LS7366_reset_counter(); |
jebradshaw | 0:cf7192f9f99a | 49 | this->ls7366->LS7366_quad_mode_x4(); |
jebradshaw | 0:cf7192f9f99a | 50 | this->ls7366->LS7366_write_DTR(0); |
jebradshaw | 0:cf7192f9f99a | 51 | |
jebradshaw | 0:cf7192f9f99a | 52 | this->set_point = 0.0; |
jebradshaw | 0:cf7192f9f99a | 53 | this->pid->setSetPoint(this->set_point); |
jebradshaw | 0:cf7192f9f99a | 54 | this->enc = this->ls7366->LS7366_read_counter(); //update class variable |
jebradshaw | 0:cf7192f9f99a | 55 | } |
jebradshaw | 0:cf7192f9f99a | 56 | |
jebradshaw | 2:653433f4ee72 | 57 | void Axis::init(void){ |
jebradshaw | 0:cf7192f9f99a | 58 | //resets the controllers internals |
jebradshaw | 0:cf7192f9f99a | 59 | this->pid->reset(); |
jebradshaw | 0:cf7192f9f99a | 60 | |
jebradshaw | 0:cf7192f9f99a | 61 | //Encoder counts limit |
jebradshaw | 2:653433f4ee72 | 62 | this->pid->setInputLimits(-this->totalCounts, this->totalCounts); |
jebradshaw | 0:cf7192f9f99a | 63 | //Pwm output from 0.0 to 1.0 |
jebradshaw | 0:cf7192f9f99a | 64 | this->pid->setOutputLimits(-1.0, 1.0); |
jebradshaw | 0:cf7192f9f99a | 65 | //If there's a bias. |
jebradshaw | 0:cf7192f9f99a | 66 | this->pid->setBias(0.0); |
jebradshaw | 0:cf7192f9f99a | 67 | this->pid->setMode(AUTO_MODE); |
jebradshaw | 0:cf7192f9f99a | 68 | |
jebradshaw | 0:cf7192f9f99a | 69 | this->pid->setInterval(this->Tdelay); |
jebradshaw | 0:cf7192f9f99a | 70 | |
jebradshaw | 0:cf7192f9f99a | 71 | //start at 0 |
jebradshaw | 0:cf7192f9f99a | 72 | this->ls7366->LS7366_reset_counter(); |
jebradshaw | 0:cf7192f9f99a | 73 | this->ls7366->LS7366_quad_mode_x4(); |
jebradshaw | 0:cf7192f9f99a | 74 | this->ls7366->LS7366_write_DTR(0); |
jebradshaw | 0:cf7192f9f99a | 75 | |
jebradshaw | 0:cf7192f9f99a | 76 | this->set_point = 0.0; |
jebradshaw | 0:cf7192f9f99a | 77 | this->pid->setSetPoint(this->set_point); |
jebradshaw | 0:cf7192f9f99a | 78 | this->enc = this->ls7366->LS7366_read_counter(); //update class variable |
jebradshaw | 0:cf7192f9f99a | 79 | |
jebradshaw | 0:cf7192f9f99a | 80 | //resets the controllers internals |
jebradshaw | 0:cf7192f9f99a | 81 | this->pid->reset(); |
jebradshaw | 0:cf7192f9f99a | 82 | //start at 0 |
jebradshaw | 0:cf7192f9f99a | 83 | this->set_point = 0.0; |
jebradshaw | 0:cf7192f9f99a | 84 | this->pid->setSetPoint(0); |
jebradshaw | 0:cf7192f9f99a | 85 | |
jebradshaw | 0:cf7192f9f99a | 86 | this->pid->setTunings(this->Pk, this->Ik, this->Dk); //turns on controller |
jebradshaw | 0:cf7192f9f99a | 87 | } |
jebradshaw | 0:cf7192f9f99a | 88 | |
jebradshaw | 0:cf7192f9f99a | 89 | void Axis::paramUpdate(void){ |
jebradshaw | 0:cf7192f9f99a | 90 | //testOut = 1; |
jebradshaw | 0:cf7192f9f99a | 91 | this->enc = this->ls7366->LS7366_read_counter(); |
jebradshaw | 0:cf7192f9f99a | 92 | this->pos = (float)this->enc; // * this->countsPerDeg * PI/180.0; //times counts/degree and convert to radians |
jebradshaw | 0:cf7192f9f99a | 93 | |
jebradshaw | 0:cf7192f9f99a | 94 | this->vel = (this->pos - this->pos_last) * this->Tdelay; |
jebradshaw | 0:cf7192f9f99a | 95 | this->acc = (this->vel - this->vel_last); |
jebradshaw | 0:cf7192f9f99a | 96 | |
jebradshaw | 0:cf7192f9f99a | 97 | this->pid->setSetPoint(this->set_point); |
jebradshaw | 0:cf7192f9f99a | 98 | |
jebradshaw | 0:cf7192f9f99a | 99 | //Update the process variable. |
jebradshaw | 0:cf7192f9f99a | 100 | this->pid->setProcessValue(this->pos); |
jebradshaw | 0:cf7192f9f99a | 101 | //Set the new output. |
jebradshaw | 0:cf7192f9f99a | 102 | this->co = this->pid->compute(); |
jebradshaw | 0:cf7192f9f99a | 103 | |
jebradshaw | 7:d0458137d6e0 | 104 | DigitalOut led3(P1_21); |
jebradshaw | 7:d0458137d6e0 | 105 | if(this->axisState){ |
jebradshaw | 7:d0458137d6e0 | 106 | if(this->motInvert==0){ |
jebradshaw | 7:d0458137d6e0 | 107 | this->motcon->mot_control(this->co); //send controller output to PWM motor control command |
jebradshaw | 7:d0458137d6e0 | 108 | led3=1; |
jebradshaw | 7:d0458137d6e0 | 109 | } |
jebradshaw | 7:d0458137d6e0 | 110 | else{ |
jebradshaw | 7:d0458137d6e0 | 111 | this->motcon->mot_control(this->co, 1); //send controller output to PWM motor control command |
jebradshaw | 7:d0458137d6e0 | 112 | led3=0; |
jebradshaw | 7:d0458137d6e0 | 113 | } |
jebradshaw | 7:d0458137d6e0 | 114 | } |
jebradshaw | 4:4079f92f9c26 | 115 | else{ |
jebradshaw | 4:4079f92f9c26 | 116 | this->co = 0.0; |
jebradshaw | 4:4079f92f9c26 | 117 | this->motcon->mot_control(0.0); //turn off motor command |
jebradshaw | 4:4079f92f9c26 | 118 | } |
jebradshaw | 4:4079f92f9c26 | 119 | |
jebradshaw | 0:cf7192f9f99a | 120 | this->pos_last = this->pos; |
jebradshaw | 0:cf7192f9f99a | 121 | this->vel_last = this->vel; |
jebradshaw | 0:cf7192f9f99a | 122 | this->set_point_last = this->set_point; |
jebradshaw | 0:cf7192f9f99a | 123 | //testOut = 0; |
jebradshaw | 0:cf7192f9f99a | 124 | } |
jebradshaw | 0:cf7192f9f99a | 125 | |
jebradshaw | 2:653433f4ee72 | 126 | void Axis::center(void){ |
jebradshaw | 2:653433f4ee72 | 127 | this->pid->setInputLimits(-this->totalCounts, this->totalCounts); |
jebradshaw | 2:653433f4ee72 | 128 | |
jebradshaw | 5:79dcaa63700c | 129 | while((*this->ptr_limit == 1) && (this->readCurrent() < mot_I_lim)){ //limit switch not pressed and mot current not exceeded |
jebradshaw | 0:cf7192f9f99a | 130 | this->set_point += 100; |
jebradshaw | 7:d0458137d6e0 | 131 | wait(.05); |
jebradshaw | 0:cf7192f9f99a | 132 | if(this->debug) |
jebradshaw | 2:653433f4ee72 | 133 | 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()); |
jebradshaw | 0:cf7192f9f99a | 134 | } |
jebradshaw | 5:79dcaa63700c | 135 | wait(.5); |
jebradshaw | 5:79dcaa63700c | 136 | while((*this->ptr_limit == 0)){ //limit switch is pressed |
jebradshaw | 5:79dcaa63700c | 137 | this->set_point -= 10; |
jebradshaw | 5:79dcaa63700c | 138 | wait(.1); |
jebradshaw | 5:79dcaa63700c | 139 | if(this->debug) |
jebradshaw | 2:653433f4ee72 | 140 | 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()); |
jebradshaw | 0:cf7192f9f99a | 141 | } |
jebradshaw | 5:79dcaa63700c | 142 | this->zero(); //zero channel |
jebradshaw | 0:cf7192f9f99a | 143 | |
jebradshaw | 5:79dcaa63700c | 144 | this->set_point = -(totalCounts/2.0); |
jebradshaw | 5:79dcaa63700c | 145 | wait(5.0); |
jebradshaw | 5:79dcaa63700c | 146 | this->zero(); |
jebradshaw | 0:cf7192f9f99a | 147 | |
jebradshaw | 2:653433f4ee72 | 148 | if(this->debug) |
jebradshaw | 2:653433f4ee72 | 149 | 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()); |
jebradshaw | 0:cf7192f9f99a | 150 | // pc.printf("End Home\r\n\r\n"); |
jebradshaw | 0:cf7192f9f99a | 151 | } |
jebradshaw | 0:cf7192f9f99a | 152 | |
jebradshaw | 2:653433f4ee72 | 153 | void Axis::moveUpdate(void){ |
jebradshaw | 2:653433f4ee72 | 154 | |
jebradshaw | 5:79dcaa63700c | 155 | /* if(*this->ptr_limit == 0){ |
jebradshaw | 2:653433f4ee72 | 156 | this->moveState = 4; //terminate the move |
jebradshaw | 2:653433f4ee72 | 157 | printf("\r\nLimit reached on axis!\r\n"); |
jebradshaw | 2:653433f4ee72 | 158 | } |
jebradshaw | 5:79dcaa63700c | 159 | */ |
jebradshaw | 2:653433f4ee72 | 160 | if(this->debug) |
jebradshaw | 2:653433f4ee72 | 161 | 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()); |
jebradshaw | 2:653433f4ee72 | 162 | |
jebradshaw | 0:cf7192f9f99a | 163 | switch(this->moveState){ |
jebradshaw | 0:cf7192f9f99a | 164 | case 0: |
jebradshaw | 0:cf7192f9f99a | 165 | break; |
jebradshaw | 0:cf7192f9f99a | 166 | |
jebradshaw | 0:cf7192f9f99a | 167 | //accelerate |
jebradshaw | 0:cf7192f9f99a | 168 | case 1: |
jebradshaw | 0:cf7192f9f99a | 169 | //testOut = 1; |
jebradshaw | 0:cf7192f9f99a | 170 | this->vel_accum += this->acc_cmd * this->Tdelay; //add acceleration to the velocity accumulator |
jebradshaw | 0:cf7192f9f99a | 171 | if(this->vel_avg_cmd > 0.0){ //check the sign of the movement |
jebradshaw | 0:cf7192f9f99a | 172 | if(this->vel_accum >= this->vel_cmd) //if the accumulator reaches or exceeds the velocity command |
jebradshaw | 0:cf7192f9f99a | 173 | this->vel_accum = this->vel_cmd; // only add the velocity command to the accumulator |
jebradshaw | 0:cf7192f9f99a | 174 | } |
jebradshaw | 0:cf7192f9f99a | 175 | else{ //if the sign was negative |
jebradshaw | 0:cf7192f9f99a | 176 | if(this->vel_accum <= this->vel_cmd) |
jebradshaw | 0:cf7192f9f99a | 177 | this->vel_accum = this->vel_cmd; |
jebradshaw | 0:cf7192f9f99a | 178 | } |
jebradshaw | 0:cf7192f9f99a | 179 | //testOut = 0; |
jebradshaw | 0:cf7192f9f99a | 180 | |
jebradshaw | 0:cf7192f9f99a | 181 | this->set_point += this->vel_accum; |
jebradshaw | 0:cf7192f9f99a | 182 | //pc.printf("T=%.2f SP=%.3f co=%.3f enc=%d pos=%.3f vel=%.3f acc=%.3f vel_accum=%.2f accelCnt=%.2f \r\n", t.read(), con0.set_point, con0.co, con0.enc, con0.pos, con0.vel, con0.acc, vel_accum, accelCnt); |
jebradshaw | 0:cf7192f9f99a | 183 | //pc.printf("acc_up,%.2f,%.3f,%.1f,%.3f,%.3f,%.2f,%.2f\r\n", this->t.read(), this->set_point, this->pos, this->vel, this->acc, this->vel_accum, this->acc_cmd); |
jebradshaw | 0:cf7192f9f99a | 184 | |
jebradshaw | 0:cf7192f9f99a | 185 | if(this->t.read()>=(this->moveTime/3.0) || (abs(this->vel_accum) > abs(this->vel_cmd))) |
jebradshaw | 0:cf7192f9f99a | 186 | this->moveState = 2; |
jebradshaw | 0:cf7192f9f99a | 187 | break; |
jebradshaw | 0:cf7192f9f99a | 188 | |
jebradshaw | 0:cf7192f9f99a | 189 | //constant velocity |
jebradshaw | 0:cf7192f9f99a | 190 | case 2: |
jebradshaw | 0:cf7192f9f99a | 191 | //testOut = 1; |
jebradshaw | 0:cf7192f9f99a | 192 | //this->vel_accum += this->vel_cmd * this->Tdelay; |
jebradshaw | 0:cf7192f9f99a | 193 | this->set_point += this->vel_cmd; |
jebradshaw | 0:cf7192f9f99a | 194 | //testOut = 0; |
jebradshaw | 0:cf7192f9f99a | 195 | //pc.printf("T=%.2f SP=%.3f co=%.3f enc=%d pos=%.3f vel=%.3f acc=%.3f vel_accum=%.2f accelCnt=%.2f \r\n", t.read(), con0.set_point, con0.co, con0.enc, con0.pos, con0.vel, con0.acc, vel_accum, accelCnt); |
jebradshaw | 0:cf7192f9f99a | 196 | //pc.printf("vel_cn,%.2f,%.3f,%.1f,%.3f,%.3f,%.2f,%.2f\r\n", this->t.read(), this->set_point, this->pos, this->vel, this->acc, this->vel_accum, this->acc_cmd); |
jebradshaw | 0:cf7192f9f99a | 197 | |
jebradshaw | 0:cf7192f9f99a | 198 | if(this->t.read()>=(2.0/3.0 * this->moveTime)) |
jebradshaw | 0:cf7192f9f99a | 199 | this->moveState = 3; |
jebradshaw | 0:cf7192f9f99a | 200 | break; |
jebradshaw | 0:cf7192f9f99a | 201 | |
jebradshaw | 0:cf7192f9f99a | 202 | //decelerate |
jebradshaw | 0:cf7192f9f99a | 203 | case 3: |
jebradshaw | 0:cf7192f9f99a | 204 | this->vel_accum -= this->acc_cmd * this->Tdelay; |
jebradshaw | 0:cf7192f9f99a | 205 | |
jebradshaw | 0:cf7192f9f99a | 206 | this->set_point += this->vel_accum; //ramp down velocity by acceleration |
jebradshaw | 0:cf7192f9f99a | 207 | //pc.printf("T=%.2f SP=%.3f co=%.3f enc=%d pos=%.3f vel=%.3f acc=%.3f vel_accum=%.2f accelCnt=%.2f \r\n", t.read(), con0.set_point, con0.co, con0.enc, con0.pos, con0.vel, con0.acc, vel_accum, accelCnt); |
jebradshaw | 0:cf7192f9f99a | 208 | //pc.printf("acc_dn,%.2f,%.3f,%.1f,%.3f,%.3f,%.2f,%.2f\r\n", this->t.read(), this->set_point, this->pos, this->vel, this->acc, this->vel_accum, this->acc_cmd); |
jebradshaw | 0:cf7192f9f99a | 209 | |
jebradshaw | 0:cf7192f9f99a | 210 | if(this->vel_avg_cmd > 0.0){ |
jebradshaw | 1:cd249816dba8 | 211 | if(this->pos_cmd <= this->pos){ |
jebradshaw | 1:cd249816dba8 | 212 | //finish with position command |
jebradshaw | 1:cd249816dba8 | 213 | this->set_point = this->pos_cmd; |
jebradshaw | 0:cf7192f9f99a | 214 | this->moveState = 4; |
jebradshaw | 1:cd249816dba8 | 215 | } |
jebradshaw | 0:cf7192f9f99a | 216 | } |
jebradshaw | 0:cf7192f9f99a | 217 | else{ |
jebradshaw | 1:cd249816dba8 | 218 | if(this->pos_cmd >= this->pos){ |
jebradshaw | 1:cd249816dba8 | 219 | //finish with position command |
jebradshaw | 5:79dcaa63700c | 220 | //this->set_point = this->pos_cmd; //May be causing jerk after multi-axis movements J Bradshaw 20151124 |
jebradshaw | 1:cd249816dba8 | 221 | this->moveState = 4; |
jebradshaw | 1:cd249816dba8 | 222 | } |
jebradshaw | 0:cf7192f9f99a | 223 | } |
jebradshaw | 0:cf7192f9f99a | 224 | |
jebradshaw | 0:cf7192f9f99a | 225 | if(this->t.read()>=this->moveTime){ |
jebradshaw | 1:cd249816dba8 | 226 | //finish with position command |
jebradshaw | 5:79dcaa63700c | 227 | //this->set_point = this->pos_cmd; //May be causing jerk after multi-axis movements J Bradshaw 20151124 |
jebradshaw | 0:cf7192f9f99a | 228 | this->moveState = 4; |
jebradshaw | 0:cf7192f9f99a | 229 | } |
jebradshaw | 0:cf7192f9f99a | 230 | break; |
jebradshaw | 0:cf7192f9f99a | 231 | |
jebradshaw | 0:cf7192f9f99a | 232 | case 4: |
jebradshaw | 0:cf7192f9f99a | 233 | this->moveProfile.detach(); //turn off the trapazoidal update ticker |
jebradshaw | 0:cf7192f9f99a | 234 | this->t.stop(); |
jebradshaw | 0:cf7192f9f99a | 235 | this->moveState = 0; |
jebradshaw | 0:cf7192f9f99a | 236 | break; |
jebradshaw | 0:cf7192f9f99a | 237 | }//switch moveStatus |
jebradshaw | 0:cf7192f9f99a | 238 | return; |
jebradshaw | 0:cf7192f9f99a | 239 | } |
jebradshaw | 0:cf7192f9f99a | 240 | |
jebradshaw | 0:cf7192f9f99a | 241 | // position - encoder position to move to |
jebradshaw | 0:cf7192f9f99a | 242 | // time - duration of the movement |
jebradshaw | 0:cf7192f9f99a | 243 | void Axis::moveTrapezoid(float positionCmd, float time){ |
jebradshaw | 0:cf7192f9f99a | 244 | this->pos_cmd = positionCmd; |
jebradshaw | 0:cf7192f9f99a | 245 | this->moveTime = time; |
jebradshaw | 4:4079f92f9c26 | 246 | float enc_distance = this->pos_cmd - (float)this->enc;// * 1.0/con0.countsPerDeg * 180.0/PI; |
jebradshaw | 0:cf7192f9f99a | 247 | |
jebradshaw | 0:cf7192f9f99a | 248 | this->vel_avg_cmd = enc_distance / time; |
jebradshaw | 0:cf7192f9f99a | 249 | this->vel_cmd = 1.5 * this->vel_avg_cmd * this->Tdelay; |
jebradshaw | 0:cf7192f9f99a | 250 | this->acc_cmd = 4.5 * (enc_distance / (this->moveTime * this->moveTime)) * this->Tdelay; |
jebradshaw | 0:cf7192f9f99a | 251 | |
jebradshaw | 0:cf7192f9f99a | 252 | //pc.printf("tx=%f encdist=%.3f vAvg=%.3f vMax=%.3f Acc=%.3f \r\n", this->moveTime, enc_distance,this->vel_avg_cmd,this->vel_cmd,this->acc_cmd); |
jebradshaw | 0:cf7192f9f99a | 253 | |
jebradshaw | 0:cf7192f9f99a | 254 | //establish encoder velocities and accelerations for position control per Tdelay |
jebradshaw | 0:cf7192f9f99a | 255 | this->vel_accum = 0.0; |
jebradshaw | 0:cf7192f9f99a | 256 | |
jebradshaw | 0:cf7192f9f99a | 257 | // this->set_point = this->pos; |
jebradshaw | 0:cf7192f9f99a | 258 | this->moveState = 1; |
jebradshaw | 0:cf7192f9f99a | 259 | this->t.reset(); |
jebradshaw | 0:cf7192f9f99a | 260 | this->t.start(); |
jebradshaw | 0:cf7192f9f99a | 261 | this->moveProfile.attach(this, &Axis::moveUpdate, this->Tdelay); |
jebradshaw | 0:cf7192f9f99a | 262 | } |
jebradshaw | 2:653433f4ee72 | 263 | |
jebradshaw | 4:4079f92f9c26 | 264 | float Axis::readCurrent(void){ |
jebradshaw | 4:4079f92f9c26 | 265 | motCurrent = this->_analog.read() * 3.3; |
jebradshaw | 4:4079f92f9c26 | 266 | return motCurrent; |
jebradshaw | 2:653433f4ee72 | 267 | } |
jebradshaw | 2:653433f4ee72 | 268 | |
jebradshaw | 2:653433f4ee72 | 269 | void Axis::axisOff(void){ |
jebradshaw | 4:4079f92f9c26 | 270 | this->co = 0; |
jebradshaw | 2:653433f4ee72 | 271 | this->axisState = 0; |
jebradshaw | 2:653433f4ee72 | 272 | } |
jebradshaw | 2:653433f4ee72 | 273 | |
jebradshaw | 2:653433f4ee72 | 274 | void Axis::axisOn(void){ |
jebradshaw | 4:4079f92f9c26 | 275 | this->co = 0.0; |
jebradshaw | 4:4079f92f9c26 | 276 | this->pid->reset(); |
jebradshaw | 4:4079f92f9c26 | 277 | //start at 0 |
jebradshaw | 4:4079f92f9c26 | 278 | this->set_point = 0.0; |
jebradshaw | 4:4079f92f9c26 | 279 | this->pid->setSetPoint(0); |
jebradshaw | 4:4079f92f9c26 | 280 | |
jebradshaw | 4:4079f92f9c26 | 281 | this->pid->setTunings(this->Pk, this->Ik, this->Dk); //turns on controller |
jebradshaw | 2:653433f4ee72 | 282 | this->axisState = 1; |
jebradshaw | 5:79dcaa63700c | 283 | } |
jebradshaw | 5:79dcaa63700c | 284 | |
jebradshaw | 5:79dcaa63700c | 285 | void Axis::zero(void){ |
jebradshaw | 5:79dcaa63700c | 286 | this->ls7366->LS7366_reset_counter(); |
jebradshaw | 5:79dcaa63700c | 287 | this->ls7366->LS7366_quad_mode_x4(); |
jebradshaw | 5:79dcaa63700c | 288 | this->ls7366->LS7366_write_DTR(0); |
jebradshaw | 5:79dcaa63700c | 289 | |
jebradshaw | 5:79dcaa63700c | 290 | this->set_point = 0.0; |
jebradshaw | 5:79dcaa63700c | 291 | this->pid->setSetPoint(0); |
jebradshaw | 2:653433f4ee72 | 292 | } |