Nenad Djalovic / Mbed 2 deprecated IntegrationCAN_7jul_copy

Dependencies:   mbed LCD_DISCO_F469NIa SD_DISCO_F469NI BSP_DISCO_F469NIa EEPROM_DISCO_F469NI

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CAN_library.cpp Source File

CAN_library.cpp

00001 #include "CAN_library.h"
00002 
00003 extern CAN can;
00004 extern Serial pc;
00005 extern SD_DISCO_F469NI sd;
00006 //extern EEPROM_DISCO_F469NI eep;
00007 extern Timer t_int,t_store;
00008 extern CANMessage msgDTA1, msgDTA2, msgDTA3, msgDTA4, msgDTA5, msgDTA6, msgLVDTFront, msgLVDTRear, msgBrakes, msgDistance;
00009 extern uint16_t rx_flag;
00010 extern uint8_t distance_flag;
00011 extern uint8_t lvdtref;
00012 
00013 extern uint16_t Capacity0,HV_Voltage0, Rpm0, Speed0, Gear0, Water_Temp0, Bat_Temp0, TPS0, Brakes0, MAP0, Air_Temp0, Lambda0, Volts0;
00014 extern uint16_t Capacity,HV_Voltage, Rpm,  Speed,  Gear,  Water_Temp,  Bat_Temp,  TPS,  Brakes,  MAP,  Air_Temp,  Lambda,  Volts;
00015 extern int FL_LVDT0, FR_LVDT0, RL_LVDT0, RR_LVDT0, FL_LVDT,  FR_LVDT,  RL_LVDT,  RR_LVDT;
00016 extern int FL_LVDT_Ref,FR_LVDT_Ref,RL_LVDT_Ref,RR_LVDT_Ref;
00017 int LVDT_Max=1024;
00018 //extern float Meter_counter,Meter_counter0;
00019 extern char cmd[3];
00020 extern int addr;
00021 extern I2C i2c;
00022 
00023 extern void showLedRpm(int rpm);
00024 
00025 
00026 void CANMsgReceive(){                     // CAN RX Interrupt Function
00027     CANMessage tmpMsg;
00028     if (can.read(tmpMsg)) {                 //Detect message
00029         //pc.printf("%x\n",tmpMsg.id);       
00030         switch(tmpMsg.id){                  //Find which DTA message is received 
00031             case(0x0 | 0x2000):
00032                 rx_flag|=(1<<0);
00033                 msgDTA1=tmpMsg;
00034                 //pc.printf("%x\n", tmpMsg.id);
00035                 break;
00036             case(0x1 | 0x2001):
00037                 rx_flag|=(1<<1);
00038                 msgDTA2=tmpMsg;
00039                 //pc.printf("rx_flag=%x\n",rx_flag);
00040                 break;
00041             case(0x2 | 0x2002):
00042                 rx_flag|=(1<<2);
00043                 msgDTA3=tmpMsg;
00044                 break;
00045             case(0x3 | 0x2003):
00046                 rx_flag|=(1<<3);
00047                 msgDTA4=tmpMsg;
00048                 break;
00049             case(0x4 | 0x2004):
00050                 rx_flag|=(1<<4);
00051                 msgDTA5=tmpMsg;
00052                 break;
00053             case(0x5 | 0x2005):
00054                 rx_flag|=(1<<5);
00055                 msgDTA6=tmpMsg;
00056                 break;
00057             case(0x1006):
00058                 rx_flag|=(1<<6);
00059                 msgLVDTFront=tmpMsg;
00060                 break;
00061             case(0x1007):
00062                 rx_flag|=(1<<7);
00063                 msgLVDTRear=tmpMsg;
00064                 break;
00065             case(0x1008):
00066                 rx_flag|=(1<<8);
00067                 msgBrakes=tmpMsg;
00068                 break;
00069             case(0x1009):
00070                 rx_flag|=(1<<9);
00071                 msgDistance=tmpMsg;
00072         };
00073         //pc.printf("rx_flag=%d\n",rx_flag);
00074     };
00075 };
00076 
00077 void UpdateInfo(){                          // Update info for received values
00078     int noID=10;
00079     uint16_t *newData;
00080     for (int i=0;i<noID;i++){               //For loop goes trough Message IDs
00081         uint16_t flagBit;
00082         flagBit = rx_flag & (1<<i);         //Set flag bit for adequate message. If there is a message waiting to be stored, flagBit will be different than 0.
00083         if (flagBit) {                      // =/=0, there is a message, =0, there is no message                      
00084             switch(i){
00085                 case(0):
00086                     newData=AllocData(msgDTA1);     //Allocate message to a receiver
00087                     UpdateInfoDTA1(newData);        //Update variables
00088                     rx_flag = rx_flag ^ (1<<0);     //Set flag of adequate bit to 0
00089                     break;
00090                 case(1):
00091                     newData=AllocData(msgDTA2);
00092                     UpdateInfoDTA2(newData);
00093                     rx_flag = rx_flag ^ (1<<1);
00094                     break;
00095                 case(2):
00096                     newData=AllocData(msgDTA3);
00097                     UpdateInfoDTA3(newData);
00098                     rx_flag = rx_flag ^ (1<<2);
00099                     break;
00100                 case(3):
00101                     newData=AllocData(msgDTA4);
00102                     UpdateInfoDTA4(newData);
00103                     rx_flag = rx_flag ^ (1<<3);
00104                     break;
00105                 case(4):
00106                     newData=AllocData(msgDTA5);
00107                     UpdateInfoDTA5(newData);
00108                     rx_flag = rx_flag ^ (1<<4);
00109                     break;
00110                 case(5):
00111                     newData=AllocData(msgDTA6);
00112                     UpdateInfoDTA6(newData);
00113                     rx_flag = rx_flag ^ (1<<5);
00114                     break;
00115                 case(6):
00116                     newData=AllocData(msgLVDTFront);
00117                     UpdateInfoLVDTFront(newData);
00118                     rx_flag = rx_flag ^ (1<<6);
00119                     break;
00120                 case(7):
00121                     newData=AllocData(msgLVDTRear);
00122                     UpdateInfoLVDTRear(newData);
00123                     rx_flag = rx_flag ^ (1<<7);
00124                     break;
00125                 case(8):
00126                     newData=AllocData(msgBrakes);
00127                     UpdateInfoBrakes(newData);
00128                     rx_flag = rx_flag ^ (1<<8);
00129                     break;
00130                 case(9):
00131                     newData=AllocData(msgDistance);
00132                     //if(distance_flag==0){
00133                     //    InitDistanceMeter(newData);
00134                     //    distance_flag=1;
00135                     //};
00136                     rx_flag = rx_flag ^ (1<<9);
00137                 // for each new id add new case statement
00138             };
00139         };
00140     };
00141     //UpdateMeterCounter();
00142 };
00143 
00144 uint16_t * AllocData(CANMessage msg){                   //Unpack CAN message 
00145     uint16_t newData[4];
00146     newData[0]=(msg.data[1]<<8) | msg.data[0];
00147     newData[1]=(msg.data[3]<<8) | msg.data[2];
00148     newData[2]=(msg.data[5]<<8) | msg.data[4];
00149     newData[3]=(msg.data[7]<<8) | msg.data[6];
00150     return newData;
00151 };
00152 
00153 /*void InitDistanceMeter(uint16_t newData[4]){
00154     uint32_t DistanceBuffer;
00155     DistanceBuffer=(uint32_t)((newData[1]<<16) | (newData[0]));
00156     Meter_counter=float(DistanceBuffer)*10;
00157     //pc.printf("Init success, start km=%d,%f\n",DistanceBuffer,Meter_counter);
00158     t_int.start();
00159     t_store.start();
00160 };*/
00161 
00162 
00163 void UpdateInfoDTA1(uint16_t newData[4]){                //Update values for DTA1 message
00164     if (newData[0]>=0 && newData[0]<=13000){
00165         Rpm0=Rpm;
00166         Rpm=newData[0];
00167         showLedRpm(Rpm);
00168     };
00169     if (newData[1]>0 && newData[1]<100){
00170         TPS0=TPS;
00171         TPS=newData[1];
00172     };
00173     if (newData[2]>0 && newData[2]<1000){
00174         Water_Temp0=Water_Temp;
00175         Water_Temp=newData[2];
00176     };
00177     if (newData[3]>0 && newData[3]<1000){
00178         Air_Temp0=Air_Temp;
00179         Air_Temp=newData[3];
00180     };
00181 };
00182 
00183 void UpdateInfoDTA2(uint16_t newData[4]){                //Update values for DTA2 message
00184     if (newData[0]>0 && newData[0]<200){
00185         MAP0=MAP;
00186         MAP=newData[0];
00187     };
00188     if (newData[1]>0 && newData[2]<10000){
00189         Lambda0=Lambda;
00190         Lambda=newData[1];
00191     };
00192     if (newData[2]>=0 && newData[2]<1500){
00193         if (abs(Speed-Speed0)>10 ){
00194             Speed0=Speed;
00195         };
00196         Speed=newData[2];
00197     };
00198 };
00199 
00200 void UpdateInfoDTA3(uint16_t newData[4]){                //Update values for DTA3 message
00201     if (newData[1]>0 && newData[1]<200){
00202         Bat_Temp0=Bat_Temp;
00203         Bat_Temp=newData[1];
00204     };
00205     if (newData[2]>0 && newData[2]<200){
00206         Volts0=Volts;
00207         Volts=newData[2];
00208     };
00209 };
00210 
00211 void UpdateInfoDTA4(uint16_t newData[4]){                //Update values for DTA4 message
00212     if (newData[0]>=0 && newData[0]<=6){
00213         Gear0=Gear;
00214         Gear=newData[0];
00215     };
00216 };
00217 
00218 void UpdateInfoDTA5(uint16_t newData[4]){                //Update values for DTA5 message
00219 };
00220 
00221 void UpdateInfoDTA6(uint16_t newData[4]){                //Update values for DTA6 message
00222 };
00223 
00224 /*void UpdateMeterCounter(){
00225     float ti=t_int.read();
00226     if(ti>0.1){
00227         Meter_counter0=Meter_counter;
00228         Meter_counter=Meter_counter+(float)(Speed*0.0277778);    // 0.1*1000/3600
00229         t_int.reset();
00230     };
00231     if(t_store>2){
00232         char DistanceBuffer8[8];
00233         uint32_t DistanceBuffer32=uint32_t(Meter_counter);
00234         DistanceBuffer32=uint32_t(Meter_counter);
00235         DistanceBuffer8[0]=uint8_t(DistanceBuffer32>>24);
00236         DistanceBuffer8[1]=uint8_t((DistanceBuffer32 & (uint32_t)0x00FF0000)>>16);
00237         DistanceBuffer8[2]=uint8_t((DistanceBuffer32 & (uint32_t)0x0000FF00)>>8);
00238         DistanceBuffer8[3]=uint8_t((DistanceBuffer32 & (uint32_t)0x000000FF));
00239         CANMessage msg;
00240         if(can.write(CANMessage((uint16_t)DISTANCE,DistanceBuffer8, 8))) {
00241             pc.printf("%d\n",DistanceBuffer32);
00242         };
00243         t_store.reset();
00244     
00245         uint32_t distance_buffer;
00246         distance_buffer=uint32_t(Meter_counter) ;
00247         pc.printf("distance=%d\n",distance_buffer);
00248         distance_buffer=0xFF000000;
00249         if(sd.WriteBlocks((uint32_t*)distance_buffer,DISTANCE_MEMORY_ADDR,1,SD_DATATIMEOUT)){
00250             pc.printf("Storage success\n");
00251         }else{
00252             pc.printf("Storage failed\n");
00253         };
00254         
00255         uint32_t DistanceBuffer32;
00256         uint8_t DistanceBuffer8[4];
00257         DistanceBuffer32=uint32_t(Meter_counter);
00258         DistanceBuffer8[0]=DistanceBuffer32>>24;
00259         DistanceBuffer8[1]=(DistanceBuffer32 & (uint32_t)0x00FF0000)>>16;
00260         DistanceBuffer8[2]=(DistanceBuffer32 & (uint32_t)0x0000FF00)>>8;
00261         DistanceBuffer8[3]=(DistanceBuffer32 & (uint32_t)0x000000FF);
00262         if(eep.WriteBuffer(DistanceBuffer8,0,4)){
00263             pc.printf("Write success,Distance=%d\n",DistanceBuffer32);
00264         };
00265     };
00266 };   
00267 
00268 void send(uint16_t ID, char dta[8]) {
00269     if(can.write(CANMessage((uint16_t)ID,dta, 8))) {
00270         printf("%x\n",ID);
00271         printf("Message sent: info1=%x%x, info2=%x%x, info3=%x%x, info4=%x%x\n", dta[1],dta[0],dta[3],dta[2],dta[5],dta[4],dta[7],dta[6]);
00272     }else{
00273         printf("Shit\n");
00274     } 
00275 };*/
00276 
00277 void UpdateInfoLVDTFront(uint16_t newData[4]){           //Update values for LVDTFront message
00278     if (newData[0]>0 && newData[0]<=1024){
00279         FL_LVDT0=FL_LVDT;                                    //LVDTs are firstly written their voltage value, then overwritten by calculated pertentage value.
00280         FL_LVDT=newData[0];
00281         if(!(lvdtref & (1<<3))){                             //Check if its the first time LVDT value is received. If yes than store it as Referrent value.
00282             if (FL_LVDT>FL_LVDT_Ref){
00283                 FL_LVDT=(FL_LVDT-FL_LVDT_Ref)*100/(LVDT_Max-FL_LVDT_Ref);
00284             }else{
00285                 FL_LVDT=(FL_LVDT-FL_LVDT_Ref)*100/FL_LVDT_Ref;
00286             };
00287         }else{    
00288             FL_LVDT_Ref=FL_LVDT;
00289             FL_LVDT=0;
00290             lvdtref=lvdtref^(1<<3);
00291         };
00292     };
00293     
00294     if (newData[1]>0 && newData[1]<=1024){
00295         FR_LVDT0=FR_LVDT;
00296         FR_LVDT=newData[1];    
00297         if(!(lvdtref & (1<<2))){
00298                 if (FR_LVDT>FR_LVDT_Ref){
00299             FR_LVDT=(FR_LVDT-FR_LVDT_Ref)*100/(LVDT_Max-FR_LVDT_Ref);
00300             }else{
00301                 FR_LVDT=(FR_LVDT-FR_LVDT_Ref)*100/FR_LVDT_Ref;
00302             };
00303         }else{    
00304             FR_LVDT_Ref=FR_LVDT;
00305             FR_LVDT=0;
00306             lvdtref=lvdtref^(1<<2);
00307         };
00308     };
00309 };
00310 
00311 void UpdateInfoLVDTRear(uint16_t newData[4]){            //Update values for LVDTRear message
00312     if (newData[0]>0 && newData[0]<=1024){
00313         RL_LVDT0=RL_LVDT;
00314         RL_LVDT=newData[0];
00315         if(!(lvdtref & (1<<1))){
00316             if (RL_LVDT>RL_LVDT_Ref){
00317                 RL_LVDT=(RL_LVDT-RL_LVDT_Ref)*100/(LVDT_Max-RL_LVDT_Ref);
00318             }else{
00319                 RL_LVDT=(RL_LVDT-RL_LVDT_Ref)*100/RL_LVDT_Ref;
00320             };
00321         }else{    
00322             RL_LVDT_Ref=RL_LVDT;
00323             RL_LVDT=0;
00324             lvdtref=lvdtref^(1<<1);
00325         };
00326     };   
00327     
00328     if (newData[1]>0 && newData[1]<=1024){
00329         RR_LVDT0=RR_LVDT;
00330         RR_LVDT=newData[1];    
00331         if(!(lvdtref & (1<<0))){
00332             if (RR_LVDT>RR_LVDT_Ref){
00333                 RR_LVDT=(RR_LVDT-RR_LVDT_Ref)*100/(LVDT_Max-RR_LVDT_Ref);
00334             }else{
00335                 RR_LVDT=(RR_LVDT-RR_LVDT_Ref)*100/RR_LVDT_Ref;
00336             };
00337         }else{    
00338             RR_LVDT_Ref=RR_LVDT;
00339             RR_LVDT=0;
00340             lvdtref=lvdtref^(1<<0);
00341         };
00342     };
00343 };
00344 
00345 void UpdateInfoBrakes(uint16_t newData[4]){              //Update values for Brakes message
00346     //if (newData[2]==0xFF || newData[2]==0x00){
00347         Brakes0=Brakes;
00348         Brakes=newData[2];
00349         //pc.printf("%x\n",Brakes);
00350     //      };
00351 };