hobbyking_cheetah source code modified 2020/12/15

Dependencies:   mbed-dev-f303 FastPWM3

Committer:
benkatz
Date:
Thu Jul 12 02:50:34 2018 +0000
Revision:
46:2d4b1dafcfe3
Parent:
45:26801179208e
Child:
47:e1196a851f76
calibration frees up memory when done;

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