qiu dehua / Mbed 2 deprecated GT_MOTOR_24NM_V03_PT1000CalTemp

Dependencies:   mbed Hobbyking_Cheetah FastPWM3

Files at this revision

API Documentation at this revision

Comitter:
benkatz
Date:
Fri Feb 19 04:13:06 2016 +0000
Parent:
1:b8bceb4daed5
Child:
3:6a0015d88d06
Commit message:
now uses TIM1 for PWM - makes layout for micro-on-board version much easier

Changed in this revision

CurrentRegulator/CurrentRegulator.cpp Show annotated file Show diff for this revision Revisions of this file
Inverter/Inverter.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/CurrentRegulator/CurrentRegulator.cpp	Fri Feb 05 01:04:57 2016 +0000
+++ b/CurrentRegulator/CurrentRegulator.cpp	Fri Feb 19 04:13:06 2016 +0000
@@ -11,7 +11,7 @@
     _Inverter = inverter;
     PWM = new SPWM(inverter, 2.0);
     _PositionSensor = position_sensor;
-    IQ_Ref = .7;
+    IQ_Ref = 5;
     ID_Ref = 0;
     V_Q = 0;
     V_D = 0;
@@ -30,7 +30,7 @@
     Q_Integral = 0;
     D_Integral = 0;
     Int_Max = .9;
-    DTC_Max = .95;
+    DTC_Max = .97;
     //theta_elec = _PositionSensor->GetElecPosition();
 
     }
@@ -63,9 +63,9 @@
         D_Integral += D_Error*_Ki*_Kp;
         
         if (Q_Integral > Int_Max) Q_Integral = Int_Max;
-        else if(D_Integral < -Int_Max) D_Integral = Int_Max;
+        else if(Q_Integral < -Int_Max) Q_Integral = -Int_Max;
         if (D_Integral > Int_Max) D_Integral = Int_Max;
-        else if(Q_Integral < -Int_Max) Q_Integral = Int_Max;       
+        else if(D_Integral < -Int_Max) D_Integral = -Int_Max;       
          
         V_Q = Q_Integral + _Kp*Q_Error;
         V_D = D_Integral + _Kp*D_Error;
--- a/Inverter/Inverter.cpp	Fri Feb 05 01:04:57 2016 +0000
+++ b/Inverter/Inverter.cpp	Fri Feb 19 04:13:06 2016 +0000
@@ -9,33 +9,49 @@
     
     Enable = new DigitalOut(PinEnable);
     
-    RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN; // enable the clock to GPIOA
-    RCC->APB1ENR |= 0x00000001; // enable TIM2 clock
-    
+    RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN; // enable the clock to GPIOC
+    //RCC->APB1ENR |= 0x00000001; // enable TIM2 clock
+    RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; // enable TIM1 clock
+
     GPIOC->MODER = (1 << 8); // set pin 4 to be general purpose output
 
     PWM_A = new FastPWM(PinA);
     PWM_B = new FastPWM(PinB);
     PWM_C = new FastPWM(PinC);
 
-    TIM2->CR1 &= ~(TIM_CR1_CEN);
-    TIM2->CR1 |= TIM_CR1_CMS;
-    TIM2->CR1 |= TIM_CR1_CEN;
-     
+    //TIM2->CR1 &= ~(TIM_CR1_CEN);
+    //TIM2->CR1 |= TIM_CR1_CMS;
+    //TIM2->CR1 |= TIM_CR1_CEN;
+
     //PWM_A->period(Period);
     
+     //ISR Setup     
+    NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn);   //Enable TIM1 IRQ
+
+    TIM1->DIER |= TIM_DIER_UIE; // enable update interrupt
+    TIM1->CR1 = 0x40;//CMS = 10, interrupt only when counting up
+    TIM1->CR1 |= TIM_CR1_ARPE; // autoreload on, 
+    TIM1->RCR |= 0x001; // update event once per up/down count of tim1 
+    TIM1->EGR |= TIM_EGR_UG;
+ 
     //PWM Setup
