N K / Mbed 2 deprecated foc-ed_in_the_bot_compact

Dependencies:   FastPWM3 mbed

Fork of foc-ed_in_the_bot_compact by Bayley Wang

Files at this revision

API Documentation at this revision

Comitter:
bwang
Date:
Tue May 17 06:44:09 2016 +0000
Parent:
4:a6669248ce4d
Child:
10:6829abb438fc
Commit message:
prepping for altermotter operation

Changed in this revision

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
config.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/PositionSensor/PositionSensor.cpp	Fri Mar 18 12:07:14 2016 +0000
+++ b/PositionSensor/PositionSensor.cpp	Tue May 17 06:44:09 2016 +0000
@@ -1,6 +1,7 @@
 
 #include "mbed.h"
 #include "PositionSensor.h"
+#include "config.h"
 #include <math.h>
 
 /*
@@ -8,8 +9,8 @@
  * offset: mechanical position offset in radians
  */
     
-PositionSensorEncoder::PositionSensorEncoder(int CPR, float offset) {
-    _CPR = CPR;
+PositionSensorEncoder::PositionSensorEncoder(int cpr, float offset) {
+    _cpr = cpr;
     _offset = offset;
     
     __GPIOA_CLK_ENABLE();
@@ -54,9 +55,9 @@
  
 float PositionSensorEncoder::GetMechPosition() {
     int raw = TIM2->CNT;
-    if (raw < 0) raw += _CPR;
-    if (raw >= _CPR) raw -= _CPR;
-    float signed_elec = fmod((1.0f * (6.28318530718f * (raw) / (float)_CPR + _offset)), 6.28318530718f);
+    if (raw < 0) raw += _cpr;
+    if (raw >= _cpr) raw -= _cpr;
+    float signed_elec = fmod(6.28318530718f * (raw) / (float)_cpr + _offset, 6.28318530718f);
     if (signed_elec < 0){
         return signed_elec + 6.28318530718f;
     }
@@ -71,9 +72,9 @@
  
 float PositionSensorEncoder::GetElecPosition() {
     int raw = TIM2->CNT;
-    if (raw < 0) raw += _CPR;
-    if (raw >= _CPR) raw -= _CPR;
-    float signed_elec = fmod((2.0f * (6.28318530718f * (raw) / (float)_CPR + _offset)), 6.28318530718f);
+    if (raw < 0) raw += _cpr;
+    if (raw >= _cpr) raw -= _cpr;
+    float signed_elec = fmod((POLE_PAIRS / RESOLVER_LOBES * (6.28318530718f * (raw) / (float)_cpr + _offset)), 6.28318530718f);
     if (signed_elec < 0) {
         return signed_elec + 6.28318530718f;
     } else {
--- a/PositionSensor/PositionSensor.h	Fri Mar 18 12:07:14 2016 +0000
+++ b/PositionSensor/PositionSensor.h	Tue May 17 06:44:09 2016 +0000
@@ -10,7 +10,7 @@
   
 class PositionSensorEncoder: public PositionSensor {
 public:
-    PositionSensorEncoder(int CPR, float offset);
+    PositionSensorEncoder(int cpr, float offset);
     virtual float GetMechPosition();
     virtual float GetElecPosition();
 private:
@@ -18,7 +18,7 @@
     DigitalIn *ZSense;
     DigitalOut *ZTest;
     void ZeroEncoderCount(void);
-    int _CPR;
+    int _cpr;
     int state;
     float _offset;
 };
--- a/config.h	Fri Mar 18 12:07:14 2016 +0000
+++ b/config.h	Tue May 17 06:44:09 2016 +0000
@@ -1,7 +1,10 @@
 #ifndef __CONFIG_H
 #define __CONFIG_H
 
-#define set_dtc(phase, value) *phase = 1.0f - (value)
+#define CPR 4096
+#define POS_OFFSET 5.3f
+#define POLE_PAIRS 3.0f
+#define RESOLVER_LOBES 3.0f
 
 #define PWMA PA_8
 #define PWMB PA_9
@@ -10,11 +13,6 @@
 
 #define IA PA_4
 #define IB PB_0
-#define TEST_DAC PA_5
-
-#define PI 3.141593f
-#define CPR 4096
-#define POS_OFFSET 5.3f
 
 #define I_SCALE_RAW 25.0f //mv/A
 #define R_UP 12000.0f //ohms
@@ -25,13 +23,16 @@
 #define I_OFFSET (AVDD * R_DOWN * R_UP / (R_DOWN * R_UP + R_BIAS * (R_DOWN + R_UP)))
 #define I_SCALE (R_BIAS * R_DOWN * I_SCALE_RAW / (R_DOWN * R_UP + R_BIAS * (R_DOWN + R_UP)))
 
+#define BUS_VOLTAGE 20.0f
 #define K_LOOP 1.0f
 #define KI_BASE 0.01f
-#define BUS_VOLTAGE 20.0f
 
 #define KP (K_LOOP / BUS_VOLTAGE)
 #define KI (KI_BASE * K_LOOP / BUS_VOLTAGE)
 
 #define INTEGRAL_MAX 1.0f
 
+#define set_dtc(phase, value) *phase = (value)
+#define PI 3.141593f
+
 #endif
\ No newline at end of file
--- a/main.cpp	Fri Mar 18 12:07:14 2016 +0000
+++ b/main.cpp	Tue May 17 06:44:09 2016 +0000
@@ -134,6 +134,8 @@
     pc.printf("Current Sensor Offset: %f mV\n\r", I_OFFSET);
     pc.printf("Current Sensor Scale: %f mv/A\n\r", I_SCALE);
     pc.printf("Bus Voltage: %f V\n\r", BUS_VOLTAGE);
+    pc.printf("Pole pairs: %d\n\r", (int) POLE_PAIRS);
+    pc.printf("Resolver lobes: %d\n\r", (int) RESOLVER_LOBES);
     pc.printf("Loop KP: %f\n\r", KP);
     pc.printf("Loop KI: %f\n\r", KI);
     pc.printf("Ia offset: %f mV\n\r", ia_supp_offset);
@@ -206,15 +208,5 @@
     startup_msg();
     
     for (;;) {
-        /*
-        q_ref = 0.0f;
-        wait(3);
-        toggle = state;
-        state = !state;
-        q_ref = -50.0f;
-        wait(3);
-        toggle = state;
-        state = !state;
-        */
     }
 }