Slurp

Dependencies:   FastPWM3 mbed

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