System Management code

Dependencies:   mbed CANBuffer Watchdog MODSERIAL mbed-rtos xbeeRelay IAP

Fork of SystemManagement by Martin Deng

Files at this revision

API Documentation at this revision

Comitter:
martydd3
Date:
Tue Oct 21 23:44:15 2014 +0000
Parent:
11:1d086618dd18
Child:
13:fbd9b3f5a07c
Commit message:
Added Error update thread, changed CAN message IDs

Changed in this revision

DC_DC/DC_DC.cpp Show annotated file Show diff for this revision Revisions of this file
DC_DC/DC_DC.h Show annotated file Show diff for this revision Revisions of this file
FanPump/FanPump.cpp Show annotated file Show diff for this revision Revisions of this file
FanPump/FanPump.h Show annotated file Show diff for this revision Revisions of this file
Get_IMD/IMD.cpp Show annotated file Show diff for this revision Revisions of this file
Get_IMD/IMD.h Show annotated file Show diff for this revision Revisions of this file
PollSwitch/PollSwitch.cpp Show annotated file Show diff for this revision Revisions of this file
PollSwitch/PollSwitch.h Show annotated file Show diff for this revision Revisions of this file
SysMngmt.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/DC_DC/DC_DC.cpp	Sun Oct 19 22:50:30 2014 +0000
+++ b/DC_DC/DC_DC.cpp	Tue Oct 21 23:44:15 2014 +0000
@@ -28,10 +28,10 @@
 }
 
 void update_dcdc(const void *arg){
-    char data[4] = {0};
+    char data[1] = {0};
     while(1){
         data[0] = status;
-        CANMessage txMessage(TX_DC_DC_ID, data, 4);
+        CANMessage txMessage(TX_DC_DC_ID, data, 1);
         tx_DC_Buffer->txWrite(txMessage);
         
         Thread::wait(100);          //10 Hz update
--- a/DC_DC/DC_DC.h	Sun Oct 19 22:50:30 2014 +0000
+++ b/DC_DC/DC_DC.h	Tue Oct 21 23:44:15 2014 +0000
@@ -5,8 +5,8 @@
 #include "FanPump.h"
 #include "rtos.h"
 
-const int TX_DC_DC_ID = ((4 << 8) | 5);
-const int RX_DC_DC_ID = ((4 << 8) | 14);
+const int TX_DC_DC_ID = ((0x4 << 8) | 0x01);
+const int RX_DC_DC_ID = ((0x4 << 8) | 0x90);
 
 class DC{
 public:
--- a/FanPump/FanPump.cpp	Sun Oct 19 22:50:30 2014 +0000
+++ b/FanPump/FanPump.cpp	Tue Oct 21 23:44:15 2014 +0000
@@ -2,10 +2,10 @@
 #include "FanPump.h"
 
 PwmOut pwmPins[PIN_NUM] = { 
-    PwmOut(P2_0),         // pump
-    PwmOut(P2_1),         // fan 1
-    PwmOut(P2_2),         // fan 2
-    PwmOut(P2_3),         // fan 3
+    PwmOut(P2_0),         // pump 1
+    PwmOut(P2_1),         // pump 2
+    PwmOut(P2_2),         // fan 1
+    PwmOut(P2_3),         // fan 2
 };
 
 PinStatus pin_status[PIN_NUM];
@@ -13,8 +13,8 @@
 
 FanPump::FanPump(CANBuffer *can){
     for(int i = 0; i < PIN_NUM; i++){
-        pin_status[i].cur_duty = 0;
-        pin_status[i].new_duty = 0;
+        pin_status[i].cur_duty = 0.0;
+        pin_status[i].new_duty = 0.0;
         
         pin_status[i].pin = &pwmPins[i];
         pin_status[i].pin->period_ms(10);
@@ -29,72 +29,76 @@
 // this is not a member function. For some reason, thread does weird things
 // with functions in classes. Apparently pointers get messed up when pointing to
 // a function in a class
-void ramp_fan(void const *arg)
+void ramp_pin(void const *arg)
 {
     PinStatus *pin_stat = (PinStatus *)arg;
-    unsigned char *cur_duty = &(pin_stat->cur_duty);
-    unsigned char *new_duty = &(pin_stat->new_duty);
+    float *cur_duty = &(pin_stat->cur_duty);
+    float *new_duty = &(pin_stat->new_duty);
     
     while(*cur_duty != *new_duty)
     {
         if(*new_duty > *cur_duty){
-            *cur_duty += 1;
+            *cur_duty += 1.0;
             
             if(*cur_duty > *new_duty)
                 *cur_duty = *new_duty;   
         } else if(*new_duty < *cur_duty){
-            *cur_duty -= 1;
+            *cur_duty -= 1.0;
             
             if(*cur_duty < *new_duty)
                 *cur_duty = *new_duty;   
         }
         
-        pin_stat->pin->write((*cur_duty)*0.01);
+        pin_stat->pin->write((*cur_duty));
         
         Thread::wait(5);    //1% duty cycle per 0.005 s
     }
 }
 
-void FanPump::set_fan(FanSelect fan, unsigned char duty){
-    if((int)fan >= PIN_NUM || duty > 100)
+void FanPump::set_fan_pump(FanPumpSelect fan_pump, float duty){
+    if((int)fan_pump >= PIN_NUM || duty > 100.0)
         return;
     
-    free_pin(fan);
+    free_pin(fan_pump);
 
-    pin_status[fan].new_duty = duty;
-    pin_threads[fan] = new Thread(ramp_fan, &pin_status[fan]);
+    pin_status[fan_pump].new_duty = duty;
+    pin_threads[fan_pump] = new Thread(ramp_pin, &pin_status[fan_pump]);
 }
 
-void FanPump::shutdown(FanSelect fan){
-    free_pin(fan);
+void FanPump::shutdown(FanPumpSelect fan_pump){
+    free_pin(fan_pump);
     
-    pin_status[fan].cur_duty = 0;
-    pin_status[fan].new_duty = 0;
-    pin_status[fan].pin->write(0);
+    pin_status[fan_pump].cur_duty = 0;
+    pin_status[fan_pump].new_duty = 0;
+    pin_status[fan_pump].pin->write(0);
 }
 
 void FanPump::shutdown_all(){
     for(int i = 0; i < PIN_NUM; i++)
-        FanPump::shutdown((FanSelect)i);    
+        FanPump::shutdown((FanPumpSelect)i);    
 }
 
-void FanPump::free_pin(FanSelect fan){
-    if(pin_threads[fan] != NULL){
-        pin_threads[fan]->terminate();
-        free(pin_threads[fan]);    
+void FanPump::free_pin(FanPumpSelect fan_pump){
+    if(pin_threads[fan_pump] != NULL){
+        pin_threads[fan_pump]->terminate();
+        free(pin_threads[fan_pump]);    
     }    
 }
 
-void update_fans(void const *arg){
-    char data[4] = {0};
+void update_fans_pumps(void const *arg){
+    char data[8] = {0};
     while(1){
-        for(int i = 0; i < PIN_NUM; i++)
-        {
-            data[i] = pin_status[i].cur_duty;
-        }
+        memcpy(&pin_status[0].cur_duty, data, sizeof(float));
+        memcpy(&pin_status[1].cur_duty, data+4, sizeof(float));
+        
+        CANMessage txPumpMessage(TX_PUMP_ID, data, 8);
+        tx_Fan_Buffer->txWrite(txPumpMessage);
         
-        CANMessage txMessage(TX_FAN_ID, data, 4);
-        tx_Fan_Buffer->txWrite(txMessage);
+        memcpy(&pin_status[2].cur_duty, data, sizeof(float));
+        memcpy(&pin_status[3].cur_duty, data+4, sizeof(float));
+        
+        CANMessage txFanMessage(TX_FAN_ID, data, 8);
+        tx_Fan_Buffer->txWrite(txFanMessage);
         
         Thread::wait(100);  // 10 Hz update    
     }    
@@ -102,5 +106,5 @@
 
 void FanPump::start_update()
 {
-    Thread update_thread(update_fans);
+    Thread update_thread(update_fans_pumps);
 }
\ No newline at end of file
--- a/FanPump/FanPump.h	Sun Oct 19 22:50:30 2014 +0000
+++ b/FanPump/FanPump.h	Tue Oct 21 23:44:15 2014 +0000
@@ -6,32 +6,34 @@
 #include "CANBuffer.h"
 
 typedef enum {
-    Pump = 0,
-    Fan1 = 1,
-    Fan2 = 2,
-    Fan3 = 3,
-} FanSelect;
+    Pump1 = 0,
+    Pump2 = 1,
+    Fan1 = 2,
+    Fan2 = 3,
+} FanPumpSelect;
 
 typedef struct{
-    unsigned char cur_duty;
-    unsigned char new_duty;
+    float cur_duty;
+    float new_duty;
     PwmOut *pin;    
 } PinStatus;
 
 const int PIN_NUM = 4;
-const int TX_FAN_ID = ((4 << 8) | 4);
-const int RX_FAN_ID = ((4 << 8) | 12);
+const int TX_FAN_ID = ((0x4 << 8) | 0x11);
+const int RX_FAN_ID = ((0x4 << 8) | 0x81);
+const int TX_PUMP_ID = ((0x4 << 8) | 0x10);
+const int RX_PUMP_ID = ((0x4 << 8) | 0x80);
 
 class FanPump{
 public:
     FanPump(CANBuffer *can);
-    void set_fan(FanSelect fan, unsigned char duty);
-    void shutdown(FanSelect fan);
+    void set_fan_pump(FanPumpSelect fan, float duty);
+    void shutdown(FanPumpSelect fan);
     void shutdown_all();
     void start_update();
     
 private:
     Thread *pin_threads[PIN_NUM];
-    void free_pin(FanSelect fan);
+    void free_pin(FanPumpSelect fan);
 };
 #endif
\ No newline at end of file
--- a/Get_IMD/IMD.cpp	Sun Oct 19 22:50:30 2014 +0000
+++ b/Get_IMD/IMD.cpp	Tue Oct 21 23:44:15 2014 +0000
@@ -35,7 +35,7 @@
 */
 void update_IMD(void const *arg){
     IMD *instance = (IMD *)arg;
-    char data[4] = {0};
+    char data[1] = {0};
     while(1){
         float freq = instance->frequency();
         float duty = instance->duty();
@@ -56,7 +56,7 @@
             data[0] = 6;
         }
         
-        CANMessage txMessage(TX_IMD_ID, data, 4);
+        CANMessage txMessage(TX_IMD_ID, data, 1);
         tx_IMD_Buffer->txWrite(txMessage);
         
         Thread::wait(100);  //10 Hz update    
--- a/Get_IMD/IMD.h	Sun Oct 19 22:50:30 2014 +0000
+++ b/Get_IMD/IMD.h	Tue Oct 21 23:44:15 2014 +0000
@@ -7,7 +7,7 @@
 #include "CANBuffer.h"
 #include "rtos.h"
 
-const int TX_IMD_ID = ((4 << 8) | 7);
+const int TX_IMD_ID = ((0x4 << 8) | 0x20);
 
 class IMD{
     public:
--- a/PollSwitch/PollSwitch.cpp	Sun Oct 19 22:50:30 2014 +0000
+++ b/PollSwitch/PollSwitch.cpp	Tue Oct 21 23:44:15 2014 +0000
@@ -47,12 +47,12 @@
 }
 
 void update_poll(const void *arg){
-    char data[4] = {0};
+    char data[2] = {0};
     while(1){
         converter.i = poll_switches();
         data[0] = converter.ch[0];
         data[1] = converter.ch[1];
-        CANMessage txMessage(TX_POLL_ID, data, 4);
+        CANMessage txMessage(TX_POLL_ID, data, 2);
         tx_Poll_Buffer->txWrite(txMessage);
         
         Thread::wait(100);      //10 Hz update
--- a/PollSwitch/PollSwitch.h	Sun Oct 19 22:50:30 2014 +0000
+++ b/PollSwitch/PollSwitch.h	Tue Oct 21 23:44:15 2014 +0000
@@ -6,7 +6,7 @@
 #include "CANBuffer.h"
 #include "rtos.h"
 
-const int TX_POLL_ID = ((4 << 8) | 6);
+const int TX_POLL_ID = ((4 << 8) | 0x02);
 
 class PollSwitch{
     public:
--- a/SysMngmt.cpp	Sun Oct 19 22:50:30 2014 +0000
+++ b/SysMngmt.cpp	Tue Oct 21 23:44:15 2014 +0000
@@ -19,12 +19,15 @@
 #include "CurrentMonitor.h"
 
 CANBuffer rxBuffer(CAN1, MEDIUM);
+Watchdog wdt;
 XBee250x XbeeTx;
 Serial pc1(USBTX,USBRX);
 
-char sys_src_id = 4;                // source address of system management
+char sys_src_id = 0x4;                // source address of system management
 char reset_id = 0x010;
 
+int SYSMGMT_ERROR_ID = 0x400;
+
 extern "C" void mbed_reset();
 
 void soft_reset(){
@@ -32,9 +35,22 @@
     mbed_reset();    
 }
 
+void error_update(void const *arg){
+    char data[1] = {0};
+    while(1){
+        data[0] = 0;
+        if(wdt.checkFlag())
+        {
+            data[0] |= 0x2;
+        }
+        CANMessage txErrorMessage(SYSMGMT_ERROR_ID, data, 1);
+        rxBuffer.txWrite(txErrorMessage);
+        Thread::wait(10);    
+    }
+}
+
 int main() {
     CANMessage rx_msg;   
-    Watchdog wdt;
      
     wdt.kick(10.0);
     pc1.baud(115200);
@@ -51,6 +67,8 @@
     imdMonitor.start_update();
     curMonitor.start_update();
 
+    Thread error_thread(error_update);
+
     while(1)
     {
         if(rxBuffer.rxRead(rx_msg)){
@@ -63,9 +81,26 @@
                 char cont_id = (rx_msg.id & 0x00FF);        // get bits 7:0
                 
                 // only control fans of dc_dc converter is on
+                if(cont_id == RX_PUMP_ID && dc_dc.is_on())
+                {
+                    float temp_float;
+                    
+                    memcpy(&rx_msg.data[0], &temp_float, sizeof(float));
+                    fanPump.set_fan_pump(Pump1, temp_float);
+                    
+                    memcpy(&rx_msg.data[4], &temp_float, sizeof(float));
+                    fanPump.set_fan_pump(Pump2, temp_float);
+                }
+                
                 if(cont_id == RX_FAN_ID && dc_dc.is_on())
                 {
-                    fanPump.set_fan((FanSelect)rx_msg.data[0], rx_msg.data[1]);
+                    float temp_float;
+                    
+                    memcpy(&rx_msg.data[0], &temp_float, sizeof(float));
+                    fanPump.set_fan_pump(Fan1, temp_float);
+                    
+                    memcpy(&rx_msg.data[4], &temp_float, sizeof(float));
+                    fanPump.set_fan_pump(Fan2, temp_float);
                 }
                 
                 if(cont_id == RX_DC_DC_ID){