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:
palimar
Date:
Thu Nov 13 23:45:12 2014 +0000
Revision:
23:6681a38918c6
Parent:
21:56890c41ecf1
Child:
24:ba3428215006
Major Milestone 1: Reset and Start/Stop Functionality works!

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;
kiran_mbed 0:313541d8f8be 4
palimar 23:6681a38918c6 5 void request_status_change(){
palimar 23:6681a38918c6 6
palimar 23:6681a38918c6 7 char drive_status_request;
palimar 23:6681a38918c6 8 ds_mutex.lock();
palimar 23:6681a38918c6 9 drive_status_request = !drive_status;
palimar 23:6681a38918c6 10 ds_mutex.unlock();
palimar 23:6681a38918c6 11 char * status_string;
kiran_mbed 0:313541d8f8be 12
palimar 13:1f05dcd9ae0e 13 if(drive_status_request){
palimar 13:1f05dcd9ae0e 14 status_string = "ON";
palimar 13:1f05dcd9ae0e 15 }
palimar 13:1f05dcd9ae0e 16 else{
palimar 13:1f05dcd9ae0e 17 status_string = "OFF";
palimar 13:1f05dcd9ae0e 18 }
palimar 23:6681a38918c6 19
palimar 23:6681a38918c6 20 CANMessage Txmsg_drive_status_request(0x501,&drive_status_request,1);
palimar 13:1f05dcd9ae0e 21 for(int i = 0; i < 10; i++){
palimar 16:d2953809fb31 22 CAN_Steering_Buffer.txWrite(Txmsg_drive_status_request);
palimar 13:1f05dcd9ae0e 23 }
palimar 6:99e754ac888f 24
kiran_mbed 0:313541d8f8be 25 display.ClearScreen();
kiran_mbed 0:313541d8f8be 26 display.SelectFont(Arial12,BLACK,ReadData);
kiran_mbed 0:313541d8f8be 27 display.GotoXY(26,16);
palimar 21:56890c41ecf1 28 display.PrintString("DRIVE STATUS REQUEST");
palimar 23:6681a38918c6 29
palimar 21:56890c41ecf1 30 printf("%s\n\r", status_string);
kiran_mbed 0:313541d8f8be 31 return;
kiran_mbed 0:313541d8f8be 32 }
kiran_mbed 0:313541d8f8be 33
palimar 9:e946cafa3cae 34 void reset()
kiran_mbed 0:313541d8f8be 35 {
palimar 23:6681a38918c6 36 reset_body = 1;
palimar 23:6681a38918c6 37 CANMessage Txmsg_reset(0x502,&reset_body,1);
palimar 23:6681a38918c6 38 for(int i = 0; i < 10; i++){
palimar 23:6681a38918c6 39 CAN_Steering_Buffer.txWriteDirect(Txmsg_reset);
palimar 23:6681a38918c6 40 }
palimar 23:6681a38918c6 41 NVIC_SystemReset();
kiran_mbed 0:313541d8f8be 42 display.ClearScreen();
kiran_mbed 0:313541d8f8be 43 display.SelectFont(Arial12,BLACK,ReadData);
kiran_mbed 0:313541d8f8be 44 display.GotoXY(16,16);
kiran_mbed 0:313541d8f8be 45 display.PrintString(" RESET INITIATED");
kiran_mbed 0:313541d8f8be 46 printf("Reset Initiated\n\r");
palimar 23:6681a38918c6 47
kiran_mbed 0:313541d8f8be 48 return;
kiran_mbed 0:313541d8f8be 49 }
kiran_mbed 0:313541d8f8be 50
kiran_mbed 0:313541d8f8be 51 void Init()
kiran_mbed 0:313541d8f8be 52 {
palimar 23:6681a38918c6 53 pc.baud(921600);
palimar 9:e946cafa3cae 54 drive_status = 0;
palimar 13:1f05dcd9ae0e 55 drive_status_request = 1;
palimar 9:e946cafa3cae 56 reset_body = 0;
kiran_mbed 0:313541d8f8be 57 ledstream.write(0);
palimar 23:6681a38918c6 58 NOT_biSWBL_HELD = true;
kiran_mbed 0:313541d8f8be 59 }
kiran_mbed 0:313541d8f8be 60
palimar 23:6681a38918c6 61 void read_messages(void const *args) {
palimar 21:56890c41ecf1 62 while (true) {
palimar 21:56890c41ecf1 63 CANMessage Rxmsg;
palimar 23:6681a38918c6 64
palimar 23:6681a38918c6 65 if(CAN_Steering_Buffer.rxRead(Rxmsg))
palimar 23:6681a38918c6 66 if(Rxmsg.id == PCM_STATE_ID){
palimar 23:6681a38918c6 67
palimar 23:6681a38918c6 68 // Mutex to protex shared variables
palimar 23:6681a38918c6 69 ds_mutex.lock();
palimar 23:6681a38918c6 70 drive_status = Rxmsg.data[0];
palimar 23:6681a38918c6 71 ds_mutex.unlock();
palimar 23:6681a38918c6 72 }
palimar 23:6681a38918c6 73
palimar 23:6681a38918c6 74 if(Rxmsg.id == BATTERY_POWER_ID)
palimar 23:6681a38918c6 75 {
palimar 23:6681a38918c6 76 float power_ratio;
palimar 23:6681a38918c6 77 ftc rcv;
palimar 23:6681a38918c6 78 rcv.FLOAT=0.0;
palimar 23:6681a38918c6 79
palimar 23:6681a38918c6 80 for(int i=0; i<4; i++){
palimar 23:6681a38918c6 81 rcv.C_FLOAT[i] = Rxmsg.data[i];
palimar 21:56890c41ecf1 82 }
palimar 23:6681a38918c6 83 power_ratio=rcv.FLOAT/80000;
palimar 23:6681a38918c6 84 ledstream.write(power_ratio);
palimar 23:6681a38918c6 85 }
palimar 21:56890c41ecf1 86 }
palimar 21:56890c41ecf1 87 }
palimar 21:56890c41ecf1 88 }
palimar 21:56890c41ecf1 89
palimar 23:6681a38918c6 90 int main(){
palimar 23:6681a38918c6 91 // Initialize, set all variables.
kiran_mbed 0:313541d8f8be 92 Init();
kiran_mbed 0:313541d8f8be 93 wait(0.1);
palimar 4:a4fb060e7840 94
palimar 4:a4fb060e7840 95 //Init Display
kiran_mbed 0:313541d8f8be 96 display.GotoXY(10,16);
kiran_mbed 0:313541d8f8be 97 display.SelectFont(Arial_14,BLACK,ReadData);
kiran_mbed 0:313541d8f8be 98 display.PrintString("Penn Electric Racing");
palimar 23:6681a38918c6 99 CAN_Steering_Buffer.mode(NoAck);
palimar 6:99e754ac888f 100
palimar 23:6681a38918c6 101 //New thread to read messages.
palimar 23:6681a38918c6 102 Thread update_thread(read_messages);
palimar 23:6681a38918c6 103 update_thread.set_priority(osPriorityLow
palimar 23:6681a38918c6 104
palimar 23:6681a38918c6 105
palimar 6:99e754ac888f 106 wait(1);
palimar 4:a4fb060e7840 107
palimar 4:a4fb060e7840 108 // Start to read buttons on main thread
kiran_mbed 0:313541d8f8be 109 while(1)
kiran_mbed 0:313541d8f8be 110 {
palimar 23:6681a38918c6 111 if(biSWBL.read() && NOT_biSWBL_HELD){
palimar 9:e946cafa3cae 112 request_status_change();
palimar 23:6681a38918c6 113 NOT_biSWBL_HELD = false;
kiran_mbed 0:313541d8f8be 114 }
palimar 23:6681a38918c6 115
palimar 23:6681a38918c6 116 else if(!biSWBL.read()){
palimar 23:6681a38918c6 117 NOT_biSWBL_HELD = true;
palimar 23:6681a38918c6 118 }
palimar 23:6681a38918c6 119
palimar 23:6681a38918c6 120 else{
palimar 23:6681a38918c6 121 // ignore BiSWBL.read()
palimar 23:6681a38918c6 122 }
palimar 23:6681a38918c6 123
palimar 23:6681a38918c6 124
palimar 23:6681a38918c6 125 if(biSWBR.read()){
palimar 9:e946cafa3cae 126 reset();
kiran_mbed 0:313541d8f8be 127 }
kiran_mbed 0:313541d8f8be 128 }
kiran_mbed 0:313541d8f8be 129 }
palimar 23:6681a38918c6 130