Bayley Wang / Mbed 2 deprecated foc-ed_in_the_bot_compact

Dependencies:   FastPWM3 mbed

Files at this revision

API Documentation at this revision

Comitter:
bwang
Date:
Sat Nov 10 08:37:08 2018 +0000
Parent:
231:753ec371b153
Child:
233:2ca26ab47b4f
Commit message:
11/10/2018 03:36 - restructured code to allow for proper encoder zeroing - MODE_ZERO and MODE_CHR execute run-once functions when they are entered, then drop to MODE_CFG

Changed in this revision

CHANGELOG.txt Show annotated file Show diff for this revision Revisions of this file
Calibration/Calibration.cpp Show annotated file Show diff for this revision Revisions of this file
CommandProcessor/cmd_mode.cpp Show annotated file Show diff for this revision Revisions of this file
errors.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
main.h Show annotated file Show diff for this revision Revisions of this file
modes.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/CHANGELOG.txt	Sat Nov 10 06:50:53 2018 +0000
+++ b/CHANGELOG.txt	Sat Nov 10 08:37:08 2018 +0000
@@ -62,4 +62,5 @@
 11/10/2018 00:50 - moved RESOLVER_RESET pin to PC_0 to match new hardware
 11/10/2018 01:26 - renamed ia_supp_offset, ib_supp_offset to ad1, ad2_supp_offset to reflect the fact that they correspond to ADC channel offsets and not currents (which can be renamed in elsewhere)
 11/10/2018 01:32 - new pin mapping for new hardware
-11/10/2018 01:50 - swapped ia and ib (back to what they were before 11/10/2018)
\ No newline at end of file
+11/10/2018 01:50 - swapped ia and ib (back to what they were before 11/10/2018)
+11/10/2018 03:36 - restructured code to allow for proper encoder zeroing - MODE_ZERO and MODE_CHR execute run-once functions when they are entered, then drop to MODE_CFG
\ No newline at end of file
--- a/Calibration/Calibration.cpp	Sat Nov 10 06:50:53 2018 +0000
+++ b/Calibration/Calibration.cpp	Sat Nov 10 08:37:08 2018 +0000
@@ -9,6 +9,8 @@
 #include "derived.h"
 #include "prefs.h"
 
+#include "errors.h"
+
 //output is in modulation depth
 void abc(float theta, float vd, float vq, float *a, float *b, float *c) {
     float valpha, vbeta;
@@ -29,7 +31,12 @@
 }
 
 void calibrate_position(IOStruct *io) {
-    io->pc->printf("%s\n", "Starting calibration procedure");
+    if (!checks_passed()) {
+        io->pc->printf("%s\n", "Errors present, exiting");
+        return;
+    }
+    io->pc->putc(127);
+    io->pc->printf("%s\n", "Calibrating...");
     
     const int n = (int) (128);
     const int n2 = 10;
@@ -74,7 +81,6 @@
         //compensate for position rollover
         if (theta_actual - theta_last < -PI) rollover += 2 * PI;
         if (theta_actual - theta_last > PI) rollover -= 2 * PI;
-        io->pc->printf("%f,%f\n", theta_actual + rollover, theta_ref);
         
         theta_last = theta_actual;
         
@@ -96,7 +102,6 @@
         
         if (theta_actual - theta_last < -PI) rollover += 2 * PI;
         if (theta_actual - theta_last > PI) rollover -= 2 * PI;
-        io->pc->printf("%f,%f\n", theta_actual + rollover, theta_ref);
         
         theta_last = theta_actual;
         
@@ -108,5 +113,8 @@
         offset += (error_f[i] + error_b[n - 1 - i]) / (2.0f * n);
     }
     offset = fmodf(offset, 2 * PI);
-    io->pc->printf("Offset: %f\n", offset);
+    io->pc->printf("Done!\n");
+    io->pc->printf("Offset = %f\n", -offset);
+    io->pc->printf("Use 'flush' to save this value to flash\n");
+    _POS_OFFSET = -offset;
 }
\ No newline at end of file
--- a/CommandProcessor/cmd_mode.cpp	Sat Nov 10 06:50:53 2018 +0000
+++ b/CommandProcessor/cmd_mode.cpp	Sat Nov 10 08:37:08 2018 +0000
@@ -9,6 +9,8 @@
 #include "defaults.h"
 #include "derived.h"
 
