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:
Sat Oct 25 16:48:09 2014 +0000
Parent:
18:915a235bc099
Child:
20:3dfa7e9461a0
Commit message:
Testing from basics;

Changed in this revision

IOobjects.cpp Show annotated file Show diff for this revision Revisions of this file
IOobjects.h Show annotated file Show diff for this revision Revisions of this file
MODSERIAL.lib Show annotated file Show diff for this revision Revisions of this file
SerialDiagnostics/SerialDiagnostics.cpp Show annotated file Show diff for this revision Revisions of this file
SerialDiagnostics/SerialDiagnostics.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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/IOobjects.cpp	Sat Oct 25 16:48:09 2014 +0000
@@ -0,0 +1,7 @@
+#include "IOobjects.h"
+ 
+// I/O objects
+MODSERIAL pc(USBTX, USBRX, 4096, 256);                  // Software buffered serial, 4kB buffer
+CANBuffer can(CAN2, MEDIUM, p0_6);                      // Software buffered CAN
+Watchdog wdt(0.11);                                     // Watchdog timer set to 110ms
+DigitalOut debugLED(LED4);
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/IOobjects.h	Sat Oct 25 16:48:09 2014 +0000
@@ -0,0 +1,14 @@
+#ifndef _IO_OBJECTS_H
+#define _IO_OBJECTS_H
+ 
+#include "mbed.h"
+#include "MODSERIAL.h"
+#include "CANBuffer.h"
+#include "Watchdog.h"
+
+extern CANBuffer can;
+extern MODSERIAL pc;
+extern Watchdog wdt;
+extern DigitalOut debugLED;
+ 
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MODSERIAL.lib	Sat Oct 25 16:48:09 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/AjK/code/MODSERIAL/#ae0408ebdd68
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SerialDiagnostics/SerialDiagnostics.cpp	Sat Oct 25 16:48:09 2014 +0000
@@ -0,0 +1,42 @@
+#include "SerialDiagnostics.h"
+
+// Macros for working with the string
+// Add newlines, add it to the working buffer 
+#define ADD_LINE                  len+=sprintf(buff+len,"%s\r\n",line);                                                    
+#define ADD_SPRINTF_LINE          padCenter(line,max_charsPerLine-2,temp,' '); len+=sprintf(buff+len,"%s\r\n",line);
+
+// Print to a string buffer, pad to maxLen chars and center it with char pad, str must be null terminated!
+void padCenter(char *buff, int LineLen, char *str, char pad) {
+    int len = strlen(str);
+    int padL = (LineLen-len)/2;                              // -1 to save room for the null terminator
+    for (int i=0; i<padL; i++) buff[i] = pad;                // Fill in the left padding chars
+    strcpy(buff+padL, str);
+    for (int i = padL+len; i<LineLen; i++) buff[i] = pad;    // Fill remaining with padding chars
+    buff[LineLen-1] = '\0';                                  // Add null terminator
+}
+
+void SerialDiagnostics::thread_serialOut(void const* args){
+    const int max_charsPerLine = 81;                 // Max chars per line
+    const int max_lines = 30;                        // Max lines that the layout prints out
+    pc.printf("\033[2J");                            // Clear the screen to get rid of reset message
+ 
+    char buff[max_charsPerLine*max_lines];           // Giant string to store the printout
+    char line[max_charsPerLine];                     // String buffer to work with one line at a time
+    char temp[max_charsPerLine];                     // String buffer to sprintf into
+    
+    while(1){
+        
+        int len = 0;
+        len += sprintf(buff+len, "\033[0;0H");                      // Home the cursor
+        padCenter(line, max_charsPerLine-2, "-", '-'); ADD_LINE     // Generate a line full of 80 -'s      
+        padCenter(line, max_charsPerLine-2, " Penn Electric Racing - REV0 System Management Module Serial Dashboard ", '-'); ADD_LINE
+        padCenter(line, max_charsPerLine-2, "-", '-'); ADD_LINE     // Generate a line full of 80 -'s 
+        
+        // Write it all at once to output tx buffer
+        for (int i = 0; i < strlen(buff); i++) {
+            pc.putc(buff[i]);   
+        }
+        
+        Thread::wait(100);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SerialDiagnostics/SerialDiagnostics.h	Sat Oct 25 16:48:09 2014 +0000
@@ -0,0 +1,13 @@
+#ifndef _SERIAL_DIAGNOSTICS_H
+#define _SERIAL_DIAGNOSTICS_H
+
+#include "MODSERIAL.h"
+#include "rtos.h"
+#include "IOobjects.h"
+
+namespace SerialDiagnostics {
+    void thread_serialOut(void const *args);
+    
+}
+
+#endif
\ No newline at end of file
--- a/SysMngmt.cpp	Sat Oct 25 15:54:19 2014 +0000
+++ b/SysMngmt.cpp	Sat Oct 25 16:48:09 2014 +0000
@@ -4,333 +4,16 @@
     Revised Oct 19, 2014: First team repository version
 */
 
-#include "XBee_Lib.h"
-#include "CANBuffer.h"
-
 #include "mbed.h"
-#include "rtos.h"
 
-#include "Watchdog.h"
-#include "FanPump.h"
-#include "DC_DC.h"
-#include "PollSwitch.h"
-#include "IMD.h"
-#include "CoulombCounter.h"
+DigitalOut myled(LED1);
 
-CANBuffer rxBuffer(CAN1, MEDIUM);
-Watchdog wdt;
-XBee250x XbeeTx;
-Serial pc1(USBTX,USBRX);
-
-char sys_src_id = 0x4;                // source address of system management
-char reset_id = 0x010;
+int main(){
 
-int SYSMGMT_ERROR_ID = 0x400;
-
-extern "C" void mbed_reset();
-
-void soft_reset(){
-    //http://developer.mbed.org/forum/mbed/topic/890/   
-    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);    
+        myled = 1;
+        wait(0.2);
+        myled = 0;
+        wait(0.2);
     }
 }
-
-int main() {
-    CANMessage rx_msg;   
-     
-    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();
-
-    Thread error_thread(error_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_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())
-                {
-                    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){
-                    dc_dc.set(rx_msg.data[0]);
-                }
-            } // check for correct src_addr
-        } // check CANBuffer
-        
-        wdt.kick();
-    } // main while loop
-    
-*/
-}
-
-/*
-
-    Below is Kiran's old code, keeping it commented as a reference. 
-
-*/
-
-/*
-
-Attach Ticker every 10msec to 
-Get IMD
-Poll Switches
-Temperature Read
-Get Battery State
-End Ticker Send message through CAN
-
-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
-
-*/
-
-
-/*
-    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;
-    uint16_t recv,temp,i=0;              //Test
-    char Result[4]={0};
-    Rxpoll=PollSwitch();
-    
-    Result[0]=(char)(Rxpoll&0x00ff);
-    Result[1]=(char)((Rxpoll&0xff00)>>8);    
-    CANMessage Txmsg(410,Result,sizeof(Result));
-    CAN_SysM.write(Txmsg);
-    
-    //Test
-    recv=(((uint16_t)Txmsg.data[1]<<8) | (0x00ff&(uint16_t)Txmsg.data[0]));
-    printf("Recv:%d\n\r",recv);
-    
-    while(i <= 12)
-    {
-        temp=recv;
-        if(((temp & (1 << i))>>i)==1)
-            pc.printf("Switch OFF:%d\n\r",i);
-        ++i;
-    }
-}    
-
-
-void Temp()
-{
-    float DC_DC_Temperature, Coolant1_Temperature, Coolant2_Temperature, ChargerFET_Temperature;
-    float Resistance;
-    float Vadc;
-    int i;
-    ftc send, recv;
-    recv.FLOAT=0.0;
-    send.FLOAT=0.0;
-        
-    Vadc=DC_DC.read()*VDD;
-    Resistance=((float)R10K*Vadc)/((float)VDD + Vadc);
-    DC_DC_Temperature=ReadTemp(TR_NXFT15XH103FA_Map, Resistance, TABLE_SIZE_NXFT15XH103FA); 
-    send.FLOAT=DC_DC_Temperature;
-    CANMessage Txmsg_DC_DC(450,send.C_FLOAT,sizeof(send.C_FLOAT));
-    CAN_SysM.write(Txmsg_DC_DC);
-    
-    for(i=0; i<4;i++)
-        recv.C_FLOAT[i]=Txmsg_DC_DC.data[i];
-    pc.printf("DC_DC:%f\n\r",recv.FLOAT);
-    
-    Vadc=ChargerFET.read()*VDD;
-    Resistance=((float)R10K*Vadc)/((float)VDD + Vadc);
-    ChargerFET_Temperature=ReadTemp(TR_NXFT15XH103FA_Map, Resistance, TABLE_SIZE_NXFT15XH103FA);       
-    send.FLOAT=ChargerFET_Temperature;
-    CANMessage Txmsg_ChargerFET(451,send.C_FLOAT,sizeof(send.C_FLOAT));
-    CAN_SysM.write(Txmsg_ChargerFET);
-    
-    for(i=0; i<4;i++)
-        recv.C_FLOAT[i]=Txmsg_ChargerFET.data[i];
-    pc.printf("ChargerFET:%f\n\r",recv.FLOAT);
-    
-    Vadc=Coolant1.read()*VDD;
-    Resistance=((float)R10K*Vadc)/((float)VDD + Vadc);
-    Coolant1_Temperature=ReadTemp(TR_NTCLP00E3103H_Map, Resistance, TABLE_SIZE_NTCLP00E3103H);
-    send.FLOAT=Coolant1_Temperature;
-    CANMessage Txmsg_Coolant1(452,send.C_FLOAT,sizeof(send.C_FLOAT));
-    CAN_SysM.write(Txmsg_Coolant1);
-    //Control Fans
-    
-    for(i=0; i<4;i++)
-        recv.C_FLOAT[i]=Txmsg_Coolant1.data[i];
-    pc.printf("Coolant1:%f\n\r",recv.FLOAT);
-    
-    Vadc=Coolant2.read()*VDD;
-    Resistance=((float)R10K*Vadc)/((float)VDD + Vadc);
-    Coolant2_Temperature=ReadTemp(TR_NTCLP00E3103H_Map, Resistance, TABLE_SIZE_NTCLP00E3103H);   
-    send.FLOAT=Coolant2_Temperature;
-    CANMessage Txmsg_Coolant2(453,send.C_FLOAT,sizeof(send.C_FLOAT));
-    CAN_SysM.write(Txmsg_Coolant2);
-    //Control Fans
-    
-    for(i=0; i<4;i++)
-        recv.C_FLOAT[i]=Txmsg_Coolant2.data[i];
-    pc.printf("Coolant2:%f\n\r",recv.FLOAT);
-}    
-
-void IMD()
-{
-    IMD_Measurement_Output IMD_Signal;
-    char status[4];
-    ftc send;
-    
-    IMD_Signal=Get_Measurement();
-    send.FLOAT=IMD_Signal.Frequency;
-    CANMessage Txmsg_Frequency(421,send.C_FLOAT,sizeof(send.C_FLOAT));
-    CAN_SysM.write(Txmsg_Frequency);
-    
-    send.FLOAT=IMD_Signal.Duty_Cycle;
-    CANMessage Txmsg_DutyCycle(422,send.C_FLOAT,sizeof(send.C_FLOAT));
-    CAN_SysM.write(Txmsg_DutyCycle);
-    
-    status[0]=Result.Encoded_Status;
-    CANMessage Txmsg_Status(423,status,sizeof(status));
-    CAN_SysM.write(Txmsg_Status);
-}    
-*/    
-
-/*
-    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);
-    
-    //ReadTemperature.attach(&Temp,0.1);
-    //ReadBatteryState.attach(&Battery,0.1);
-    
-
-        Initialize Timer2 for Battery State
-        
-        LPC_SC                      0x400F C000 (System Control)
-        ->PCONP                     0x400F C0C4 (Power Control for Peripherals Register) 
-        |= (1<<22)                  22 Bit (Timer 2 power/clock control bit)
-        
-        ->PCLKSEL1                  Peripheral Clock Selection register 1 (controls rate of clock signal supplied to peripheral)
-        |= ((1<<12) | (1<<13));     12:13 Bits (Peripheral Clock Selection for TIMER2)
-        
-        LPC_TIM2                    0x4009 0000 (Timer 2)
-        ->TCR                       0x4009 0004 (Timer Control Register)
-        |= (1<<0);                  0 Bit (Counter Enable)
-        
-        ->MR0                       0x4009 0018 (Match Register)
-        
-        ->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)
-            sets priority of an interrupt
-        
-        IRQn_Type
-            Interrupt number definitions
-            Interrupt Request (IRQ)
-        
-        NVIC_EnableIRQ(IRQn_Type IRQn)
-            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
-    NVIC_SetPriority(TIMER3_IRQn,255);                  //mbed Timer/Ticker/Wait Interrupt
-    NVIC_SetPriority(CAN_IRQn,2);
-    
-    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
-*/