hobbyking_cheetah source code modified 2020/12/15
Dependencies: mbed-dev-f303 FastPWM3
PositionSensor/PositionSensor.cpp@12:c473a25f54f7, 2016-05-22 (annotated)
- Committer:
- benkatz
- Date:
- Sun May 22 03:38:19 2016 +0000
- Revision:
- 12:c473a25f54f7
- Parent:
- 11:c83b18d41e54
- Child:
- 13:a3fa0a31b114
experimental feed-forward decoupling;
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 | 10:370851e6e132 | 6 | PositionSensorSPI::PositionSensorSPI(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 | 11:c83b18d41e54 | 19 | int PositionSensorSPI::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 | 8:10ae7bc88d6e | 27 | float PositionSensorSPI::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 | 8:10ae7bc88d6e | 43 | float PositionSensorSPI::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 | 9:d7eb815cb057 | 52 | float PositionSensorSPI::GetMechVelocity(){ |
benkatz | 9:d7eb815cb057 | 53 | return 0; |
benkatz | 9:d7eb815cb057 | 54 | } |
benkatz | 9:d7eb815cb057 | 55 | |
benkatz | 9:d7eb815cb057 | 56 | void PositionSensorSPI::ZeroPosition(){ |
benkatz | 9:d7eb815cb057 | 57 | rotations = 0; |
benkatz | 9:d7eb815cb057 | 58 | MechOffset = GetMechPosition(); |
benkatz | 9:d7eb815cb057 | 59 | } |
benkatz | 8:10ae7bc88d6e | 60 | |
benkatz | 8:10ae7bc88d6e | 61 | |
benkatz | 0:4e1c4df6aabd | 62 | |
benkatz | 10:370851e6e132 | 63 | PositionSensorEncoder::PositionSensorEncoder(int CPR, float offset, int ppairs) { |
benkatz | 10:370851e6e132 | 64 | _ppairs = ppairs; |
benkatz | 0:4e1c4df6aabd | 65 | _CPR = CPR; |
benkatz | 0:4e1c4df6aabd | 66 | _offset = offset; |
benkatz | 3:6a0015d88d06 | 67 | MechPosition = 0; |
benkatz | 0:4e1c4df6aabd | 68 | |
benkatz | 0:4e1c4df6aabd | 69 | // Enable clock for GPIOA |
benkatz | 0:4e1c4df6aabd | 70 | __GPIOA_CLK_ENABLE(); //equivalent from hal_rcc.h |
benkatz | 0:4e1c4df6aabd | 71 | |
benkatz | 0:4e1c4df6aabd | 72 | 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 | 73 | 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 | 74 | GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR6 | GPIO_OSPEEDER_OSPEEDR7 ; //Low speed /*!< GPIO port output speed register, Address offset: 0x08 */ |
benkatz | 0:4e1c4df6aabd | 75 | 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 | 76 | GPIOA->AFR[0] |= 0x22000000 ; //AF02 for PA6 & PA7 /*!< GPIO alternate function registers, Address offset: 0x20-0x24 */ |
benkatz | 0:4e1c4df6aabd | 77 | GPIOA->AFR[1] |= 0x00000000 ; //nibbles here refer to gpio8..15 /*!< GPIO alternate function registers, Address offset: 0x20-0x24 */ |
benkatz | 0:4e1c4df6aabd | 78 | |
benkatz | 0:4e1c4df6aabd | 79 | // configure TIM3 as Encoder input |
benkatz | 0:4e1c4df6aabd | 80 | // Enable clock for TIM3 |
benkatz | 0:4e1c4df6aabd | 81 | __TIM3_CLK_ENABLE(); |
benkatz | 0:4e1c4df6aabd | 82 | |
benkatz | 0:4e1c4df6aabd | 83 | TIM3->CR1 = 0x0001; // CEN(Counter ENable)='1' < TIM control register 1 |
benkatz | 0:4e1c4df6aabd | 84 | TIM3->SMCR = TIM_ENCODERMODE_TI12; // SMS='011' (Encoder mode 3) < TIM slave mode control register |
benkatz | 1:b8bceb4daed5 | 85 | TIM3->CCMR1 = 0xf1f1; // CC1S='01' CC2S='01' < TIM capture/compare mode register 1, maximum digital filtering |
benkatz | 0:4e1c4df6aabd | 86 | TIM3->CCMR2 = 0x0000; // < TIM capture/compare mode register 2 |
benkatz | 0:4e1c4df6aabd | 87 | TIM3->CCER = 0x0011; // CC1P CC2P < TIM capture/compare enable register |
benkatz | 0:4e1c4df6aabd | 88 | TIM3->PSC = 0x0000; // Prescaler = (0+1) < TIM prescaler |
benkatz | 8:10ae7bc88d6e | 89 | TIM3->ARR = CPR-1; // reload at 0xfffffff < TIM auto-reload register |
benkatz | 0:4e1c4df6aabd | 90 | |
benkatz | 7:dc5f27756e02 | 91 | TIM3->CNT = 0x000; //reset the counter before we use it |
benkatz | 0:4e1c4df6aabd | 92 | |
benkatz | 3:6a0015d88d06 | 93 | // Extra Timer for velocity measurement |
benkatz | 9:d7eb815cb057 | 94 | |
benkatz | 3:6a0015d88d06 | 95 | __TIM2_CLK_ENABLE(); |
benkatz | 3:6a0015d88d06 | 96 | TIM3->CR2 = 0x030; //MMS = 101 |
benkatz | 3:6a0015d88d06 | 97 | |
benkatz | 3:6a0015d88d06 | 98 | TIM2->PSC = 0x03; |
benkatz | 3:6a0015d88d06 | 99 | //TIM2->CR2 |= TIM_CR2_TI1S; |
benkatz | 3:6a0015d88d06 | 100 | TIM2->SMCR = 0x24; //TS = 010 for ITR2, SMS = 100 (reset counter at edge) |
benkatz | 3:6a0015d88d06 | 101 | TIM2->CCMR1 = 0x3;// CC1S = 11, IC1 mapped on TRC |
benkatz | 3:6a0015d88d06 | 102 | |
benkatz | 3:6a0015d88d06 | 103 | //TIM2->CR2 |= TIM_CR2_TI1S; |
benkatz | 3:6a0015d88d06 | 104 | TIM2->CCER |= TIM_CCER_CC1P; |
benkatz | 3:6a0015d88d06 | 105 | //TIM2->CCER |= TIM_CCER_CC1NP; |
benkatz | 3:6a0015d88d06 | 106 | TIM2->CCER |= TIM_CCER_CC1E; |
benkatz | 3:6a0015d88d06 | 107 | |
benkatz | 3:6a0015d88d06 | 108 | |
benkatz | 3:6a0015d88d06 | 109 | TIM2->CR1 = 0x01; //CEN |
benkatz | 9:d7eb815cb057 | 110 | |
benkatz | 3:6a0015d88d06 | 111 | TIM3->CR1 = 0x01; // CEN |
benkatz | 8:10ae7bc88d6e | 112 | ZPulse = new InterruptIn(PC_4); |
benkatz | 8:10ae7bc88d6e | 113 | ZSense = new DigitalIn(PC_4); |
benkatz | 8:10ae7bc88d6e | 114 | //ZPulse = new InterruptIn(PB_0); |
benkatz | 8:10ae7bc88d6e | 115 | //ZSense = new DigitalIn(PB_0); |
benkatz | 0:4e1c4df6aabd | 116 | ZPulse->enable_irq(); |
benkatz | 0:4e1c4df6aabd | 117 | ZPulse->rise(this, &PositionSensorEncoder::ZeroEncoderCount); |
benkatz | 7:dc5f27756e02 | 118 | //ZPulse->fall(this, &PositionSensorEncoder::ZeroEncoderCountDown); |
benkatz | 0:4e1c4df6aabd | 119 | ZPulse->mode(PullDown); |
benkatz | 7:dc5f27756e02 | 120 | flag = 0; |
benkatz | 0:4e1c4df6aabd | 121 | |
benkatz | 0:4e1c4df6aabd | 122 | |
benkatz | 1:b8bceb4daed5 | 123 | //ZTest = new DigitalOut(PC_2); |
benkatz | 1:b8bceb4daed5 | 124 | //ZTest->write(1); |
benkatz | 0:4e1c4df6aabd | 125 | |
benkatz | 0:4e1c4df6aabd | 126 | |
benkatz | 0:4e1c4df6aabd | 127 | } |
benkatz | 0:4e1c4df6aabd | 128 | |
benkatz | 0:4e1c4df6aabd | 129 | float PositionSensorEncoder::GetMechPosition() { //returns rotor angle in radians. |
benkatz | 8:10ae7bc88d6e | 130 | int raw = TIM3->CNT; |
benkatz | 7:dc5f27756e02 | 131 | float unsigned_mech = (6.28318530718f/(float)_CPR) * (float) ((raw)%_CPR); |
benkatz | 7:dc5f27756e02 | 132 | return (float) unsigned_mech;// + 6.28318530718f* (float) rotations; |
benkatz | 0:4e1c4df6aabd | 133 | } |
benkatz | 0:4e1c4df6aabd | 134 | |
benkatz | 0:4e1c4df6aabd | 135 | float PositionSensorEncoder::GetElecPosition() { //returns rotor electrical angle in radians. |
benkatz | 7:dc5f27756e02 | 136 | int raw = TIM3->CNT; |
benkatz | 10:370851e6e132 | 137 | float elec = ((6.28318530718f/(float)_CPR) * (float) ((_ppairs*raw)%_CPR)) - _offset; |
benkatz | 8:10ae7bc88d6e | 138 | if(elec < 0) elec += 6.28318530718f; |
benkatz | 8:10ae7bc88d6e | 139 | return elec; |
benkatz | 0:4e1c4df6aabd | 140 | } |
benkatz | 0:4e1c4df6aabd | 141 | |
benkatz | 3:6a0015d88d06 | 142 | float PositionSensorEncoder::GetElecVelocity(){ |
benkatz | 12:c473a25f54f7 | 143 | return _ppairs*GetMechVelocity(); |
benkatz | 3:6a0015d88d06 | 144 | } |
benkatz | 3:6a0015d88d06 | 145 | |
benkatz | 3:6a0015d88d06 | 146 | float PositionSensorEncoder::GetMechVelocity(){ |
benkatz | 9:d7eb815cb057 | 147 | float out = 0; |
benkatz | 3:6a0015d88d06 | 148 | float rawPeriod = TIM2->CCR1; //Clock Ticks |
benkatz | 9:d7eb815cb057 | 149 | |
benkatz | 3:6a0015d88d06 | 150 | float dir = -2.0f*(float)(((TIM3->CR1)>>4)&1)+1.0f; // +/- 1 |
benkatz | 9:d7eb815cb057 | 151 | float meas = dir*90000000.0f*(6.28318530718f/(float)_CPR)/rawPeriod; |
benkatz | 11:c83b18d41e54 | 152 | out = meas; |
benkatz | 9:d7eb815cb057 | 153 | if(meas == vel_old){ |
benkatz | 9:d7eb815cb057 | 154 | out = .95f*out_old; |
benkatz | 9:d7eb815cb057 | 155 | } |
benkatz | 9:d7eb815cb057 | 156 | else{ |
benkatz | 9:d7eb815cb057 | 157 | out = meas; |
benkatz | 9:d7eb815cb057 | 158 | } |
benkatz | 9:d7eb815cb057 | 159 | |
benkatz | 9:d7eb815cb057 | 160 | /* |
benkatz | 9:d7eb815cb057 | 161 | if(abs(vel) < 2.0f){ |
benkatz | 9:d7eb815cb057 | 162 | vel = 0; |
benkatz | 9:d7eb815cb057 | 163 | } |
benkatz | 9:d7eb815cb057 | 164 | */ |
benkatz | 9:d7eb815cb057 | 165 | vel_old = meas; |
benkatz | 9:d7eb815cb057 | 166 | |
benkatz | 9:d7eb815cb057 | 167 | //out = .5f*out + .5f*out_old; |
benkatz | 9:d7eb815cb057 | 168 | out_old = out; |
benkatz | 9:d7eb815cb057 | 169 | return out; |
benkatz | 3:6a0015d88d06 | 170 | } |
benkatz | 3:6a0015d88d06 | 171 | |
benkatz | 0:4e1c4df6aabd | 172 | void PositionSensorEncoder::ZeroEncoderCount(void){ |
benkatz | 7:dc5f27756e02 | 173 | if (ZSense->read() == 1 & flag == 0){ |
benkatz | 0:4e1c4df6aabd | 174 | if (ZSense->read() == 1){ |
benkatz | 8:10ae7bc88d6e | 175 | GPIOC->ODR ^= (1 << 4); |
benkatz | 7:dc5f27756e02 | 176 | TIM3->CNT = 0x000; |
benkatz | 1:b8bceb4daed5 | 177 | //state = !state; |
benkatz | 1:b8bceb4daed5 | 178 | //ZTest->write(state); |
benkatz | 7:dc5f27756e02 | 179 | GPIOC->ODR ^= (1 << 4); |
benkatz | 7:dc5f27756e02 | 180 | //flag = 1; |
benkatz | 7:dc5f27756e02 | 181 | } |
benkatz | 7:dc5f27756e02 | 182 | } |
benkatz | 7:dc5f27756e02 | 183 | } |
benkatz | 7:dc5f27756e02 | 184 | |
benkatz | 7:dc5f27756e02 | 185 | void PositionSensorEncoder::ZeroEncoderCountDown(void){ |
benkatz | 7:dc5f27756e02 | 186 | if (ZSense->read() == 0){ |
benkatz | 7:dc5f27756e02 | 187 | if (ZSense->read() == 0){ |
benkatz | 7:dc5f27756e02 | 188 | GPIOC->ODR ^= (1 << 4); |
benkatz | 7:dc5f27756e02 | 189 | flag = 0; |
benkatz | 7:dc5f27756e02 | 190 | float dir = -2.0f*(float)(((TIM3->CR1)>>4)&1)+1.0f; |
benkatz | 7:dc5f27756e02 | 191 | if(dir != dir){ |
benkatz | 7:dc5f27756e02 | 192 | dir = dir; |
benkatz | 7:dc5f27756e02 | 193 | rotations += dir; |
benkatz | 7:dc5f27756e02 | 194 | } |
benkatz | 7:dc5f27756e02 | 195 | |
benkatz | 7:dc5f27756e02 | 196 | GPIOC->ODR ^= (1 << 4); |
benkatz | 7:dc5f27756e02 | 197 | |
benkatz | 0:4e1c4df6aabd | 198 | } |
benkatz | 0:4e1c4df6aabd | 199 | } |
benkatz | 0:4e1c4df6aabd | 200 | } |