auto-measurements

Dependencies:   FastPWM3 mbed-dev

Fork of Hobbyking_Cheetah_Compact by Ben Katz

Committer:
benkatz
Date:
Tue Mar 29 01:05:46 2016 +0000
Revision:
7:dc5f27756e02
Parent:
6:4ee1cdc43aa8
Child:
8:10ae7bc88d6e
propper svm;

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 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 7:dc5f27756e02 32 TIM3->ARR = 0xfffffff; // reload at 0xfffffff < TIM auto-reload register
benkatz 0:4e1c4df6aabd 33
benkatz 7:dc5f27756e02 34 TIM3->CNT = 0x000; //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 7:dc5f27756e02 60 //ZPulse->fall(this, &PositionSensorEncoder::ZeroEncoderCountDown);
benkatz 0:4e1c4df6aabd 61 ZPulse->mode(PullDown);
benkatz 7:dc5f27756e02 62 flag = 0;
benkatz 0:4e1c4df6aabd 63
benkatz 0:4e1c4df6aabd 64
benkatz 1:b8bceb4daed5 65 //ZTest = new DigitalOut(PC_2);
benkatz 1:b8bceb4daed5 66 //ZTest->write(1);
benkatz 0:4e1c4df6aabd 67
benkatz 0:4e1c4df6aabd 68
benkatz 0:4e1c4df6aabd 69 }
benkatz 0:4e1c4df6aabd 70
benkatz 0:4e1c4df6aabd 71 float PositionSensorEncoder::GetMechPosition() { //returns rotor angle in radians.
benkatz 0:4e1c4df6aabd 72 int raw = TIM3->CNT-0x8000;
benkatz 7:dc5f27756e02 73 float unsigned_mech = (6.28318530718f/(float)_CPR) * (float) ((raw)%_CPR);
benkatz 7:dc5f27756e02 74 return (float) unsigned_mech;// + 6.28318530718f* (float) rotations;
benkatz 0:4e1c4df6aabd 75 }
benkatz 0:4e1c4df6aabd 76
benkatz 0:4e1c4df6aabd 77 float PositionSensorEncoder::GetElecPosition() { //returns rotor electrical angle in radians.
benkatz 7:dc5f27756e02 78
benkatz 7:dc5f27756e02 79 int raw = TIM3->CNT;
benkatz 7:dc5f27756e02 80 float unsigned_elec = (6.28318530718f/(float)_CPR) * (float) ((7*raw)%_CPR);
benkatz 7:dc5f27756e02 81 return unsigned_elec;
benkatz 0:4e1c4df6aabd 82 }
benkatz 0:4e1c4df6aabd 83
benkatz 3:6a0015d88d06 84 float PositionSensorEncoder::GetElecVelocity(){
benkatz 3:6a0015d88d06 85 float rawPeriod = TIM2->CCR1; //Clock Ticks
benkatz 3:6a0015d88d06 86 float dir = (((TIM3->CR1)>>4)&1)*2-1; // +/- 1
benkatz 3:6a0015d88d06 87 return dir*7*90000000.0f*(6.28318530718f/(float)_CPR)/rawPeriod;
benkatz 3:6a0015d88d06 88 }
benkatz 3:6a0015d88d06 89
benkatz 3:6a0015d88d06 90 float PositionSensorEncoder::GetMechVelocity(){
benkatz 3:6a0015d88d06 91 float rawPeriod = TIM2->CCR1; //Clock Ticks
benkatz 3:6a0015d88d06 92 float dir = -2.0f*(float)(((TIM3->CR1)>>4)&1)+1.0f; // +/- 1
benkatz 7:dc5f27756e02 93 return dir*90000000.0f*(6.28318530718f/(float)_CPR)/rawPeriod;
benkatz 3:6a0015d88d06 94 }
benkatz 3:6a0015d88d06 95
benkatz 0:4e1c4df6aabd 96 void PositionSensorEncoder::ZeroEncoderCount(void){
benkatz 7:dc5f27756e02 97 if (ZSense->read() == 1 & flag == 0){
benkatz 0:4e1c4df6aabd 98 if (ZSense->read() == 1){
benkatz 7:dc5f27756e02 99 GPIOC->ODR ^= (1 << 4);
benkatz 7:dc5f27756e02 100 dir = -2*((int)(((TIM3->CR1)-0x000)>>4)&1)+1;
benkatz 7:dc5f27756e02 101 int old_count = _CPR*rotations + TIM3->CNT;
benkatz 7:dc5f27756e02 102 if( abs(_CPR*(rotations+dir) - old_count) <= _CPR>>2){
benkatz 7:dc5f27756e02 103 rotations += dir;
benkatz 7:dc5f27756e02 104 }
benkatz 7:dc5f27756e02 105
benkatz 7:dc5f27756e02 106 TIM3->CNT = 0x000;
benkatz 7:dc5f27756e02 107
benkatz 1:b8bceb4daed5 108 //state = !state;
benkatz 1:b8bceb4daed5 109 //ZTest->write(state);
benkatz 7:dc5f27756e02 110 GPIOC->ODR ^= (1 << 4);
benkatz 7:dc5f27756e02 111 //flag = 1;
benkatz 7:dc5f27756e02 112 }
benkatz 7:dc5f27756e02 113 }
benkatz 7:dc5f27756e02 114 }
benkatz 7:dc5f27756e02 115
benkatz 7:dc5f27756e02 116 void PositionSensorEncoder::ZeroEncoderCountDown(void){
benkatz 7:dc5f27756e02 117 if (ZSense->read() == 0){
benkatz 7:dc5f27756e02 118 if (ZSense->read() == 0){
benkatz 7:dc5f27756e02 119 GPIOC->ODR ^= (1 << 4);
benkatz 7:dc5f27756e02 120 flag = 0;
benkatz 7:dc5f27756e02 121 float dir = -2.0f*(float)(((TIM3->CR1)>>4)&1)+1.0f;
benkatz 7:dc5f27756e02 122 if(dir != dir){
benkatz 7:dc5f27756e02 123 dir = dir;
benkatz 7:dc5f27756e02 124 rotations += dir;
benkatz 7:dc5f27756e02 125 }
benkatz 7:dc5f27756e02 126
benkatz 7:dc5f27756e02 127 GPIOC->ODR ^= (1 << 4);
benkatz 7:dc5f27756e02 128
benkatz 0:4e1c4df6aabd 129 }
benkatz 0:4e1c4df6aabd 130 }
benkatz 0:4e1c4df6aabd 131 }