Slurp

Dependencies:   FastPWM3 mbed

Committer:
austinbrown124
Date:
Sat May 20 21:42:20 2017 +0000
Revision:
0:9edd6ec0f56a
First Commit

Who changed what in which revision?

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