Just a regular publish

Dependencies:   mbed imu_driver

main.cpp

Committer:
open4416
Date:
2019-11-12
Revision:
4:f05bef255292
Parent:
2:c7a3a8c1aeed
Child:
5:8116016abee0

File content as of revision 4:f05bef255292:

#include "mbed.h"
#define dt  0.01f
#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))

DigitalOut  Cool(PA_15,0);
DigitalOut  LED(D13, 0);                            //Internal LED output, general purpose
CAN     can1(PA_11, PA_12, 1000000);                //1Mbps, contain critical torque command message
Serial  pc(USBTX, USBRX, 115200);
Ticker  ticker1;                                    //General

Ticker  ticker2;                                    //Logger

CANMessage  can_msg_1;
CANMessage  can_msg_2;
CANMessage  can_msg_send;
char    data_msg[8] = {0,0,0,0,0,0,0,0};

int16_t IGBT_Temp_A = 0;                            // *0.1 to get float value
int16_t IGBT_Temp_B = 0;                            // *0.1 to get float value
int16_t IGBT_Temp_C = 0;                            // *0.1 to get float value
int16_t IGBT_Temp = 0;                              // *0.1 to get float value
int16_t Motor_Temp = 0;                             // *0.1 to get float value
int16_t DC_Current = 0;                             // *0.1 to get float value
int16_t DC_Voltage = 0;                             // *0.1 to get float value
int16_t Motor_Speed = 0;


uint8_t CAN_flag_1 = 0;
uint8_t CAN_flag_2 = 0;
uint8_t timer_flag = 0;
uint8_t RTD_State = 0;                              //use as bool
uint8_t SD_OK = 0;                                  //SD OK flag
uint8_t Logging = 0;                                //Logging flag

int16_t     Max_temp_within_BMS1 = 0;               //*0.01 to degC
int16_t     Max_temp_within_BMS2 = 0;               //*0.01 to degC
int16_t     Max_temp = 0;                           //*0.01 to degC

uint16_t    Module_Total_BMS1 = 0;                  //*0.01 to Voltage ~ 120 V
uint16_t    Module_Total_BMS2 = 0;                  //*0.01 to Voltage ~ 120 V
uint16_t    Module_Total = 0;                       //*0.01 to Voltage ~ 240 V

uint16_t    Module_Min_BMS1 = 0;                    //*0.0001 to Voltage ~ 3.0 V
uint16_t    Module_Min_BMS2 = 0;                    //*0.0001 to Voltage ~ 3.0 V
uint16_t    Module_Min = 0;                         //*0.0001 to Voltage ~ 3.0 V

uint16_t    Module_Max_BMS1 = 0;                    //*0.0001 to Voltage ~ 3.0 V
uint16_t    Module_Max_BMS2 = 0;                    //*0.0001 to Voltage ~ 3.0 V
uint16_t    Module_Max = 0;                         //*0.0001 to Voltage ~ 3.0 V

void send(char ID);
void CAN_RX1(void);
void CAN_RX2(void);
void Send2CAN1(void);

void start_log(void);
void stop_log(void);
void autoname(void);
int16_t max_val(int16_t i1, int16_t i2, int16_t i3);

void timer1_interrupt(void)
{
    timer_flag = 1;
}
void timer2_interrupt(void);

int main()
{
    CAN_flag_1 = 0;
    CAN_flag_2 = 0;

    can1.attach(&CAN_RX1, CAN::RxIrq);              //CAN1 Recieve Irq
    ticker1.attach(&timer1_interrupt, 1);           //1sec

    //General task
    while(1) {
        if(timer_flag == 1) {

            if(CAN_flag_1) {
                if(RTD_State) {
                    Cool = 1;
                    if(!Logging) {
                        autoname();                                     //GENERATE NEW FILE EACH call fp ready or stuck
                        if(SD_OK) {
                            Logging = 1;
                            start_log();
                        }
                    }
                } else {
                    Cool = 0;
                    if(Logging) {
                        Logging = 0;
                        stop_log();
                    }
                }
                CAN_flag_1 = 0;
            }

            if(CAN_flag_2) {
                //Total
                Module_Total = Module_Total_BMS1 + Module_Total_BMS2;

                //Min
                if(Module_Min_BMS1 < Module_Min_BMS2) {
                    Module_Min = Module_Min_BMS1;
                } else {
                    Module_Min = Module_Min_BMS2;
                }

                //Max
                if(Module_Max_BMS1 < Module_Max_BMS2) {
                    Module_Max = Module_Max_BMS2;
                } else {
                    Module_Max = Module_Max_BMS1;
                }

                //Temp
                if(Max_temp_within_BMS1 < Max_temp_within_BMS2) {
                    Max_temp = Max_temp_within_BMS2;
                } else {
                    Max_temp = Max_temp_within_BMS1;
                }

                CAN_flag_2 = 0;
                Send2CAN1();
            }

//            pc.printf("SOC: %.2f\n", Module_Total*0.01f);
//            pc.printf("Min: %.3f\n", Module_Min*0.0001f);
//            pc.printf("Max: %.3f\n", Module_Max*0.0001f);
//            pc.printf("Max1: %.3f\n", Module_Max_BMS1*0.0001f);
//            pc.printf("Max2: %.3f\n", Module_Max_BMS2*0.0001f);
//            pc.printf("Temp: %.1f\n", Max_temp*0.01f);
            timer_flag = 0;
        }
    }
}

