C++ class for controlling DC motor with encoder feedback. Dependencies include LS7366LIB, MotCon, and PID.
Dependencies: LS7366LIB MotCon2 PID
Axis.cpp@1:cd249816dba8, 2015-09-24 (annotated)
- Committer:
- jebradshaw
- Date:
- Thu Sep 24 16:47:02 2015 +0000
- Revision:
- 1:cd249816dba8
- Parent:
- 0:cf7192f9f99a
- Child:
- 2:653433f4ee72
working 2 axis with I2C expander for limit switches
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 | 1:cd249816dba8 | 7 | Axis::Axis(SPI& spi, PinName cs, PinName pwm, PinName dir, int* limit): _spi(spi), _cs(cs), _pwm(pwm), _dir(dir) { |
jebradshaw | 0:cf7192f9f99a | 8 | _cs = 1; // Initialize chip select as off (high) |
jebradshaw | 0:cf7192f9f99a | 9 | _pwm = 0.0; |
jebradshaw | 0:cf7192f9f99a | 10 | _dir = 0; |
jebradshaw | 0:cf7192f9f99a | 11 | co = 0.0; |
jebradshaw | 0:cf7192f9f99a | 12 | Tdelay = .01; |
jebradshaw | 0:cf7192f9f99a | 13 | Pk = 120.0; //80.0; //rough gains, seem to work well but could use tuning |
jebradshaw | 0:cf7192f9f99a | 14 | Ik = 55.0; //35.0; |
jebradshaw | 0:cf7192f9f99a | 15 | Dk = 0.0; |
jebradshaw | 0:cf7192f9f99a | 16 | set_point = 0.0; |
jebradshaw | 0:cf7192f9f99a | 17 | set_point_last = 0.0; |
jebradshaw | 0:cf7192f9f99a | 18 | pos = 0.0; |
jebradshaw | 0:cf7192f9f99a | 19 | vel = 0.0; |
jebradshaw | 0:cf7192f9f99a | 20 | acc = 0.0; |
jebradshaw | 0:cf7192f9f99a | 21 | pos_cmd = 0.0; |
jebradshaw | 0:cf7192f9f99a | 22 | vel_cmd = 0.0; |
jebradshaw | 0:cf7192f9f99a | 23 | vel_avg_cmd = 0; |
jebradshaw | 0:cf7192f9f99a | 24 | acc_cmd = 0.0; |
jebradshaw | 0:cf7192f9f99a | 25 | vel_max = 2700.0 * Tdelay; //counts * Tdelay |
jebradshaw | 0:cf7192f9f99a | 26 | acc_max = 1200.0 * Tdelay; //counts/sec/sec * Tdelay |
jebradshaw | 0:cf7192f9f99a | 27 | p_higher = 0.0; |
jebradshaw | 0:cf7192f9f99a | 28 | p_lower = 0.0; |
jebradshaw | 0:cf7192f9f99a | 29 | vel_accum = 0.0; |
jebradshaw | 0:cf7192f9f99a | 30 | moveTime = 0.0; |
jebradshaw | 0:cf7192f9f99a | 31 | countsPerDeg = 0.0; |
jebradshaw | 0:cf7192f9f99a | 32 | enc = 0; |
jebradshaw | 0:cf7192f9f99a | 33 | moveStatus = 0; //status flag to indicate state of profile movement |
jebradshaw | 0:cf7192f9f99a | 34 | moveState = 0; //used for state machine in movement profiles |
jebradshaw | 0:cf7192f9f99a | 35 | debug = 0; |
jebradshaw | 0:cf7192f9f99a | 36 | update.attach(this, &Axis::paramUpdate, Tdelay); |
jebradshaw | 0:cf7192f9f99a | 37 | |
jebradshaw | 0:cf7192f9f99a | 38 | pid = new PID(0.0,0.0,0.0,Tdelay); //Kc, Ti, Td, interval |
jebradshaw | 0:cf7192f9f99a | 39 | ls7366 = new LS7366(spi, cs); //LS7366 encoder interface IC |
jebradshaw | 0:cf7192f9f99a | 40 | motcon = new MotCon(pwm, dir); |
jebradshaw | 1:cd249816dba8 | 41 | ptr_limit = limit; |
jebradshaw | 0:cf7192f9f99a | 42 | |
jebradshaw | 0:cf7192f9f99a | 43 | //start at 0 |
jebradshaw | 0:cf7192f9f99a | 44 | this->ls7366->LS7366_reset_counter(); |
jebradshaw | 0:cf7192f9f99a | 45 | this->ls7366->LS7366_quad_mode_x4(); |
jebradshaw | 0:cf7192f9f99a | 46 | this->ls7366->LS7366_write_DTR(0); |
jebradshaw | 0:cf7192f9f99a | 47 | |
jebradshaw | 0:cf7192f9f99a | 48 | this->set_point = 0.0; |
jebradshaw | 0:cf7192f9f99a | 49 | this->pid->setSetPoint(this->set_point); |
jebradshaw | 0:cf7192f9f99a | 50 | this->enc = this->ls7366->LS7366_read_counter(); //update class variable |
jebradshaw | 0:cf7192f9f99a | 51 | } |
jebradshaw | 0:cf7192f9f99a | 52 | |
jebradshaw | 0:cf7192f9f99a | 53 | void Axis::init(float cpd){ |
jebradshaw | 1:cd249816dba8 | 54 | //_limit.mode(PullUp); |
jebradshaw | 0:cf7192f9f99a | 55 | this->countsPerDeg = cpd; //90.0/10680.0; |
jebradshaw | 0:cf7192f9f99a | 56 | //resets the controllers internals |
jebradshaw | 0:cf7192f9f99a | 57 | this->pid->reset(); |
jebradshaw | 0:cf7192f9f99a | 58 | |
jebradshaw | 0:cf7192f9f99a | 59 | //Encoder counts limit |
jebradshaw | 0:cf7192f9f99a | 60 | this->pid->setInputLimits(-20000.0, 20000.0); |
jebradshaw | 0:cf7192f9f99a | 61 | //Pwm output from 0.0 to 1.0 |
jebradshaw | 0:cf7192f9f99a | 62 | this->pid->setOutputLimits(-1.0, 1.0); |
jebradshaw | 0:cf7192f9f99a | 63 | //If there's a bias. |
jebradshaw | 0:cf7192f9f99a | 64 | this->pid->setBias(0.0); |
jebradshaw | 0:cf7192f9f99a | 65 | this->pid->setMode(AUTO_MODE); |
jebradshaw | 0:cf7192f9f99a | 66 | |
jebradshaw | 0:cf7192f9f99a | 67 | this->pid->setInterval(this->Tdelay); |
jebradshaw | 0:cf7192f9f99a | 68 | |
jebradshaw | 0:cf7192f9f99a | 69 | //start at 0 |
jebradshaw | 0:cf7192f9f99a | 70 | this->ls7366->LS7366_reset_counter(); |
jebradshaw | 0:cf7192f9f99a | 71 | this->ls7366->LS7366_quad_mode_x4(); |
jebradshaw | 0:cf7192f9f99a | 72 | this->ls7366->LS7366_write_DTR(0); |
jebradshaw | 0:cf7192f9f99a | 73 | |
jebradshaw | 0:cf7192f9f99a | 74 | this->set_point = 0.0; |
jebradshaw | 0:cf7192f9f99a | 75 | this->pid->setSetPoint(this->set_point); |
jebradshaw | 0:cf7192f9f99a | 76 | this->enc = this->ls7366->LS7366_read_counter(); //update class variable |
jebradshaw | 0:cf7192f9f99a | 77 | |
jebradshaw | 0:cf7192f9f99a | 78 | //resets the controllers internals |
jebradshaw | 0:cf7192f9f99a | 79 | this->pid->reset(); |
jebradshaw | 0:cf7192f9f99a | 80 | //start at 0 |
jebradshaw | 0:cf7192f9f99a | 81 | this->set_point = 0.0; |
jebradshaw | 0:cf7192f9f99a | 82 | this->pid->setSetPoint(0); |
jebradshaw | 0:cf7192f9f99a | 83 | |
jebradshaw | 0:cf7192f9f99a | 84 | this->pid->setTunings(this->Pk, this->Ik, this->Dk); //turns on controller |
jebradshaw | 0:cf7192f9f99a | 85 | } |
jebradshaw | 0:cf7192f9f99a | 86 | |
jebradshaw | 0:cf7192f9f99a | 87 | void Axis::paramUpdate(void){ |
jebradshaw | 0:cf7192f9f99a | 88 | //testOut = 1; |
jebradshaw | 0:cf7192f9f99a | 89 | this->enc = this->ls7366->LS7366_read_counter(); |
jebradshaw | 0:cf7192f9f99a | 90 | this->pos = (float)this->enc; // * this->countsPerDeg * PI/180.0; //times counts/degree and convert to radians |
jebradshaw | 0:cf7192f9f99a | 91 | |
jebradshaw | 0:cf7192f9f99a | 92 | this->vel = (this->pos - this->pos_last) * this->Tdelay; |
jebradshaw | 0:cf7192f9f99a | 93 | this->acc = (this->vel - this->vel_last); |
jebradshaw | 0:cf7192f9f99a | 94 | |
jebradshaw | 0:cf7192f9f99a | 95 | this->pid->setSetPoint(this->set_point); |
jebradshaw | 0:cf7192f9f99a | 96 | |
jebradshaw | 0:cf7192f9f99a | 97 | //Update the process variable. |
jebradshaw | 0:cf7192f9f99a | 98 | this->pid->setProcessValue(this->pos); |
jebradshaw | 0:cf7192f9f99a | 99 | //Set the new output. |
jebradshaw | 0:cf7192f9f99a | 100 | this->co = this->pid->compute(); |
jebradshaw | 0:cf7192f9f99a | 101 | |
jebradshaw | 0:cf7192f9f99a | 102 | this->motcon->mot_control(this->co); //send controller output to PWM motor control command |
jebradshaw | 0:cf7192f9f99a | 103 | |
jebradshaw | 0:cf7192f9f99a | 104 | this->pos_last = this->pos; |
jebradshaw | 0:cf7192f9f99a | 105 | this->vel_last = this->vel; |
jebradshaw | 0:cf7192f9f99a | 106 | this->set_point_last = this->set_point; |
jebradshaw | 0:cf7192f9f99a | 107 | //testOut = 0; |
jebradshaw | 0:cf7192f9f99a | 108 | } |
jebradshaw | 0:cf7192f9f99a | 109 | |
jebradshaw | 0:cf7192f9f99a | 110 | void Axis::home(long halfcounts){ |
jebradshaw | 0:cf7192f9f99a | 111 | this->pid->setInputLimits(-20000.0, 20000.0); |
jebradshaw | 1:cd249816dba8 | 112 | while(*this->ptr_limit == 1){ |
jebradshaw | 0:cf7192f9f99a | 113 | this->set_point += 100; |
jebradshaw | 0:cf7192f9f99a | 114 | this->pid->setSetPoint(this->set_point); |
jebradshaw | 0:cf7192f9f99a | 115 | wait(.05); |
jebradshaw | 0:cf7192f9f99a | 116 | if(this->debug) |
jebradshaw | 1:cd249816dba8 | 117 | printf("T=%.2f SP=%.3f co=%.3f pos=%.3f vel=%.3f acc=%.3f limit=%d\r\n", t.read(), this->set_point, this->co, this->pos, this->vel, this->acc,*this->ptr_limit); |
jebradshaw | 0:cf7192f9f99a | 118 | } |
jebradshaw | 0:cf7192f9f99a | 119 | this->set_point -= 1000; |
jebradshaw | 0:cf7192f9f99a | 120 | this->pid->setSetPoint(this->set_point); |
jebradshaw | 0:cf7192f9f99a | 121 | wait(.5); |
jebradshaw | 1:cd249816dba8 | 122 | |
jebradshaw | 1:cd249816dba8 | 123 | while(*this->ptr_limit == 1){ |
jebradshaw | 0:cf7192f9f99a | 124 | this->set_point += 10; |
jebradshaw | 0:cf7192f9f99a | 125 | this->pid->setSetPoint(this->set_point); |
jebradshaw | 0:cf7192f9f99a | 126 | wait(.02); |
jebradshaw | 0:cf7192f9f99a | 127 | if(this->debug) |
jebradshaw | 1:cd249816dba8 | 128 | printf("T=%.2f SP=%.3f co=%.3f pos=%.3f vel=%.3f acc=%.3f limit=%d\r\n", t.read(), this->set_point, this->co, this->pos, this->vel, this->acc,*this->ptr_limit); |
jebradshaw | 0:cf7192f9f99a | 129 | } |
jebradshaw | 0:cf7192f9f99a | 130 | |
jebradshaw | 0:cf7192f9f99a | 131 | this->ls7366->LS7366_write_DTR(0); //zero encoder channel |
jebradshaw | 0:cf7192f9f99a | 132 | this->set_point = 0.0; |
jebradshaw | 0:cf7192f9f99a | 133 | this->pid->setSetPoint(this->set_point); |
jebradshaw | 0:cf7192f9f99a | 134 | |
jebradshaw | 0:cf7192f9f99a | 135 | this->pid->setInputLimits(-17000.0, 0.0); //reset span limits |
jebradshaw | 0:cf7192f9f99a | 136 | for(int positionCmd = 0;positionCmd > -halfcounts;positionCmd-=30){ |
jebradshaw | 0:cf7192f9f99a | 137 | this->set_point = positionCmd; //move arm to center |
jebradshaw | 0:cf7192f9f99a | 138 | wait(.01); |
jebradshaw | 0:cf7192f9f99a | 139 | if(this->debug) |
jebradshaw | 1:cd249816dba8 | 140 | printf("T=%.2f SP=%.3f co=%.3f pos=%.3f vel=%.3f acc=%.3f limit=%d\r\n", t.read(), this->set_point, this->co, this->pos, this->vel, this->acc,*this->ptr_limit); |
jebradshaw | 0:cf7192f9f99a | 141 | } |
jebradshaw | 0:cf7192f9f99a | 142 | this->set_point = -halfcounts; |
jebradshaw | 0:cf7192f9f99a | 143 | this->pid->setSetPoint(this->set_point); |
jebradshaw | 0:cf7192f9f99a | 144 | |
jebradshaw | 0:cf7192f9f99a | 145 | //let PID settle to set point |
jebradshaw | 0:cf7192f9f99a | 146 | while((this->enc > (this->set_point + 100)) || (this->enc < (this->set_point - 100))){ |
jebradshaw | 0:cf7192f9f99a | 147 | //pc.printf("T=%.2f SP=%.3f co=%.3f enc=%d pos=%.3f Pk=%.2f Ik=%.2f vel=%.3f acc=%.3f\r\n", t.read(), this->set_point, this->co, this->enc, this->pos, this->Pk, this->Ik, this->vel, this->acc); |
jebradshaw | 0:cf7192f9f99a | 148 | wait(.01); |
jebradshaw | 0:cf7192f9f99a | 149 | } |
jebradshaw | 0:cf7192f9f99a | 150 | //pc.printf("T=%.2f SP=%.3f co=%.3f enc=%d pos=%.3f Pk=%.2f Ik=%.2f vel=%.3f acc=%.3f\r\n", t.read(), this->set_point, this->co, this->enc, this->pos, this->Pk, this->Ik, this->vel, this->acc); |
jebradshaw | 0:cf7192f9f99a | 151 | |
jebradshaw | 0:cf7192f9f99a | 152 | this->ls7366->LS7366_write_DTR(0); //zero encoder channel |
jebradshaw | 0:cf7192f9f99a | 153 | this->set_point = 0.0; |
jebradshaw | 0:cf7192f9f99a | 154 | this->pid->setSetPoint(this->set_point); |
jebradshaw | 0:cf7192f9f99a | 155 | |
jebradshaw | 0:cf7192f9f99a | 156 | this->pid->setInputLimits(-6500.0, 6500.0); //reset span limits |
jebradshaw | 0:cf7192f9f99a | 157 | this->pid->setSetPoint(this->set_point); |
jebradshaw | 0:cf7192f9f99a | 158 | |
jebradshaw | 0:cf7192f9f99a | 159 | // pc.printf("Home:T=%.2f SP=%.3f co=%.3f enc=%d pos=%.3f Pk=%.2f Ik=%.2f vel=%.3f acc=%.3f\r\n", t.read(), this->set_point, this->co, this->enc, this->pos, this->Pk, this->Ik, this->vel, this->acc); |
jebradshaw | 0:cf7192f9f99a | 160 | // pc.printf("End Home\r\n\r\n"); |
jebradshaw | 0:cf7192f9f99a | 161 | } |
jebradshaw | 0:cf7192f9f99a | 162 | |
jebradshaw | 0:cf7192f9f99a | 163 | void Axis::moveUpdate(void){ |
jebradshaw | 0:cf7192f9f99a | 164 | switch(this->moveState){ |
jebradshaw | 0:cf7192f9f99a | 165 | case 0: |
jebradshaw | 0:cf7192f9f99a | 166 | break; |
jebradshaw | 0:cf7192f9f99a | 167 | |
jebradshaw | 0:cf7192f9f99a | 168 | //accelerate |
jebradshaw | 0:cf7192f9f99a | 169 | case 1: |
jebradshaw | 0:cf7192f9f99a | 170 | //testOut = 1; |
jebradshaw | 0:cf7192f9f99a | 171 | this->vel_accum += this->acc_cmd * this->Tdelay; //add acceleration to the velocity accumulator |
jebradshaw | 0:cf7192f9f99a | 172 | if(this->vel_avg_cmd > 0.0){ //check the sign of the movement |
jebradshaw | 0:cf7192f9f99a | 173 | if(this->vel_accum >= this->vel_cmd) //if the accumulator reaches or exceeds the velocity command |
jebradshaw | 0:cf7192f9f99a | 174 | this->vel_accum = this->vel_cmd; // only add the velocity command to the accumulator |
jebradshaw | 0:cf7192f9f99a | 175 | } |
jebradshaw | 0:cf7192f9f99a | 176 | else{ //if the sign was negative |
jebradshaw | 0:cf7192f9f99a | 177 | if(this->vel_accum <= this->vel_cmd) |
jebradshaw | 0:cf7192f9f99a | 178 | this->vel_accum = this->vel_cmd; |
jebradshaw | 0:cf7192f9f99a | 179 | } |
jebradshaw | 0:cf7192f9f99a | 180 | //testOut = 0; |
jebradshaw | 0:cf7192f9f99a | 181 | |
jebradshaw | 0:cf7192f9f99a | 182 | this->set_point += this->vel_accum; |
jebradshaw | 0:cf7192f9f99a | 183 | //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 | 184 | //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 | 185 | |
jebradshaw | 0:cf7192f9f99a | 186 | if(this->t.read()>=(this->moveTime/3.0) || (abs(this->vel_accum) > abs(this->vel_cmd))) |
jebradshaw | 0:cf7192f9f99a | 187 | this->moveState = 2; |
jebradshaw | 0:cf7192f9f99a | 188 | break; |
jebradshaw | 0:cf7192f9f99a | 189 | |
jebradshaw | 0:cf7192f9f99a | 190 | //constant velocity |
jebradshaw | 0:cf7192f9f99a | 191 | case 2: |
jebradshaw | 0:cf7192f9f99a | 192 | //testOut = 1; |
jebradshaw | 0:cf7192f9f99a | 193 | //this->vel_accum += this->vel_cmd * this->Tdelay; |
jebradshaw | 0:cf7192f9f99a | 194 | this->set_point += this->vel_cmd; |
jebradshaw | 0:cf7192f9f99a | 195 | //testOut = 0; |
jebradshaw | 0:cf7192f9f99a | 196 | //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 | 197 | //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 | 198 | |
jebradshaw | 0:cf7192f9f99a | 199 | if(this->t.read()>=(2.0/3.0 * this->moveTime)) |
jebradshaw | 0:cf7192f9f99a | 200 | this->moveState = 3; |
jebradshaw | 0:cf7192f9f99a | 201 | break; |
jebradshaw | 0:cf7192f9f99a | 202 | |
jebradshaw | 0:cf7192f9f99a | 203 | //decelerate |
jebradshaw | 0:cf7192f9f99a | 204 | case 3: |
jebradshaw | 0:cf7192f9f99a | 205 | this->vel_accum -= this->acc_cmd * this->Tdelay; |
jebradshaw | 0:cf7192f9f99a | 206 | |
jebradshaw | 0:cf7192f9f99a | 207 | this->set_point += this->vel_accum; //ramp down velocity by acceleration |
jebradshaw | 0:cf7192f9f99a | 208 | //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 | 209 | //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 | 210 | |
jebradshaw | 0:cf7192f9f99a | 211 | if(this->vel_avg_cmd > 0.0){ |
jebradshaw | 1:cd249816dba8 | 212 | if(this->pos_cmd <= this->pos){ |
jebradshaw | 1:cd249816dba8 | 213 | //finish with position command |
jebradshaw | 1:cd249816dba8 | 214 | this->set_point = this->pos_cmd; |
jebradshaw | 0:cf7192f9f99a | 215 | this->moveState = 4; |
jebradshaw | 1:cd249816dba8 | 216 | } |
jebradshaw | 0:cf7192f9f99a | 217 | } |
jebradshaw | 0:cf7192f9f99a | 218 | else{ |
jebradshaw | 1:cd249816dba8 | 219 | if(this->pos_cmd >= this->pos){ |
jebradshaw | 1:cd249816dba8 | 220 | //finish with position command |
jebradshaw | 1:cd249816dba8 | 221 | this->set_point = this->pos_cmd; |
jebradshaw | 1:cd249816dba8 | 222 | this->moveState = 4; |
jebradshaw | 1:cd249816dba8 | 223 | } |
jebradshaw | 0:cf7192f9f99a | 224 | } |
jebradshaw | 0:cf7192f9f99a | 225 | |
jebradshaw | 0:cf7192f9f99a | 226 | if(this->t.read()>=this->moveTime){ |
jebradshaw | 1:cd249816dba8 | 227 | //finish with position command |
jebradshaw | 1:cd249816dba8 | 228 | this->set_point = this->pos_cmd; |
jebradshaw | 0:cf7192f9f99a | 229 | this->moveState = 4; |
jebradshaw | 0:cf7192f9f99a | 230 | } |
jebradshaw | 0:cf7192f9f99a | 231 | break; |
jebradshaw | 0:cf7192f9f99a | 232 | |
jebradshaw | 0:cf7192f9f99a | 233 | case 4: |
jebradshaw | 0:cf7192f9f99a | 234 | this->moveProfile.detach(); //turn off the trapazoidal update ticker |
jebradshaw | 0:cf7192f9f99a | 235 | this->t.stop(); |
jebradshaw | 0:cf7192f9f99a | 236 | this->moveState = 0; |
jebradshaw | 0:cf7192f9f99a | 237 | break; |
jebradshaw | 0:cf7192f9f99a | 238 | }//switch moveStatus |
jebradshaw | 0:cf7192f9f99a | 239 | return; |
jebradshaw | 0:cf7192f9f99a | 240 | } |
jebradshaw | 0:cf7192f9f99a | 241 | |
jebradshaw | 0:cf7192f9f99a | 242 | // position - encoder position to move to |
jebradshaw | 0:cf7192f9f99a | 243 | // time - duration of the movement |
jebradshaw | 0:cf7192f9f99a | 244 | void Axis::moveTrapezoid(float positionCmd, float time){ |
jebradshaw | 0:cf7192f9f99a | 245 | this->pos_cmd = positionCmd; |
jebradshaw | 0:cf7192f9f99a | 246 | this->moveTime = time; |
jebradshaw | 0:cf7192f9f99a | 247 | float enc_distance = pos_cmd - (float)this->enc;// * 1.0/con0.countsPerDeg * 180.0/PI; |
jebradshaw | 0:cf7192f9f99a | 248 | |
jebradshaw | 0:cf7192f9f99a | 249 | this->vel_avg_cmd = enc_distance / time; |
jebradshaw | 0:cf7192f9f99a | 250 | this->vel_cmd = 1.5 * this->vel_avg_cmd * this->Tdelay; |
jebradshaw | 0:cf7192f9f99a | 251 | this->acc_cmd = 4.5 * (enc_distance / (this->moveTime * this->moveTime)) * this->Tdelay; |
jebradshaw | 0:cf7192f9f99a | 252 | |
jebradshaw | 0:cf7192f9f99a | 253 | //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 | 254 | |
jebradshaw | 0:cf7192f9f99a | 255 | //establish encoder velocities and accelerations for position control per Tdelay |
jebradshaw | 0:cf7192f9f99a | 256 | this->vel_accum = 0.0; |
jebradshaw | 0:cf7192f9f99a | 257 | |
jebradshaw | 0:cf7192f9f99a | 258 | // this->set_point = this->pos; |
jebradshaw | 0:cf7192f9f99a | 259 | this->moveState = 1; |
jebradshaw | 0:cf7192f9f99a | 260 | this->t.reset(); |
jebradshaw | 0:cf7192f9f99a | 261 | this->t.start(); |
jebradshaw | 0:cf7192f9f99a | 262 | this->moveProfile.attach(this, &Axis::moveUpdate, this->Tdelay); |
jebradshaw | 0:cf7192f9f99a | 263 | } |