auto-measurements
Dependencies: FastPWM3 mbed-dev
Fork of Hobbyking_Cheetah_Compact by
PositionSensor/PositionSensor.cpp@6:4ee1cdc43aa8, 2016-03-12 (annotated)
- Committer:
- benkatz
- Date:
- Sat Mar 12 19:55:52 2016 +0000
- Revision:
- 6:4ee1cdc43aa8
- Parent:
- 5:51c6560bf624
- Child:
- 7:dc5f27756e02
Switched from sinusoidal pwm to svm
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 | 0:4e1c4df6aabd | 6 | |
benkatz | 0:4e1c4df6aabd | 7 | PositionSensorEncoder::PositionSensorEncoder(int CPR, float offset) { |
benkatz | 0:4e1c4df6aabd | 8 | _CPR = CPR; |
benkatz | 0:4e1c4df6aabd | 9 | _offset = offset; |
benkatz | 3:6a0015d88d06 | 10 | MechPosition = 0; |
benkatz | 0:4e1c4df6aabd | 11 | |
benkatz | 0:4e1c4df6aabd | 12 | // Enable clock for GPIOA |
benkatz | 0:4e1c4df6aabd | 13 | __GPIOA_CLK_ENABLE(); //equivalent from hal_rcc.h |
benkatz | 0:4e1c4df6aabd | 14 | |
benkatz | 0:4e1c4df6aabd | 15 | 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 | 16 | 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 | 17 | GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR6 | GPIO_OSPEEDER_OSPEEDR7 ; //Low speed /*!< GPIO port output speed register, Address offset: 0x08 */ |
benkatz | 0:4e1c4df6aabd | 18 | 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 | 19 | GPIOA->AFR[0] |= 0x22000000 ; //AF02 for PA6 & PA7 /*!< GPIO alternate function registers, Address offset: 0x20-0x24 */ |
benkatz | 0:4e1c4df6aabd | 20 | GPIOA->AFR[1] |= 0x00000000 ; //nibbles here refer to gpio8..15 /*!< GPIO alternate function registers, Address offset: 0x20-0x24 */ |
benkatz | 0:4e1c4df6aabd | 21 | |
benkatz | 0:4e1c4df6aabd | 22 | // configure TIM3 as Encoder input |
benkatz | 0:4e1c4df6aabd | 23 | // Enable clock for TIM3 |
benkatz | 0:4e1c4df6aabd | 24 | __TIM3_CLK_ENABLE(); |
benkatz | 0:4e1c4df6aabd | 25 | |
benkatz | 0:4e1c4df6aabd | 26 | TIM3->CR1 = 0x0001; // CEN(Counter ENable)='1' < TIM control register 1 |
benkatz | 0:4e1c4df6aabd | 27 | TIM3->SMCR = TIM_ENCODERMODE_TI12; // SMS='011' (Encoder mode 3) < TIM slave mode control register |
benkatz | 1:b8bceb4daed5 | 28 | TIM3->CCMR1 = 0xf1f1; // CC1S='01' CC2S='01' < TIM capture/compare mode register 1, maximum digital filtering |
benkatz | 0:4e1c4df6aabd | 29 | TIM3->CCMR2 = 0x0000; // < TIM capture/compare mode register 2 |
benkatz | 0:4e1c4df6aabd | 30 | TIM3->CCER = 0x0011; // CC1P CC2P < TIM capture/compare enable register |
benkatz | 0:4e1c4df6aabd | 31 | TIM3->PSC = 0x0000; // Prescaler = (0+1) < TIM prescaler |
benkatz | 0:4e1c4df6aabd | 32 | TIM3->ARR = 0xffffffff; // reload at 0xfffffff < TIM auto-reload register |
benkatz | 0:4e1c4df6aabd | 33 | |
benkatz | 0:4e1c4df6aabd | 34 | TIM3->CNT = 0x8000; //reset the counter before we use it |
benkatz | 0:4e1c4df6aabd | 35 | |
benkatz | 3:6a0015d88d06 | 36 | // Extra Timer for velocity measurement |
benkatz | 3:6a0015d88d06 | 37 | /* |
benkatz | 3:6a0015d88d06 | 38 | __TIM2_CLK_ENABLE(); |
benkatz | 3:6a0015d88d06 | 39 | TIM3->CR2 = 0x030; //MMS = 101 |
benkatz | 3:6a0015d88d06 | 40 | |
benkatz | 3:6a0015d88d06 | 41 | TIM2->PSC = 0x03; |
benkatz | 3:6a0015d88d06 | 42 | //TIM2->CR2 |= TIM_CR2_TI1S; |
benkatz | 3:6a0015d88d06 | 43 | TIM2->SMCR = 0x24; //TS = 010 for ITR2, SMS = 100 (reset counter at edge) |
benkatz | 3:6a0015d88d06 | 44 | TIM2->CCMR1 = 0x3;// CC1S = 11, IC1 mapped on TRC |
benkatz | 3:6a0015d88d06 | 45 | |
benkatz | 3:6a0015d88d06 | 46 | //TIM2->CR2 |= TIM_CR2_TI1S; |
benkatz | 3:6a0015d88d06 | 47 | TIM2->CCER |= TIM_CCER_CC1P; |
benkatz | 3:6a0015d88d06 | 48 | //TIM2->CCER |= TIM_CCER_CC1NP; |
benkatz | 3:6a0015d88d06 | 49 | TIM2->CCER |= TIM_CCER_CC1E; |
benkatz | 3:6a0015d88d06 | 50 | |
benkatz | 3:6a0015d88d06 | 51 | |
benkatz | 3:6a0015d88d06 | 52 | TIM2->CR1 = 0x01; //CEN |
benkatz | 3:6a0015d88d06 | 53 | */ |
benkatz | 3:6a0015d88d06 | 54 | TIM3->CR1 = 0x01; // CEN |
benkatz | 3:6a0015d88d06 | 55 | |
benkatz | 0:4e1c4df6aabd | 56 | ZPulse = new InterruptIn(PB_0); |
benkatz | 0:4e1c4df6aabd | 57 | ZSense = new DigitalIn(PB_0); |
benkatz | 0:4e1c4df6aabd | 58 | ZPulse->enable_irq(); |
benkatz | 0:4e1c4df6aabd | 59 | ZPulse->rise(this, &PositionSensorEncoder::ZeroEncoderCount); |
benkatz | 0:4e1c4df6aabd | 60 | ZPulse->mode(PullDown); |
benkatz | 0:4e1c4df6aabd | 61 | |
benkatz | 0:4e1c4df6aabd | 62 | |
benkatz | 1:b8bceb4daed5 | 63 | //ZTest = new DigitalOut(PC_2); |
benkatz | 1:b8bceb4daed5 | 64 | //ZTest->write(1); |
benkatz | 0:4e1c4df6aabd | 65 | |
benkatz | 0:4e1c4df6aabd | 66 | |
benkatz | 0:4e1c4df6aabd | 67 | } |
benkatz | 0:4e1c4df6aabd | 68 | |
benkatz | 0:4e1c4df6aabd | 69 | float PositionSensorEncoder::GetMechPosition() { //returns rotor angle in radians. |
benkatz | 0:4e1c4df6aabd | 70 | int raw = TIM3->CNT-0x8000; |
benkatz | 0:4e1c4df6aabd | 71 | if (raw < 0) raw += _CPR; |
benkatz | 0:4e1c4df6aabd | 72 | if (raw >= _CPR) raw -= _CPR; |
benkatz | 5:51c6560bf624 | 73 | float signed_mech = fmod(((6.28318530718f*(raw)/(float)_CPR + _offset)), 6.28318530718f); |
benkatz | 3:6a0015d88d06 | 74 | if (signed_mech < 0){ |
benkatz | 3:6a0015d88d06 | 75 | return signed_mech + 6.28318530718f; |
benkatz | 3:6a0015d88d06 | 76 | } |
benkatz | 3:6a0015d88d06 | 77 | else{ |
benkatz | 3:6a0015d88d06 | 78 | return signed_mech; |
benkatz | 3:6a0015d88d06 | 79 | } |
benkatz | 0:4e1c4df6aabd | 80 | } |
benkatz | 0:4e1c4df6aabd | 81 | |
benkatz | 0:4e1c4df6aabd | 82 | float PositionSensorEncoder::GetElecPosition() { //returns rotor electrical angle in radians. |
benkatz | 0:4e1c4df6aabd | 83 | int raw = TIM3->CNT-0x8000; |
benkatz | 0:4e1c4df6aabd | 84 | if (raw < 0) raw += _CPR; |
benkatz | 0:4e1c4df6aabd | 85 | if (raw >= _CPR) raw -= _CPR; |
benkatz | 3:6a0015d88d06 | 86 | float signed_elec = fmod((7.0f*(6.28318530718f*(raw)/(float)_CPR + _offset)), 6.28318530718f); //7 pole pairs |
benkatz | 0:4e1c4df6aabd | 87 | //float signed_elec = (7*(6.28318530718*(TIM3->CNT-0x8000)/(float)_CPR + _offset)); |
benkatz | 0:4e1c4df6aabd | 88 | if (signed_elec < 0){ |
benkatz | 0:4e1c4df6aabd | 89 | return signed_elec + 6.28318530718f; |
benkatz | 0:4e1c4df6aabd | 90 | } |
benkatz | 0:4e1c4df6aabd | 91 | else{ |
benkatz | 0:4e1c4df6aabd | 92 | return signed_elec; |
benkatz | 0:4e1c4df6aabd | 93 | } |
benkatz | 0:4e1c4df6aabd | 94 | } |
benkatz | 0:4e1c4df6aabd | 95 | |
benkatz | 3:6a0015d88d06 | 96 | float PositionSensorEncoder::GetElecVelocity(){ |
benkatz | 3:6a0015d88d06 | 97 | float rawPeriod = TIM2->CCR1; //Clock Ticks |
benkatz | 3:6a0015d88d06 | 98 | float dir = (((TIM3->CR1)>>4)&1)*2-1; // +/- 1 |
benkatz | 3:6a0015d88d06 | 99 | return dir*7*90000000.0f*(6.28318530718f/(float)_CPR)/rawPeriod; |
benkatz | 3:6a0015d88d06 | 100 | } |
benkatz | 3:6a0015d88d06 | 101 | |
benkatz | 3:6a0015d88d06 | 102 | float PositionSensorEncoder::GetMechVelocity(){ |
benkatz | 3:6a0015d88d06 | 103 | float rawPeriod = TIM2->CCR1; //Clock Ticks |
benkatz | 3:6a0015d88d06 | 104 | float dir = -2.0f*(float)(((TIM3->CR1)>>4)&1)+1.0f; // +/- 1 |
benkatz | 3:6a0015d88d06 | 105 | return dir*90000000.0f*(6.28318530718f/(float)_CPR)/rawPeriod; |
benkatz | 3:6a0015d88d06 | 106 | |
benkatz | 3:6a0015d88d06 | 107 | } |
benkatz | 3:6a0015d88d06 | 108 | |
benkatz | 0:4e1c4df6aabd | 109 | void PositionSensorEncoder::ZeroEncoderCount(void){ |
benkatz | 0:4e1c4df6aabd | 110 | if (ZSense->read() == 1){ |
benkatz | 0:4e1c4df6aabd | 111 | if (ZSense->read() == 1){ |
benkatz | 0:4e1c4df6aabd | 112 | TIM3->CNT=0x8000; |
benkatz | 1:b8bceb4daed5 | 113 | //state = !state; |
benkatz | 1:b8bceb4daed5 | 114 | //ZTest->write(state); |
benkatz | 0:4e1c4df6aabd | 115 | } |
benkatz | 0:4e1c4df6aabd | 116 | } |
benkatz | 0:4e1c4df6aabd | 117 | } |