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:
23:6681a38918c6
Parent:
21:56890c41ecf1
Child:
24:ba3428215006
diff -r 56890c41ecf1 -r 6681a38918c6 Steering.cpp
--- a/Steering.cpp	Sat Oct 25 17:24:58 2014 +0000
+++ b/Steering.cpp	Thu Nov 13 23:45:12 2014 +0000
@@ -1,226 +1,23 @@
 #include "Steering.h"
 
-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;    
-}
+bool NOT_biSWBL_HELD;
 
-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 request_status_change(){
+    
+    char drive_status_request;
+    ds_mutex.lock();
+    drive_status_request = !drive_status;
+    ds_mutex.unlock();
+    char * status_string;
 
-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 request_status_change()
-{
-    
-    char drive_status_request = !drive_status;
-    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);
     }
@@ -229,69 +26,69 @@
     display.SelectFont(Arial12,BLACK,ReadData);
     display.GotoXY(26,16);
     display.PrintString("DRIVE STATUS REQUEST");
-    
+
     printf("%s\n\r", status_string);
     return;
 }
 
 void reset()
 {
-    CAN_Steering_Buffer.txWriteDirect(Txmsg_reset);
-    mbed_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);
     display.PrintString(" RESET INITIATED");
     printf("Reset Initiated\n\r");
-    
+
     return;
 }    
 
-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);
+    pc.baud(921600);
     drive_status = 0;
     drive_status_request = 1;
     reset_body = 0;
     ledstream.write(0);
-    call_ledstream.attach(&Powerstream,0.1);
+    NOT_biSWBL_HELD = true;
 }
 
-void update_vars(void const *args) {
+void read_messages(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();
+     
+        if(CAN_Steering_Buffer.rxRead(Rxmsg))
+            if(Rxmsg.id == PCM_STATE_ID){
+              
+                // Mutex to protex shared variables
+                ds_mutex.lock();
+                drive_status = Rxmsg.data[0];
+                ds_mutex.unlock();
+            }
+            
+            if(Rxmsg.id == BATTERY_POWER_ID)
+            {
+                float power_ratio;
+                ftc rcv;
+                rcv.FLOAT=0.0;
+                
+                for(int i=0; i<4; i++){
+                    rcv.C_FLOAT[i] = Rxmsg.data[i];
                 }
+                power_ratio=rcv.FLOAT/80000;
+                ledstream.write(power_ratio);
+            }
         }
     }
 }
 
-int main()
-{
-    // Init all
+int main(){
+    // Initialize, set all variables.
     Init();
     wait(0.1);
     
@@ -299,23 +96,35 @@
     display.GotoXY(10,16);
     display.SelectFont(Arial_14,BLACK,ReadData);
     display.PrintString("Penn Electric Racing");
+    CAN_Steering_Buffer.mode(NoAck);
     
-    Thread update_thread(update_vars);
-    
+     //New thread to read messages.
+     Thread update_thread(read_messages);
+     update_thread.set_priority(osPriorityLow
+     
+
     wait(1);
 
     // Start to read buttons on main thread
-    
     while(1)
     {
-        if(biSWBL.read())
-        {
+        if(biSWBL.read() && NOT_biSWBL_HELD){
             request_status_change();
+            NOT_biSWBL_HELD = false;
         }
-        if(biSWBR.read())
-        { 
+        
+        else if(!biSWBL.read()){
+            NOT_biSWBL_HELD = true;
+        }
+        
+        else{
+            // ignore BiSWBL.read()
+        }
+        
+        
+        if(biSWBR.read()){ 
             reset();
         }
-         
     }        
 }    
+