WANG YUCHAO / Mbed 2 deprecated Motor_DRV8323RH_for_20190

Dependencies:   mbed FastPWM3

Files at this revision

API Documentation at this revision

Comitter:
benkatz
Date:
Thu May 12 05:02:52 2016 +0000
Parent:
9:d7eb815cb057
Child:
11:c83b18d41e54
Commit message:
Now featuring hobbyking startup tone!

Changed in this revision

CurrentRegulator/CurrentRegulator.h 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
PositionSensor/PositionSensor.cpp Show annotated file Show diff for this revision Revisions of this file
PositionSensor/PositionSensor.h Show annotated file Show diff for this revision Revisions of this file
SVM/SVM.cpp Show annotated file Show diff for this revision Revisions of this file
Transforms/Transforms.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.h	Tue May 10 01:15:57 2016 +0000
+++ b/CurrentRegulator/CurrentRegulator.h	Thu May 12 05:02:52 2016 +0000
@@ -21,7 +21,7 @@
         Inverter* _Inverter;
         PositionSensor* _PositionSensor;
         SVM* PWM;
-        Serial* pc;
+        //Serial* pc;
         int count;
             
     
--- a/Inverter/Inverter.cpp	Tue May 10 01:15:57 2016 +0000
+++ b/Inverter/Inverter.cpp	Thu May 12 05:02:52 2016 +0000
@@ -58,6 +58,8 @@
      DAC->CR |= 0x00000001; // DAC control reg, both channels ON
      GPIOA->MODER |= 0x00000300; // PA04 as analog output   
      
+     
+     //Enabled pin must be on for current sensors to turn on
      EnableInverter();
      SetDTC(0.0f, 0.0f, 0.0f);
      wait(.2);
--- a/PositionSensor/PositionSensor.cpp	Tue May 10 01:15:57 2016 +0000
+++ b/PositionSensor/PositionSensor.cpp	Thu May 12 05:02:52 2016 +0000
@@ -3,9 +3,10 @@
 #include "PositionSensor.h"
 //#include <math.h>
 
