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:
Sun Oct 19 22:46:46 2014 +0000
Parent:
9:ada056631cac
Child:
11:1d086618dd18
Commit message:
Copied Kiran's calculations for monitoring batttery currents; To do: Test on car, Xbees, 10 Hz Can Message (what data is needed?)

Changed in this revision

CANBuffer.lib Show annotated file Show diff for this revision Revisions of this file
CurrentMonitor/CurrentMonitor.cpp Show annotated file Show diff for this revision Revisions of this file
CurrentMonitor/CurrentMonitor.h Show annotated file Show diff for this revision Revisions of this file
DC_DC/DC_DC.cpp 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
Watchdog.lib Show annotated file Show diff for this revision Revisions of this file
--- a/CANBuffer.lib	Thu Oct 16 15:13:49 2014 +0000
+++ b/CANBuffer.lib	Sun Oct 19 22:46:46 2014 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/teams/Penn-Electric-Racing/code/CANBuffer/#36e62c1f7039
+http://developer.mbed.org/teams/Penn-Electric-Racing/code/CANBuffer/#110f268af846
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CurrentMonitor/CurrentMonitor.cpp	Sun Oct 19 22:46:46 2014 +0000
@@ -0,0 +1,68 @@
+#include "mbed.h"
+#include "CurrentMonitor.h"
+#include "Store_RTC.h"
+
+#define MSEC_HRS 2.77778e-7
+#define DC_DC_ISENSE_OFFSET_V 0.5
+#define DC_DC_ISENSE_INCREMENT 0.133
+
+#define BAT_ISENSE_OFFSET_V 1.65
+#define BAT_ISENSE_INCREMENT 0.5297
+
+CANBuffer *tx_Current_Buffer;
+
+double BATmA_Hr;
+float DCA_msec,BATA_msec;
+float Bat_I_Ratio,DC_I_Ratio;
+
+AnalogIn BatISense(p19);
+AnalogIn DCSense(p20);
+
+RTCStore store;
+
+union converter{
+    float data;
+    char ch[4];    
+} convert;
+
+CurrentMonitor::CurrentMonitor(CANBuffer *can){
+    tx_Current_Buffer = can;
+}
+
+void update_current(const void *arg){
+    char data[4] = {0};
+    while(1){
+        float bat_reading = store.read(1);
+        convert.data = bat_reading;
+        
+        data[0] = convert.ch[0];
+        data[1] = convert.ch[1];
+        data[2] = convert.ch[2];
+        data[3] = convert.ch[3];
+        
+        CANMessage txMessage(TX_CURRENT_ID, data, 4);
+        tx_Current_Buffer->txWrite(txMessage);
+        
+        Thread::wait(100);          //10 Hz update
+    }
+}
+
+void monitor_current(const void *arg){
+    while(1){
+        Bat_I_Ratio=BatISense.read();
+        BATA_msec=(((Bat_I_Ratio*3.3) - BAT_ISENSE_OFFSET_V)/BAT_ISENSE_INCREMENT);
+        BATmA_Hr+=(BATA_msec*MSEC_HRS);
+        store.write(BATmA_Hr,0);
+                
+        DC_I_Ratio=DCSense.read(); 
+        DCA_msec=(((DC_I_Ratio*3.3) - DC_DC_ISENSE_OFFSET_V)/DC_DC_ISENSE_INCREMENT);
+        store.write(DCA_msec,1);
+        
+        Thread::wait(1);            //1 Khz coulomb counter   
+    }   
+}
+
+void CurrentMonitor::start_update(){
+    Thread monitor_thread(monitor_current);
+    Thread update_thread(update_current);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CurrentMonitor/CurrentMonitor.h	Sun Oct 19 22:46:46 2014 +0000
@@ -0,0 +1,18 @@
+#ifndef _FILE_CURRENTMONITOR_H
+#define _FILE_CURRENTMONITOR_H
+
+#include "mbed.h"
+#include "rtos.h"
+#include "CANBuffer.h"
+
+const int TX_CURRENT_ID = ((4 << 8) | 3);
+
+class CurrentMonitor{
+public:
+    CurrentMonitor(CANBuffer *can);
+    void start_update(); 
+
+private:
+
+};
+#endif
\ No newline at end of file
--- a/DC_DC/DC_DC.cpp	Thu Oct 16 15:13:49 2014 +0000
+++ b/DC_DC/DC_DC.cpp	Sun Oct 19 22:46:46 2014 +0000
@@ -1,11 +1,12 @@
 #include "mbed.h"
 #include "DC_DC.h"
 
-DigitalOut dc_pin(p20);
 FanPump *fanPump;
 CANBuffer *tx_DC_Buffer;
 bool status;
 
+DigitalOut dc_pin(p20);
+
 DC::DC(FanPump *fp, CANBuffer *can){
     status = false;
     dc_pin = !status;
@@ -26,18 +27,17 @@
     dc_pin = !status;
 }
 
-void update(const void *arg){
+void update_dcdc(const void *arg){
     char data[4] = {0};
     while(1){
         data[0] = status;
         CANMessage txMessage(TX_DC_DC_ID, data, 4);
-        CANMessage msg(1);
-        tx_DC_Buffer->txWrite(msg);
+        tx_DC_Buffer->txWrite(txMessage);
         
         Thread::wait(100);          //10 Hz update
     }
 }
 
 void DC::start_update(){
-    Thread update_thread(update);
+    Thread update_thread(update_dcdc);
 }
\ No newline at end of file
--- a/SysMngmt.cpp	Thu Oct 16 15:13:49 2014 +0000
+++ b/SysMngmt.cpp	Sun Oct 19 22:46:46 2014 +0000
@@ -4,12 +4,18 @@
     Revised Sept 30, 2014: Began analyzing and commenting program, trying to figure out what the hell it does (Martin Deng)
 */
 
-#include "SysMngmt.h"
+//#include "SysMngmt.h"
+
+/*
 #include "Get_IMD.h"
 #include "PollSwitch.h"
 #include "TemperatureRead.h"
+*/
+
 #include "Store_RTC.h"
 #include "XBee_Lib.h"
+
+
 #include "CANBuffer.h"
 
 #include "mbed.h"
@@ -20,6 +26,68 @@
 #include "DC_DC.h"
 #include "PollSwitch.h"
 #include "IMD.h"
+#include "CurrentMonitor.h"
+
+CANBuffer rxBuffer(CAN1, MEDIUM);
+//XBee250x XbeeTx;
+Serial pc1(USBTX,USBRX);
+
+char sys_src_id = 4;                // source address of system management
+char reset_id = 0x010;
+
+extern "C" void mbed_reset();
+
+void soft_reset(){
+    //http://developer.mbed.org/forum/mbed/topic/890/   
+    mbed_reset();    
+}
+
+int main() {
+    CANMessage rx_msg;   
+    Watchdog wdt;
+     
+    wdt.kick(10.0);
+    pc1.baud(115200);
+
+    FanPump fanPump(&rxBuffer);
+    DC dc_dc(&fanPump, &rxBuffer);
+    PollSwitch pollSwitch(&rxBuffer);
+    IMD imdMonitor(&rxBuffer);
+    CurrentMonitor curMonitor(&rxBuffer);
+    
+    fanPump.start_update();
+    dc_dc.start_update();
+    pollSwitch.start_update();
+    imdMonitor.start_update();
+    curMonitor.start_update();
+
+    while(1)
+    {
+        if(rxBuffer.rxRead(rx_msg)){
+            if(rx_msg.id == reset_id)
+                soft_reset();
+            
+            char src_addr = (rx_msg.id & 0x0700) >> 8;      // get bits 10:8
+            
+            if(src_addr == sys_src_id){
+                char cont_id = (rx_msg.id & 0x00FF);        // get bits 7:0
+                
+                // only control fans of dc_dc converter is on
+                if(cont_id == RX_FAN_ID && dc_dc.is_on())
+                {
+                    fanPump.set_fan((FanSelect)rx_msg.data[0], rx_msg.data[1]);
+                }
+                
+                if(cont_id == RX_DC_DC_ID){
+                    dc_dc.set(rx_msg.data[0]);
+                }
+            } // check for correct src_addr
+        } // check CANBuffer
+        
+        wdt.kick();
+    } // main while loop
+}
+
 
 //Possible problems in IMD coz change of counter
 //Possible problems in BatteryStatus coz change in library
@@ -51,34 +119,10 @@
     
     Interrupt handler, This is probably linked to the Timer 2 interrupt request somewhere by the libraries
 
-extern "C" void TIMER2_IRQHandler(void)
-{
-    if((LPC_TIM2->IR & 0x01) == 0x01) // if MR0 interrupt
-    {
-        // This probably shouldn't be here, never have a printf() in an interrupt, locks up the main thread
-        // printf("Every 1ms\n\r");
-        
-        LPC_TIM2->IR |= 1 << 0; // Clear MR0 interrupt flag
-        
-        // gonna hope all these calculations are correct
-        // writes to RTC store, but there's no read code here
-        // 2 misleading things. First, RTC store writes to a general purpose register
-        // secondly, no code reads from this register in this .cpp, but maybe some other code does
-        
-        Bat_I_Ratio=BatISense.read();
-        BATA_msec=(((Bat_I_Ratio*3.3) - BAT_ISENSE_OFFSET_V)/BAT_ISENSE_INCREMENT);
-        BATmA_Hr+=(BATA_msec*MSEC_HRS);
-        store.write(BATmA_Hr,0);
-                
-        DC_I_Ratio=DCSense.read(); 
-        DCA_msec=(((DC_I_Ratio*3.3) - DC_DC_ISENSE_OFFSET_V)/DC_DC_ISENSE_INCREMENT);
-        store.write(DCA_msec,1);
-        
-        LPC_TIM2->TCR       |=  (1<<1);                     //Reset Timer1
-        LPC_TIM2->TCR       &=  ~(1<<1);                    //Re Enable Timer1   
-    }
-}    
+*/
 
+
+/*
     Appears to read a whole bunch of DigitalOut pins in void PollSwitch(), store the value in uint16_t Rxpoll,
     and write the result to the CAN bus
 
@@ -261,51 +305,3 @@
     Main Loop: Currently reads CANMessages from Can interface (Pins: rd = p30, td = p29)
     Send CANMessage data through XBee radio transmitters
 */
-
-
-CANBuffer rxBuffer(CAN1, MEDIUM);
-XBee250x XbeeTx;
-Serial pc1(USBTX,USBRX);
-
-char sys_src_id = 4;                // source address of system management
-
-int main() {
-    CANMessage rx_msg;   
-    Watchdog wdt;
-     
-    wdt.kick(10.0);
-    pc1.baud(115200);
-
-    FanPump fanPump(&rxBuffer);
-    DC dc_dc(&fanPump, &rxBuffer);
-    PollSwitch pollSwitch(&rxBuffer);
-    IMD imdMonitor(&rxBuffer);
-    
-    fanPump.start_update();
-    dc_dc.start_update();
-    pollSwitch.start_update();
-    imdMonitor.start_update();
-
-    while(1)
-    {
-        if(rxBuffer.rxRead(rx_msg)){
-            char src_addr = (rx_msg.id & 0x0700) >> 8;      // get bits 10:8
-            
-            if(src_addr == sys_src_id){
-                char cont_id = (rx_msg.id & 0x00FF);        // get bits 7:0
-                
-                // only control fans of dc_dc converter is on
-                if(cont_id == RX_FAN_ID && dc_dc.is_on())
-                {
-                    fanPump.set_fan((FanSelect)rx_msg.data[0], rx_msg.data[1]);
-                }
-                
-                if(cont_id == RX_DC_DC_ID){
-                    dc_dc.set(rx_msg.data[0]);
-                }
-            } // check for correct src_addr
-        } // check CANBuffer
-        
-        wdt.kick();
-    } // main while loop
-}
--- a/Watchdog.lib	Thu Oct 16 15:13:49 2014 +0000
+++ b/Watchdog.lib	Sun Oct 19 22:46:46 2014 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/teams/Penn-Electric-Racing/code/Watchdog/#80edf726eb04
+http://developer.mbed.org/teams/Penn-Electric-Racing/code/Watchdog/#390a291e8c4b