![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
last working
Fork of foc-ed_in_the_bot_compact by
PositionSensor/PositionSensor.cpp@10:6829abb438fc, 2016-05-17 (annotated)
- Committer:
- nki
- Date:
- Tue May 17 09:58:58 2016 +0000
- Revision:
- 10:6829abb438fc
- Parent:
- 9:074575151e4b
Bayley tweaked- that night we took the altermoter apart;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bwang | 0:bac9c3a3a6ca | 1 | |
bwang | 0:bac9c3a3a6ca | 2 | #include "mbed.h" |
bwang | 0:bac9c3a3a6ca | 3 | #include "PositionSensor.h" |
bwang | 9:074575151e4b | 4 | #include "config.h" |
bwang | 0:bac9c3a3a6ca | 5 | #include <math.h> |
bwang | 0:bac9c3a3a6ca | 6 | |
bwang | 0:bac9c3a3a6ca | 7 | /* |
bwang | 0:bac9c3a3a6ca | 8 | * CPR: counts per revolution (4x lines per revolution) |
bwang | 0:bac9c3a3a6ca | 9 | * offset: mechanical position offset in radians |
bwang | 0:bac9c3a3a6ca | 10 | */ |
bwang | 0:bac9c3a3a6ca | 11 | |
bwang | 9:074575151e4b | 12 | PositionSensorEncoder::PositionSensorEncoder(int cpr, float offset) { |
bwang | 9:074575151e4b | 13 | _cpr = cpr; |
bwang | 0:bac9c3a3a6ca | 14 | _offset = offset; |
bwang | 0:bac9c3a3a6ca | 15 | |
bwang | 0:bac9c3a3a6ca | 16 | __GPIOA_CLK_ENABLE(); |
bwang | 0:bac9c3a3a6ca | 17 | __GPIOB_CLK_ENABLE(); |
bwang | 0:bac9c3a3a6ca | 18 | |
bwang | 0:bac9c3a3a6ca | 19 | GPIOB->MODER |= GPIO_MODER_MODER3_1; |
bwang | 0:bac9c3a3a6ca | 20 | GPIOB->OTYPER |= GPIO_OTYPER_OT_3 | GPIO_OTYPER_OT_10 ; |
bwang | 0:bac9c3a3a6ca | 21 | GPIOB->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR3 | GPIO_OSPEEDER_OSPEEDR10 ; |
bwang | 0:bac9c3a3a6ca | 22 | GPIOB->AFR[0] |= 0x00001000 ; |
bwang | 0:bac9c3a3a6ca | 23 | |
bwang | 0:bac9c3a3a6ca | 24 | GPIOA->MODER |= GPIO_MODER_MODER15_1; |
bwang | 0:bac9c3a3a6ca | 25 | GPIOA->OTYPER |= GPIO_OTYPER_OT_15; |
bwang | 0:bac9c3a3a6ca | 26 | GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR15; |
bwang | 0:bac9c3a3a6ca | 27 | GPIOA->AFR[1] |= 0x10000000 ; |
bwang | 0:bac9c3a3a6ca | 28 | |
bwang | 0:bac9c3a3a6ca | 29 | __TIM2_CLK_ENABLE(); |
bwang | 0:bac9c3a3a6ca | 30 | |
bwang | 0:bac9c3a3a6ca | 31 | TIM2->CR1 = 0x0001; |
bwang | 0:bac9c3a3a6ca | 32 | TIM2->SMCR = TIM_ENCODERMODE_TI12; |
bwang | 0:bac9c3a3a6ca | 33 | TIM2->CCMR1 = 0xf1f1; |
bwang | 0:bac9c3a3a6ca | 34 | TIM2->CCMR2 = 0x0000; |
bwang | 0:bac9c3a3a6ca | 35 | TIM2->CCER = 0x0011; |
bwang | 0:bac9c3a3a6ca | 36 | TIM2->PSC = 0x0000; |
bwang | 0:bac9c3a3a6ca | 37 | TIM2->ARR = 0xffffffff; |
bwang | 0:bac9c3a3a6ca | 38 | |
bwang | 0:bac9c3a3a6ca | 39 | TIM2->CNT = 0; |
bwang | 0:bac9c3a3a6ca | 40 | |
bwang | 0:bac9c3a3a6ca | 41 | ZPulse = new InterruptIn(PB_12); |
bwang | 0:bac9c3a3a6ca | 42 | ZSense = new DigitalIn(PB_12); |
bwang | 0:bac9c3a3a6ca | 43 | ZPulse->enable_irq(); |
bwang | 0:bac9c3a3a6ca | 44 | ZPulse->rise(this, &PositionSensorEncoder::ZeroEncoderCount); |
bwang | 0:bac9c3a3a6ca | 45 | ZPulse->mode(PullDown); |
bwang | 0:bac9c3a3a6ca | 46 | |
bwang | 0:bac9c3a3a6ca | 47 | ZTest = new DigitalOut(PC_10); |
bwang | 0:bac9c3a3a6ca | 48 | ZTest->write(1); |
bwang | 0:bac9c3a3a6ca | 49 | state = 0; |
bwang | 0:bac9c3a3a6ca | 50 | } |
bwang | 0:bac9c3a3a6ca | 51 | |
bwang | 0:bac9c3a3a6ca | 52 | /* |
bwang | 0:bac9c3a3a6ca | 53 | * Returns mechanical position in radians |
bwang | 0:bac9c3a3a6ca | 54 | */ |
bwang | 0:bac9c3a3a6ca | 55 | |
bwang | 0:bac9c3a3a6ca | 56 | float PositionSensorEncoder::GetMechPosition() { |
bwang | 0:bac9c3a3a6ca | 57 | int raw = TIM2->CNT; |
bwang | 9:074575151e4b | 58 | if (raw < 0) raw += _cpr; |
bwang | 9:074575151e4b | 59 | if (raw >= _cpr) raw -= _cpr; |
bwang | 9:074575151e4b | 60 | float signed_elec = fmod(6.28318530718f * (raw) / (float)_cpr + _offset, 6.28318530718f); |
bwang | 0:bac9c3a3a6ca | 61 | if (signed_elec < 0){ |
bwang | 0:bac9c3a3a6ca | 62 | return signed_elec + 6.28318530718f; |
bwang | 0:bac9c3a3a6ca | 63 | } |
bwang | 0:bac9c3a3a6ca | 64 | else{ |
bwang | 0:bac9c3a3a6ca | 65 | return signed_elec; |
bwang | 0:bac9c3a3a6ca | 66 | } |
bwang | 0:bac9c3a3a6ca | 67 | } |
bwang | 0:bac9c3a3a6ca | 68 | |
bwang | 0:bac9c3a3a6ca | 69 | /* |
bwang | 0:bac9c3a3a6ca | 70 | * Returns electrical position in radians |
bwang | 0:bac9c3a3a6ca | 71 | */ |
bwang | 0:bac9c3a3a6ca | 72 | |
bwang | 0:bac9c3a3a6ca | 73 | float PositionSensorEncoder::GetElecPosition() { |
bwang | 0:bac9c3a3a6ca | 74 | int raw = TIM2->CNT; |
bwang | 9:074575151e4b | 75 | if (raw < 0) raw += _cpr; |
bwang | 9:074575151e4b | 76 | if (raw >= _cpr) raw -= _cpr; |
bwang | 9:074575151e4b | 77 | float signed_elec = fmod((POLE_PAIRS / RESOLVER_LOBES * (6.28318530718f * (raw) / (float)_cpr + _offset)), 6.28318530718f); |
bwang | 0:bac9c3a3a6ca | 78 | if (signed_elec < 0) { |
bwang | 0:bac9c3a3a6ca | 79 | return signed_elec + 6.28318530718f; |
bwang | 0:bac9c3a3a6ca | 80 | } else { |
bwang | 0:bac9c3a3a6ca | 81 | return signed_elec; |
bwang | 0:bac9c3a3a6ca | 82 | } |
bwang | 0:bac9c3a3a6ca | 83 | } |
bwang | 0:bac9c3a3a6ca | 84 | |
bwang | 0:bac9c3a3a6ca | 85 | void PositionSensorEncoder::ZeroEncoderCount(void){ |
bwang | 0:bac9c3a3a6ca | 86 | if (ZSense->read() == 1){ |
bwang | 0:bac9c3a3a6ca | 87 | if (ZSense->read() == 1){ |
bwang | 0:bac9c3a3a6ca | 88 | TIM2->CNT=0; |
bwang | 0:bac9c3a3a6ca | 89 | } |
bwang | 0:bac9c3a3a6ca | 90 | } |
bwang | 0:bac9c3a3a6ca | 91 | } |