Penn Electric Racing / Mbed 2 deprecated SystemManagement

Dependencies:   mbed CANBuffer Watchdog MODSERIAL mbed-rtos xbeeRelay IAP

Fork of SystemManagement by Martin Deng

Revision:
2:baeb80c778f7
Parent:
1:e02eb179aed3
Child:
3:1345f882d8f3
--- a/SysMngmt.cpp	Sat Oct 04 16:19:45 2014 +0000
+++ b/SysMngmt.cpp	Sun Oct 05 17:35:20 2014 +0000
@@ -12,10 +12,8 @@
 #include"XBee_Lib.h"
 #include"CANBuffer.h"
 
-
-RTCStore store;
-CANBuffer RecieveBuffer(CAN1, MEDIUM);
-XBee250x XbeeTx;
+#include"mbed.h"
+#include"rtos.h"
 
 //Possible problems in IMD coz change of counter
 //Possible problems in BatteryStatus coz change in library
@@ -32,25 +30,21 @@
 CAN interrupt Rx Interrupt
 Recieve CAN message into a buffer. Return
 Buffer values(as long as !empty) -> SD Card, Xbee -> remove element
-*/
 
-/*
 extern "C" void CAN_IRQHandler(void)
 {
     CANMessage Rxmsg;
     CAN_SysM.read(Rxmsg);
     RecieveBuffer.add(Rxmsg);
 }
-*/
 
-/*
     http://developer.mbed.org/users/AjK/notebook/getting-closer-to-the-hardware/
     
     extern "C" means this is linked assuming it's C code
     C++ linker appearently adds extra crap with the function arguments keeping functions of this kind from linking properly
     
     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
@@ -79,10 +73,9 @@
     }
 }    
 
-/*
     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
-*/
+
 void Poll()
 {
     uint16_t Rxpoll;
@@ -200,15 +193,14 @@
     CAN_SysM.write(Txmsg_DCA_msec);
 }    
 
-/*
     Activates a whole crapload of functions and pins on the chip
-*/
+
 void Init()
 {   
-    /* 
+    
         Timers to call various functions at different intervals
         These things behave weirdly when wait(ms) is involved. Probably have to rewrite
-    */
+
     
     //ReadIMD.attach(&IMD,0.1);
     //PollSDSwitch.attach(&Poll,0.1);
@@ -216,7 +208,7 @@
     //ReadTemperature.attach(&Temp,0.1);
     //ReadBatteryState.attach(&Battery,0.1);
     
-    /*
+
         Initialize Timer2 for Battery State
         
         LPC_SC                      0x400F C000 (System Control)
@@ -235,14 +227,13 @@
         ->MCR                       0x4009 0014 (Match Control Register) What to do when Match Register matches the Timer Counter
         |= (1<<0);                  0 Bit (Interrupt on MR0, interrupt generated when MR0 matches the value in TC)
         
-    */
+
     LPC_SC->PCONP       |= (1<<22);                     //PoewerOn Timer/Counter2
     LPC_SC->PCLKSEL1    |= ((1<<12) | (1<<13));         //Prescale Timer2 CCLK/8  
     LPC_TIM2->TCR       |=  (1<<0);                     //Enable Timer2
     LPC_TIM2->MR0       =   11999;                      // 1msec
     LPC_TIM2->MCR       |=  (1<<0);
-    
-    /*
+
         Nested Vectored Interrupt Controller (NVIC)
         
         NVIC_SetPriority(IRQn_Type IRQn, uint32_t priority)
@@ -256,7 +247,7 @@
             Enable external interrupt (in this case, the TIMER2_IRQHandler(void) function above gets called every time
             Timer2 generates an interrupt signal)
             
-    */
+
     NVIC_SetPriority(TIMER0_IRQn,200);                  //IMD Capture Interrupt
     NVIC_SetPriority(TIMER1_IRQn,200);                  //IMD 1msec sampling Interrupt
     NVIC_SetPriority(TIMER2_IRQn,1);                    //Battery 1msec sampling Interrupt
@@ -266,34 +257,112 @@
     NVIC_EnableIRQ(TIMER2_IRQn);                        //Enable TIMER2 IRQ 
     
     CAN_SysM.mode(CAN::GlobalTest);
+    
+
     //NVIC_EnableIRQ(CAN_IRQn);
     //NVIC_EnableIRQ(CANActivity_IRQn);
 }    
 
-/*
+
     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;
+
+int sysCANId = 0;                   // change this later
+
+char fanId = 0;                     // 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);
+
+// duty goes from 0 to 100
+void rampFans(void const *arg){
     
-*/
+    static int fan1_duty = 0;
+    static int fan2_duty = 0;
+    static int fan3_duty = 0;
+    static int pump_duty = 0;
+    
+    int *data = (int *)arg;
+    
+    int duty = data[7];
+    if(duty < 0 || duty > 100)
+        return;
+    
+    int pin_ID = data[6];
+    int *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);
+    }
+}
+
 int main() {
-    CANMessage Rxmsg;    
-    
-    Init();
-    
+    CANMessage rx_msg;    
+
     while(1)
     {
-        if(CAN_SysM.read(Rxmsg))
-            RecieveBuffer.txWrite(Rxmsg);
-        while(RecieveBuffer.rxRead(Rxmsg))
-        {
-           XbeeTx.send(Rxmsg);
-        }    
-        
-        //Poll();
-        //Temp();
-        //Battery();
+        if(rxBuffer.rxRead(rx_msg) && rx_msg.id == sysCANId){
+            if(rx_msg.data[0] == fanId){
+                
+                int pin_id = (int)rx_msg.data[6];
+                
+                if(pin_id < 4 && pin_id >= 0 && 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);
+            }
+        }
     }
-    //Temp();
-    //IMD();
-    //Battery();
 }