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