Winnie Liu / Hobbyking_Cheetah_modify1105

Dependencies:   mbed-dev-f303 FastPWM3

Files at this revision

API Documentation at this revision

Comitter:
benkatz
Date:
Sat Mar 12 08:04:51 2016 +0000
Parent:
3:6a0015d88d06
Child:
5:51c6560bf624
Commit message:
serial debugging;

Changed in this revision

CurrentRegulator/CurrentRegulator.cpp Show annotated file Show diff for this revision Revisions of this file
CurrentRegulator/CurrentRegulator.h Show annotated file Show diff for this revision Revisions of this file
ImpedanceController/ImpedanceController.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
TorqueController/TorqueController.cpp Show annotated file Show diff for this revision Revisions of this file
TorqueController/TorqueController.h 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	Wed Mar 09 04:00:48 2016 +0000
+++ b/CurrentRegulator/CurrentRegulator.cpp	Sat Mar 12 08:04:51 2016 +0000
@@ -32,9 +32,16 @@
     Int_Max = .9;
     DTC_Max = .97;
     //theta_elec = _PositionSensor->GetElecPosition();
+    //pc = new Serial(PA_2, PA_3);
+    //pc->baud(115200);
 
     }
 
+void CurrentRegulator::SendSPI(){
+    
+    
+    }
+
 void CurrentRegulator::UpdateRef(float D, float Q){
     IQ_Ref = Q;
     ID_Ref = D;
@@ -51,7 +58,7 @@
     //IQ_Ref = -IQ_Ref;
     //    }
 
-    DAC->DHR12R1 = (int) (I_Q*490.648f) + 2048;
+    //DAC->DHR12R1 = (int) (I_Q*490.648f) + 2048;
     //DAC->DHR12R1 = (int) (I_Alpha*4096.0f) + 2048;
     }
     
@@ -78,8 +85,23 @@
     
     
 void CurrentRegulator::Commutate(){
+    //count += 1;
     theta_elec = _PositionSensor->GetElecPosition();
     SampleCurrent(); //Grab most recent current sample
     Update();   //Run control loop
     SetVoltage();   //Set inverter duty cycles
+    /*
+    if (count==500){
+        //printf("%d %d %d %d\n\r", (int) (I_Q*1000), (int) (I_D*1000), (int) (I_A*1000), int (I_B*1000));
+        pc->putc((unsigned char) (theta_elec*40.0f));
+        pc->putc((unsigned char) (I_A*100.0f+127));
+        pc->putc((unsigned char) (I_B*100.0f+127));
+        pc->putc((unsigned char) (I_Alpha*100.0f+127));
+        pc->putc((unsigned char) (I_Beta*100.0f+127));
+        pc->putc((unsigned char) (I_Q*100.0f+127));
+        pc->putc((unsigned char) (I_D*100.0f+127));
+        pc->putc((0xff));
+        count = 0;
+        }
+        */
     }
\ No newline at end of file
--- a/CurrentRegulator/CurrentRegulator.h	Wed Mar 09 04:00:48 2016 +0000
+++ b/CurrentRegulator/CurrentRegulator.h	Sat Mar 12 08:04:51 2016 +0000
@@ -15,10 +15,12 @@
         void SampleCurrent();
         void SetVoltage();
         void Update();
+        void SendSPI();
         Inverter* _Inverter;
         PositionSensor* _PositionSensor;
         SPWM* PWM;
-        int count;
+        //Serial* pc;
+        //int count;
             
     
     
--- a/ImpedanceController/ImpedanceController.h	Wed Mar 09 04:00:48 2016 +0000
+++ b/ImpedanceController/ImpedanceController.h	Sat Mar 12 08:04:51 2016 +0000
@@ -3,9 +3,10 @@
 
 class ImpedanceController{
     public:
-        
+        //CurrentRegulator();
+        virtual void SetImpedance(float K, float B, float );
     
     private:
-        float reference, K, B, output;   //Referene position (rad), motor stiffness (N-m/rad), motor damping (N-m*s/rad), output(N-m)
+        float reference, _K, _B, output;   //Referene position (rad), motor stiffness (N-m/rad), motor damping (N-m*s/rad), output(N-m)
     
     };
\ No newline at end of file
--- a/Inverter/Inverter.cpp	Wed Mar 09 04:00:48 2016 +0000
+++ b/Inverter/Inverter.cpp	Sat Mar 12 08:04:51 2016 +0000
@@ -99,13 +99,13 @@
 
 void Inverter::SampleCurrent(void){
  //   Dbg->write(1);
-    GPIOC->ODR ^= (1 << 4); //Toggle pin for debugging
+    //GPIOC->ODR ^= (1 << 4); //Toggle pin for debugging
     I_B = _I_Scale*((float) (ADC1->DR) -  I_B_Offset);
     I_C = _I_Scale*((float) (ADC2->DR)-  I_C_Offset);
     I_A = -I_B - I_C;
     //DAC->DHR12R1 = ADC2->DR; 
     //DAC->DHR12R1 = TIM3->CNT>>2;//ADC2->DR; // pass ADC -> DAC, also clears EOC flag
     ADC1->CR2  |= 0x40000000; 
-    GPIOC->ODR ^= (1 << 4); //toggle pin for debugging
+    //GPIOC->ODR ^= (1 << 4); //toggle pin for debugging
     }
     
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TorqueController/TorqueController.cpp	Sat Mar 12 08:04:51 2016 +0000
@@ -0,0 +1,24 @@
+
+//cogging torque and torque ripple compensation will go here later
+
+#include "CurrentRegulator.h"
+#include "TorqueController.h"
+
+TorqueController::TorqueController(float Kt, CurrentRegulator *regulator)
+    {
+    _CurrentRegulator = regulator;
+    _Kt = Kt;
+    
+    }
+
+void TorqueController::SetTorque(float torque)
+    {
+        SetCurrent(0, torque/_Kt);
+    }
+
+void TorqueController::SetCurrent(float Id, float Iq)
+    {
+        _CurrentRegulator->UpdateRef(Id, Iq);
+        _CurrentRegulator->Commutate();
+        
+    }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TorqueController/TorqueController.h	Sat Mar 12 08:04:51 2016 +0000
@@ -0,0 +1,17 @@
+#ifndef CURRENTRETULATOR_H
+#define CURRENTREGULATOR_H
+
+class TorqueController{
+public:
+    TorqueController(float Kt, CurrentRegulator *regulator);
+    virtual void SetTorque(float torque);
+
+private:    
+    virtual void SetCurrent(float Id, float Iq);
+    CurrentRegulator* _CurrentRegulator;
+    float _Kt;
+    };
+    
+    
+
+#endif
\ No newline at end of file
--- a/main.cpp	Wed Mar 09 04:00:48 2016 +0000
+++ b/main.cpp	Sat Mar 12 08:04:51 2016 +0000
@@ -14,10 +14,15 @@
 Ticker  testing;
 
 
+
+//SPI spi(PB_15, PB_14, PB_13);
+//GPIOB->MODER = (1 << 8); // set pin 4 to be general purpose output
+
+//DigitalOut chipselect(PB_12);
+
 using namespace FastMath;
 using namespace Transforms;
 
-//float offset = 0;//-0.24;
 
 // Current Sampling IRQ
 /*
@@ -60,7 +65,6 @@
        
 int main() {
     wait(1);
-
     testing.attach(&Loop, .0001);
     //testing.attach(&PrintStuff, .05);
     //inverter.SetDTC(.1, 0, 0);