DRV8323RS Version

Dependencies:   mbed-dev-f303 FastPWM3

Committer:
benkatz
Date:
Wed Dec 05 04:07:46 2018 +0000
Revision:
47:e1196a851f76
Parent:
46:2d4b1dafcfe3
Child:
48:74a40481740c
albert revision;

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