auto-measurements

Dependencies:   FastPWM3 mbed-dev

Fork of Hobbyking_Cheetah_Compact by Ben Katz

Committer:
benkatz
Date:
Sun Jun 04 19:00:22 2017 +0000
Revision:
28:8c7e29f719c5
Parent:
26:2b865c00d7e9
Use external oscillator

Who changed what in which revision?

UserRevisionLine numberNew contents of line
benkatz 0:4e1c4df6aabd 1
benkatz 0:4e1c4df6aabd 2 #include "mbed.h"
benkatz 0:4e1c4df6aabd 3 #include "PositionSensor.h"
benkatz 22:60276ba87ac6 4 //#include "offset_lut.h"
benkatz 6:4ee1cdc43aa8 5 //#include <math.h>
benkatz 0:4e1c4df6aabd 6
benkatz 14:80ce59119d93 7 PositionSensorAM5147::PositionSensorAM5147(int CPR, float offset, int ppairs){
benkatz 14:80ce59119d93 8 //_CPR = CPR;
benkatz 14:80ce59119d93 9 _CPR = CPR;
benkatz 14:80ce59119d93 10 _ppairs = ppairs;
benkatz 22:60276ba87ac6 11 ElecOffset = offset;
benkatz 14:80ce59119d93 12 rotations = 0;
benkatz 14:80ce59119d93 13 spi = new SPI(PC_12, PC_11, PC_10);
benkatz 26:2b865c00d7e9 14 spi->format(8, 1); // mbed v>127 breaks 16-bit spi, so transaction is broken into 2 8-bit words
benkatz 26:2b865c00d7e9 15 spi->frequency(25000000);
benkatz 14:80ce59119d93 16 cs = new DigitalOut(PA_15);
benkatz 14:80ce59119d93 17 cs->write(1);
benkatz 22:60276ba87ac6 18 readAngleCmd = 0xffff;
benkatz 14:80ce59119d93 19 MechOffset = 0;
benkatz 22:60276ba87ac6 20 modPosition = 0;
benkatz 22:60276ba87ac6 21 oldModPosition = 0;
benkatz 22:60276ba87ac6 22 oldVel = 0;
benkatz 22:60276ba87ac6 23 raw = 0;
benkatz 22:60276ba87ac6 24 }
benkatz 22:60276ba87ac6 25
benkatz 22:60276ba87ac6 26 void PositionSensorAM5147::Sample(){
benkatz 22:60276ba87ac6 27 cs->write(0);
benkatz 26:2b865c00d7e9 28 int raw1 = spi->write(0xFF);
benkatz 26:2b865c00d7e9 29 int raw2 = spi->write(0xFF);
benkatz 26:2b865c00d7e9 30 raw = (raw1<<8)|raw2;
benkatz 26:2b865c00d7e9 31 //raw = spi->write(readAngleCmd);
benkatz 25:f5741040c4bb 32 raw &= 0x3FFF; //Extract last 14 bits
benkatz 22:60276ba87ac6 33 cs->write(1);
benkatz 25:f5741040c4bb 34 int off_1 = offset_lut[raw>>7];
benkatz 25:f5741040c4bb 35 int off_2 = offset_lut[((raw>>7)+1)%128];
benkatz 25:f5741040c4bb 36 int off_interp = off_1 + ((off_2 - off_1)*(raw - ((raw>>7)<<7))>>7); // Interpolate between lookup table entries
benkatz 25:f5741040c4bb 37 int angle = raw + off_interp; // Correct for nonlinearity with lookup table from calibration
benkatz 22:60276ba87ac6 38 if(angle - old_counts > _CPR/2){
benkatz 22:60276ba87ac6 39 rotations -= 1;
benkatz 22:60276ba87ac6 40 }
benkatz 22:60276ba87ac6 41 else if (angle - old_counts < -_CPR/2){
benkatz 22:60276ba87ac6 42 rotations += 1;
benkatz 22:60276ba87ac6 43 }
benkatz 22:60276ba87ac6 44
benkatz 22:60276ba87ac6 45 old_counts = angle;
benkatz 22:60276ba87ac6 46 oldModPosition = modPosition;
benkatz 22:60276ba87ac6 47 modPosition = ((6.28318530718f * ((float) angle))/ (float)_CPR);
benkatz 22:60276ba87ac6 48 position = (6.28318530718f * ((float) angle+(_CPR*rotations)))/ (float)_CPR;
benkatz 22:60276ba87ac6 49 MechPosition = position - MechOffset;
benkatz 26:2b865c00d7e9 50 float elec = ((6.28318530718f/(float)_CPR) * (float) ((_ppairs*angle)%_CPR)) + ElecOffset;
benkatz 22:60276ba87ac6 51 if(elec < 0) elec += 6.28318530718f;
benkatz 23:2adf23ee0305 52 else if(elec > 6.28318530718f) elec -= 6.28318530718f ;
benkatz 22:60276ba87ac6 53 ElecPosition = elec;
benkatz 22:60276ba87ac6 54
benkatz 22:60276ba87ac6 55 float vel;
benkatz 22:60276ba87ac6 56 if(modPosition<.1f && oldModPosition>6.1f){
benkatz 22:60276ba87ac6 57 vel = (modPosition - oldModPosition + 6.28318530718f)*40000.0f;
benkatz 22:60276ba87ac6 58 }
benkatz 22:60276ba87ac6 59 else if(modPosition>6.1f && oldModPosition<0.1f){
benkatz 22:60276ba87ac6 60 vel = (modPosition - oldModPosition - 6.28318530718f)*40000.0f;
benkatz 22:60276ba87ac6 61 }
benkatz 22:60276ba87ac6 62 else{
benkatz 22:60276ba87ac6 63 vel = (modPosition-oldModPosition)*40000.0f;
benkatz 22:60276ba87ac6 64 }
benkatz 22:60276ba87ac6 65
benkatz 22:60276ba87ac6 66 int n = 16;
benkatz 22:60276ba87ac6 67 float sum = vel;
benkatz 22:60276ba87ac6 68 for (int i = 1; i < (n); i++){
benkatz 22:60276ba87ac6 69 velVec[n - i] = velVec[n-i-1];
benkatz 22:60276ba87ac6 70 sum += velVec[n-i];
benkatz 22:60276ba87ac6 71 }
benkatz 22:60276ba87ac6 72 velVec[0] = vel;
benkatz 22:60276ba87ac6 73 MechVelocity = sum/(float)n;
benkatz 22:60276ba87ac6 74 ElecVelocity = MechVelocity*_ppairs;
benkatz 14:80ce59119d93 75 }
benkatz 8:10ae7bc88d6e 76
benkatz 14:80ce59119d93 77 int PositionSensorAM5147::GetRawPosition(){
benkatz 22:60276ba87ac6 78 return raw;
benkatz 14:80ce59119d93 79 }
benkatz 14:80ce59119d93 80
benkatz 14:80ce59119d93 81 float PositionSensorAM5147::GetMechPosition(){
benkatz 14:80ce59119d93 82 return MechPosition;
benkatz 14:80ce59119d93 83 }
benkatz 14:80ce59119d93 84
benkatz 14:80ce59119d93 85 float PositionSensorAM5147::GetElecPosition(){
benkatz 22:60276ba87ac6 86 return ElecPosition;
benkatz 14:80ce59119d93 87 }
benkatz 14:80ce59119d93 88
benkatz 14:80ce59119d93 89 float PositionSensorAM5147::GetMechVelocity(){
benkatz 22:60276ba87ac6 90 return MechVelocity;
benkatz 14:80ce59119d93 91 }
benkatz 14:80ce59119d93 92
benkatz 14:80ce59119d93 93 void PositionSensorAM5147::ZeroPosition(){
benkatz 14:80ce59119d93 94 rotations = 0;
benkatz 28:8c7e29f719c5 95 MechOffset = 0;
benkatz 28:8c7e29f719c5 96 Sample();
benkatz 14:80ce59119d93 97 MechOffset = GetMechPosition();
benkatz 14:80ce59119d93 98 }
benkatz 0:4e1c4df6aabd 99
benkatz 22:60276ba87ac6 100 void PositionSensorAM5147::SetElecOffset(float offset){
benkatz 22:60276ba87ac6 101 ElecOffset = offset;
benkatz 22:60276ba87ac6 102 }
benkatz 22:60276ba87ac6 103
benkatz 22:60276ba87ac6 104 int PositionSensorAM5147::GetCPR(){
benkatz 22:60276ba87ac6 105 return _CPR;
benkatz 22:60276ba87ac6 106 }
benkatz 22:60276ba87ac6 107
benkatz 22:60276ba87ac6 108
benkatz 22:60276ba87ac6 109 void PositionSensorAM5147::WriteLUT(int new_lut[128]){
benkatz 22:60276ba87ac6 110 memcpy(offset_lut, new_lut, sizeof(offset_lut));
benkatz 22:60276ba87ac6 111 }
benkatz 20:bf9ea5125d52 112
benkatz 22:60276ba87ac6 113
benkatz 10:370851e6e132 114 PositionSensorEncoder::PositionSensorEncoder(int CPR, float offset, int ppairs) {
benkatz 10:370851e6e132 115 _ppairs = ppairs;
benkatz 0:4e1c4df6aabd 116 _CPR = CPR;
benkatz 0:4e1c4df6aabd 117 _offset = offset;
benkatz 3:6a0015d88d06 118 MechPosition = 0;
benkatz 22:60276ba87ac6 119 out_old = 0;
benkatz 22:60276ba87ac6 120 oldVel = 0;
benkatz 22:60276ba87ac6 121 raw = 0;
benkatz 0:4e1c4df6aabd 122
benkatz 0:4e1c4df6aabd 123 // Enable clock for GPIOA
benkatz 0:4e1c4df6aabd 124 __GPIOA_CLK_ENABLE(); //equivalent from hal_rcc.h
benkatz 0:4e1c4df6aabd 125
benkatz 0:4e1c4df6aabd 126 GPIOA->MODER |= GPIO_MODER_MODER6_1 | GPIO_MODER_MODER7_1 ; //PA6 & PA7 as Alternate Function /*!< GPIO port mode register, Address offset: 0x00 */
benkatz 0:4e1c4df6aabd 127 GPIOA->OTYPER |= GPIO_OTYPER_OT_6 | GPIO_OTYPER_OT_7 ; //PA6 & PA7 as Inputs /*!< GPIO port output type register, Address offset: 0x04 */
benkatz 0:4e1c4df6aabd 128 GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR6 | GPIO_OSPEEDER_OSPEEDR7 ; //Low speed /*!< GPIO port output speed register, Address offset: 0x08 */
benkatz 0:4e1c4df6aabd 129 GPIOA->PUPDR |= GPIO_PUPDR_PUPDR6_1 | GPIO_PUPDR_PUPDR7_1 ; //Pull Down /*!< GPIO port pull-up/pull-down register, Address offset: 0x0C */
benkatz 0:4e1c4df6aabd 130 GPIOA->AFR[0] |= 0x22000000 ; //AF02 for PA6 & PA7 /*!< GPIO alternate function registers, Address offset: 0x20-0x24 */
benkatz 0:4e1c4df6aabd 131 GPIOA->AFR[1] |= 0x00000000 ; //nibbles here refer to gpio8..15 /*!< GPIO alternate function registers, Address offset: 0x20-0x24 */
benkatz 0:4e1c4df6aabd 132
benkatz 0:4e1c4df6aabd 133 // configure TIM3 as Encoder input
benkatz 0:4e1c4df6aabd 134 // Enable clock for TIM3
benkatz 0:4e1c4df6aabd 135 __TIM3_CLK_ENABLE();
benkatz 0:4e1c4df6aabd 136
benkatz 22:60276ba87ac6 137 TIM3->CR1 = 0x0001; // CEN(Counter ENable)='1' < TIM control register 1
benkatz 22:60276ba87ac6 138 TIM3->SMCR = TIM_ENCODERMODE_TI12; // SMS='011' (Encoder mode 3) < TIM slave mode control register
benkatz 22:60276ba87ac6 139 TIM3->CCMR1 = 0x1111; // CC1S='01' CC2S='01' < TIM capture/compare mode register 1, maximum digital filtering
benkatz 22:60276ba87ac6 140 TIM3->CCMR2 = 0x0000; // < TIM capture/compare mode register 2
benkatz 22:60276ba87ac6 141 TIM3->CCER = 0x0011; // CC1P CC2P < TIM capture/compare enable register
benkatz 22:60276ba87ac6 142 TIM3->PSC = 0x0000; // Prescaler = (0+1) < TIM prescaler
benkatz 22:60276ba87ac6 143 TIM3->ARR = CPR; // IM auto-reload register
benkatz 0:4e1c4df6aabd 144
benkatz 7:dc5f27756e02 145 TIM3->CNT = 0x000; //reset the counter before we use it
benkatz 0:4e1c4df6aabd 146
benkatz 3:6a0015d88d06 147 // Extra Timer for velocity measurement
benkatz 9:d7eb815cb057 148
benkatz 3:6a0015d88d06 149 __TIM2_CLK_ENABLE();
benkatz 22:60276ba87ac6 150 TIM3->CR2 = 0x030; //MMS = 101
benkatz 3:6a0015d88d06 151
benkatz 3:6a0015d88d06 152 TIM2->PSC = 0x03;
benkatz 3:6a0015d88d06 153 //TIM2->CR2 |= TIM_CR2_TI1S;
benkatz 22:60276ba87ac6 154 TIM2->SMCR = 0x24; //TS = 010 for ITR2, SMS = 100 (reset counter at edge)
benkatz 22:60276ba87ac6 155 TIM2->CCMR1 = 0x3; // CC1S = 11, IC1 mapped on TRC
benkatz 3:6a0015d88d06 156
benkatz 3:6a0015d88d06 157 //TIM2->CR2 |= TIM_CR2_TI1S;
benkatz 3:6a0015d88d06 158 TIM2->CCER |= TIM_CCER_CC1P;
benkatz 3:6a0015d88d06 159 //TIM2->CCER |= TIM_CCER_CC1NP;
benkatz 3:6a0015d88d06 160 TIM2->CCER |= TIM_CCER_CC1E;
benkatz 3:6a0015d88d06 161
benkatz 3:6a0015d88d06 162
benkatz 22:60276ba87ac6 163 TIM2->CR1 = 0x01; //CEN, enable timer
benkatz 9:d7eb815cb057 164
benkatz 22:60276ba87ac6 165 TIM3->CR1 = 0x01; // CEN
benkatz 8:10ae7bc88d6e 166 ZPulse = new InterruptIn(PC_4);
benkatz 8:10ae7bc88d6e 167 ZSense = new DigitalIn(PC_4);
benkatz 8:10ae7bc88d6e 168 //ZPulse = new InterruptIn(PB_0);
benkatz 8:10ae7bc88d6e 169 //ZSense = new DigitalIn(PB_0);
benkatz 0:4e1c4df6aabd 170 ZPulse->enable_irq();
benkatz 0:4e1c4df6aabd 171 ZPulse->rise(this, &PositionSensorEncoder::ZeroEncoderCount);
benkatz 7:dc5f27756e02 172 //ZPulse->fall(this, &PositionSensorEncoder::ZeroEncoderCountDown);
benkatz 0:4e1c4df6aabd 173 ZPulse->mode(PullDown);
benkatz 7:dc5f27756e02 174 flag = 0;
benkatz 0:4e1c4df6aabd 175
benkatz 0:4e1c4df6aabd 176
benkatz 1:b8bceb4daed5 177 //ZTest = new DigitalOut(PC_2);
benkatz 1:b8bceb4daed5 178 //ZTest->write(1);
benkatz 22:60276ba87ac6 179 }
benkatz 0:4e1c4df6aabd 180
benkatz 22:60276ba87ac6 181 void PositionSensorEncoder::Sample(){
benkatz 0:4e1c4df6aabd 182
benkatz 22:60276ba87ac6 183 }
benkatz 22:60276ba87ac6 184
benkatz 0:4e1c4df6aabd 185
benkatz 22:60276ba87ac6 186 float PositionSensorEncoder::GetMechPosition() { //returns rotor angle in radians.
benkatz 8:10ae7bc88d6e 187 int raw = TIM3->CNT;
benkatz 7:dc5f27756e02 188 float unsigned_mech = (6.28318530718f/(float)_CPR) * (float) ((raw)%_CPR);
benkatz 7:dc5f27756e02 189 return (float) unsigned_mech;// + 6.28318530718f* (float) rotations;
benkatz 0:4e1c4df6aabd 190 }
benkatz 0:4e1c4df6aabd 191
benkatz 22:60276ba87ac6 192 float PositionSensorEncoder::GetElecPosition() { //returns rotor electrical angle in radians.
benkatz 7:dc5f27756e02 193 int raw = TIM3->CNT;
benkatz 10:370851e6e132 194 float elec = ((6.28318530718f/(float)_CPR) * (float) ((_ppairs*raw)%_CPR)) - _offset;
benkatz 8:10ae7bc88d6e 195 if(elec < 0) elec += 6.28318530718f;
benkatz 8:10ae7bc88d6e 196 return elec;
benkatz 0:4e1c4df6aabd 197 }
benkatz 0:4e1c4df6aabd 198
benkatz 13:a3fa0a31b114 199
benkatz 3:6a0015d88d06 200
benkatz 3:6a0015d88d06 201 float PositionSensorEncoder::GetMechVelocity(){
benkatz 22:60276ba87ac6 202
benkatz 9:d7eb815cb057 203 float out = 0;
benkatz 3:6a0015d88d06 204 float rawPeriod = TIM2->CCR1; //Clock Ticks
benkatz 22:60276ba87ac6 205 int currentTime = TIM2->CNT;
benkatz 22:60276ba87ac6 206 if(currentTime > 2000000){rawPeriod = currentTime;}
benkatz 3:6a0015d88d06 207 float dir = -2.0f*(float)(((TIM3->CR1)>>4)&1)+1.0f; // +/- 1
benkatz 22:60276ba87ac6 208 float meas = dir*180000000.0f*(6.28318530718f/(float)_CPR)/rawPeriod;
benkatz 22:60276ba87ac6 209 if(isinf(meas)){ meas = 1;}
benkatz 11:c83b18d41e54 210 out = meas;
benkatz 22:60276ba87ac6 211 //if(meas == oldVel){
benkatz 22:60276ba87ac6 212 // out = .9f*out_old;
benkatz 22:60276ba87ac6 213 // }
benkatz 22:60276ba87ac6 214
benkatz 22:60276ba87ac6 215
benkatz 22:60276ba87ac6 216 oldVel = meas;
benkatz 22:60276ba87ac6 217 out_old = out;
benkatz 22:60276ba87ac6 218 int n = 16;
benkatz 22:60276ba87ac6 219 float sum = out;
benkatz 22:60276ba87ac6 220 for (int i = 1; i < (n); i++){
benkatz 22:60276ba87ac6 221 velVec[n - i] = velVec[n-i-1];
benkatz 22:60276ba87ac6 222 sum += velVec[n-i];
benkatz 9:d7eb815cb057 223 }
benkatz 22:60276ba87ac6 224 velVec[0] = out;
benkatz 22:60276ba87ac6 225 return sum/(float)n;
benkatz 3:6a0015d88d06 226 }
benkatz 13:a3fa0a31b114 227
benkatz 13:a3fa0a31b114 228 float PositionSensorEncoder::GetElecVelocity(){
benkatz 13:a3fa0a31b114 229 return _ppairs*GetMechVelocity();
benkatz 13:a3fa0a31b114 230 }
benkatz 13:a3fa0a31b114 231
benkatz 0:4e1c4df6aabd 232 void PositionSensorEncoder::ZeroEncoderCount(void){
benkatz 7:dc5f27756e02 233 if (ZSense->read() == 1 & flag == 0){
benkatz 0:4e1c4df6aabd 234 if (ZSense->read() == 1){
benkatz 8:10ae7bc88d6e 235 GPIOC->ODR ^= (1 << 4);
benkatz 7:dc5f27756e02 236 TIM3->CNT = 0x000;
benkatz 1:b8bceb4daed5 237 //state = !state;
benkatz 1:b8bceb4daed5 238 //ZTest->write(state);
benkatz 7:dc5f27756e02 239 GPIOC->ODR ^= (1 << 4);
benkatz 7:dc5f27756e02 240 //flag = 1;
benkatz 7:dc5f27756e02 241 }
benkatz 7:dc5f27756e02 242 }
benkatz 7:dc5f27756e02 243 }
benkatz 22:60276ba87ac6 244
benkatz 22:60276ba87ac6 245 void PositionSensorEncoder::ZeroPosition(void){
benkatz 22:60276ba87ac6 246
benkatz 22:60276ba87ac6 247 }
benkatz 7:dc5f27756e02 248
benkatz 7:dc5f27756e02 249 void PositionSensorEncoder::ZeroEncoderCountDown(void){
benkatz 7:dc5f27756e02 250 if (ZSense->read() == 0){
benkatz 7:dc5f27756e02 251 if (ZSense->read() == 0){
benkatz 7:dc5f27756e02 252 GPIOC->ODR ^= (1 << 4);
benkatz 7:dc5f27756e02 253 flag = 0;
benkatz 7:dc5f27756e02 254 float dir = -2.0f*(float)(((TIM3->CR1)>>4)&1)+1.0f;
benkatz 7:dc5f27756e02 255 if(dir != dir){
benkatz 7:dc5f27756e02 256 dir = dir;
benkatz 7:dc5f27756e02 257 rotations += dir;
benkatz 7:dc5f27756e02 258 }
benkatz 7:dc5f27756e02 259
benkatz 7:dc5f27756e02 260 GPIOC->ODR ^= (1 << 4);
benkatz 7:dc5f27756e02 261
benkatz 0:4e1c4df6aabd 262 }
benkatz 0:4e1c4df6aabd 263 }
benkatz 22:60276ba87ac6 264 }
benkatz 22:60276ba87ac6 265 void PositionSensorEncoder::SetElecOffset(float offset){
benkatz 22:60276ba87ac6 266
benkatz 22:60276ba87ac6 267 }
benkatz 22:60276ba87ac6 268
benkatz 22:60276ba87ac6 269 int PositionSensorEncoder::GetRawPosition(void){
benkatz 22:60276ba87ac6 270 return 0;
benkatz 22:60276ba87ac6 271 }
benkatz 22:60276ba87ac6 272
benkatz 22:60276ba87ac6 273 int PositionSensorEncoder::GetCPR(){
benkatz 22:60276ba87ac6 274 return _CPR;
benkatz 22:60276ba87ac6 275 }
benkatz 22:60276ba87ac6 276
benkatz 22:60276ba87ac6 277
benkatz 22:60276ba87ac6 278 void PositionSensorEncoder::WriteLUT(int new_lut[128]){
benkatz 22:60276ba87ac6 279 memcpy(offset_lut, new_lut, sizeof(offset_lut));
benkatz 22:60276ba87ac6 280 }