auto-measurements

Dependencies:   FastPWM3 mbed-dev

Fork of Hobbyking_Cheetah_Compact by Ben Katz

Committer:
benkatz
Date:
Fri Feb 05 01:04:57 2016 +0000
Revision:
1:b8bceb4daed5
Parent:
0:4e1c4df6aabd
Child:
3:6a0015d88d06
Added some comments, cleaned things up a bit

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 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 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
benkatz 0:4e1c4df6aabd 42
benkatz 1:b8bceb4daed5 43 //ZTest = new DigitalOut(PC_2);
benkatz 1:b8bceb4daed5 44 //ZTest->write(1);
benkatz 0:4e1c4df6aabd 45
benkatz 0:4e1c4df6aabd 46
benkatz 0:4e1c4df6aabd 47 }
benkatz 0:4e1c4df6aabd 48
benkatz 0:4e1c4df6aabd 49 float PositionSensorEncoder::GetMechPosition() { //returns rotor angle in radians.
benkatz 0:4e1c4df6aabd 50 int raw = TIM3->CNT-0x8000;
benkatz 0:4e1c4df6aabd 51 if (raw < 0) raw += _CPR;
benkatz 0:4e1c4df6aabd 52 if (raw >= _CPR) raw -= _CPR;
benkatz 0:4e1c4df6aabd 53 return 6.28318530718f*(raw)/(float)_CPR + _offset;
benkatz 0:4e1c4df6aabd 54 }
benkatz 0:4e1c4df6aabd 55
benkatz 0:4e1c4df6aabd 56 float PositionSensorEncoder::GetElecPosition() { //returns rotor electrical angle in radians.
benkatz 0:4e1c4df6aabd 57 int raw = TIM3->CNT-0x8000;
benkatz 0:4e1c4df6aabd 58 if (raw < 0) raw += _CPR;
benkatz 0:4e1c4df6aabd 59 if (raw >= _CPR) raw -= _CPR;
benkatz 0:4e1c4df6aabd 60 float signed_elec = fmod((7.0f*(6.28318530718f*(raw)/(float)_CPR + _offset)), 6.28318530718f);
benkatz 0:4e1c4df6aabd 61 //float signed_elec = (7*(6.28318530718*(TIM3->CNT-0x8000)/(float)_CPR + _offset));
benkatz 0:4e1c4df6aabd 62 if (signed_elec < 0){
benkatz 0:4e1c4df6aabd 63 return signed_elec + 6.28318530718f;
benkatz 0:4e1c4df6aabd 64 }
benkatz 0:4e1c4df6aabd 65 else{
benkatz 0:4e1c4df6aabd 66 return signed_elec;
benkatz 0:4e1c4df6aabd 67 }
benkatz 0:4e1c4df6aabd 68 }
benkatz 0:4e1c4df6aabd 69
benkatz 0:4e1c4df6aabd 70 void PositionSensorEncoder::ZeroEncoderCount(void){
benkatz 0:4e1c4df6aabd 71 if (ZSense->read() == 1){
benkatz 0:4e1c4df6aabd 72 if (ZSense->read() == 1){
benkatz 0:4e1c4df6aabd 73 TIM3->CNT=0x8000;
benkatz 1:b8bceb4daed5 74 //state = !state;
benkatz 1:b8bceb4daed5 75 //ZTest->write(state);
benkatz 0:4e1c4df6aabd 76 }
benkatz 0:4e1c4df6aabd 77 }
benkatz 0:4e1c4df6aabd 78 }