last working

Dependencies:   FastPWM3 mbed

Fork of foc-ed_in_the_bot_compact by Bayley Wang

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "math.h"
00003 #include "PositionSensor.h"
00004 #include "FastPWM.h"
00005 #include "Transforms.h"
00006 #include "config.h"
00007 
00008 FastPWM *a;
00009 FastPWM *b;
00010 FastPWM *c;
00011 DigitalOut en(EN);
00012 DigitalOut toggle(PC_10);
00013 
00014 PositionSensorEncoder pos(CPR, 0);
00015 
00016 Serial pc(USBTX, USBRX);
00017 
00018 int state = 0;
00019 int adval1, adval2;
00020 float ia, ib, ic, alpha, beta, d, q, vd, vq, p;
00021 
00022 float ia_supp_offset = 0.0f, ib_supp_offset = 0.0f; //current sensor offset due to bias resistor inaccuracies, etc (mV)
00023 
00024 float d_integral = 0.0f, q_integral = 0.0f;
00025 float last_d = 0.0f, last_q = 0.0f;
00026 float d_ref = -0.0f, q_ref = -50.0f;
00027 
00028 void commutate();
00029 void zero_current();
00030 void config_globals();
00031 void startup_msg();
00032 
00033 extern "C" void TIM1_UP_TIM10_IRQHandler(void) {
00034     if (TIM1->SR & TIM_SR_UIF ) {
00035         toggle = 1;
00036         ADC1->CR2  |= 0x40000000; 
00037         volatile int delay;
00038         for (delay = 0; delay < 35; delay++);
00039         toggle = 0;
00040         adval1 = ADC1->DR;
00041         adval2 = ADC2->DR;
00042         commutate();
00043     }
00044     TIM1->SR = 0x00;
00045 }
00046 
00047 void zero_current(){
00048     for (int i = 0; i < 1000; i++){
00049         ia_supp_offset += (float) (ADC1->DR);
00050         ib_supp_offset += (float) (ADC2->DR);
00051         ADC1->CR2  |= 0x40000000;
00052         wait_us(100); 
00053     }
00054     ia_supp_offset /= 1000.0f;
00055     ib_supp_offset /= 1000.0f;
00056     ia_supp_offset = ia_supp_offset / 4096.0f * AVDD - I_OFFSET;
00057     ib_supp_offset = ib_supp_offset / 4096.0f * AVDD - I_OFFSET;
00058 }
00059 
00060 void config_globals() {
00061     pc.baud(115200);
00062     
00063     //Enable clocks for GPIOs
00064     RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
00065     RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN;
00066     RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN;
00067     
00068     RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; //enable TIM1 clock
00069     
00070     a = new FastPWM(PWMA);
00071     b = new FastPWM(PWMB);
00072     c = new FastPWM(PWMC);
00073     
00074     NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn); //Enable TIM1 IRQ
00075 
00076     TIM1->DIER |= TIM_DIER_UIE; //enable update interrupt
00077     TIM1->CR1 = 0x40; //CMS = 10, interrupt only when counting up
00078     TIM1->CR1 |= TIM_CR1_ARPE; //autoreload on, 
00079     TIM1->RCR |= 0x01; //update event once per up/down count of tim1 
00080     TIM1->EGR |= TIM_EGR_UG;
00081     
00082     TIM1->PSC = 0x00; //no prescaler, timer counts up in sync with the peripheral clock
00083     TIM1->ARR = 0x4650; //5 Khz
00084     TIM1->CCER |= ~(TIM_CCER_CC1NP); //Interupt when low side is on.
00085     TIM1->CR1 |= TIM_CR1_CEN;
00086     
00087     //ADC Setup
00088     RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; // clock for ADC1
00089     RCC->APB2ENR |= RCC_APB2ENR_ADC2EN; // clock for ADC2
00090     
00091     ADC->CCR = 0x00000006; //Regular simultaneous mode, 3 channels
00092     
00093     ADC1->CR2 |= ADC_CR2_ADON; //ADC1 on
00094     ADC1->SQR3 = 0x0000004; //PA_4 as ADC1, sequence 0
00095     
00096     ADC2->CR2 |= ADC_CR2_ADON; //ADC2 ON
00097     ADC2->SQR3 = 0x00000008; //PB_0 as ADC2, sequence 1
00098     
00099     GPIOA->MODER |= (1 << 8);
00100     GPIOA->MODER |= (1 << 9);
00101     
00102     GPIOA->MODER |= (1 << 2);
00103     GPIOA->MODER |= (1 << 3);
00104     
00105     GPIOA->MODER |= (1 << 0);
00106     GPIOA->MODER |= (1 << 1);
00107     
00108     GPIOB->MODER |= (1 << 0);
00109     GPIOB->MODER |= (1 << 1);
00110     
00111     GPIOC->MODER |= (1 << 2);
00112     GPIOC->MODER |= (1 << 3);
00113     
00114     //DAC setup
00115     RCC->APB1ENR |= 0x20000000;
00116     DAC->CR |= DAC_CR_EN2;
00117     
00118     GPIOA->MODER |= (1 << 10);
00119     GPIOA->MODER |= (1 << 11);
00120     
00121     //Zero duty cycles
00122     set_dtc(a, 0.0f);
00123     set_dtc(b, 0.0f);
00124     set_dtc(c, 0.0f);
00125     
00126     wait_ms(250);
00127     zero_current();
00128     en = 1;
00129 }
00130 
00131 void startup_msg() {
00132     pc.printf("%s\n\r\n\r", "FOC'ed in the Bot Rev A.");
00133     pc.printf("%s\n\r", "====Config Data====");
00134     pc.printf("Current Sensor Offset: %f mV\n\r", I_OFFSET);
00135     pc.printf("Current Sensor Scale: %f mv/A\n\r", I_SCALE);
00136     pc.printf("Bus Voltage: %f V\n\r", BUS_VOLTAGE);
00137     pc.printf("Pole pairs: %d\n\r", (int) POLE_PAIRS);
00138     pc.printf("Resolver lobes: %d\n\r", (int) RESOLVER_LOBES);
00139     pc.printf("Loop KP: %f\n\r", KP);
00140     pc.printf("Loop KI: %f\n\r", KI);
00141     pc.printf("Ia offset: %f mV\n\r", ia_supp_offset);
00142     pc.printf("Ib offset: %f mV\n\r", ib_supp_offset);
00143     pc.printf("\n\r");
00144 }    
00145 
00146 void commutate() {
00147     p = pos.GetElecPosition() - POS_OFFSET;
00148     if (p < 0) p += 2 * PI;
00149     
00150     float sin_p = sinf(p);
00151     float cos_p = cosf(p);
00152     
00153     float pos_dac = 0.85f * p / (2 * PI) + 0.05f;
00154     DAC->DHR12R2 = (unsigned int) (pos_dac * 4096);
00155     
00156     ia = ((float) adval1 / 4096.0f * AVDD - I_OFFSET - ia_supp_offset) / I_SCALE;
00157     ib = ((float) adval2 / 4096.0f * AVDD - I_OFFSET - ib_supp_offset) / I_SCALE;
00158     ic = -ia - ib;
00159     
00160     float u = ia;
00161     float v = ib;
00162     
00163     alpha = u;
00164     beta = 1 / sqrtf(3.0f) * u + 2 / sqrtf(3.0f) * v;
00165     
00166     d = alpha * cos_p - beta * sin_p;
00167     q = -alpha * sin_p - beta * cos_p;
00168     
00169     float d_err = d_ref - d;
00170     float q_err = q_ref - q;
00171     
00172     d_integral += d_err * KI;
00173     q_integral += q_err * KI;
00174     
00175     if (q_integral > INTEGRAL_MAX) q_integral = INTEGRAL_MAX;
00176     if (d_integral > INTEGRAL_MAX) d_integral = INTEGRAL_MAX;
00177     if (q_integral < -INTEGRAL_MAX) q_integral = -INTEGRAL_MAX;
00178     if (d_integral < -INTEGRAL_MAX) d_integral = -INTEGRAL_MAX;
00179     
00180     vd = KP * d_err + d_integral;
00181     vq = KP * q_err + q_integral;
00182     
00183     if (vd < -1.0f) vd = -1.0f;
00184     if (vd > 1.0f) vd = 1.0f;
00185     if (vq < -1.0f) vq = -1.0f;
00186     if (vq > 1.0f) vq = 1.0f;
00187     
00188     //DAC->DHR12R2 = (unsigned int) (-q * 20 + 2048);
00189     //DAC->DHR12R2 = (unsigned int) (-vd * 2000 + 2048);
00190     
00191     //vd = 0.0f;
00192     //vq = -1.0f;
00193     
00194     float valpha = vd * cos_p - vq * sin_p;
00195     float vbeta = vd * sin_p + vq * cos_p;
00196     
00197     float va = valpha;
00198     float vb = -0.5f * valpha - sqrtf(3) / 2.0f * vbeta;
00199     float vc = -0.5f * valpha + sqrtf(3) / 2.0f * vbeta;
00200     
00201     set_dtc(a, 0.5f + 0.5f * va);
00202     set_dtc(b, 0.5f + 0.5f * vb);
00203     set_dtc(c, 0.5f + 0.5f * vc);
00204 }
00205     
00206 int main() {
00207     config_globals();
00208     startup_msg();
00209     
00210     for (;;) {
00211         //pc.printf("%f\r\n", p);
00212         //wait_ms(100);
00213     }
00214 }