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

Committer:
tmccurbin
Date:
Sat Nov 15 19:07:13 2014 +0000
Revision:
32:535acb159709
Parent:
26:e0256fd314eb
Child:
35:b42afc973902
Child:
43:47a3ac562be8
New font, Toggle Button Added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kiran_mbed 0:313541d8f8be 1 #include "Steering.h"
kiran_mbed 0:313541d8f8be 2
palimar 23:6681a38918c6 3 bool NOT_biSWBL_HELD;
tmccurbin 32:535acb159709 4 bool NOT_biSWTR_HELD;
kiran_mbed 0:313541d8f8be 5
tmccurbin 32:535acb159709 6 void update_display(void const *args){
tmccurbin 32:535acb159709 7
palimar 26:e0256fd314eb 8
tmccurbin 32:535acb159709 9 while(true){
tmccurbin 32:535acb159709 10
tmccurbin 32:535acb159709 11 switch(curr_screen){
palimar 26:e0256fd314eb 12
tmccurbin 32:535acb159709 13 case HOME_SCREEN:
tmccurbin 32:535acb159709 14
tmccurbin 32:535acb159709 15 display.ClearScreen();
tmccurbin 32:535acb159709 16 display.SelectFont(Arial10,BLACK,ReadData);
tmccurbin 32:535acb159709 17 display.GotoXY(37,0);
tmccurbin 32:535acb159709 18 display.PrintString("Home Screen");
tmccurbin 32:535acb159709 19 break;
palimar 26:e0256fd314eb 20
tmccurbin 32:535acb159709 21 case BATTERY_SCREEN:
tmccurbin 32:535acb159709 22
tmccurbin 32:535acb159709 23 display.ClearScreen();
tmccurbin 32:535acb159709 24 display.SelectFont(Arial10,BLACK,ReadData);
tmccurbin 32:535acb159709 25 display.GotoXY(33,0);
tmccurbin 32:535acb159709 26 display.PrintString("Battery Screen");
tmccurbin 32:535acb159709 27 break;
palimar 26:e0256fd314eb 28
tmccurbin 32:535acb159709 29 default:
tmccurbin 32:535acb159709 30 break;
palimar 26:e0256fd314eb 31
tmccurbin 32:535acb159709 32 }
tmccurbin 32:535acb159709 33 wait(2);
tmccurbin 32:535acb159709 34 }
tmccurbin 32:535acb159709 35 }
tmccurbin 32:535acb159709 36
tmccurbin 32:535acb159709 37 void toggle_screen(){
tmccurbin 32:535acb159709 38 curr_screen = (curr_screen+1) % 2;
palimar 26:e0256fd314eb 39 }
palimar 26:e0256fd314eb 40
palimar 23:6681a38918c6 41 void request_status_change(){
palimar 23:6681a38918c6 42
palimar 23:6681a38918c6 43 char drive_status_request;
palimar 23:6681a38918c6 44 ds_mutex.lock();
palimar 23:6681a38918c6 45 drive_status_request = !drive_status;
palimar 23:6681a38918c6 46 ds_mutex.unlock();
palimar 23:6681a38918c6 47 char * status_string;
kiran_mbed 0:313541d8f8be 48
palimar 13:1f05dcd9ae0e 49 if(drive_status_request){
palimar 13:1f05dcd9ae0e 50 status_string = "ON";
palimar 13:1f05dcd9ae0e 51 }
palimar 13:1f05dcd9ae0e 52 else{
palimar 13:1f05dcd9ae0e 53 status_string = "OFF";
palimar 13:1f05dcd9ae0e 54 }
palimar 23:6681a38918c6 55
palimar 23:6681a38918c6 56 CANMessage Txmsg_drive_status_request(0x501,&drive_status_request,1);
palimar 13:1f05dcd9ae0e 57 for(int i = 0; i < 10; i++){
palimar 16:d2953809fb31 58 CAN_Steering_Buffer.txWrite(Txmsg_drive_status_request);
palimar 13:1f05dcd9ae0e 59 }
palimar 6:99e754ac888f 60
kiran_mbed 0:313541d8f8be 61 display.ClearScreen();
kiran_mbed 0:313541d8f8be 62 display.SelectFont(Arial12,BLACK,ReadData);
kiran_mbed 0:313541d8f8be 63 display.GotoXY(26,16);
palimar 21:56890c41ecf1 64 display.PrintString("DRIVE STATUS REQUEST");
palimar 23:6681a38918c6 65
palimar 21:56890c41ecf1 66 printf("%s\n\r", status_string);
kiran_mbed 0:313541d8f8be 67 return;
kiran_mbed 0:313541d8f8be 68 }
kiran_mbed 0:313541d8f8be 69
palimar 9:e946cafa3cae 70 void reset()
kiran_mbed 0:313541d8f8be 71 {
palimar 23:6681a38918c6 72 reset_body = 1;
palimar 23:6681a38918c6 73 CANMessage Txmsg_reset(0x502,&reset_body,1);
palimar 23:6681a38918c6 74 for(int i = 0; i < 10; i++){
palimar 23:6681a38918c6 75 CAN_Steering_Buffer.txWriteDirect(Txmsg_reset);
tmccurbin 22:ab33195882e3 76 }
palimar 23:6681a38918c6 77 NVIC_SystemReset();
kiran_mbed 0:313541d8f8be 78 display.ClearScreen();
kiran_mbed 0:313541d8f8be 79 display.SelectFont(Arial12,BLACK,ReadData);
kiran_mbed 0:313541d8f8be 80 display.GotoXY(16,16);
kiran_mbed 0:313541d8f8be 81 display.PrintString(" RESET INITIATED");
kiran_mbed 0:313541d8f8be 82 printf("Reset Initiated\n\r");
kiran_mbed 0:313541d8f8be 83
kiran_mbed 0:313541d8f8be 84 return;
kiran_mbed 0:313541d8f8be 85 }
kiran_mbed 0:313541d8f8be 86
kiran_mbed 0:313541d8f8be 87 void Init()
kiran_mbed 0:313541d8f8be 88 {
palimar 23:6681a38918c6 89 pc.baud(921600);
palimar 26:e0256fd314eb 90 curr_screen = HOME_SCREEN;
palimar 9:e946cafa3cae 91 drive_status = 0;
palimar 13:1f05dcd9ae0e 92 drive_status_request = 1;
palimar 9:e946cafa3cae 93 reset_body = 0;
kiran_mbed 0:313541d8f8be 94 ledstream.write(0);
palimar 23:6681a38918c6 95 NOT_biSWBL_HELD = true;
tmccurbin 32:535acb159709 96 NOT_biSWTR_HELD = true;
kiran_mbed 0:313541d8f8be 97 }
kiran_mbed 0:313541d8f8be 98
palimar 23:6681a38918c6 99 void read_messages(void const *args) {
palimar 24:ba3428215006 100
palimar 21:56890c41ecf1 101 while (true) {
palimar 21:56890c41ecf1 102 CANMessage Rxmsg;
palimar 23:6681a38918c6 103
palimar 23:6681a38918c6 104 if(CAN_Steering_Buffer.rxRead(Rxmsg))
palimar 23:6681a38918c6 105 if(Rxmsg.id == PCM_STATE_ID){
palimar 23:6681a38918c6 106
palimar 23:6681a38918c6 107 // Mutex to protex shared variables
palimar 23:6681a38918c6 108 ds_mutex.lock();
palimar 23:6681a38918c6 109 drive_status = Rxmsg.data[0];
palimar 23:6681a38918c6 110 ds_mutex.unlock();
palimar 23:6681a38918c6 111 }
palimar 23:6681a38918c6 112
palimar 23:6681a38918c6 113 if(Rxmsg.id == BATTERY_POWER_ID)
palimar 23:6681a38918c6 114 {
palimar 23:6681a38918c6 115 float power_ratio;
palimar 23:6681a38918c6 116 ftc rcv;
palimar 23:6681a38918c6 117 rcv.FLOAT=0.0;
palimar 23:6681a38918c6 118
palimar 23:6681a38918c6 119 for(int i=0; i<4; i++){
palimar 23:6681a38918c6 120 rcv.C_FLOAT[i] = Rxmsg.data[i];
palimar 21:56890c41ecf1 121 }
palimar 23:6681a38918c6 122 power_ratio=rcv.FLOAT/80000;
palimar 23:6681a38918c6 123 ledstream.write(power_ratio);
palimar 23:6681a38918c6 124 }
palimar 21:56890c41ecf1 125 }
palimar 21:56890c41ecf1 126 }
palimar 24:ba3428215006 127
palimar 21:56890c41ecf1 128
palimar 23:6681a38918c6 129 int main(){
palimar 23:6681a38918c6 130 // Initialize, set all variables.
kiran_mbed 0:313541d8f8be 131 Init();
kiran_mbed 0:313541d8f8be 132 wait(0.1);
palimar 4:a4fb060e7840 133
palimar 4:a4fb060e7840 134 //Init Display
kiran_mbed 0:313541d8f8be 135 display.GotoXY(10,16);
kiran_mbed 0:313541d8f8be 136 display.SelectFont(Arial_14,BLACK,ReadData);
kiran_mbed 0:313541d8f8be 137 display.PrintString("Penn Electric Racing");
palimar 23:6681a38918c6 138 CAN_Steering_Buffer.mode(NoAck);
palimar 6:99e754ac888f 139
tmccurbin 32:535acb159709 140 wait(2);
palimar 26:e0256fd314eb 141
palimar 23:6681a38918c6 142 //New thread to read messages.
tmccurbin 32:535acb159709 143 Thread update_thread(read_messages);
palimar 26:e0256fd314eb 144
palimar 26:e0256fd314eb 145 // display the screen.
tmccurbin 32:535acb159709 146 Thread display_thread(update_display);
palimar 26:e0256fd314eb 147
palimar 4:a4fb060e7840 148
palimar 4:a4fb060e7840 149 // Start to read buttons on main thread
kiran_mbed 0:313541d8f8be 150 while(1)
kiran_mbed 0:313541d8f8be 151 {
palimar 23:6681a38918c6 152 if(biSWBL.read() && NOT_biSWBL_HELD){
palimar 9:e946cafa3cae 153 request_status_change();
palimar 23:6681a38918c6 154 NOT_biSWBL_HELD = false;
kiran_mbed 0:313541d8f8be 155 }
palimar 23:6681a38918c6 156
palimar 23:6681a38918c6 157 else if(!biSWBL.read()){
palimar 23:6681a38918c6 158 NOT_biSWBL_HELD = true;
kiran_mbed 0:313541d8f8be 159 }
palimar 23:6681a38918c6 160
palimar 23:6681a38918c6 161 else{
palimar 23:6681a38918c6 162 // ignore BiSWBL.read()
palimar 23:6681a38918c6 163 }
palimar 23:6681a38918c6 164
tmccurbin 32:535acb159709 165 if(biSWTR.read() && NOT_biSWTR_HELD){
tmccurbin 32:535acb159709 166 printf("ff\r\n");
tmccurbin 32:535acb159709 167 toggle_screen();
tmccurbin 32:535acb159709 168 NOT_biSWTR_HELD = false;
tmccurbin 32:535acb159709 169 }
tmccurbin 32:535acb159709 170
tmccurbin 32:535acb159709 171 else if(!biSWTR.read()){
tmccurbin 32:535acb159709 172 NOT_biSWTR_HELD = true;
tmccurbin 32:535acb159709 173 }
tmccurbin 32:535acb159709 174
tmccurbin 32:535acb159709 175 else{
tmccurbin 32:535acb159709 176 // ignore BiSWTR.read()
tmccurbin 32:535acb159709 177 }
tmccurbin 32:535acb159709 178
tmccurbin 32:535acb159709 179
palimar 23:6681a38918c6 180 if(biSWBR.read()){
palimar 9:e946cafa3cae 181 reset();
kiran_mbed 0:313541d8f8be 182 }
kiran_mbed 0:313541d8f8be 183 }
kiran_mbed 0:313541d8f8be 184 }
palimar 23:6681a38918c6 185