Ikhee Jo / T-Motor_AK80_BaseCode

Dependencies:   mbed-dev-f303 FastPWM3

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PositionSensor.cpp Source File

PositionSensor.cpp

00001 
00002 #include "mbed.h"
00003 #include "PositionSensor.h"
00004 #include "../math_ops.h"
00005 //#include "offset_lut.h"
00006 //#include <math.h>
00007 
00008 PositionSensorAM5147::PositionSensorAM5147(int CPR, float offset, int ppairs){
00009     //_CPR = CPR;
00010     _CPR = CPR;
00011     _ppairs = ppairs;
00012     ElecOffset = offset;
00013     rotations = 0;
00014     spi = new SPI(PC_12, PC_11, PC_10);
00015     spi->format(16, 1);                                                          // mbed v>127 breaks 16-bit spi, so transaction is broken into 2 8-bit words
00016     spi->frequency(25000000);
00017     
00018     cs = new DigitalOut(PA_15);
00019     cs->write(1);
00020     readAngleCmd = 0xffff;   
00021     MechOffset = offset;
00022     modPosition = 0;
00023     oldModPosition = 0;
00024     oldVel = 0;
00025     raw = 0;
00026     first_sample = 0;
00027     for(int i = 0; i<100; i++)              // Initial measurement is really noisy
00028     {
00029         spi->write(0);
00030         wait_us(100);
00031     }
00032     
00033     GPIOA->ODR &= ~(1 << 15);
00034     spi->write(0x8980);
00035     wait_ms(20);
00036     
00037 
00038     }
00039     
00040 void PositionSensorAM5147::Sample(float dt){
00041     GPIOA->ODR &= ~(1 << 15);
00042     //raw = spi->write(readAngleCmd);
00043     //raw &= 0x3FFF;   
00044     raw = spi->write(0);
00045     raw = raw>>2;                                                             //Extract last 14 bits
00046     GPIOA->ODR |= (1 << 15);
00047     int off_1 = offset_lut[raw>>7];
00048     int off_2 = offset_lut[((raw>>7)+1)%128];
00049     int off_interp = off_1 + ((off_2 - off_1)*(raw - ((raw>>7)<<7))>>7);        // Interpolate between lookup table entries
00050     int angle = raw + off_interp;                                               // Correct for nonlinearity with lookup table from calibration
00051     
00052     if(first_sample){
00053         if(angle - old_counts > _CPR/2){
00054             rotations -= 1;
00055             }
00056         else if (angle - old_counts < -_CPR/2){
00057             rotations += 1;
00058             }
00059     }
00060     if(!first_sample){first_sample = 1;}
00061     
00062     old_counts = angle;
00063     oldModPosition = modPosition;
00064     modPosition = ((2.0f*PI * ((float) angle))/ (float)_CPR);
00065     position = (2.0f*PI * ((float) angle+(_CPR*rotations)))/ (float)_CPR;
00066     MechPosition = position - MechOffset;
00067     float elec = ((2.0f*PI/(float)_CPR) * (float) ((_ppairs*angle)%_CPR)) + ElecOffset;
00068     if(elec < 0) elec += 2.0f*PI;
00069     else if(elec > 2.0f*PI) elec -= 2.0f*PI ; 
00070     ElecPosition = elec;
00071     
00072     float vel;
00073     //if(modPosition<.1f && oldModPosition>6.1f){
00074 
00075     if((modPosition-oldModPosition) < -3.0f){
00076         vel = (modPosition - oldModPosition + 2.0f*PI)/dt;
00077         }
00078     //else if(modPosition>6.1f && oldModPosition<0.1f){
00079     else if((modPosition - oldModPosition) > 3.0f){
00080         vel = (modPosition - oldModPosition - 2.0f*PI)/dt;
00081         }
00082     else{
00083         vel = (modPosition-oldModPosition)/dt;
00084     }    
00085     
00086     int n = 40;
00087     float sum = vel;
00088     for (int i = 1; i < (n); i++){
00089         velVec[n - i] = velVec[n-i-1];
00090         sum += velVec[n-i];
00091         }
00092     velVec[0] = vel;
00093     MechVelocity =  sum/((float)n);
00094     ElecVelocity = MechVelocity*_ppairs;
00095     ElecVelocityFilt = 0.99f*ElecVelocityFilt + 0.01f*ElecVelocity;
00096     }
00097 
00098 int PositionSensorAM5147::GetRawPosition(){
00099     return raw;
00100     }
00101 
00102 float PositionSensorAM5147::GetMechPositionFixed(){
00103     return MechPosition+MechOffset;
00104     }
00105     
00106 float PositionSensorAM5147::GetMechPosition(){
00107     return MechPosition;
00108     }
00109 
00110 float PositionSensorAM5147::GetElecPosition(){
00111     return ElecPosition;
00112     }
00113 
00114 float PositionSensorAM5147::GetElecVelocity(){
00115     return ElecVelocity;
00116     }
00117 
00118 float PositionSensorAM5147::GetMechVelocity(){
00119     return MechVelocity;
00120     }
00121 
00122 void PositionSensorAM5147::ZeroPosition(){
00123     rotations = 0;
00124     MechOffset = 0;
00125     Sample(.00025f);
00126     MechOffset = GetMechPosition();
00127     }
00128     
00129 void PositionSensorAM5147::SetElecOffset(float offset){
00130     ElecOffset = offset;
00131     }
00132 void PositionSensorAM5147::SetMechOffset(float offset){
00133     MechOffset = offset;
00134     first_sample = 0;
00135     }
00136 
00137 int PositionSensorAM5147::GetCPR(){
00138     return _CPR;
00139     }
00140 
00141 
00142 void PositionSensorAM5147::WriteLUT(int new_lut[128]){
00143     memcpy(offset_lut, new_lut, sizeof(offset_lut));
00144     }
00145     
00146 
00147 
00148 PositionSensorEncoder::PositionSensorEncoder(int CPR, float offset, int ppairs) {
00149     _ppairs = ppairs;
00150     _CPR = CPR;
00151     _offset = offset;
00152     MechPosition = 0;
00153     out_old = 0;
00154     oldVel = 0;
00155     raw = 0;
00156     
00157     // Enable clock for GPIOA
00158     __GPIOA_CLK_ENABLE(); //equivalent from hal_rcc.h
00159  
00160     GPIOA->MODER   |= GPIO_MODER_MODER6_1 | GPIO_MODER_MODER7_1 ;           //PA6 & PA7 as Alternate Function   /*!< GPIO port mode register,               Address offset: 0x00      */
00161     GPIOA->OTYPER  |= GPIO_OTYPER_OT_6 | GPIO_OTYPER_OT_7 ;                 //PA6 & PA7 as Inputs               /*!< GPIO port output type register,        Address offset: 0x04      */
00162     GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR6 | GPIO_OSPEEDER_OSPEEDR7 ;     //Low speed                         /*!< GPIO port output speed register,       Address offset: 0x08      */
00163     GPIOA->PUPDR   |= GPIO_PUPDR_PUPDR6_1 | GPIO_PUPDR_PUPDR7_1 ;           //Pull Down                         /*!< GPIO port pull-up/pull-down register,  Address offset: 0x0C      */
00164     GPIOA->AFR[0]  |= 0x22000000 ;                                          //AF02 for PA6 & PA7                /*!< GPIO alternate function registers,     Address offset: 0x20-0x24 */
00165     GPIOA->AFR[1]  |= 0x00000000 ;                                          //nibbles here refer to gpio8..15   /*!< GPIO alternate function registers,     Address offset: 0x20-0x24 */
00166    
00167     // configure TIM3 as Encoder input
00168     // Enable clock for TIM3
00169     __TIM3_CLK_ENABLE();
00170  
00171     TIM3->CR1   = 0x0001;                                                   // CEN(Counter ENable)='1'     < TIM control register 1
00172     TIM3->SMCR  = TIM_ENCODERMODE_TI12;                                     // SMS='011' (Encoder mode 3)  < TIM slave mode control register
00173     TIM3->CCMR1 = 0x1111;                                                   // CC1S='01' CC2S='01'         < TIM capture/compare mode register 1, maximum digital filtering
00174     TIM3->CCMR2 = 0x0000;                                                   //                             < TIM capture/compare mode register 2
00175     TIM3->CCER  = 0x0011;                                                   // CC1P CC2P                   < TIM capture/compare enable register
00176     TIM3->PSC   = 0x0000;                                                   // Prescaler = (0+1)           < TIM prescaler
00177     TIM3->ARR   = CPR;                                                      // IM auto-reload register
00178   
00179     TIM3->CNT = 0x000;  //reset the counter before we use it  
00180     
00181     // Extra Timer for velocity measurement
00182     
00183     __TIM2_CLK_ENABLE();
00184     TIM3->CR2 = 0x030;                                                      //MMS = 101
00185     
00186     TIM2->PSC = 0x03;
00187     //TIM2->CR2 |= TIM_CR2_TI1S;
00188     TIM2->SMCR = 0x24;                                                      //TS = 010 for ITR2, SMS = 100 (reset counter at edge)
00189     TIM2->CCMR1 = 0x3;                                                      // CC1S = 11, IC1 mapped on TRC
00190     
00191     //TIM2->CR2 |= TIM_CR2_TI1S;
00192     TIM2->CCER |= TIM_CCER_CC1P;
00193     //TIM2->CCER |= TIM_CCER_CC1NP;
00194     TIM2->CCER |= TIM_CCER_CC1E;
00195     
00196     
00197     TIM2->CR1 = 0x01;                                                       //CEN,  enable timer
00198     
00199     TIM3->CR1   = 0x01;                                                     // CEN
00200     ZPulse = new InterruptIn(PC_4);
00201     ZSense = new DigitalIn(PC_4);
00202     //ZPulse = new InterruptIn(PB_0);
00203     //ZSense = new DigitalIn(PB_0);
00204     ZPulse->enable_irq();
00205     ZPulse->rise(this, &PositionSensorEncoder::ZeroEncoderCount);
00206     //ZPulse->fall(this, &PositionSensorEncoder::ZeroEncoderCountDown);
00207     ZPulse->mode(PullDown);
00208     flag = 0;
00209 
00210     
00211     //ZTest = new DigitalOut(PC_2);
00212     //ZTest->write(1);
00213     }
00214     
00215 void PositionSensorEncoder::Sample(float dt){
00216     
00217     }
00218 
00219  
00220 float PositionSensorEncoder::GetMechPosition() {                            //returns rotor angle in radians.
00221     int raw = TIM3->CNT;
00222     float unsigned_mech = (6.28318530718f/(float)_CPR) * (float) ((raw)%_CPR);
00223     return (float) unsigned_mech;// + 6.28318530718f* (float) rotations;
00224 }
00225 
00226 float PositionSensorEncoder::GetElecPosition() {                            //returns rotor electrical angle in radians.
00227     int raw = TIM3->CNT;
00228     float elec = ((6.28318530718f/(float)_CPR) * (float) ((_ppairs*raw)%_CPR)) - _offset;
00229     if(elec < 0) elec += 6.28318530718f;
00230     return elec;
00231 }
00232 
00233 
00234     
00235 float PositionSensorEncoder::GetMechVelocity(){
00236 
00237     float out = 0;
00238     float rawPeriod = TIM2->CCR1; //Clock Ticks
00239     int currentTime = TIM2->CNT;
00240     if(currentTime > 2000000){rawPeriod = currentTime;}
00241     float  dir = -2.0f*(float)(((TIM3->CR1)>>4)&1)+1.0f;    // +/- 1
00242     float meas = dir*180000000.0f*(6.28318530718f/(float)_CPR)/rawPeriod; 
00243     if(isinf(meas)){ meas = 1;}
00244     out = meas;
00245     //if(meas == oldVel){
00246      //   out = .9f*out_old;
00247      //   }
00248     
00249  
00250     oldVel = meas;
00251     out_old = out;
00252     int n = 16;
00253     float sum = out;
00254     for (int i = 1; i < (n); i++){
00255         velVec[n - i] = velVec[n-i-1];
00256         sum += velVec[n-i];
00257         }
00258     velVec[0] = out;
00259     return sum/(float)n;
00260     }
00261     
00262 float PositionSensorEncoder::GetElecVelocity(){
00263     return _ppairs*GetMechVelocity();
00264     }
00265     
00266 void PositionSensorEncoder::ZeroEncoderCount(void){
00267     if (ZSense->read() == 1 & flag == 0){
00268         if (ZSense->read() == 1){
00269             GPIOC->ODR ^= (1 << 4);   
00270             TIM3->CNT = 0x000;
00271             //state = !state;
00272             //ZTest->write(state);
00273             GPIOC->ODR ^= (1 << 4);
00274             //flag = 1;
00275         }
00276         }
00277     }
00278 
00279 void PositionSensorEncoder::ZeroPosition(void){
00280     
00281     }
00282     
00283 void PositionSensorEncoder::ZeroEncoderCountDown(void){
00284     if (ZSense->read() == 0){
00285         if (ZSense->read() == 0){
00286             GPIOC->ODR ^= (1 << 4);
00287             flag = 0;
00288             float dir = -2.0f*(float)(((TIM3->CR1)>>4)&1)+1.0f;
00289             if(dir != dir){
00290                 dir = dir;
00291                 rotations +=  dir;
00292                 }
00293 
00294             GPIOC->ODR ^= (1 << 4);
00295 
00296         }
00297         }
00298     }
00299 void PositionSensorEncoder::SetElecOffset(float offset){
00300     
00301     }
00302     
00303 int PositionSensorEncoder::GetRawPosition(void){
00304     return 0;
00305     }
00306     
00307 int PositionSensorEncoder::GetCPR(){
00308     return _CPR;
00309     }
00310     
00311 
00312 void PositionSensorEncoder::WriteLUT(int new_lut[128]){
00313     memcpy(offset_lut, new_lut, sizeof(offset_lut));
00314     }