Modifying the HKCC for no readily apparent reason

Dependencies:   FastPWM3

Revision:
55:5afc2865ef7f
Parent:
51:6cd89bd6fcaa
Child:
57:57a108e15b52
--- a/main.cpp	Fri Feb 19 22:40:19 2021 -0800
+++ b/main.cpp	Thu Feb 25 21:53:26 2021 -0800
@@ -10,7 +10,7 @@
 #define SETUP_MODE 4
 #define ENCODER_MODE 5
 
-#define VERSION_NUM "1.9"
+#define VERSION_NUM "2.0"
 
 
 float __float_reg[64];                                                          // Floats stored in flash
@@ -32,6 +32,7 @@
 #include "PreferenceWriter.h"
 #include "CAN_com.h"
 #include "DRV.h"
+#include <cstdio>
  
 PreferenceWriter prefs(6);
 
@@ -39,7 +40,6 @@
 ControllerStruct controller;
 ObserverStruct observer;
 COMStruct com;
-Serial pc(PA_2, PA_3);
 
 
 CAN          can(PB_8, PB_9, 1000000);      // CAN Rx pin name, CAN Tx pin name
@@ -52,17 +52,23 @@
 //DigitalOut drv_en_gate(PA_11);
 DRV832x drv(&drv_spi, &drv_cs);
 
+static BufferedSerial pc{PA_2, PA_3, CONFIG_BAUD};
+FileHandle *mbed::mbed_override_console(int fd) {
+    return &pc;
+}
+
 PositionSensorAM5147 spi(16384, 0.0, NPP);  
 
-volatile int count = 0;
+// volatile int count{0};
 volatile int state = REST_MODE;
 volatile int state_change;
 