-    TIM2->PSC = 0x0; // no prescaler, timer counts up in sync with the peripheral clock
-    TIM2->ARR = 0x8CA; // 
-    TIM2->CCER |= TIM_CCER_CC1NP;
-         
-    //ISR Setup     
-    NVIC->ISER[0] |= 1<< (TIM2_IRQn); // enable the TIM2 IRQ
-     
-    TIM2->DIER |= TIM_DIER_UIE; // enable update interrupt
-    TIM2->CR1 |= TIM_CR1_ARPE; // autoreload on, 
-    TIM2->EGR |= TIM_EGR_UG;   
 
+    TIM1->PSC = 0x0; // no prescaler, timer counts up in sync with the peripheral clock
+    TIM1->ARR = 0x1194; // 20 Khz
+    TIM1->CCER |= ~(TIM_CCER_CC1NP); //Interupt when low side is on.
+    TIM1->CR1 |= TIM_CR1_CEN;  
+    
+    //NVIC->ISER[0] |= 1<< (TIM2_IRQn); // enable the TIM2 IRQ    
+     //TIM2->DIER |= TIM_DIER_UIE; // enable update interrupt
+    //TIM2->CR1 |= TIM_CR1_ARPE; // autoreload on, 
+    //TIM2->EGR |= TIM_EGR_UG;               
+    //TIM2->PSC = 0x0; // no prescaler, timer counts up in sync with the peripheral clock
+    //TIM2->ARR = 0x8CA; // 
+    //TIM2->CCER |= TIM_CCER_CC1NP;
+      //TIM1->CR1 &= ~(TIM_CR1_CEN);
+    //TIM1->CR1 |= TIM_CR1_CMS;      
+
+    
     // ADC Setup
      RCC->APB2ENR |= RCC_APB2ENR_ADC2EN; // clock for ADC2
      RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; // clock for ADC1
@@ -60,9 +76,9 @@
     }
 
 void Inverter::SetDTC(float DTC_A, float DTC_B, float DTC_C){
-        PWM_A->write(DTC_A);
-        PWM_B->write(DTC_B);
-        PWM_C->write(DTC_C);
+        PWM_A->write(1.0f-DTC_A);
+        PWM_B->write(1.0f-DTC_B);
+        PWM_C->write(1.0f-DTC_C);
     }
 
 void Inverter::EnableInverter(){
--- a/main.cpp	Fri Feb 05 01:04:57 2016 +0000
+++ b/main.cpp	Fri Feb 19 04:13:06 2016 +0000
@@ -7,17 +7,20 @@
 #include "CurrentRegulator.h"
 
 PositionSensorEncoder encoder(8192,0);
-Inverter inverter(PA_5, PB_10, PB_3, PB_7, 0.02014160156, 0.00005);
+//Inverter inverter(PA_5, PB_10, PB_3, PB_7, 0.02014160156, 0.00005);
+Inverter inverter(PA_8, PA_9, PA_10, PB_7, 0.02014160156, 0.00005);
 CurrentRegulator foc(&inverter, &encoder, .005, .5);
 
 Ticker  testing;
 
+
 using namespace FastMath;
 using namespace Transforms;
 
 //float offset = 0;//-0.24;
 
 // Current Sampling IRQ
+/*
 extern "C" void TIM2_IRQHandler(void) {
   // flash on update event
   if (TIM2->SR & TIM_SR_UIF & TIM2->CNT>0x465) {
@@ -25,6 +28,15 @@
       }
   TIM2->SR = 0x0; // reset the status register
 }
+*/
+extern "C" void TIM1_UP_TIM10_IRQHandler(void) {
+  // toggle on update event
+  if (TIM1->SR & TIM_SR_UIF ) {
+      inverter.SampleCurrent();
+      }
+  TIM1->SR = 0x0; // reset the status register
+    //GPIOC->ODR ^= (1 << 4); //Toggle pin for debugging
+}
 
     
 void Loop(void){
@@ -44,9 +56,10 @@
     wait(1);
 
     testing.attach(&Loop, .0001);
+    //inverter.SetDTC(.2, .5, .97);
     //inverter.EnableInverter();
 
     while(1) {
-        
+
     }
 }