void timer2_interrupt(void)
{
}

void start_log(void)
{

}

void stop_log(void)
{

}

void autoname(void)
{

}

void CAN_RX1(void)
{
    if(can1.read(can_msg_1)) {
        switch(can_msg_1.id) {                                                  //Filtered the input message and do corresponding action
            case 0xF0:
                //Internal state address
                RTD_State = can_msg_1.data[7] & 0x01;                           //get bit "0", result ( 1 = RTD, 0 = OFF )
                CAN_flag_1 = 1;
                break;

            case 0xA0 :
                //Temperature infrom address 1
                IGBT_Temp_A = can_msg_1.data[1] << 8 | can_msg_1.data[0];
                IGBT_Temp_B = can_msg_1.data[3] << 8 | can_msg_1.data[2];
                IGBT_Temp_C = can_msg_1.data[5] << 8 | can_msg_1.data[4];
                IGBT_Temp = max_val(IGBT_Temp_A,IGBT_Temp_B,IGBT_Temp_C);
                CAN_flag_1 = 1;
                break;

            case 0xA2 :
                //Temperature infrom address 3
                Motor_Temp = can_msg_1.data[5] << 8 | can_msg_1.data[4];
                CAN_flag_1 = 1;
                break;

            case 0xA5:
                Motor_Speed = can_msg_1.data[3] << 8 | can_msg_1.data[2];
                break;

            case 0xA6 :
                //Current infrom address
                DC_Current = can_msg_1.data[7] << 8 | can_msg_1.data[6];
                CAN_flag_1 = 1;
                break;

            case 0xA7 :
                //Voltage infrom address
                DC_Voltage = can_msg_1.data[1] << 8 | can_msg_1.data[0];
                CAN_flag_1 = 1;
                break;
        }
    }
}

//void CAN_RX2(void)
//{
//    if(can2.read(can_msg_2)) {
//        switch(can_msg_2.id) {
////            case 0xD1:
////                //BMS1 soc
////                Module_Total_BMS1 = can_msg_2.data[1] << 8 | can_msg_2.data[0];
////                Module_Total_BMS1 += can_msg_2.data[3] << 8 | can_msg_2.data[2];
////                Module_Total_BMS1 += can_msg_2.data[5] << 8 | can_msg_2.data[4];
////                CAN_flag_2 = 1;
////                break;
////            case 0xD2:
////                //BMS1 min Volt
////                Module_Min_BMS1 = can_msg_2.data[7] << 8 | can_msg_2.data[6];
////                CAN_flag_2 = 1;
////                break;
////            case 0xD3:
////                //BMS1 max Volt
////                Module_Max_BMS1 = can_msg_2.data[7] << 8 | can_msg_2.data[6];
////                CAN_flag_2 = 1;
////                break;
//            case 0xD4:
//                //BMS1 max temperature
//                Max_temp_within_BMS1 = can_msg_2.data[7] << 8 | can_msg_2.data[6];
//                CAN_flag_2 = 1;
//                break;
//
////            case 0xE1:
////                //BMS2 soc
////                Module_Total_BMS2 = can_msg_2.data[1] << 8 | can_msg_2.data[0];
////                Module_Total_BMS2 += can_msg_2.data[3] << 8 | can_msg_2.data[2];
////                Module_Total_BMS2 += can_msg_2.data[5] << 8 | can_msg_2.data[4];
////                CAN_flag_2 = 1;
////                break;
////            case 0xE2:
////                //BMS2 min Volt
////                Module_Min_BMS2 = can_msg_2.data[7] << 8 | can_msg_2.data[6];
////                CAN_flag_2 = 1;
////                break;
////            case 0xE3:
////                //BMS2 max Volt
////                Module_Max_BMS2 = can_msg_2.data[7] << 8 | can_msg_2.data[6];
////                CAN_flag_2 = 1;
////                break;
//            case 0xE4:
//                //BMS2 max temperature
//                Max_temp_within_BMS2 = can_msg_2.data[7] << 8 | can_msg_2.data[6];
//                CAN_flag_2 = 1;
//                break;
//        }
//    }
//}

void Send2CAN1(void)
{
    data_msg[0] = Module_Total;
    data_msg[1] = Module_Total >> 8;
    data_msg[2] = Module_Min;
    data_msg[3] = Module_Min >> 8;
    data_msg[4] = Module_Max;
    data_msg[5] = Module_Max >> 8;
    data_msg[6] = Max_temp;
    data_msg[7] = Max_temp >> 8;
    send(0xF1);
}

void send(char ID)
{
    can_msg_send = CANMessage(ID,data_msg,8,CANData,CANStandard);
    can1.write(can_msg_send);
}

int16_t max_val(int16_t i1, int16_t i2, int16_t i3)
{
    int16_t max = i1;
    if(i2 > max) max = i2;
    if(i3 > max) max = i3;
    return max;
}