+
 void onMsgReceived() {
     //msgAvailable = true;
     printf("%df\n\r", rxMsg.id);
     can.read(rxMsg);  
-    if((rxMsg.id == CAN_ID)){
+    if(rxMsg.id == CAN_ID){
         controller.timeout = 0;
         if(((rxMsg.data[0]==0xFF) & (rxMsg.data[1]==0xFF) & (rxMsg.data[2]==0xFF) & (rxMsg.data[3]==0xFF) & (rxMsg.data[4]==0xFF) & (rxMsg.data[5]==0xFF) & (rxMsg.data[6]==0xFF) & (rxMsg.data[7]==0xFC))){
             state = MOTOR_MODE;
@@ -71,7 +77,7 @@
         else if(((rxMsg.data[0]==0xFF) & (rxMsg.data[1]==0xFF) & (rxMsg.data[2]==0xFF) & (rxMsg.data[3]==0xFF) * (rxMsg.data[4]==0xFF) & (rxMsg.data[5]==0xFF) & (rxMsg.data[6]==0xFF) & (rxMsg.data[7]==0xFD))){
             state = REST_MODE;
             state_change = 1;
-            gpio.led->write(0);; 
+            gpio.led->write(0);
             }
         else if(((rxMsg.data[0]==0xFF) & (rxMsg.data[1]==0xFF) & (rxMsg.data[2]==0xFF) & (rxMsg.data[3]==0xFF) * (rxMsg.data[4]==0xFF) & (rxMsg.data[5]==0xFF) & (rxMsg.data[6]==0xFF) & (rxMsg.data[7]==0xFE))){
             spi.ZeroPosition();
@@ -133,8 +139,8 @@
     drv.enable_gd();
     //gpio.enable->write(1);
     controller.ovp_flag = 0;
-    reset_foc(&controller);                                                     // Tesets integrators, and other control loop parameters
-    wait(.001);
+    reset_foc(&controller);  
+    ThisThread::sleep_for(1ms);                                                   // Tesets integrators, and other control loop parameters
     controller.i_d_ref = 0;
     controller.i_q_ref = 0;                                                     // Current Setpoints
     gpio.led->write(1);                                                     // Turn on status LED
@@ -148,8 +154,8 @@
     gpio.led->write(1);                                                    // Turn on status LED
     order_phases(&spi, &gpio, &controller, &prefs);                             // Check phase ordering
     calibrate(&spi, &gpio, &controller, &prefs);                                // Perform calibration procedure
-    gpio.led->write(0);;                                                     // Turn off status LED
-    wait(.2);
+    gpio.led->write(0);                                                     // Turn off status LED
+    ThisThread::sleep_for(200ms);
     printf("\n\r Calibration complete.  Press 'esc' to return to menu\n\r");
     drv.disable_gd();
     //gpio.enable->write(0);
@@ -159,14 +165,14 @@
 void print_encoder(void){
     printf(" Mechanical Angle:  %f    Electrical Angle:  %f    Raw:  %d\n\r", spi.GetMechPosition(), spi.GetElecPosition(), spi.GetRawPosition());
     //printf("%d\n\r", spi.GetRawPosition());
-    wait(.001);
+    ThisThread::sleep_for(1ms);
     }
 
 /// Current Sampling Interrupt ///
 /// This runs at 40 kHz, regardless of of the mode the controller is in ///
 extern "C" void TIM1_UP_TIM10_IRQHandler(void) {
   if (TIM1->SR & TIM_SR_UIF ) {
-
+        static volatile int count{0};                                           // Initialize count variable as static
         ///Sample current always ///
         ADC1->CR2  |= 0x40000000;                                               // Begin sample and conversion
         //volatile int delay;   
@@ -182,7 +188,6 @@
         controller.dtheta_elec = spi.GetElecVelocity();
         controller.v_bus = 0.95f*controller.v_bus + 0.05f*((float)controller.adc3_raw)*V_SCALE; //filter the dc link voltage measurement
         ///
-        
         /// Check state machine state, and run the appropriate function ///
         switch(state){
             case REST_MODE:                                                     // Do nothing
@@ -252,7 +257,7 @@
 /// Called when data received over serial ///
 void serial_interrupt(void){
     while(pc.readable()){
-        char c = pc.getc();
+        char c = getchar();
         if(c == 27){
                 state = REST_MODE;
                 state_change = 1;
@@ -336,7 +341,7 @@
                     cmd_val[char_count-1] = c;
                     
                 }
-                pc.putc(c);
+                putchar(c);
                 char_count++;
                 }
             }
@@ -363,8 +368,7 @@
     controller.v_bus = V_BUS;
     controller.mode = 0;
     Init_All_HW(&gpio);                                                         // Setup PWM, ADC, GPIO
-    wait(.1);
-    
+    ThisThread::sleep_for(100ms);
     gpio.enable->write(1);
     wait_us(100);
     drv.calibrate();
@@ -379,7 +383,7 @@
     zero_current(&controller.adc1_offset, &controller.adc2_offset);             // Measure current sensor zero-offset
     drv.disable_gd();
 
-    wait(.1);
+    ThisThread::sleep_for(100ms);
     /*
     gpio.enable->write(1);
     TIM1->CCR3 = 0x708*(1.0f);                        // Write duty cycles
@@ -392,7 +396,7 @@
     TIM1->CR1 ^= TIM_CR1_UDIS;
     //TIM1->CR1 |= TIM_CR1_UDIS; //enable interrupt
     
-    wait(.1);
+    ThisThread::sleep_for(100ms);
     NVIC_SetPriority(TIM1_UP_TIM10_IRQn, 2);                                             // commutation > communication
     
     NVIC_SetPriority(CAN1_RX0_IRQn, 3);
@@ -419,11 +423,10 @@
     memcpy(&lut, &ENCODER_LUT, sizeof(lut));
     spi.WriteLUT(lut);                                                          // Set potision sensor nonlinearity lookup table
     init_controller_params(&controller);
-
-    pc.baud(921600);                                                            // set serial baud rate
-    wait(.01);
-    pc.printf("\n\r\n\r HobbyKing Cheetah\n\r\n\r");
-    wait(.01);
+                                                           // set serial baud rate
+    ThisThread::sleep_for(10ms);
+    printf("\n\r\n\r HobbyKing Cheetah\n\r\n\r");
+    ThisThread::sleep_for(10ms);
     printf("\n\r Debug Info:\n\r");
     printf(" Firmware Version: %s\n\r", VERSION_NUM);
     printf(" ADC1 Offset: %d    ADC2 Offset: %d\n\r", controller.adc1_offset, controller.adc2_offset);
@@ -441,7 +444,7 @@
     //printf(" %d\n\r", drv.read_register(OCPCR));
     //drv.disable_gd();
     
-    pc.attach(&serial_interrupt);                                               // attach serial interrupt
+    pc.sigio(callback(serial_interrupt));                                               // attach serial interrupt
     
     state_change = 1;
 
@@ -449,8 +452,7 @@
     int counter = 0;
     while(1) {
         drv.print_faults();
-       wait(.1);
-       //printf("%.4f\n\r", controller.v_bus);
+        ThisThread::sleep_for(100ms);       //printf("%.4f\n\r", controller.v_bus);
        /*
         if(state == MOTOR_MODE)
         {