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@25:fc63a5bffffc, 2014-11-14 (annotated)
- Committer:
- palimar
- Date:
- Fri Nov 14 00:21:05 2014 +0000
- Revision:
- 25:fc63a5bffffc
- Parent:
- 24:ba3428215006
- Parent:
- 22:ab33195882e3
- Child:
- 26:e0256fd314eb
Merged Tips
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 | 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); |
tmccurbin | 22:ab33195882e3 | 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"); |
kiran_mbed | 0:313541d8f8be | 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 | 24:ba3428215006 | 62 | |
palimar | 21:56890c41ecf1 | 63 | while (true) { |
palimar | 21:56890c41ecf1 | 64 | CANMessage Rxmsg; |
palimar | 23:6681a38918c6 | 65 | |
palimar | 23:6681a38918c6 | 66 | if(CAN_Steering_Buffer.rxRead(Rxmsg)) |
palimar | 23:6681a38918c6 | 67 | if(Rxmsg.id == PCM_STATE_ID){ |
palimar | 23:6681a38918c6 | 68 | |
palimar | 23:6681a38918c6 | 69 | // Mutex to protex shared variables |
palimar | 23:6681a38918c6 | 70 | ds_mutex.lock(); |
palimar | 23:6681a38918c6 | 71 | drive_status = Rxmsg.data[0]; |
palimar | 23:6681a38918c6 | 72 | ds_mutex.unlock(); |
palimar | 23:6681a38918c6 | 73 | } |
palimar | 23:6681a38918c6 | 74 | |
palimar | 23:6681a38918c6 | 75 | if(Rxmsg.id == BATTERY_POWER_ID) |
palimar | 23:6681a38918c6 | 76 | { |
palimar | 23:6681a38918c6 | 77 | float power_ratio; |
palimar | 23:6681a38918c6 | 78 | ftc rcv; |
palimar | 23:6681a38918c6 | 79 | rcv.FLOAT=0.0; |
palimar | 23:6681a38918c6 | 80 | |
palimar | 23:6681a38918c6 | 81 | for(int i=0; i<4; i++){ |
palimar | 23:6681a38918c6 | 82 | rcv.C_FLOAT[i] = Rxmsg.data[i]; |
palimar | 21:56890c41ecf1 | 83 | } |
palimar | 23:6681a38918c6 | 84 | power_ratio=rcv.FLOAT/80000; |
palimar | 23:6681a38918c6 | 85 | ledstream.write(power_ratio); |
palimar | 23:6681a38918c6 | 86 | } |
palimar | 21:56890c41ecf1 | 87 | } |
palimar | 21:56890c41ecf1 | 88 | } |
palimar | 24:ba3428215006 | 89 | |
palimar | 21:56890c41ecf1 | 90 | |
palimar | 23:6681a38918c6 | 91 | int main(){ |
palimar | 23:6681a38918c6 | 92 | // Initialize, set all variables. |
kiran_mbed | 0:313541d8f8be | 93 | Init(); |
kiran_mbed | 0:313541d8f8be | 94 | wait(0.1); |
palimar | 4:a4fb060e7840 | 95 | |
palimar | 4:a4fb060e7840 | 96 | //Init Display |
kiran_mbed | 0:313541d8f8be | 97 | display.GotoXY(10,16); |
kiran_mbed | 0:313541d8f8be | 98 | display.SelectFont(Arial_14,BLACK,ReadData); |
kiran_mbed | 0:313541d8f8be | 99 | display.PrintString("Penn Electric Racing"); |
palimar | 23:6681a38918c6 | 100 | CAN_Steering_Buffer.mode(NoAck); |
palimar | 6:99e754ac888f | 101 | |
palimar | 23:6681a38918c6 | 102 | //New thread to read messages. |
palimar | 23:6681a38918c6 | 103 | Thread update_thread(read_messages); |
palimar | 24:ba3428215006 | 104 | update_thread.set_priority(osPriorityLow); |
palimar | 23:6681a38918c6 | 105 | |
palimar | 23:6681a38918c6 | 106 | |
palimar | 6:99e754ac888f | 107 | wait(1); |
palimar | 4:a4fb060e7840 | 108 | |
palimar | 4:a4fb060e7840 | 109 | // Start to read buttons on main thread |
kiran_mbed | 0:313541d8f8be | 110 | while(1) |
kiran_mbed | 0:313541d8f8be | 111 | { |
palimar | 23:6681a38918c6 | 112 | if(biSWBL.read() && NOT_biSWBL_HELD){ |
palimar | 9:e946cafa3cae | 113 | request_status_change(); |
palimar | 23:6681a38918c6 | 114 | NOT_biSWBL_HELD = false; |
kiran_mbed | 0:313541d8f8be | 115 | } |
palimar | 23:6681a38918c6 | 116 | |
palimar | 23:6681a38918c6 | 117 | else if(!biSWBL.read()){ |
palimar | 23:6681a38918c6 | 118 | NOT_biSWBL_HELD = true; |
kiran_mbed | 0:313541d8f8be | 119 | } |
palimar | 23:6681a38918c6 | 120 | |
palimar | 23:6681a38918c6 | 121 | else{ |
palimar | 23:6681a38918c6 | 122 | // ignore BiSWBL.read() |
palimar | 23:6681a38918c6 | 123 | } |
palimar | 23:6681a38918c6 | 124 | |
palimar | 23:6681a38918c6 | 125 | |
palimar | 23:6681a38918c6 | 126 | if(biSWBR.read()){ |
palimar | 9:e946cafa3cae | 127 | reset(); |
kiran_mbed | 0:313541d8f8be | 128 | } |
kiran_mbed | 0:313541d8f8be | 129 | } |
kiran_mbed | 0:313541d8f8be | 130 | } |
palimar | 23:6681a38918c6 | 131 |