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