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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers OldVersion.h Source File

OldVersion.h

00001 /*
00002 Steering.cpp
00003 #include "Steering.h"
00004 
00005 bool NOT_biSWBL_HELD;
00006 bool NOT_biSWTR_HELD;
00007 
00008 void update_display(void const *args){
00009     
00010     while(true){
00011         
00012         if(screen_flags[curr_screen] || should_redraw){
00013         screen_flags[curr_screen] = 0;
00014         should_redraw = false;
00015         switch(curr_screen){
00016     
00017             case HOME_SCREEN:{
00018                 
00019                 display.ClearScreen();
00020                 display.SelectFont(Arial10,BLACK,ReadData);
00021                 display.GotoXY(37,0);
00022                 display.PrintString(" Home Screen");
00023                 
00024                 printf("Battery Voltage: %d\n\r", vars_list->get_value(PCM_STATE_ID));
00025               
00026                 display.GotoXY(16,16);
00027                 if(vars_list->get_value(PCM_STATE_ID) == 0){
00028                     display.PutString(3, 30, "Drive Status: OFF");
00029                 }
00030                 else{
00031                     display.PrintString("Drive Status: ON");
00032                 }
00033                 break;
00034                 }
00035             
00036             case BATTERY_SCREEN:
00037             
00038                 display.ClearScreen();
00039                 display.SelectF ont(Arial10,BLACK,ReadData);
00040                 display.GotoXY(33,0);
00041                 display.PrintString(" Battery Screen");
00042                 break;
00043         
00044             default:
00045                 break;    
00046         
00047             }
00048         }
00049         wait(2);   
00050     }
00051 }
00052 
00053 void toggle_screen(){
00054     should_redraw = true;
00055     curr_screen = (curr_screen+1) % NUM_SCREEN; 
00056 }
00057 
00058 void request_status_change(){
00059     
00060     char drive_status_request;
00061     ds_mutex.lock();
00062     drive_status_request = !(vars_list->get_value(PCM_STATE_ID));
00063 
00064     ds_mutex.unlock();
00065     char * status_string;
00066 
00067     if(drive_status_request){
00068         status_string = "ON";
00069     }
00070     else{
00071         status_string = "OFF";
00072     }
00073 
00074     CANMessage Txmsg_drive_status_request(0x501,&drive_status_request,1);//Encode Tx messages
00075     for(int i = 0; i < 10; i++){
00076         CAN_Steering_Buffer.txWrite(Txmsg_drive_status_request);
00077     }
00078 
00079     printf("%s\n\r", status_string);
00080     return;
00081 }
00082 
00083 void reset()
00084 {
00085     reset_body = 1;
00086     CANMessage Txmsg_reset(0x502,&reset_body,1);                        //Encode Tx messages
00087     for(int i = 0; i < 10; i++){
00088         CAN_Steering_Buffer.txWriteDirect(Txmsg_reset);
00089     }
00090     NVIC_SystemReset();
00091     display.ClearScreen();
00092     display.SelectFont(Arial12,BLACK,ReadData);
00093     display.GotoXY(16,16);
00094     printf("Reset Initiated\n\r");
00095 
00096     return;
00097 }    
00098 
00099 void Init()
00100 {
00101     should_redraw = true;
00102     pc.baud(921600);
00103     curr_screen = HOME_SCREEN;
00104     drive_status = 0;
00105     drive_status_request = 1;
00106     reset_body = 0;
00107     ledstream.write(0);
00108     NOT_biSWBL_HELD = true;
00109     NOT_biSWTR_HELD = true;
00110     
00111     vars_list = new variables();//Avoid Heap
00112     
00113     vars_list->add(PCM_STATE_ID, HOME_SCREEN);
00114     vars_list->add(BATTERY_VOLTAGE_ID, BATTERY_SCREEN);
00115     vars_list->add(BATTERY_POWER_ID, BATTERY_SCREEN);
00116     vars_list->add(BATTERY_CURRENT_ID, BATTERY_SCREEN);
00117     
00118 }
00119 
00120 void read_messages(void const *args) {
00121     
00122     while (true) {
00123         
00124         CANMessage Rxmsg;
00125         if(CAN_Steering_Buffer.rxRead(Rxmsg)){
00126             id_node = vars_list->get_node(Rxmsg.id);
00127             if(id_node != NULL){
00128                 if(id_node->value != Rxmsg.data[0]){
00129                     screen_flags[id_node->screen] = 1;  
00130                 }
00131                 id_node->value = Rxmsg.data[0];
00132             }
00133         }
00134     }
00135 }
00136 
00137 int main(){
00138     // Initialize, set all variables.
00139     Init();
00140     wait(0.1);
00141     
00142     //Init Display
00143     display.GotoXY(10,16);
00144     display.SelectFont(Arial_14,BLACK,ReadData);
00145     display.PrintString("Penn Electric Racing");
00146     CAN_Steering_Buffer.mode(NoAck);
00147     
00148     wait(0.5);
00149      
00150      //New thread to read messages.
00151     Thread update_thread(read_messages);                                //Prioritise
00152         
00153     // display the screen.
00154     Thread display_thread(update_display);                              //Prioritise
00155       
00156 
00157     // Start to read buttons on main thread
00158     while(1)
00159     {
00160        // Thread::wait(100);
00161         if(biSWBL.read() && NOT_biSWBL_HELD){
00162             request_status_change();
00163             NOT_biSWBL_HELD = false;
00164         }
00165         
00166         else if(!biSWBL.read()){
00167             NOT_biSWBL_HELD = true;
00168         }
00169         
00170         else{
00171             // ignore BiSWBL.read()
00172         }
00173         
00174         if(biSWTR.read() && NOT_biSWTR_HELD){
00175             toggle_screen();
00176             NOT_biSWTR_HELD = false;
00177         }
00178         
00179         else if(!biSWTR.read()){
00180             NOT_biSWTR_HELD = true;
00181         }
00182         
00183         else{
00184             // ignore BiSWTR.read()
00185         }
00186         
00187         if(biSWBR.read()){ 
00188             reset();
00189         }
00190     }        
00191 }
00192 
00193 --------------------------------------------------------------------------------------------------------------
00194 Steering.h
00195 #ifndef _STEERING_H
00196 #define _STEERING_H
00197 
00198 #include "mbed.h"
00199 #include "rtos.h"
00200 #include "KS0108.h"
00201 
00202 #include "Arial10.h"
00203 #include "Arial12.h"
00204 #include "Arial14.h"
00205 #include "Comic24.h"
00206 #include "vivaldi16.h"
00207 #include "CANBuffer.h"
00208 #include "variables.h"
00209 
00210 
00211 #include "LPCDigitalIn.h"
00212 
00213 #define SWITCH_ID 410
00214 
00215 #define BATTERY_VOLTAGE_ID 0x304
00216 #define BATTERY_POWER_ID 0x306
00217 #define BATTERY_CURRENT_ID 0x305
00218 
00219 #define PCM_STATE_ID 0x201
00220 
00221 #define BATTERY_MIN_CELLVOLTAGE_ID 0x301
00222 #define BATTERY_MAX_CELLVOLTAGE_ID 0x300
00223 #define BATTERY_AVG_CELLVOLTAGE_ID 0x302
00224 
00225 #define BATTERY_MIN_CELLTEMPERATURE_ID 0x30A
00226 #define BATTERY_MAX_CELLTEMPERATURE_ID 0x309
00227 #define BATTERY_AVG_CELLTEMPERATURE_ID 0x30B
00228 #define AMS_BATTERY_STATE 0x30E          // AIRS 7 and 6 // Precharge 3
00229 
00230 #define NUM_SCREEN 2
00231 
00232 #define HOME_SCREEN 0
00233 #define BATTERY_SCREEN 1
00234 
00235 Serial pc(USBTX,USBRX);
00236 CANBuffer CAN_Steering_Buffer(CAN1, MEDIUM, p3_26);
00237 
00238 
00239 KS0108 display(p26, p21, p22, p23, p25, p24, p8, p7, p6, p5, p13, p14, p12, p11); 
00240 //Ticker call_ledstream;
00241 
00242 LPCDigitalOut l1(p1_28,1);//    SW2
00243 LPCDigitalOut l2(p1_26,1);//    SW4
00244     
00245 LPCDigitalOut l3(p1_24,1);//    SW6
00246 LPCDigitalOut l4(p0_24,1);//    SW8
00247 
00248 LPCDigitalOut u1(p1_14,0);//    SW9 
00249 LPCDigitalOut u2(p1_9,0);//     SW11
00250 
00251 LPCDigitalOut u3(p1_4,0);//     SW13
00252 LPCDigitalOut u4(p1_0,0);//     SW15
00253 
00254 AnalogOut ledstream(p18);  // This appears to iniialize the pin as an analog out, and probably defaults to low.  Without this, the LED bar would have a few lights on.
00255 
00256 //SW1 - SW3
00257 LPCDigitalOut boSW1(p1_29,1);
00258 LPCDigitalIn biSWBR(p1_27, PullDown);     //BRight
00259 
00260 LPCDigitalOut boSW5(p1_25,1);
00261 LPCDigitalIn biSWBL(p1_22, PullDown);     //BLeft
00262 
00263 LPCDigitalOut boSW10(p1_10,1);
00264 LPCDigitalIn biSWTR(p1_8, PullDown);     //TRight
00265 
00266 LPCDigitalOut boSW14(p1_1,1);
00267 LPCDigitalIn biSWTL(p0_25, PullDown);    //TLeft
00268 
00269 typedef union convert{
00270         float FLOAT;
00271         char C_FLOAT[4];
00272         }ftc;
00273 
00274 char SwitchName[15][13]={
00275     "fuse",
00276     "ams",
00277     "imd",
00278     "pcm",
00279     "brkp",
00280     "lft",
00281     "intl",
00282     "brko",
00283     "ckpt",
00284     "rgt",
00285     "hvd",
00286     "tsms"
00287 };
00288 
00289 int SwitchPosition[13][2]={
00290     {0,16},     //fuse
00291     {25,16},    //ams
00292     {50,16},    //imd
00293     {70,16},    //pcm
00294     {93,16},    //brkp
00295     {117,16},   //lft
00296     {0,32},     //intl
00297     {17,32},    //brko
00298     {42,32},    //ckpt
00299     {65,32},    //rgt
00300     {81,32},    //hvd
00301     {102,32},   //tsm
00302     };
00303     
00304 int curr_screen;
00305 int screen_flags[NUM_SCREEN]; 
00306 bool should_redraw;
00307 
00308 char drive_status;
00309 char drive_status_request;
00310 char reset_body;
00311 
00312 node * id_node = NULL;
00313 variables * vars_list;
00314 
00315 
00316 Mutex ds_mutex;
00317 
00318 /*
00319 void read_messages(void const *args) {
00320     
00321     while (true) {
00322         CANMessage Rxmsg;
00323      
00324         if(CAN_Steering_Buffer.rxRead(Rxmsg))
00325             if(Rxmsg.id == PCM_STATE_ID){
00326               
00327                 // Mutex to protex shared variables
00328                 ds_mutex.lock();
00329                 drive_status = Rxmsg.data[0];
00330                 ds_mutex.unlock();
00331             }
00332             
00333             if(Rxmsg.id == BATTERY_POWER_ID)
00334             {
00335                 float power_ratio;
00336                 ftc rcv;
00337                 rcv.FLOAT=0.0;
00338                 
00339                 for(int i=0; i<4; i++){
00340                     rcv.C_FLOAT[i] = Rxmsg.data[i];
00341                 }
00342                 power_ratio=rcv.FLOAT/80000;
00343                 ledstream.write(power_ratio);
00344             }
00345         }
00346     }
00347 */
00348 
00349 #endif /* STEERING_H */    
00350 -----------------------------------------------------------------------------------------------------------------------------
00351 Variables.h
00352 #include "node.h"
00353 
00354 class variables{
00355     
00356     public:
00357     
00358         variables();
00359         ~variables();
00360         void add(int i, int s);
00361         int get_screen(int i);
00362         char get_value(int i);
00363         int set_value(int i, char v);
00364         node * get_node(int i);
00365         
00366     
00367         int size;
00368         node * head;
00369         node * tail;
00370 
00371 };
00372 
00373 variables::variables(){
00374     size = 0;
00375     head = NULL;
00376     tail = NULL;
00377 }
00378 
00379 variables::~variables(){
00380 
00381     node * curr = head;
00382     node * next = head;
00383     
00384     while(curr != NULL){
00385         next = curr->next;
00386         delete(curr);
00387         curr = next;
00388     }
00389     
00390     head =  NULL;
00391     tail = NULL;
00392     size = 0;
00393 }
00394 
00395 void variables::add(int i, int s){
00396     
00397     if(head == NULL){
00398         head = new node(i, s);
00399         tail = head;
00400     }
00401     
00402     else{
00403         tail->next = new node(i,s);
00404         tail = tail->next;
00405     }
00406     size++;
00407 }
00408 
00409 node * variables::get_node(int i){
00410 
00411     node * curr = head;
00412     
00413     while(curr != NULL){
00414         if(curr->id == i){
00415             return curr;
00416         }
00417         else{
00418             curr = curr->next;
00419         }
00420     }
00421     return NULL;
00422 }    
00423 
00424 int variables::get_screen(int i){
00425 
00426     node * curr = head;
00427     
00428     while(curr != NULL){
00429         if(curr->id == i){
00430             return curr->screen;
00431         }
00432         else{
00433             curr = curr->next;
00434         }
00435     }
00436     return -1;
00437 }
00438 
00439 int variables::set_value(int i, char v){
00440 
00441     node * curr = head;
00442     
00443     while(curr != NULL){
00444         if(curr->id == i){
00445             curr->set_val(v);
00446             return 0;
00447         }
00448         else{
00449             curr = curr->next;
00450         }
00451     }
00452     return -1;
00453 }
00454 
00455 char variables::get_value(int i){
00456 
00457     node * curr = head;
00458     
00459     while(curr != NULL){
00460         if(curr->id == i){
00461             return curr->value;
00462         }
00463         else{
00464             curr = curr->next;
00465         }
00466     }
00467     return 0;
00468 }
00469 --------------------------------------------------------------------------------------------------------------
00470 Node.h
00471 class node{
00472     
00473     public:
00474     
00475         node();
00476         node(int id, int screen);
00477         void set_val(char v);
00478         ~node();
00479         
00480         int screen;
00481         int id;
00482         char value;
00483         node * next;
00484         
00485 };
00486 
00487 node::node(){
00488     screen = 0;
00489     id = 0;
00490     next = NULL;
00491 }
00492 
00493 node::node(int i, int s){
00494     id = i;
00495     screen = s;
00496     next = NULL;
00497 }
00498 
00499 void node::set_val(char v){
00500     value = v;
00501 }
00502 
00503 node::~node(){
00504 }
00505 */