Test the set param mode
PositionSensor/PositionSensor.cpp@8:10ae7bc88d6e, 2016-04-13 (annotated)
- Committer:
- benkatz
- Date:
- Wed Apr 13 04:09:56 2016 +0000
- Revision:
- 8:10ae7bc88d6e
- Parent:
- 7:dc5f27756e02
- Child:
- 9:d7eb815cb057
Multi-turn mechanical position, MA700 position sensing over SPI. Placeholder torque controller for cogging/ripple compensation;
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 | 8:10ae7bc88d6e | 6 | PositionSensorSPI::PositionSensorSPI(int CPR, float offset){ |
benkatz | 8:10ae7bc88d6e | 7 | //_CPR = CPR; |
benkatz | 8:10ae7bc88d6e | 8 | _CPR = 2048; |
benkatz | 8:10ae7bc88d6e | 9 | _offset = offset; |
benkatz | 8:10ae7bc88d6e | 10 | rotations = 0; |
benkatz | 8:10ae7bc88d6e | 11 | spi = new SPI(PC_12, PC_11, PC_10); |
benkatz | 8:10ae7bc88d6e | 12 | spi->format(16, 0); |
benkatz | 8:10ae7bc88d6e | 13 | cs = new DigitalOut(PA_15); |
benkatz | 8:10ae7bc88d6e | 14 | cs->write(1); |
benkatz | 8:10ae7bc88d6e | 15 | |
benkatz | 8:10ae7bc88d6e | 16 | } |
benkatz | 8:10ae7bc88d6e | 17 | |
benkatz | 8:10ae7bc88d6e | 18 | float PositionSensorSPI::GetMechPosition(){ |
benkatz | 8:10ae7bc88d6e | 19 | cs->write(0); |
benkatz | 8:10ae7bc88d6e | 20 | int response = spi->write(0)>>4; |
benkatz | 8:10ae7bc88d6e | 21 | cs->write(1); |
benkatz | 8:10ae7bc88d6e | 22 | if(response - old_counts > _CPR/4){ |
benkatz | 8:10ae7bc88d6e | 23 | rotations -= 1; |
benkatz | 8:10ae7bc88d6e | 24 | } |
benkatz | 8:10ae7bc88d6e | 25 | else if (response - old_counts < -_CPR/4){ |
benkatz | 8:10ae7bc88d6e | 26 | rotations += 1; |
benkatz | 8:10ae7bc88d6e | 27 | } |
benkatz | 8:10ae7bc88d6e | 28 | old_counts = response; |
benkatz | 8:10ae7bc88d6e | 29 | MechPosition = (6.28318530718f * ((float) response+(_CPR*rotations)))/ (float)_CPR; |
benkatz | 8:10ae7bc88d6e | 30 | return MechPosition; |
benkatz | 8:10ae7bc88d6e | 31 | |
benkatz | 8:10ae7bc88d6e | 32 | } |
benkatz | 8:10ae7bc88d6e | 33 | |
benkatz | 8:10ae7bc88d6e | 34 | float PositionSensorSPI::GetElecPosition(){ |
benkatz | 8:10ae7bc88d6e | 35 | cs->write(0); |
benkatz | 8:10ae7bc88d6e | 36 | int response = spi->write(0)>>4; |
benkatz | 8:10ae7bc88d6e | 37 | cs->write(1); |
benkatz | 8:10ae7bc88d6e | 38 | float elec = ((6.28318530718f/(float)_CPR) * (float) ((7*response)%_CPR)) - _offset; |
benkatz | 8:10ae7bc88d6e | 39 | if(elec < 0) elec += 6.28318530718f; |
benkatz | 8:10ae7bc88d6e | 40 | return elec; |
benkatz | 8:10ae7bc88d6e | 41 | } |
benkatz | 8:10ae7bc88d6e | 42 | |
benkatz | 8:10ae7bc88d6e | 43 | |
benkatz | 0:4e1c4df6aabd | 44 | |
benkatz | 0:4e1c4df6aabd | 45 | PositionSensorEncoder::PositionSensorEncoder(int CPR, float offset) { |
benkatz | 0:4e1c4df6aabd | 46 | _CPR = CPR; |
benkatz | 0:4e1c4df6aabd | 47 | _offset = offset; |
benkatz | 3:6a0015d88d06 | 48 | MechPosition = 0; |
benkatz | 0:4e1c4df6aabd | 49 | |
benkatz | 0:4e1c4df6aabd | 50 | // Enable clock for GPIOA |
benkatz | 0:4e1c4df6aabd | 51 | __GPIOA_CLK_ENABLE(); //equivalent from hal_rcc.h |
benkatz | 0:4e1c4df6aabd | 52 | |
benkatz | 0:4e1c4df6aabd | 53 | 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 | 54 | 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 | 55 | GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR6 | GPIO_OSPEEDER_OSPEEDR7 ; //Low speed /*!< GPIO port output speed register, Address offset: 0x08 */ |
benkatz | 0:4e1c4df6aabd | 56 | 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 | 57 | GPIOA->AFR[0] |= 0x22000000 ; //AF02 for PA6 & PA7 /*!< GPIO alternate function registers, Address offset: 0x20-0x24 */ |
benkatz | 0:4e1c4df6aabd | 58 | GPIOA->AFR[1] |= 0x00000000 ; //nibbles here refer to gpio8..15 /*!< GPIO alternate function registers, Address offset: 0x20-0x24 */ |
benkatz | 0:4e1c4df6aabd | 59 | |
benkatz | 0:4e1c4df6aabd | 60 | // configure TIM3 as Encoder input |
benkatz | 0:4e1c4df6aabd | 61 | // Enable clock for TIM3 |
benkatz | 0:4e1c4df6aabd | 62 | __TIM3_CLK_ENABLE(); |
benkatz | 0:4e1c4df6aabd | 63 | |
benkatz | 0:4e1c4df6aabd | 64 | TIM3->CR1 = 0x0001; // CEN(Counter ENable)='1' < TIM control register 1 |
benkatz | 0:4e1c4df6aabd | 65 | TIM3->SMCR = TIM_ENCODERMODE_TI12; // SMS='011' (Encoder mode 3) < TIM slave mode control register |
benkatz | 1:b8bceb4daed5 | 66 | TIM3->CCMR1 = 0xf1f1; // CC1S='01' CC2S='01' < TIM capture/compare mode register 1, maximum digital filtering |
benkatz | 0:4e1c4df6aabd | 67 | TIM3->CCMR2 = 0x0000; // < TIM capture/compare mode register 2 |
benkatz | 0:4e1c4df6aabd | 68 | TIM3->CCER = 0x0011; // CC1P CC2P < TIM capture/compare enable register |
benkatz | 0:4e1c4df6aabd | 69 | TIM3->PSC = 0x0000; // Prescaler = (0+1) < TIM prescaler |
benkatz | 8:10ae7bc88d6e | 70 | TIM3->ARR = CPR-1; // reload at 0xfffffff < TIM auto-reload register |
benkatz | 0:4e1c4df6aabd | 71 | |
benkatz | 7:dc5f27756e02 | 72 | TIM3->CNT = 0x000; //reset the counter before we use it |
benkatz | 0:4e1c4df6aabd | 73 | |
benkatz | 3:6a0015d88d06 | 74 | // Extra Timer for velocity measurement |
benkatz | 3:6a0015d88d06 | 75 | /* |
benkatz | 3:6a0015d88d06 | 76 | __TIM2_CLK_ENABLE(); |
benkatz | 3:6a0015d88d06 | 77 | TIM3->CR2 = 0x030; //MMS = 101 |
benkatz | 3:6a0015d88d06 | 78 | |
benkatz | 3:6a0015d88d06 | 79 | TIM2->PSC = 0x03; |
benkatz | 3:6a0015d88d06 | 80 | //TIM2->CR2 |= TIM_CR2_TI1S; |
benkatz | 3:6a0015d88d06 | 81 | TIM2->SMCR = 0x24; //TS = 010 for ITR2, SMS = 100 (reset counter at edge) |
benkatz | 3:6a0015d88d06 | 82 | TIM2->CCMR1 = 0x3;// CC1S = 11, IC1 mapped on TRC |
benkatz | 3:6a0015d88d06 | 83 | |
benkatz | 3:6a0015d88d06 | 84 | //TIM2->CR2 |= TIM_CR2_TI1S; |
benkatz | 3:6a0015d88d06 | 85 | TIM2->CCER |= TIM_CCER_CC1P; |
benkatz | 3:6a0015d88d06 | 86 | //TIM2->CCER |= TIM_CCER_CC1NP; |
benkatz | 3:6a0015d88d06 | 87 | TIM2->CCER |= TIM_CCER_CC1E; |
benkatz | 3:6a0015d88d06 | 88 | |
benkatz | 3:6a0015d88d06 | 89 | |
benkatz | 3:6a0015d88d06 | 90 | TIM2->CR1 = 0x01; //CEN |
benkatz | 3:6a0015d88d06 | 91 | */ |
benkatz | 3:6a0015d88d06 | 92 | TIM3->CR1 = 0x01; // CEN |
benkatz | 8:10ae7bc88d6e | 93 | ZPulse = new InterruptIn(PC_4); |
benkatz | 8:10ae7bc88d6e | 94 | ZSense = new DigitalIn(PC_4); |
benkatz | 8:10ae7bc88d6e | 95 | //ZPulse = new InterruptIn(PB_0); |
benkatz | 8:10ae7bc88d6e | 96 | //ZSense = new DigitalIn(PB_0); |
benkatz | 0:4e1c4df6aabd | 97 | ZPulse->enable_irq(); |
benkatz | 0:4e1c4df6aabd | 98 | ZPulse->rise(this, &PositionSensorEncoder::ZeroEncoderCount); |
benkatz | 7:dc5f27756e02 | 99 | //ZPulse->fall(this, &PositionSensorEncoder::ZeroEncoderCountDown); |
benkatz | 0:4e1c4df6aabd | 100 | ZPulse->mode(PullDown); |
benkatz | 7:dc5f27756e02 | 101 | flag = 0; |
benkatz | 0:4e1c4df6aabd | 102 | |
benkatz | 0:4e1c4df6aabd | 103 | |
benkatz | 1:b8bceb4daed5 | 104 | //ZTest = new DigitalOut(PC_2); |
benkatz | 1:b8bceb4daed5 | 105 | //ZTest->write(1); |
benkatz | 0:4e1c4df6aabd | 106 | |
benkatz | 0:4e1c4df6aabd | 107 | |
benkatz | 0:4e1c4df6aabd | 108 | } |
benkatz | 0:4e1c4df6aabd | 109 | |
benkatz | 0:4e1c4df6aabd | 110 | float PositionSensorEncoder::GetMechPosition() { //returns rotor angle in radians. |
benkatz | 8:10ae7bc88d6e | 111 | int raw = TIM3->CNT; |
benkatz | 7:dc5f27756e02 | 112 | float unsigned_mech = (6.28318530718f/(float)_CPR) * (float) ((raw)%_CPR); |
benkatz | 7:dc5f27756e02 | 113 | return (float) unsigned_mech;// + 6.28318530718f* (float) rotations; |
benkatz | 0:4e1c4df6aabd | 114 | } |
benkatz | 0:4e1c4df6aabd | 115 | |
benkatz | 0:4e1c4df6aabd | 116 | float PositionSensorEncoder::GetElecPosition() { //returns rotor electrical angle in radians. |
benkatz | 7:dc5f27756e02 | 117 | int raw = TIM3->CNT; |
benkatz | 8:10ae7bc88d6e | 118 | float elec = ((6.28318530718f/(float)_CPR) * (float) ((7*raw)%_CPR)) - _offset; |
benkatz | 8:10ae7bc88d6e | 119 | if(elec < 0) elec += 6.28318530718f; |
benkatz | 8:10ae7bc88d6e | 120 | return elec; |
benkatz | 0:4e1c4df6aabd | 121 | } |
benkatz | 0:4e1c4df6aabd | 122 | |
benkatz | 3:6a0015d88d06 | 123 | float PositionSensorEncoder::GetElecVelocity(){ |
benkatz | 3:6a0015d88d06 | 124 | float rawPeriod = TIM2->CCR1; //Clock Ticks |
benkatz | 3:6a0015d88d06 | 125 | float dir = (((TIM3->CR1)>>4)&1)*2-1; // +/- 1 |
benkatz | 3:6a0015d88d06 | 126 | return dir*7*90000000.0f*(6.28318530718f/(float)_CPR)/rawPeriod; |
benkatz | 3:6a0015d88d06 | 127 | } |
benkatz | 3:6a0015d88d06 | 128 | |
benkatz | 3:6a0015d88d06 | 129 | float PositionSensorEncoder::GetMechVelocity(){ |
benkatz | 3:6a0015d88d06 | 130 | float rawPeriod = TIM2->CCR1; //Clock Ticks |
benkatz | 3:6a0015d88d06 | 131 | float dir = -2.0f*(float)(((TIM3->CR1)>>4)&1)+1.0f; // +/- 1 |
benkatz | 7:dc5f27756e02 | 132 | return dir*90000000.0f*(6.28318530718f/(float)_CPR)/rawPeriod; |
benkatz | 3:6a0015d88d06 | 133 | } |
benkatz | 3:6a0015d88d06 | 134 | |
benkatz | 0:4e1c4df6aabd | 135 | void PositionSensorEncoder::ZeroEncoderCount(void){ |
benkatz | 7:dc5f27756e02 | 136 | if (ZSense->read() == 1 & flag == 0){ |
benkatz | 0:4e1c4df6aabd | 137 | if (ZSense->read() == 1){ |
benkatz | 8:10ae7bc88d6e | 138 | GPIOC->ODR ^= (1 << 4); |
benkatz | 7:dc5f27756e02 | 139 | TIM3->CNT = 0x000; |
benkatz | 1:b8bceb4daed5 | 140 | //state = !state; |
benkatz | 1:b8bceb4daed5 | 141 | //ZTest->write(state); |
benkatz | 7:dc5f27756e02 | 142 | GPIOC->ODR ^= (1 << 4); |
benkatz | 7:dc5f27756e02 | 143 | //flag = 1; |
benkatz | 7:dc5f27756e02 | 144 | } |
benkatz | 7:dc5f27756e02 | 145 | } |
benkatz | 7:dc5f27756e02 | 146 | } |
benkatz | 7:dc5f27756e02 | 147 | |
benkatz | 7:dc5f27756e02 | 148 | void PositionSensorEncoder::ZeroEncoderCountDown(void){ |
benkatz | 7:dc5f27756e02 | 149 | if (ZSense->read() == 0){ |
benkatz | 7:dc5f27756e02 | 150 | if (ZSense->read() == 0){ |
benkatz | 7:dc5f27756e02 | 151 | GPIOC->ODR ^= (1 << 4); |
benkatz | 7:dc5f27756e02 | 152 | flag = 0; |
benkatz | 7:dc5f27756e02 | 153 | float dir = -2.0f*(float)(((TIM3->CR1)>>4)&1)+1.0f; |
benkatz | 7:dc5f27756e02 | 154 | if(dir != dir){ |
benkatz | 7:dc5f27756e02 | 155 | dir = dir; |
benkatz | 7:dc5f27756e02 | 156 | rotations += dir; |
benkatz | 7:dc5f27756e02 | 157 | } |
benkatz | 7:dc5f27756e02 | 158 | |
benkatz | 7:dc5f27756e02 | 159 | GPIOC->ODR ^= (1 << 4); |
benkatz | 7:dc5f27756e02 | 160 | |
benkatz | 0:4e1c4df6aabd | 161 | } |
benkatz | 0:4e1c4df6aabd | 162 | } |
benkatz | 0:4e1c4df6aabd | 163 | } |