The Code Repository for the REV0 Steering Wheel.
Dependencies: CANBuffer KS0108_fork mbed-rtos mbed CAN Addresses
Fork of REVO_Updated_Steering by
Steering.cpp
- Committer:
- tmccurbin
- Date:
- 2014-11-04
- Revision:
- 22:ab33195882e3
- Parent:
- 21:56890c41ecf1
- Child:
- 25:fc63a5bffffc
File content as of revision 22:ab33195882e3:
#include "Steering.h" #define Reset_Threshold 32000 #define Drive_Threshold 32000 int BL_Button_Status = 0; int BR_Button_Status = 0; void HomeScreen() { ftc rcv; rcv.FLOAT=0.0; char dat[4]; float power_ratio=0.0; printf("Homescreen\n\r"); display.ClearScreen(); display.SelectFont(Arial12,BLACK,ReadData); display.GotoXY(0,0); display.PrintString(" HOME SCREEN"); while( !(biSWTL.read() || biSWTR.read() || biSWBR.read()) ) { CANMessage Rxmsg; if(CAN_Steering_Buffer.rxRead(Rxmsg)) { for(int i=0; i<4; i++) rcv.C_FLOAT[i]=Rxmsg.data[i]; if(Rxmsg.id == BATTERY_VOLTAGE_ID) { display.GotoXY(0,16); display.PrintString(" PV: "); sprintf(dat,"%2.2f",rcv.FLOAT); display.PrintString(dat); display.PrintString("V"); } if(Rxmsg.id == BATTERY_POWER_ID) { display.GotoXY(64,16); display.PrintString(" PP: "); power_ratio=rcv.FLOAT/10000; sprintf(dat,"%2.2f",power_ratio); display.PrintString(dat); display.PrintString("kW"); } if(Rxmsg.id == PCM_STATE_ID) { display.GotoXY(0,32); display.PrintString(" STATE: "); display.PrintNumber(rcv.FLOAT); } } } return; } void AMSScreen1() { CANMessage Rxmsg; ftc rcv; char dat[4]; rcv.FLOAT=0.0; printf("AMSScreen1"); display.ClearScreen(); display.SelectFont(Arial12,BLACK,ReadData); while( !(biSWTL.read() || biSWTR.read() || biSWBL.read()) ) { printf("iN LOOP1"); if(CAN_Steering_Buffer.rxRead(Rxmsg)) { for(int i=0; i<4; i++) rcv.C_FLOAT[i]=Rxmsg.data[i]; if(Rxmsg.id == BATTERY_VOLTAGE_ID) { display.GotoXY(0,0); display.PrintString(" pv: "); sprintf(dat,"%2.2f",rcv.FLOAT); display.PrintString(dat); } if(Rxmsg.id == BATTERY_POWER_ID) { display.GotoXY(0,16); display.PrintString(" pp: "); sprintf(dat,"%2.2f",rcv.FLOAT); display.PrintString(dat); } if(Rxmsg.id == BATTERY_CURRENT_ID) { display.GotoXY(0,32); display.PrintString(" pi: "); sprintf(dat,"%2.2f",rcv.FLOAT); display.PrintString(dat); } //Column 2 if(Rxmsg.id == BATTERY_MIN_CELLVOLTAGE_ID) { display.GotoXY(49,0); display.PrintString(" min cell V: "); sprintf(dat,"%2.2f",rcv.FLOAT); display.PrintString(dat); } if(Rxmsg.id == BATTERY_MAX_CELLVOLTAGE_ID) { display.GotoXY(49,16); display.PrintString(" max cell V: "); sprintf(dat,"%2.2f",rcv.FLOAT); display.PrintString(dat); } if(Rxmsg.id == BATTERY_AVG_CELLVOLTAGE_ID) { display.GotoXY(49,32); display.PrintString(" avg cell V: "); sprintf(dat,"%2.2f",rcv.FLOAT); display.PrintString(dat); } } screen=1; } return; } void AMSScreen2() { CANMessage Rxmsg; ftc rcv; char dat[4]; //ftc send; rcv.FLOAT=0.0; //send.FLOAT=65.6432; printf("AMSScreen2\n\r"); display.ClearScreen(); display.SelectFont(Arial12,BLACK,ReadData); while( !(biSWTL.read() || biSWTR.read() || biSWBL.read()) ) { printf("iN LOOP2"); if(CAN_Steering_Buffer.rxRead(Rxmsg)) { for(int i=0; i<4; i++) rcv.C_FLOAT[i]=Rxmsg.data[i]; if(Rxmsg.id == BATTERY_MIN_CELLTEMPERATURE_ID) { display.GotoXY(0,0); display.PrintString(" mn cll T: "); sprintf(dat,"%2.2f",rcv.FLOAT); display.PrintString(dat); } if(Rxmsg.id == BATTERY_MAX_CELLTEMPERATURE_ID) { display.GotoXY(0,16); display.PrintString(" mx cll T: "); sprintf(dat,"%2.2f",rcv.FLOAT); display.PrintString(dat); } if(Rxmsg.id == BATTERY_AVG_CELLTEMPERATURE_ID) { display.GotoXY(0,32); display.PrintString(" avg cell T: "); sprintf(dat,"%2.2f",rcv.FLOAT); display.PrintString(dat); } //Column 2 if(Rxmsg.id == AMS_BATTERY_STATE) { if((rcv.C_FLOAT[0] & 0xC0) == 0xC0) { display.GotoXY(70,0); display.PrintString(" AIRS: "); display.PrintString("CLOS"); } else { display.GotoXY(70,0); display.PrintString(" AIRS: "); display.PrintString("OPEN"); } if((rcv.C_FLOAT[0] & 0x04) == 0x04) { display.GotoXY(70,16); display.PrintString(" Prchrg: "); display.PrintString("on"); } else { display.GotoXY(70,16); display.PrintString(" Prchrg: "); display.PrintString("off"); } } if(Rxmsg.id == AMS_BATTERY_STATE) { display.GotoXY(70,16); display.PrintString(" Prchrg: "); display.PrintString("off"); } } screen=0; } return; } void display_speed_data(void const *args) { while (true) { display.GotoXY(10,16); display.SelectFont(Arial_14,BLACK,ReadData); char buf[10]; int a = rand()%100; sprintf(buf, "%d", a); display.PrintString("Speed: "); display.PrintString(buf); display.PrintString("mph"); } } void Drive_Request() { char drive_status_request = !drive_status; char * status_string; if(drive_status_request){ status_string = "ON"; } else{ status_string = "OFF"; } for(int i = 0; i < 10; i++){ CAN_Steering_Buffer.txWrite(Txmsg_drive_status_request); } //Button Press code if (BL_Button_Status == 0) { display.ClearScreen(); display.SelectFont(Arial12,BLACK,ReadData); display.GotoXY(26,16); display.PrintString("Drive Initiated"); printf("Drive Initiated\n\r"); BL_Button_Status = BL_Button_Status + 1; } else { BL_Button_Status = BL_Button_Status + 1; } printf("%s\n\r", status_string); return; } void reset() { if (BR_Button_Status == 0) { display.ClearScreen(); display.SelectFont(Arial12,BLACK,ReadData); display.GotoXY(16,16); display.PrintString("Hold To Reset"); printf("Hold to reset\n\r"); BR_Button_Status = BR_Button_Status + 1; } else if (BR_Button_Status < Reset_Threshold) { BR_Button_Status = BR_Button_Status + 1; } else if (BR_Button_Status >= Reset_Threshold) { for (int i = 0; i < 10; i ++) { CAN_Steering_Buffer.txWriteDirect(Txmsg_reset); printf("Reset Initiated\n\r"); } BR_Button_Status = BR_Button_Status + 1; mbed_reset(); } } void Powerstream() { CANMessage Rxmsg; float power_ratio; ftc rcv; rcv.FLOAT=0.0; if(CAN_Steering_Buffer.rxRead(Rxmsg)) { if(Rxmsg.id == BATTERY_POWER_ID) { for(int i=0; i<4; i++){ rcv.C_FLOAT[i]=Rxmsg.data[i]; } power_ratio=rcv.FLOAT/80000; ledstream.write(power_ratio); } } } void Init() { pc.baud(230400); drive_status = 0; drive_status_request = 1; reset_body = 0; ledstream.write(0); call_ledstream.attach(&Powerstream,0.1); } void update_vars(void const *args) { while (true) { CANMessage Rxmsg; if(CAN_Steering_Buffer.rxRead(Rxmsg)){ if(Rxmsg.id == PCM_STATE_ID){ ds_mutex.lock(); drive_status = Rxmsg.data[0]; ds_mutex.unlock(); } } } } int main() { // Init all Init(); wait(0.1); //Init Display display.GotoXY(10,16); display.SelectFont(Arial_14,BLACK,ReadData); display.PrintString("Penn Electric Racing"); Thread update_thread(update_vars); wait(1); // Start to read buttons on main thread while(1) { if(biSWBL.read()) { Drive_Request; } else if(biSWBR.read()) { reset(); } else { BR_Button_Status = 0; BL_Button_Status = 0; } printf("BL %d BR %d \n", 'BL_Button_Status', 'BR_Button_Status'); } }