System Management code

Dependencies:   CANBuffer mbed SystemManagement mbed-rtos

Dependents:   SystemManagement

System Management code for Penn Electric Racing

Functions:

Controls Fans and Pumps via instruction from CAN Messages, ramps them up over time to prevent damage

Turns on/off DC-DC converter via instruction from CAN Messages

SysMngmt.cpp

Committer:
martydd3
Date:
2014-10-01
Revision:
0:e516fcccccda
Child:
1:e02eb179aed3

File content as of revision 0:e516fcccccda:

/*
    Reads CAN Messages and various data inputs, outputs using Xbee radio modules

    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"


RTCStore store;
CANBuffer RecieveBuffer(CAN1, MEDIUM);
XBee250x XbeeTx;

//Possible problems in IMD coz change of counter
//Possible problems in BatteryStatus coz change in library

/*

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);
}
*/

extern "C" void TIMER2_IRQHandler(void)
{
    if((LPC_TIM2->IR & 0x01) == 0x01) // if MR0 interrupt
    {
        //printf("Every 1ms\n\r");
        LPC_TIM2->IR |= 1 << 0; // Clear MR0 interrupt flag
        
        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   
    }
}    


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);
}    

void Battery()
{
    RTCStore reg;
    ftc send;

    send.FLOAT=reg.read(0);
    CANMessage Txmsg_BATmA_Hr(440,send.C_FLOAT,sizeof(send.C_FLOAT));
    CAN_SysM.write(Txmsg_BATmA_Hr);
    
    send.FLOAT=reg.read(1);
    CANMessage Txmsg_DCA_msec(442,send.C_FLOAT,sizeof(send.C_FLOAT));     
    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);
    
    //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)
        
    */
    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);
    
    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
    
*/
int main() {
    CANMessage Rxmsg;    
    
    Init();
    
    while(1)
    {
        if(CAN_SysM.read(Rxmsg))
            RecieveBuffer.txWrite(Rxmsg);
        while(RecieveBuffer.rxRead(Rxmsg))
        {
           XbeeTx.send(Rxmsg);
        }    
        
        //Poll();
        //Temp();
        //Battery();
    }
    //Temp();
    //IMD();
    //Battery();
}