hobbyking_cheetah source code modified 2020/12/15

Dependencies:   mbed-dev-f303 FastPWM3

Committer:
benkatz
Date:
Fri Feb 05 00:52:53 2016 +0000
Revision:
0:4e1c4df6aabd
Child:
1:b8bceb4daed5
It works!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
benkatz 0:4e1c4df6aabd 1
benkatz 0:4e1c4df6aabd 2 #include "mbed.h"
benkatz 0:4e1c4df6aabd 3 #include "PositionSensor.h"
benkatz 0:4e1c4df6aabd 4 #include <math.h>
benkatz 0:4e1c4df6aabd 5
benkatz 0:4e1c4df6aabd 6
benkatz 0:4e1c4df6aabd 7
benkatz 0:4e1c4df6aabd 8 PositionSensorEncoder::PositionSensorEncoder(int CPR, float offset) {
benkatz 0:4e1c4df6aabd 9 _CPR = CPR;
benkatz 0:4e1c4df6aabd 10 _offset = offset;
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 0:4e1c4df6aabd 28 TIM3->CCMR1 = 0xf1f1; // CC1S='01' CC2S='01' < TIM capture/compare mode register 1
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 0:4e1c4df6aabd 36 ZPulse = new InterruptIn(PB_0);
benkatz 0:4e1c4df6aabd 37 ZSense = new DigitalIn(PB_0);
benkatz 0:4e1c4df6aabd 38 ZPulse->enable_irq();
benkatz 0:4e1c4df6aabd 39 ZPulse->rise(this, &PositionSensorEncoder::ZeroEncoderCount);
benkatz 0:4e1c4df6aabd 40 ZPulse->mode(PullDown);
benkatz 0:4e1c4df6aabd 41 //TIM5->CCMR1 = 0xf1f1; // CC1S='01' CC2S='01' < TIM capture/compare mode register 1
benkatz 0:4e1c4df6aabd 42
benkatz 0:4e1c4df6aabd 43
benkatz 0:4e1c4df6aabd 44 ZTest = new DigitalOut(PC_2);
benkatz 0:4e1c4df6aabd 45 ZTest->write(1);
benkatz 0:4e1c4df6aabd 46 //int state = 0;
benkatz 0:4e1c4df6aabd 47
benkatz 0:4e1c4df6aabd 48
benkatz 0:4e1c4df6aabd 49 }
benkatz 0:4e1c4df6aabd 50
benkatz 0:4e1c4df6aabd 51 float PositionSensorEncoder::GetMechPosition() { //returns rotor angle in radians.
benkatz 0:4e1c4df6aabd 52 int raw = TIM3->CNT-0x8000;
benkatz 0:4e1c4df6aabd 53 if (raw < 0) raw += _CPR;
benkatz 0:4e1c4df6aabd 54 if (raw >= _CPR) raw -= _CPR;
benkatz 0:4e1c4df6aabd 55 return 6.28318530718f*(raw)/(float)_CPR + _offset;
benkatz 0:4e1c4df6aabd 56 }
benkatz 0:4e1c4df6aabd 57
benkatz 0:4e1c4df6aabd 58 float PositionSensorEncoder::GetElecPosition() { //returns rotor electrical angle in radians.
benkatz 0:4e1c4df6aabd 59 int raw = TIM3->CNT-0x8000;
benkatz 0:4e1c4df6aabd 60 if (raw < 0) raw += _CPR;
benkatz 0:4e1c4df6aabd 61 if (raw >= _CPR) raw -= _CPR;
benkatz 0:4e1c4df6aabd 62 float signed_elec = fmod((7.0f*(6.28318530718f*(raw)/(float)_CPR + _offset)), 6.28318530718f);
benkatz 0:4e1c4df6aabd 63 //float signed_elec = (7*(6.28318530718*(TIM3->CNT-0x8000)/(float)_CPR + _offset));
benkatz 0:4e1c4df6aabd 64 if (signed_elec < 0){
benkatz 0:4e1c4df6aabd 65 return signed_elec + 6.28318530718f;
benkatz 0:4e1c4df6aabd 66 }
benkatz 0:4e1c4df6aabd 67 else{
benkatz 0:4e1c4df6aabd 68 return signed_elec;
benkatz 0:4e1c4df6aabd 69 }
benkatz 0:4e1c4df6aabd 70 }
benkatz 0:4e1c4df6aabd 71
benkatz 0:4e1c4df6aabd 72 void PositionSensorEncoder::ZeroEncoderCount(void){
benkatz 0:4e1c4df6aabd 73 if (ZSense->read() == 1){
benkatz 0:4e1c4df6aabd 74 if (ZSense->read() == 1){
benkatz 0:4e1c4df6aabd 75 TIM3->CNT=0x8000;
benkatz 0:4e1c4df6aabd 76 state = !state;
benkatz 0:4e1c4df6aabd 77 ZTest->write(state);
benkatz 0:4e1c4df6aabd 78 }
benkatz 0:4e1c4df6aabd 79 }
benkatz 0:4e1c4df6aabd 80 }