+#include "Calibration.h"
+
 void cmd_setp(Serial *pc, char *buf) {
     if (BREMS_src == CMD_SRC_TERMINAL) control.user_cmd = atof(buf);
 }
@@ -24,7 +26,22 @@
         return;
     }
     BREMS_mode = n;
-    pc->printf("Set mode to %s\n", mode_to_str(n));
+    
+    //ZERO and CHR are special modes - they execute run-once functions
+    //and override commutation, then exit to CFG
+    if (BREMS_mode == MODE_ZERO) {
+        calibrate_position(&io);
+        control.user_cmd = 0.0f;
+        BREMS_mode = MODE_CFG;
+    }
+    else if (BREMS_mode == MODE_CHR) {
+        control.user_cmd = 0.0f;
+        BREMS_mode = MODE_CFG;
+    }
+    else
+    {
+        pc->printf("Set mode to %s\n", mode_to_str(n));
+    }
 }
 
 void cmd_src(Serial *pc, char *buf) {
--- a/errors.h	Sat Nov 10 06:50:53 2018 +0000
+++ b/errors.h	Sat Nov 10 08:37:08 2018 +0000
@@ -35,6 +35,6 @@
 extern int masks[32];
 
 void init_masks();
-
+bool checks_passed();
 
 #endif
\ No newline at end of file
--- a/main.cpp	Sat Nov 10 06:50:53 2018 +0000
+++ b/main.cpp	Sat Nov 10 08:37:08 2018 +0000
@@ -20,6 +20,8 @@
 #include "derived.h"
 #include "main.h"
 
+#include "errors.h"
+
 IOStruct io;
 ReadDataStruct read;
 FOCStruct foc;
@@ -109,11 +111,7 @@
     case MODE_RUN:
         break;
     case MODE_ZERO:
-        if (control.user_cmd > 0.5f) {
-            va = 0.9f; vb = -0.9f; vc = -0.9f;
-        } else {
-            va = 0.0f; vb = 0.0f; vc = 0.0f;
-        }
+        //duty cycles set by calibrate_position()
         break;
     case MODE_CHR:
         //i have no idea lol;
@@ -133,10 +131,12 @@
         va = 0.0f; vb = 0.0f; vc = 0.0f;
     }
     
-    //output to timers
-    set_dtc(io.a, 0.5f + 0.5f * va * LINEAR_MODULATION_MAX);
-    set_dtc(io.b, 0.5f + 0.5f * vb * LINEAR_MODULATION_MAX);
-    set_dtc(io.c, 0.5f + 0.5f * vc * LINEAR_MODULATION_MAX);
+    //output to timers (some of the calibration modes may want to override this)
+    if (!mode_overrides_timers()) {
+        set_dtc(io.a, 0.5f + 0.5f * va * LINEAR_MODULATION_MAX);
+        set_dtc(io.b, 0.5f + 0.5f * vb * LINEAR_MODULATION_MAX);
+        set_dtc(io.c, 0.5f + 0.5f * vc * LINEAR_MODULATION_MAX);
+    }
 }
 
 void slow_loop() {
--- a/main.h	Sat Nov 10 06:50:53 2018 +0000
+++ b/main.h	Sat Nov 10 08:37:08 2018 +0000
@@ -8,10 +8,10 @@
 void go_enabled();
 void go_disabled();
 bool is_driving();
-bool checks_passed();
 
 bool mode_enables_output();
 bool mode_enables_logging();
+bool mode_overrides_timers();
 
 void update_velocity();
 void log();
--- a/modes.cpp	Sat Nov 10 06:50:53 2018 +0000
+++ b/modes.cpp	Sat Nov 10 08:37:08 2018 +0000
@@ -24,4 +24,17 @@
     default:
         return false;
     }
+}
+
+bool mode_overrides_timers() {
+    switch(BREMS_mode) {
+    case MODE_ZERO:
+    case MODE_CHR:
+        return true;
+    case MODE_RUN:
+    case MODE_CFG:
+        return false;
+    default:
+        return false;
+    }
 }
\ No newline at end of file