robot

Dependencies:   FastPWM3 mbed

Committer:
bwang
Date:
Sun Oct 30 22:41:00 2016 +0000
Revision:
16:f283d6032fe5
Parent:
15:b583cd30b063
Child:
17:2b852039bb05
more structural changes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bwang 0:bac9c3a3a6ca 1 #include "mbed.h"
bwang 0:bac9c3a3a6ca 2 #include "math.h"
bwang 0:bac9c3a3a6ca 3 #include "PositionSensor.h"
bwang 0:bac9c3a3a6ca 4 #include "FastPWM.h"
bwang 15:b583cd30b063 5 #include "config_motor.h"
bwang 15:b583cd30b063 6 #include "config_loop.h"
bwang 15:b583cd30b063 7 #include "config_inverter.h"
dicarloj 13:41d102a53caf 8 #include "pwm_in.h"
bwang 0:bac9c3a3a6ca 9
bwang 1:7b61790f6be9 10 FastPWM *a;
bwang 1:7b61790f6be9 11 FastPWM *b;
bwang 1:7b61790f6be9 12 FastPWM *c;
bwang 0:bac9c3a3a6ca 13 DigitalOut en(EN);
bwang 16:f283d6032fe5 14 PwmIn throttle(TH_PIN, 1100, 1900);
bwang 0:bac9c3a3a6ca 15 PositionSensorEncoder pos(CPR, 0);
bwang 0:bac9c3a3a6ca 16
bwang 0:bac9c3a3a6ca 17 Serial pc(USBTX, USBRX);
bwang 0:bac9c3a3a6ca 18
bwang 1:7b61790f6be9 19 int adval1, adval2;
bwang 2:eabe8feaaabb 20 float ia, ib, ic, alpha, beta, d, q, vd, vq, p;
bwang 15:b583cd30b063 21 float p_mech, last_p_mech, w;
bwang 2:eabe8feaaabb 22
bwang 1:7b61790f6be9 23 float ia_supp_offset = 0.0f, ib_supp_offset = 0.0f; //current sensor offset due to bias resistor inaccuracies, etc (mV)
bwang 1:7b61790f6be9 24
bwang 2:eabe8feaaabb 25 float d_integral = 0.0f, q_integral = 0.0f;
bwang 2:eabe8feaaabb 26 float last_d = 0.0f, last_q = 0.0f;
bwang 14:59c4fcc1a4f7 27 float d_ref = 0.0f, q_ref = 0.0f;
bwang 2:eabe8feaaabb 28
bwang 16:f283d6032fe5 29 bool control_enabled = false;
dicarloj 13:41d102a53caf 30
bwang 4:a6669248ce4d 31 void commutate();
bwang 3:9b20da3f0055 32 void zero_current();
bwang 3:9b20da3f0055 33 void config_globals();
bwang 3:9b20da3f0055 34 void startup_msg();
bwang 2:eabe8feaaabb 35
bwang 16:f283d6032fe5 36 void go_enabled();
bwang 16:f283d6032fe5 37 void go_disabled();
bwang 16:f283d6032fe5 38 float fminf(float, float);
bwang 16:f283d6032fe5 39 float fmaxf(float, float);
dicarloj 13:41d102a53caf 40
bwang 1:7b61790f6be9 41 extern "C" void TIM1_UP_TIM10_IRQHandler(void) {
bwang 1:7b61790f6be9 42 if (TIM1->SR & TIM_SR_UIF ) {
bwang 4:a6669248ce4d 43 ADC1->CR2 |= 0x40000000;
bwang 4:a6669248ce4d 44 volatile int delay;
bwang 4:a6669248ce4d 45 for (delay = 0; delay < 35; delay++);
bwang 1:7b61790f6be9 46 adval1 = ADC1->DR;
bwang 1:7b61790f6be9 47 adval2 = ADC2->DR;
bwang 4:a6669248ce4d 48 commutate();
bwang 1:7b61790f6be9 49 }
bwang 1:7b61790f6be9 50 TIM1->SR = 0x00;
bwang 1:7b61790f6be9 51 }
bwang 1:7b61790f6be9 52
bwang 1:7b61790f6be9 53 void zero_current(){
bwang 1:7b61790f6be9 54 for (int i = 0; i < 1000; i++){
bwang 1:7b61790f6be9 55 ia_supp_offset += (float) (ADC1->DR);
bwang 1:7b61790f6be9 56 ib_supp_offset += (float) (ADC2->DR);
bwang 1:7b61790f6be9 57 ADC1->CR2 |= 0x40000000;
bwang 1:7b61790f6be9 58 wait_us(100);
bwang 1:7b61790f6be9 59 }
bwang 1:7b61790f6be9 60 ia_supp_offset /= 1000.0f;
bwang 1:7b61790f6be9 61 ib_supp_offset /= 1000.0f;
bwang 1:7b61790f6be9 62 ia_supp_offset = ia_supp_offset / 4096.0f * AVDD - I_OFFSET;
bwang 1:7b61790f6be9 63 ib_supp_offset = ib_supp_offset / 4096.0f * AVDD - I_OFFSET;
bwang 1:7b61790f6be9 64 }
bwang 0:bac9c3a3a6ca 65
bwang 0:bac9c3a3a6ca 66 void config_globals() {
bwang 0:bac9c3a3a6ca 67 pc.baud(115200);
bwang 0:bac9c3a3a6ca 68
bwang 1:7b61790f6be9 69 //Enable clocks for GPIOs
bwang 1:7b61790f6be9 70 RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
bwang 1:7b61790f6be9 71 RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN;
bwang 1:7b61790f6be9 72 RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN;
bwang 1:7b61790f6be9 73
bwang 1:7b61790f6be9 74 RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; //enable TIM1 clock
bwang 1:7b61790f6be9 75
bwang 1:7b61790f6be9 76 a = new FastPWM(PWMA);
bwang 1:7b61790f6be9 77 b = new FastPWM(PWMB);
bwang 1:7b61790f6be9 78 c = new FastPWM(PWMC);
bwang 1:7b61790f6be9 79
bwang 1:7b61790f6be9 80 NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn); //Enable TIM1 IRQ
bwang 1:7b61790f6be9 81
bwang 1:7b61790f6be9 82 TIM1->DIER |= TIM_DIER_UIE; //enable update interrupt
bwang 1:7b61790f6be9 83 TIM1->CR1 = 0x40; //CMS = 10, interrupt only when counting up
bwang 1:7b61790f6be9 84 TIM1->CR1 |= TIM_CR1_ARPE; //autoreload on,
bwang 1:7b61790f6be9 85 TIM1->RCR |= 0x01; //update event once per up/down count of tim1
bwang 1:7b61790f6be9 86 TIM1->EGR |= TIM_EGR_UG;
bwang 1:7b61790f6be9 87
bwang 1:7b61790f6be9 88 TIM1->PSC = 0x00; //no prescaler, timer counts up in sync with the peripheral clock
bwang 1:7b61790f6be9 89 TIM1->ARR = 0x4650; //5 Khz
bwang 1:7b61790f6be9 90 TIM1->CCER |= ~(TIM_CCER_CC1NP); //Interupt when low side is on.
bwang 1:7b61790f6be9 91 TIM1->CR1 |= TIM_CR1_CEN;
bwang 1:7b61790f6be9 92
bwang 1:7b61790f6be9 93 //ADC Setup
bwang 1:7b61790f6be9 94 RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; // clock for ADC1
bwang 1:7b61790f6be9 95 RCC->APB2ENR |= RCC_APB2ENR_ADC2EN; // clock for ADC2
bwang 1:7b61790f6be9 96
bwang 1:7b61790f6be9 97 ADC->CCR = 0x00000006; //Regular simultaneous mode, 3 channels
bwang 1:7b61790f6be9 98
bwang 1:7b61790f6be9 99 ADC1->CR2 |= ADC_CR2_ADON; //ADC1 on
bwang 1:7b61790f6be9 100 ADC1->SQR3 = 0x0000004; //PA_4 as ADC1, sequence 0
bwang 0:bac9c3a3a6ca 101
bwang 1:7b61790f6be9 102 ADC2->CR2 |= ADC_CR2_ADON; //ADC2 ON
bwang 1:7b61790f6be9 103 ADC2->SQR3 = 0x00000008; //PB_0 as ADC2, sequence 1
bwang 1:7b61790f6be9 104
bwang 1:7b61790f6be9 105 GPIOA->MODER |= (1 << 8);
bwang 1:7b61790f6be9 106 GPIOA->MODER |= (1 << 9);
bwang 1:7b61790f6be9 107
bwang 1:7b61790f6be9 108 GPIOA->MODER |= (1 << 2);
bwang 1:7b61790f6be9 109 GPIOA->MODER |= (1 << 3);
bwang 1:7b61790f6be9 110
bwang 1:7b61790f6be9 111 GPIOA->MODER |= (1 << 0);
bwang 1:7b61790f6be9 112 GPIOA->MODER |= (1 << 1);
bwang 1:7b61790f6be9 113
bwang 1:7b61790f6be9 114 GPIOB->MODER |= (1 << 0);
bwang 1:7b61790f6be9 115 GPIOB->MODER |= (1 << 1);
bwang 1:7b61790f6be9 116
bwang 1:7b61790f6be9 117 GPIOC->MODER |= (1 << 2);
bwang 1:7b61790f6be9 118 GPIOC->MODER |= (1 << 3);
bwang 1:7b61790f6be9 119
bwang 1:7b61790f6be9 120 //DAC setup
bwang 1:7b61790f6be9 121 RCC->APB1ENR |= 0x20000000;
bwang 1:7b61790f6be9 122 DAC->CR |= DAC_CR_EN2;
bwang 1:7b61790f6be9 123
bwang 1:7b61790f6be9 124 GPIOA->MODER |= (1 << 10);
bwang 1:7b61790f6be9 125 GPIOA->MODER |= (1 << 11);
bwang 1:7b61790f6be9 126
bwang 1:7b61790f6be9 127 //Zero duty cycles
bwang 1:7b61790f6be9 128 set_dtc(a, 0.0f);
bwang 1:7b61790f6be9 129 set_dtc(b, 0.0f);
bwang 1:7b61790f6be9 130 set_dtc(c, 0.0f);
bwang 1:7b61790f6be9 131
bwang 1:7b61790f6be9 132 wait_ms(250);
bwang 1:7b61790f6be9 133 zero_current();
bwang 15:b583cd30b063 134 p_mech = pos.GetMechPosition();
bwang 0:bac9c3a3a6ca 135 en = 1;
bwang 0:bac9c3a3a6ca 136 }
bwang 0:bac9c3a3a6ca 137
bwang 0:bac9c3a3a6ca 138 void startup_msg() {
bwang 0:bac9c3a3a6ca 139 pc.printf("%s\n\r\n\r", "FOC'ed in the Bot Rev A.");
bwang 0:bac9c3a3a6ca 140 pc.printf("%s\n\r", "====Config Data====");
bwang 0:bac9c3a3a6ca 141 pc.printf("Current Sensor Offset: %f mV\n\r", I_OFFSET);
bwang 0:bac9c3a3a6ca 142 pc.printf("Current Sensor Scale: %f mv/A\n\r", I_SCALE);
bwang 0:bac9c3a3a6ca 143 pc.printf("Bus Voltage: %f V\n\r", BUS_VOLTAGE);
bwang 9:074575151e4b 144 pc.printf("Pole pairs: %d\n\r", (int) POLE_PAIRS);
bwang 9:074575151e4b 145 pc.printf("Resolver lobes: %d\n\r", (int) RESOLVER_LOBES);
bwang 0:bac9c3a3a6ca 146 pc.printf("Loop KP: %f\n\r", KP);
bwang 0:bac9c3a3a6ca 147 pc.printf("Loop KI: %f\n\r", KI);
bwang 1:7b61790f6be9 148 pc.printf("Ia offset: %f mV\n\r", ia_supp_offset);
bwang 1:7b61790f6be9 149 pc.printf("Ib offset: %f mV\n\r", ib_supp_offset);
bwang 0:bac9c3a3a6ca 150 pc.printf("\n\r");
bwang 0:bac9c3a3a6ca 151 }
bwang 0:bac9c3a3a6ca 152
bwang 4:a6669248ce4d 153 void commutate() {
bwang 16:f283d6032fe5 154 if(control_enabled && !throttle.get_enabled()) go_disabled();
bwang 16:f283d6032fe5 155 if(!control_enabled && throttle.get_enabled()) go_enabled();
bwang 15:b583cd30b063 156
bwang 2:eabe8feaaabb 157 p = pos.GetElecPosition() - POS_OFFSET;
bwang 0:bac9c3a3a6ca 158 if (p < 0) p += 2 * PI;
bwang 0:bac9c3a3a6ca 159
bwang 15:b583cd30b063 160 last_p_mech = p_mech;
bwang 15:b583cd30b063 161 p_mech = pos.GetMechPosition();
bwang 15:b583cd30b063 162 float dp_mech = p_mech - last_p_mech;
bwang 15:b583cd30b063 163 if (dp_mech < 0.0f) dp_mech += 2 * PI;
bwang 15:b583cd30b063 164 if (dp_mech > 2 * PI) dp_mech -= 2 * PI;
bwang 15:b583cd30b063 165 float loop_period = (float) (TIM1->ARR) / 90.0f;
bwang 15:b583cd30b063 166 float w_raw = dp_mech * (float) 1e6 / loop_period; //rad/s
bwang 15:b583cd30b063 167 w = W_FILTER_STRENGTH * w + (1.0f - W_FILTER_STRENGTH) * w_raw;
bwang 15:b583cd30b063 168
bwang 16:f283d6032fe5 169 q_ref = throttle.get_throttle() * Q_MAX;
bwang 16:f283d6032fe5 170 d_ref = 0.0f;
bwang 16:f283d6032fe5 171
bwang 2:eabe8feaaabb 172 float sin_p = sinf(p);
bwang 2:eabe8feaaabb 173 float cos_p = cosf(p);
bwang 2:eabe8feaaabb 174
bwang 14:59c4fcc1a4f7 175 //float pos_dac = 0.85f * p / (2 * PI) + 0.05f; //uncomment me to write position to the DAC
bwang 4:a6669248ce4d 176 //DAC->DHR12R2 = (unsigned int) (pos_dac * 4096);
bwang 0:bac9c3a3a6ca 177
bwang 1:7b61790f6be9 178 ia = ((float) adval1 / 4096.0f * AVDD - I_OFFSET - ia_supp_offset) / I_SCALE;
bwang 1:7b61790f6be9 179 ib = ((float) adval2 / 4096.0f * AVDD - I_OFFSET - ib_supp_offset) / I_SCALE;
bwang 2:eabe8feaaabb 180 ic = -ia - ib;
bwang 0:bac9c3a3a6ca 181
bwang 10:f49df0fe0382 182 float u = CURRENT_U;
bwang 10:f49df0fe0382 183 float v = CURRENT_V;
bwang 2:eabe8feaaabb 184
bwang 2:eabe8feaaabb 185 alpha = u;
bwang 2:eabe8feaaabb 186 beta = 1 / sqrtf(3.0f) * u + 2 / sqrtf(3.0f) * v;
bwang 2:eabe8feaaabb 187
bwang 2:eabe8feaaabb 188 d = alpha * cos_p - beta * sin_p;
bwang 2:eabe8feaaabb 189 q = -alpha * sin_p - beta * cos_p;
bwang 2:eabe8feaaabb 190
bwang 3:9b20da3f0055 191 float d_err = d_ref - d;
bwang 3:9b20da3f0055 192 float q_err = q_ref - q;
bwang 2:eabe8feaaabb 193
bwang 2:eabe8feaaabb 194 d_integral += d_err * KI;
bwang 2:eabe8feaaabb 195 q_integral += q_err * KI;
bwang 2:eabe8feaaabb 196
bwang 2:eabe8feaaabb 197 if (q_integral > INTEGRAL_MAX) q_integral = INTEGRAL_MAX;
bwang 2:eabe8feaaabb 198 if (d_integral > INTEGRAL_MAX) d_integral = INTEGRAL_MAX;
bwang 2:eabe8feaaabb 199 if (q_integral < -INTEGRAL_MAX) q_integral = -INTEGRAL_MAX;
bwang 2:eabe8feaaabb 200 if (d_integral < -INTEGRAL_MAX) d_integral = -INTEGRAL_MAX;
bwang 2:eabe8feaaabb 201
bwang 14:59c4fcc1a4f7 202 if(control_enabled) {
dicarloj 13:41d102a53caf 203 vd = KP * d_err + d_integral;
dicarloj 13:41d102a53caf 204 vq = KP * q_err + q_integral;
bwang 14:59c4fcc1a4f7 205 } else {
dicarloj 13:41d102a53caf 206 vd = 0;
dicarloj 13:41d102a53caf 207 vq = 0;
dicarloj 13:41d102a53caf 208 }
bwang 2:eabe8feaaabb 209
bwang 2:eabe8feaaabb 210 if (vd < -1.0f) vd = -1.0f;
bwang 2:eabe8feaaabb 211 if (vd > 1.0f) vd = 1.0f;
bwang 2:eabe8feaaabb 212 if (vq < -1.0f) vq = -1.0f;
bwang 2:eabe8feaaabb 213 if (vq > 1.0f) vq = 1.0f;
bwang 2:eabe8feaaabb 214
bwang 15:b583cd30b063 215 //DAC->DHR12R2 = (unsigned int) (-q * 20 + 2048); //uncomment me to write I_q to the DAC
bwang 2:eabe8feaaabb 216 //DAC->DHR12R2 = (unsigned int) (-vd * 2000 + 2048);
bwang 2:eabe8feaaabb 217
bwang 14:59c4fcc1a4f7 218 //vd = 0.0f; //uncomment me for voltage mode testing
bwang 12:5723a4fa5864 219 //vq = 1.0f;
bwang 4:a6669248ce4d 220
bwang 2:eabe8feaaabb 221 float valpha = vd * cos_p - vq * sin_p;
bwang 2:eabe8feaaabb 222 float vbeta = vd * sin_p + vq * cos_p;
bwang 2:eabe8feaaabb 223
bwang 2:eabe8feaaabb 224 float va = valpha;
bwang 2:eabe8feaaabb 225 float vb = -0.5f * valpha - sqrtf(3) / 2.0f * vbeta;
bwang 2:eabe8feaaabb 226 float vc = -0.5f * valpha + sqrtf(3) / 2.0f * vbeta;
bwang 2:eabe8feaaabb 227
dicarloj 13:41d102a53caf 228 float voff = (fminf(va, fminf(vb, vc)) + fmaxf(va, fmaxf(vb, vc)))/2.0f;
dicarloj 13:41d102a53caf 229 va = va - voff;
dicarloj 13:41d102a53caf 230 vb = vb - voff;
dicarloj 13:41d102a53caf 231 vc = vc - voff;
dicarloj 13:41d102a53caf 232
bwang 2:eabe8feaaabb 233 set_dtc(a, 0.5f + 0.5f * va);
bwang 2:eabe8feaaabb 234 set_dtc(b, 0.5f + 0.5f * vb);
bwang 2:eabe8feaaabb 235 set_dtc(c, 0.5f + 0.5f * vc);
bwang 0:bac9c3a3a6ca 236 }
bwang 0:bac9c3a3a6ca 237
bwang 0:bac9c3a3a6ca 238 int main() {
bwang 0:bac9c3a3a6ca 239 config_globals();
bwang 0:bac9c3a3a6ca 240 startup_msg();
bwang 0:bac9c3a3a6ca 241
bwang 0:bac9c3a3a6ca 242 for (;;) {
bwang 12:5723a4fa5864 243 //pc.printf("%f\n\r", p);
bwang 12:5723a4fa5864 244 //wait(0.1);
bwang 0:bac9c3a3a6ca 245 }
bwang 0:bac9c3a3a6ca 246 }
bwang 16:f283d6032fe5 247
bwang 16:f283d6032fe5 248 void go_enabled() {
bwang 16:f283d6032fe5 249 d_integral = 0.0f;
bwang 16:f283d6032fe5 250 q_integral = 0.0f;
bwang 16:f283d6032fe5 251 control_enabled = true;
bwang 16:f283d6032fe5 252 en = 1;
bwang 16:f283d6032fe5 253 }
bwang 16:f283d6032fe5 254
bwang 16:f283d6032fe5 255 void go_disabled() {
bwang 16:f283d6032fe5 256 control_enabled = false;
bwang 16:f283d6032fe5 257 en = 0;
bwang 16:f283d6032fe5 258 }
bwang 16:f283d6032fe5 259
bwang 16:f283d6032fe5 260 float fminf(float a, float b) {
bwang 16:f283d6032fe5 261 if(a < b) return a;
bwang 16:f283d6032fe5 262 return b;
bwang 16:f283d6032fe5 263 }
bwang 16:f283d6032fe5 264
bwang 16:f283d6032fe5 265 float fmaxf(float a, float b) {
bwang 16:f283d6032fe5 266 if(a > b) return a;
bwang 16:f283d6032fe5 267 return b;
bwang 16:f283d6032fe5 268 }