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