Penn Electric Racing / Mbed 2 deprecated SystemManagement

Dependencies:   mbed CANBuffer Watchdog MODSERIAL mbed-rtos xbeeRelay IAP

Fork of SystemManagement by Martin Deng

Revision:
6:6a04210a3f4f
Parent:
5:9258b685fea6
Child:
7:5f6e31faa08e
--- a/SysMngmt.cpp	Wed Oct 08 20:56:41 2014 +0000
+++ b/SysMngmt.cpp	Fri Oct 10 20:24:22 2014 +0000
@@ -4,16 +4,20 @@
     Revised Sept 30, 2014: Began analyzing and commenting program, trying to figure out what the hell it does (Martin Deng)
 */
 
-#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 "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"
-#include"rtos.h"
+#include "mbed.h"
+#include "rtos.h"
+
+#include "Watchdog.cpp"
+#include "FanPump.h"
+#include "DC_DC.h"
 
 //Possible problems in IMD coz change of counter
 //Possible problems in BatteryStatus coz change in library
@@ -259,138 +263,21 @@
 
 CANBuffer rxBuffer(CAN1, MEDIUM);
 XBee250x XbeeTx;
+Serial pc1(USBTX,USBRX);
 
 char sys_src_id = 4;                // source address of system management
 
-char fan_id = 1;                     // first byte of CANData, last byte states the duty cycle, second-last states which fan to control
-
-Thread *fan_threads[4] = {NULL, NULL, NULL, NULL};     // Threads currently running on the 4 output pins
-
-/*
-    Pins for Fans and Pumps
-    PUMP_PWM = p2.0
-    FAN1_PWM = p2.1
-    FAN2_PWM = p2.2
-    FAN3_PWM = p2.3
-*/
-
-PwmOut pump_pwm(P2_0);
-PwmOut fan1_pwm(P2_1);
-PwmOut fan2_pwm(P2_2);
-PwmOut fan3_pwm(P2_3);
-
-char fan1_duty = 0;
-char fan2_duty = 0;
-char fan3_duty = 0;
-char pump_duty = 0;
-
-int tx_fan_id = ((int)sys_src_id << 8)&((int)fan_id);
-
-// duty goes from 0 to 100
-void rampFans(void const *arg){
-    
-    char *data = (char *)arg;
-    
-    // bounds checking
-    char duty = data[1];
-    if(duty > 100)
-        return;
-    
-    char pin_id = data[0];
-    if(pin_id > 3)
-        return;
-    
-    char *cur_duty;
-    PwmOut *out_pin;
-    
-    switch(pin_id){
-        case 0: cur_duty = &pump_duty;
-            out_pin = &pump_pwm;
-            break;
-        case 1: cur_duty = &fan1_duty;
-            out_pin = &fan1_pwm;
-            break;
-        case 2: cur_duty = &fan2_duty;
-            out_pin = &fan2_pwm;
-            break;
-        case 3: cur_duty = &fan3_duty;
-            out_pin = &fan3_pwm;
-            break;
-        default:
-            return;
-    }
-    
-    while(*cur_duty != duty){
-        
-        if(duty > *cur_duty){
-            *cur_duty += 10;
-            
-            if(*cur_duty > duty)
-                *cur_duty = duty;   
-        } else if(duty < *cur_duty){
-            *cur_duty -= 10;
-            
-            if(*cur_duty < duty)
-                *cur_duty = duty;   
-        }
-        
-        out_pin->write((*cur_duty)*0.01);
-        
-        Thread::wait(10);
-    }
-}
-
-void updateFans(void const *arg){
-    char data[4] = {0, 0, 0, 0};
-    while(1){
-        data[0] = pump_duty;
-        data[1] = fan1_duty;
-        data[2] = fan2_duty;
-        data[3] = fan3_duty;
-        CANMessage txMessage(tx_fan_id, data, 4);
-        rxBuffer.txWrite(txMessage);
-        Thread::wait(100);
-    }    
-}
-
-char dc_id = 0;                   // first byte of CANData, last byte states whether to toggle on (1) or off (0)
-DigitalOut dcPin(p20);
-bool dc_on = false;
-
-int tx_dc_id = ((int)sys_src_id << 8)&((int)dc_id);
-
-void toggleDC_DC(bool toggle){
-    
-    //dcPin turns on DC_DC converter when 0, off when 1
-    
-    if(toggle && !dc_on){
-        dcPin = 0;
-        dc_on = true;
-    } else if(!toggle && dc_on){
-        dcPin = 1;
-        dc_on = false;   
-    }
-}
-
-void updateDC(void const *arg){
-    char data[4] = {0, 0, 0, 0};
-    while(1){
-        data[0] = dc_on;
-        CANMessage txMessage(tx_dc_id, data, 4);
-        rxBuffer.txWrite(txMessage);
-        Thread::wait(100);
-    }  
-}
+Watchdog wdt;
 
 int main() {
     CANMessage rx_msg;    
-
-    //turn off DC-DC converter on startup
-    toggleDC_DC(false);
+    wdt.kick(10.0);
+    pc1.baud(115200);
 
-    //CANMessage update threads
-    Thread fan_update(updateFans);
-    Thread dc_update(updateDC);
+    FanPump fanPump(&rxBuffer);
+    DC dc_dc(&fanPump, &rxBuffer);
+    
+    fanPump.start_update();
 
     while(1)
     {
@@ -401,29 +288,17 @@
                 char cont_id = (rx_msg.id & 0x00FF);        // get bits 7:0
                 
                 // only control fans of dc_dc converter is on
-                if(cont_id == fan_id && dc_on)
+                if(cont_id == RX_FAN_ID && dc_dc.is_on())
                 {
-                    char duty = rx_msg.data[1];
-                    if(duty > 100)
-                        break;
-                    
-                    char pin_id = rx_msg.data[0];
-                    if(pin_id > 3)
-                        break;
-                
-                    if(fan_threads[pin_id] != NULL){
-                        fan_threads[pin_id]->terminate();
-                        free(fan_threads[pin_id]);   
-                    }
-                
-                    fan_threads[pin_id] = new Thread(rampFans, rx_msg.data);
+                    fanPump.set_fan((FanSelect)rx_msg.data[0], rx_msg.data[1]);
                 }
                 
-                if(cont_id == dc_id){
-                    toggleDC_DC(rx_msg.data[0]);
+                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
 }