The Code Repository for the REV0 Steering Wheel.

Dependencies:   CANBuffer KS0108_fork mbed-rtos mbed CAN Addresses

Fork of REVO_Updated_Steering by Penn Electric

Revision:
36:8544a8900884
Parent:
35:b42afc973902
Child:
38:d04a430d7fe3
--- a/Steering.cpp	Sat Nov 22 22:24:53 2014 +0000
+++ b/Steering.cpp	Thu Jan 29 04:27:19 2015 +0000
@@ -1,187 +1,297 @@
 #include "Steering.h"
 
-bool NOT_biSWBL_HELD;
-bool NOT_biSWTR_HELD;
+// To-Do:
+// Look at SYS management CAN translations: shutdown switches, DCDC status, & IMD status
+// Figure out screen/variable updating options
+// Firgure out fault protocall
+
+void Init();
+void ProcessButtons(void const *args);
+void RequestStatusChange();
+void ResetSteeringWheel();
+void ResetCar();
+void ProcessCANMsg(CANMessage& msg);
+void UpdateDisplay();
+void ProcessButtons(void const *args);
+void ConnectedStatusCANMsg();
+
+float CANFloat;
+float CANFloat2;
+
+Ticker StatusMsg;
 
-void update_display(void const *args){
-    
-    while(true){
-        
-        if(screen_flags[curr_screen] || should_redraw){
-        screen_flags[curr_screen] = 0;
-        should_redraw = false;
-        switch(curr_screen){
-    
-            case HOME_SCREEN:{
-                
-                display.ClearScreen();
-                display.SelectFont(Arial10,BLACK,ReadData);
-                display.GotoXY(37,0);
-                display.PrintString(" Home Screen");
-              
-                display.GotoXY(22,30);
-                if(vars_list->get_value(PCM_STATE_ID) == 0){
-                    display.PutString(17, 30, "Drive Status: OFF");
-                }
-                else{
-                    display.PrintString("Drive Status: ON");
-                }
-                break;
-                }
-            
-            case BATTERY_SCREEN:
-            
-                display.ClearScreen();
-                display.SelectFont(Arial10,BLACK,ReadData);
-                display.GotoXY(33,0);
-                display.PrintString(" Battery Screen");
-                break;
-        
-            default:
-                break;    
-        
-            }
+int main()
+{
+    Init();
+
+    display.GotoXY(10,16);
+    display.SelectFont(Arial_14,BLACK,ReadData);
+    display.PrintString("Penn Electric Racing");
+    wait(2);
+    display.ClearScreen();
+    display.GotoXY(10,16);
+    display.PrintString("Live the dream.");
+    wait(2);
+
+    Thread Thread2(ProcessButtons);
+    Thread Thread1(DisplayCurrScreen);
+
+    while(1) {
+        CANMessage Rxmsg;
+        if (SteeringCANPort.rxRead(Rxmsg)) {
+            ProcessCANMsg(Rxmsg);
         }
-        wait(2);   
     }
 }
 
-void toggle_screen(){
-    should_redraw = true;
-    curr_screen = (curr_screen+1) % NUM_SCREEN; 
-}
-
-void request_status_change(){
-    
-    char drive_status_request;
-    ds_mutex.lock();
-    drive_status_request = !(vars_list->get_value(PCM_STATE_ID));
-
-    ds_mutex.unlock();
-    char * status_string;
-
-    if(drive_status_request){
-        status_string = "ON";
-    }
-    else{
-        status_string = "OFF";
-    }
-
-    CANMessage Txmsg_drive_status_request(0x501,&drive_status_request,1);
-    for(int i = 0; i < 10; i++){
-        CAN_Steering_Buffer.txWrite(Txmsg_drive_status_request);
-    }
-
-    printf("%s\n\r", status_string);
-    return;
-}
-
-void reset()
-{
-    reset_body = 1;
-    CANMessage Txmsg_reset(0x502,&reset_body,1);
-    for(int i = 0; i < 10; i++){
-        CAN_Steering_Buffer.txWriteDirect(Txmsg_reset);
-    }
-    NVIC_SystemReset();
-    display.ClearScreen();
-    display.SelectFont(Arial12,BLACK,ReadData);
-    display.GotoXY(16,16);
-    printf("Reset Initiated\n\r");
-
-    return;
-}    
 
 void Init()
 {
-    should_redraw = true;
+    StatusMsg.attach(&ConnectedStatusCANMsg,0.1);
     pc.baud(921600);
-    curr_screen = HOME_SCREEN;
-    drive_status = 0;
-    drive_status_request = 1;
-    reset_body = 0;
-    ledstream.write(0);
-    NOT_biSWBL_HELD = true;
-    NOT_biSWTR_HELD = true;
-    
-    vars_list = new variables();
-    
-    vars_list->add(PCM_STATE_ID, HOME_SCREEN);
-    vars_list->add(BATTERY_VOLTAGE_ID, BATTERY_SCREEN);
-    vars_list->add(BATTERY_POWER_ID, BATTERY_SCREEN);
-    vars_list->add(BATTERY_CURRENT_ID, BATTERY_SCREEN);
-    
+    BLButtonRed;
+    BRButtonGreen;
+    TLButtonGreen;
+    TRButtonGreen;
+    DriveStatus = 0; // Off
+    //initialize screen Startup Process
+    wait(2);
 }
 
-void read_messages(void const *args) {
-    
-    while (true) {
-        
-        CANMessage Rxmsg;
-        if(CAN_Steering_Buffer.rxRead(Rxmsg)){
-            id_node = vars_list->get_node(Rxmsg.id);
-            if(id_node != NULL){
-                if(id_node->value != Rxmsg.data[0]){
-                    screen_flags[id_node->screen] = 1;  
-                }
-                id_node->value = Rxmsg.data[0];
+
+void ProcessButtons(void const *args)
+{
+    while(1) {
+        Thread::wait(50);
+        if(biSWBR.read()) {
+            Thread::wait(750);
+            if (biSWBR.read()) {
+                ResetCar();
+            }
+            NVIC_SystemReset();
+            ResetSteeringWheel();
+        }
+
+        if(biSWBL.read()) {
+            Thread::wait(1000);
+            if (biSWBL.read()) {
+                RequestStatusChange();
             }
         }
+
+        if(biSWTR.read() && CurrScreen != 5) {
+            //If the toggle button is pressed and the screen isn't on Fault_Screen
+            ToggleScreen();
+            Thread::wait(250);
+        } else if (biSWTR.read() && CurrScreen == 5) {// If the screen was on Fault, go to Home
+            CurrScreen = 0;
+            Thread::wait(250);
+        }
+
+        if(biSWTL.read() && CurrScreen != 0) {
+            //If the Home button is pressed and the screen isn't on Fault_Screen
+            CurrScreen = 0;
+            Thread::wait(250);
+        } else if (biSWTL.read() && CurrScreen == 0) {// If the screen was on Fault, go to Home
+            CurrScreen = 5;
+            Thread::wait(250);
+        }
     }
 }
 
-int main(){
-    // Initialize, set all variables.
-    Init();
-    wait(0.1);
-    
-    //Init Display
-    display.GotoXY(10,16);
-    display.SelectFont(Arial_14,BLACK,ReadData);
-    display.PrintString("Penn Electric Racing");
-    CAN_Steering_Buffer.mode(NoAck);
-    
-    wait(2);
-     
-     //New thread to read messages.
-    Thread update_thread(read_messages);
-    
-    // display the screen.
-    Thread display_thread(update_display);
-      
+
+void UpdateDisplay()
+{
+}
+
+
+void ProcessCANMsg(CANMessage& Rxmsg)
+{
+    if (SteeringCANPort.rxRead(Rxmsg)) {
+        printf("Message ID: 0x%x len: %d hex: 0x%x float: %f\r\n", Rxmsg.id, Rxmsg.len, *((int*)((void*)(&Rxmsg.data[0]))), *((float*)((void*)(&Rxmsg.data[0]))));
+        for (int i=0; i < Rxmsg.len; i++) printf("D[%d]: %x ", i, Rxmsg.data[i]);
+        switch (Rxmsg.id) {
 
-    // Start to read buttons on main thread
-    while(1)
-    {
-        if(biSWBL.read() && NOT_biSWBL_HELD){
-            request_status_change();
-            NOT_biSWBL_HELD = false;
+                // HOME SCREEN *******************************************************************************************
+            case SOC_TX_ID:
+                CANFloat = GetFloat;
+                sprintf(BigBatLifeBuffer,"%3.2d", int(CANFloat*100));                   // Big battery life percentage
+                display.PutString(1,40,BigBatLifeBuffer);
+                break;
+            case SYS_GLV_SOC_ID:
+                CANFloat = GetFloat;
+                sprintf(SmallBatLifeBuffer,"%3.2d", int(CANFloat*100));                 // Small battery life percentage 
+                display.PutString(1,60,SmallBatLifeBuffer);
+                break;
+            case PCM_FRPM_ID:
+                CANFloat  = GetFloat;
+                CANFloat2 = GetFloat4;
+                sprintf(CarSpdBuffer,"%3.2d", int((CANFloat+CANFloat2)*WheelCircumference*30));     // Miles per hour
+                display.PutString(1,108,CarSpdBuffer);
+            case TEMP_MMA_TX_ID_BASE2:
+                CANFloat = GetFloat;
+                sprintf(AvgBatTBuffer,"%3.2d", int((CANFloat*9.0/5.0)+32));          // Degrees F
+                display.PutString(3,39,AvgBatTBuffer);
+                //display.PutString(5,108,AvgBatTBuffer); this is for the battery screen
+            case TEMP_MMA_TX_ID_BASE:
+                CANFloat2 = GetFloat4;
+                sprintf(MaxBatTBuffer,"%3.2d", int((CANFloat2*9.0/5.0)+32));          // Figure out Max/Min and Figure out various screen display
+                display.PutString(3,103,MaxBatTBuffer);
+                //display.PutString(5,20,MaxBatTBuffer); this is for the battery screen
+                CANFloat = GetFloat;
+                sprintf(MinBatTBuffer,"%3.2d", int((CANFloat*9.0/5.0)+32));
+                //display.PutString(5,67,MinBatTBuffer); this is for the battery screen
+            case MOC_MOTEMP_ID:
+                CANFloat = GetFloat;
+                sprintf(LMtrTmpBuffer,"%3.2d", int((CANFloat*9.0/5.0)+32));          // We need to figure out how to handle the bytes for left and right motors
+                display.PutString(5,34,LMtrTmpBuffer);
+                // display.PutString(1,34,LMtrTmpBuffer); this is for the motor screen
+                CANFloat2 = GetFloat4;
+                sprintf(RMtrTmpBuffer,"%3.2d", int((CANFloat2*9.0/5.0)+32));          // We need to figure out how to handle the bytes for left and right motors
+                display.PutString(5,34,RMtrTmpBuffer);
+                //display.PutString(1,98,LMtrTmpBuffer); this is for the motor screen
+                // BATTERY SCREEN***********************************************************************************
+            case VOLTAGE_TX_ID:
+                CANFloat = GetFloat;
+                sprintf(BigBatVoltBuffer,"%3.2d", int(CANFloat));                       // Volts
+                //display.PutString(1,10,BigBatVoltBuffer);
+                //display.PutString(3,94,BigBatVoltBuffer); this is for the charging screen
+            case POWER_TX_ID:
+                CANFloat = GetFloat;
+                sprintf(BigBatPowerBuffer,"%3.2d", int(CANFloat));                       // Watts
+                //display.PutString(1,55,BigBatPowerBuffer);
+            case CURRENT_TX_ID:
+                CANFloat = GetFloat;
+                sprintf(BigBatCurrentBuffer,"%3.2d", int(CANFloat));                       // Amps
+                //display.PutString(1,100,BigBatCurrentBuffer);
+            case CELLV_MMA_TX_ID_BASE:
+                CANFloat2 = GetFloat4;
+                sprintf(VmaxBuffer,"%3.2d", int(CANFloat2));                                 // We need to figure out how to handle max and min
+                //display.PutString(3,20,VmaxBuffer);
+                CANFloat = GetFloat;
+                sprintf(VminBuffer,"%3.2d", int(CANFloat));
+                //display.PutString(3,67,VminBuffer);
+            case CELLV_MMA_TX_ID_BASE2:
+                CANFloat = GetFloat;
+                sprintf(VavgBuffer,"%3.2d", int(CANFloat));                                 // Volts
+                //display.PutString(3,108,VavgBuffer);
+            case BOARDTEMP_TX_ID:
+                CANFloat = GetFloat;
+                sprintf(BoardTempBuffer,"%3.2d", int((CANFloat*9.0/5.0)+32));                                 // Degrees F
+                //display.PutString(7,87,BoardTempBuffer);
+
+                // MOTOR SCREEN*****************************************************************
+            case MOC_AIRTEMP_ID:
+                CANFloat = GetFloat;
+                sprintf(LAirTmpBuffer,"%3.2d", int((CANFloat*9.0/5.0)+32));                   // Figure out the left and the right
+                //display.PutString(2,34,LAirTmpBuffer);
+                CANFloat2 = GetFloat4;
+                sprintf(RAirTmpBuffer,"%3.2d", int((CANFloat2*9.0/5.0)+32));                   // Degrees F
+                //display.PutString(2,98,RAirTmpBuffer);
+            case MOC_IGBTTEMP_ID:
+                CANFloat = GetFloat;
+                sprintf(LIGBTmpBuffer,"%3.2d", int((CANFloat*9.0/5.0)+32));                   // Figure out the left and the right
+                //display.PutString(3,34,LIGBTmpBuffer);
+                CANFloat2 = GetFloat4;
+                sprintf(RIGBTmpBuffer,"%3.2d", int((CANFloat2*9.0/5.0)+32));                   // Degrees F
+                //display.PutString(3,98,RIGBTmpBuffer);
+            case MOC_RPM_ID:
+                CANFloat = GetFloat;
+                sprintf(Rpm_LBuffer,"%3.2d", int(CANFloat));                   // Figure out the left and the right
+                //display.PutString(5,30,Rpm_LBuffer);
+                CANFloat2 = GetFloat4;
+                sprintf(Rpm_RBuffer,"%3.2d", int(CANFloat2));
+                //display.PutString(5,107,Rpm_RBuffer);
+            case PCM_TORQUERQ_ID:
+                CANFloat = GetFloat;
+                sprintf(TRequestBufferL,"%3.2d", int(CANFloat*100));             // Percentage
+                //display.PutString(7,89,TRequestBufferL);
+                CANFloat2 = GetFloat4;
+                sprintf(TRequestBufferR,"%3.2d", int(CANFloat2*100));             // Percentage
+                //display.PutString(7,89,TRequestBufferR);
+
+                // SYSTEM MANAGEMENT SCREEN***************************************************
+            case SYS_GLV_CURRENT_ID:
+                CANFloat = GetFloat;
+                sprintf(SmallBatCurrentBuffer,"%3.2d", int(CANFloat));             // Amps
+                //display.PutString(1,30,SmallBatCurrentBuffer);
+            case SYS_SWITCHES_ID:
+                CANFloat = GetFloat;
+                sprintf(ShtdSwtchBuffer,"%3.2d", int(CANFloat));             // How the heck are we interpreting this data
+                //display.PutString(1,108,ShtdSwtchBuffer);
+            case SYS_PWM_FAN_ID:
+                CANFloat = GetFloat;
+                sprintf(Fan1Buffer,"%3.2d", int(CANFloat*100));                   // Duty Cycle. Figure out the left and the right
+                //display.PutString(3,30,Fan1Buffer);
+                CANFloat2 = GetFloat4;
+                sprintf(Fan2Buffer,"%3.2d", int(CANFloat2*100));
+                //display.PutString(3,107,Fan2Buffer);
+            case SYS_PWM_PUMP_ID:
+                CANFloat = GetFloat;
+                sprintf(Pump1Buffer,"%3.2d", int(CANFloat*100));                   // Duty Cycle. Figure out the left and the right
+                //display.PutString(5,30,Pump1Buffer);
+                CANFloat2 = GetFloat4;
+                sprintf(Pump2Buffer,"%3.2d", int(CANFloat2*100));
+                //display.PutString(5,107,Pump2Buffer);
+            case SYS_DCDC_STATUS_ID:
+                CANFloat = GetFloat;
+                sprintf(DCDCBuffer,"%3.2d", int(CANFloat));             // How the heck are we interpreting this data
+                //display.PutString(7,30,DCDCBuffer);
+            case SYS_IMD_RESIST_ID:
+                CANFloat = GetFloat;
+                sprintf(IMDRBuffer,"%3.2d", int(CANFloat));             // How the heck are we interpreting this data
+                //display.PutString(7,107,IMDRBuffer);
+
+                // CHARGER SCREEN *************************************************************
+            case CHARGECURR_TX_ID:
+                CANFloat = GetFloat;
+                sprintf(ChargeCurrBuffer,"%3.2d", int(CANFloat));                       // Amps
+                //display.PutString(3,94,ChargeCurrBuffer);
+                // OTHER **********************************************************************
+            case MODE_TX_ID:
+                CANFloat = GetFloat;
+                sprintf(&DriveStatus,"%3.2d", int(CANFloat));                       //confirm that I am interpreting this correctly
+                if (DriveStatus == 1) {
+                    BLButtonGreen;
+                } else {
+                    BLButtonRed;
+                }
+            default:
+                break;
         }
-        
-        else if(!biSWBL.read()){
-            NOT_biSWBL_HELD = true;
-        }
-        
-        else{
-            // ignore BiSWBL.read()
-        }
-        
-        if(biSWTR.read() && NOT_biSWTR_HELD){
-            toggle_screen();
-            NOT_biSWTR_HELD = false;
-        }
-        
-        else if(!biSWTR.read()){
-            NOT_biSWTR_HELD = true;
-        }
-        
-        else{
-            // ignore BiSWTR.read()
-        }
-        
-        if(biSWBR.read()){ 
-            reset();
-        }
-    }        
-}    
+    }
+}
+
+void ConnectedStatusCANMsg()
+{
+    CANMessage ConnectedStatus(0x600, &ConnectedStatusBuffer,1);
+    SteeringCANPort.txWrite(ConnectedStatus);
+}
+
 
+void RequestStatusChange()
+{
+    DriveStatusRequestBuffer = !DriveStatus;
+    CANMessage TxDriveStatusRequest(0x601, &DriveStatusRequestBuffer,1);
+    for (int j=0; j<10; j++) {
+        SteeringCANPort.txWrite(TxDriveStatusRequest);
+    }
+    display.PutString(7,0,"    DRIVE REQUEST SENT    ");
+    Thread::wait(750);
+    PrevScreen = !CurrScreen; // To force a screen update
+}
+
+
+void ResetSteeringWheel()
+{
+    NVIC_SystemReset();
+}
+
+
+void ResetCar()
+{
+    CANMessage TxCarReset(0x602,&TxResetBuffer,1);
+    for (int k=0; k<10; k++) {
+        SteeringCANPort.txWriteDirect(TxCarReset);
+    }
+}
\ No newline at end of file