auto-measurements

Dependencies:   FastPWM3 mbed-dev

Fork of Hobbyking_Cheetah_Compact by Ben Katz

Committer:
benkatz
Date:
Wed Apr 05 20:54:16 2017 +0000
Revision:
23:2adf23ee0305
Parent:
22:60276ba87ac6
Child:
25:f5741040c4bb
Added bayley's flash writer

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