-PositionSensorSPI::PositionSensorSPI(int CPR, float offset){
+PositionSensorSPI::PositionSensorSPI(int CPR, float offset, int ppairs){
     //_CPR = CPR;
-    _CPR = 2048;
+    _CPR = CPR;
+    _ppairs = ppairs;
     _offset = offset;
     rotations = 0;
     spi = new SPI(PC_12, PC_11, PC_10);
@@ -35,7 +36,7 @@
     cs->write(0);
     int response = spi->write(0)>>4;
     cs->write(1);
-    float elec = ((6.28318530718f/(float)_CPR) * (float) ((7*response)%_CPR)) - _offset;
+    float elec = ((6.28318530718f/(float)_CPR) * (float) ((_ppairs*response)%_CPR)) - _offset;
     if(elec < 0) elec += 6.28318530718f;
     return elec;
     }
@@ -51,7 +52,8 @@
     
 
     
-PositionSensorEncoder::PositionSensorEncoder(int CPR, float offset) {
+PositionSensorEncoder::PositionSensorEncoder(int CPR, float offset, int ppairs) {
+    _ppairs = ppairs;
     _CPR = CPR;
     _offset = offset;
     MechPosition = 0;
@@ -124,7 +126,7 @@
 
 float PositionSensorEncoder::GetElecPosition() {        //returns rotor electrical angle in radians.
     int raw = TIM3->CNT;
-    float elec = ((6.28318530718f/(float)_CPR) * (float) ((7*raw)%_CPR)) - _offset;
+    float elec = ((6.28318530718f/(float)_CPR) * (float) ((_ppairs*raw)%_CPR)) - _offset;
     if(elec < 0) elec += 6.28318530718f;
     return elec;
 }
@@ -132,7 +134,7 @@
 float PositionSensorEncoder::GetElecVelocity(){
     float rawPeriod = TIM2->CCR1; //Clock Ticks
     float  dir = (((TIM3->CR1)>>4)&1)*2-1;    // +/- 1
-    return dir*7*90000000.0f*(6.28318530718f/(float)_CPR)/rawPeriod;
+    return dir*_ppairs*90000000.0f*(6.28318530718f/(float)_CPR)/rawPeriod;
     }
     
 float PositionSensorEncoder::GetMechVelocity(){
--- a/PositionSensor/PositionSensor.h	Tue May 10 01:15:57 2016 +0000
+++ b/PositionSensor/PositionSensor.h	Thu May 12 05:02:52 2016 +0000
@@ -10,7 +10,7 @@
   
 class PositionSensorEncoder: public PositionSensor {
 public:
-    PositionSensorEncoder(int CPR, float offset);
+    PositionSensorEncoder(int CPR, float offset, int ppairs);
     virtual float GetMechPosition();
     virtual float GetElecPosition();
     virtual float GetMechVelocity();
@@ -21,21 +21,21 @@
     //DigitalOut *ZTest;
     virtual void ZeroEncoderCount(void);
     virtual void ZeroEncoderCountDown(void);
-    int _CPR, flag, rotations;
+    int _CPR, flag, rotations, _ppairs;
     //int state;
     float _offset, MechPosition, dir, test_pos, vel_old, out_old;
 };
 
 class PositionSensorSPI: public PositionSensor{
 public:
-    PositionSensorSPI(int CPR, float offset);
+    PositionSensorSPI(int CPR, float offset, int ppairs);
     virtual float GetMechPosition();
     virtual float GetElecPosition();
     virtual float GetMechVelocity();
     virtual void ZeroPosition();
 private:
     float _offset, MechPosition, MechOffset;
-    int _CPR, rotations, old_counts;
+    int _CPR, rotations, old_counts, _ppairs;
     SPI *spi;
     DigitalOut *cs;
 };
--- a/SVM/SVM.cpp	Tue May 10 01:15:57 2016 +0000
+++ b/SVM/SVM.cpp	Thu May 12 05:02:52 2016 +0000
@@ -10,6 +10,7 @@
     _V_Bus = V_Bus;
     }
 
+//sinusoidal PWM
 void SPWM::Update_DTC(float V_A, float V_B, float V_C){
     float DTC_A = V_A/_V_Bus + .5f;
     float DTC_B = V_B/_V_Bus + .5f;
@@ -29,6 +30,7 @@
     _V_Bus = V_Bus;
     }
     
+//space vector pwm (better bus utilization)
 void SVPWM::Update_DTC(float V_A, float V_B, float V_C){
     
     float Voff = (min(V_A, V_B, V_C) + max(V_A, V_B, V_C))/2.0f;
--- a/Transforms/Transforms.cpp	Tue May 10 01:15:57 2016 +0000
+++ b/Transforms/Transforms.cpp	Thu May 12 05:02:52 2016 +0000
@@ -9,8 +9,8 @@
     //float sine = sin(theta);
     float cosine = FastCos(theta);
     float sine = FastSin(theta);
-    *d = alpha*cosine - beta*sine;
-    *q = -beta*cosine - alpha*sine;
+    *d = alpha*cosine - beta*sine;      //This is a hack - effectively using -beta instead of beta
+    *q = -beta*cosine - alpha*sine;     //I think because I'm using pi as the d axis offset instead of zero, but I need to investigate more.
     //*d = alpha*cosine + beta*sine;
     //*q = beta*cosine - alpha*sine;
     //DAC->DHR12R1 = (int) (*q*49.648f) + 2048;
--- a/main.cpp	Tue May 10 01:15:57 2016 +0000
+++ b/main.cpp	Thu May 12 05:02:52 2016 +0000
@@ -25,13 +25,15 @@
 //Inverter inverter(PA_5, PB_10, PB_3, PB_7, 0.02014160156, 0.00005);
 Inverter inverter(PA_10, PA_9, PA_8, PA_11, 0.02014160156, 0.00005);  //hall motter
 //Inverter inverter(PA_10, PA_9, PA_8, PB_7, 0.01007080078, 0.00005);  //test motter
-PositionSensorSPI spi(2048, 2.75f);   ///1  I really need an eeprom or something to store this....
-//PositionSensorSPI spi(2048, 1.34f); ///2
-//PositionSensorSPI spi(2048, 1);
+//PositionSensorSPI spi(2048, 2.75f, 7);   ///1  I really need an eeprom or something to store this....
+//PositionSensorSPI spi(2048, 1.34f, 7); ///2
+PositionSensorSPI spi(2048, 3.0, 21);
 int motorID = 40; ///1
 //int motorID = 50;  ///2
 
-PositionSensorEncoder encoder(1024, 0);
+//PositionSensorEncoder encoder(1024, 0, 7);
+PositionSensorEncoder encoder(1024, 0, 21);
+
 
 CurrentRegulator foc(&inverter, &spi, .005, .5);  //hall sensor
 TorqueController torqueController(.031f, &foc);
@@ -89,6 +91,43 @@
 }
 
 
+void hk_start(void){
+    inverter.SetDTC(0, 0, 0);
+    inverter.EnableInverter();
+    for(int i = 0; i<120; i++){
+        torqueController.SetTorque(.4);
+        wait(0.000956);
+        torqueController.SetTorque(-.4);
+        wait(0.000956);
+        }
+    for(int i = 0; i<120; i++){
+        torqueController.SetTorque(.4);
+        wait(0.0008513);
+        torqueController.SetTorque(-.4);
+        wait(0.0008513);
+        }    
+    for(int i = 0; i<120; i++){
+        torqueController.SetTorque(.4);
+        wait(0.00075843);
+        torqueController.SetTorque(-.4);
+        wait(0.00075843);
+        }   
+        torqueController.SetTorque(0);
+    wait(.4);
+    for (int j = 0; j<3; j++){
+        for(int i = 0; i<120; i++){
+            torqueController.SetTorque(.4);
+            wait(0.000956);
+            torqueController.SetTorque(-.4);
+            wait(0.000956);
+            }   
+            torqueController.SetTorque(0);
+            wait(.2);
+
+        }
+    
+    }
+
 
 /*
 void voltage_foc(void){
@@ -167,8 +206,8 @@
          
 void Loop(void){
     
-    impedanceController.SetImpedance(cmd_float[1], cmd_float[2], cmd_float[0]);
-    //impedanceController.SetImpedance(-.4, -0.006, 0);
+    //impedanceController.SetImpedance(cmd_float[1], cmd_float[2], cmd_float[0]);
+    impedanceController.SetImpedance(-.04, -0.00, 0);
 
     count = count+1;
     
@@ -191,8 +230,8 @@
     //float position = encoder.GetElecPosition();
     //float position = encoder.GetMechPosition();
     //float m = spi.GetMechPosition();
-    float e = spi.GetElecPosition();
-    printf("%f\n\r", e);
+    //float e = spi.GetElecPosition();
+    //printf("%f\n\r", e);
     //printf("%f   %f   %f   %f \n\r", m, cmd_float[0], cmd_float[1], cmd_float[2]);
     //printf("%d   %d   %d\n\r", raw[0], raw[1], raw[2]);
     }
@@ -225,7 +264,8 @@
     wait(.1);
     inverter.SetDTC(0.2, 0.2, 0.2);
     inverter.EnableInverter();
-    foc.Reset();
+    hk_start();
+    //foc.Reset();
     testing.attach(&Loop, .0001);
     NVIC_SetPriority(TIM5_IRQn, 2);
     pc.baud(115200);