Konacan kod

Dependencies:   BSP_DISCO_F469NIa LCD_DISCO_F469NIa SD_DISCO_F469NI mbed

Committer:
formulas
Date:
Wed May 16 14:48:37 2018 +0000
Revision:
0:38c3afd99aeb
a

Who changed what in which revision?

UserRevisionLine numberNew contents of line
formulas 0:38c3afd99aeb 1 #include "CAN_library.h"
formulas 0:38c3afd99aeb 2
formulas 0:38c3afd99aeb 3 extern CAN can;
formulas 0:38c3afd99aeb 4 extern Serial pc;
formulas 0:38c3afd99aeb 5 extern CANMessage msgDTA1, msgDTA2, msgDTA3, msgDTA4, msgDTA5, msgDTA6, msgLVDTFront, msgLVDTRear, msgBrakes;
formulas 0:38c3afd99aeb 6 extern uint16_t rx_flag;
formulas 0:38c3afd99aeb 7 extern uint8_t lvdtref;
formulas 0:38c3afd99aeb 8
formulas 0:38c3afd99aeb 9 extern uint16_t Rpm0, Speed0, Gear0, Water_Temp0, Oil_Temp0, TPS0, Brakes0, Oil_P0, MAP0, Air_Temp0, Lambda0, Volts0, Crank0;
formulas 0:38c3afd99aeb 10 extern uint16_t Rpm, Speed, Gear, Water_Temp, Oil_Temp, TPS, Brakes, Oil_P, MAP, Air_Temp, Lambda, Volts, Crank;
formulas 0:38c3afd99aeb 11 extern int FL_LVDT0, FR_LVDT0, RL_LVDT0, RR_LVDT0, FL_LVDT, FR_LVDT, RL_LVDT, RR_LVDT;
formulas 0:38c3afd99aeb 12 extern int FL_LVDT_Ref,FR_LVDT_Ref,RL_LVDT_Ref,RR_LVDT_Ref;
formulas 0:38c3afd99aeb 13 int LVDT_Max=150;
formulas 0:38c3afd99aeb 14
formulas 0:38c3afd99aeb 15 void CANMsgReceive(){ // CAN RX Interrupt Function
formulas 0:38c3afd99aeb 16 CANMessage tmpMsg;
formulas 0:38c3afd99aeb 17 if (can.read(tmpMsg)) { //Detect message
formulas 0:38c3afd99aeb 18 //pc.printf("%x\n",tmpMsg.id);
formulas 0:38c3afd99aeb 19 switch(tmpMsg.id){ //Find which DTA message is received
formulas 0:38c3afd99aeb 20 case(0):
formulas 0:38c3afd99aeb 21 rx_flag|=(1<<0);
formulas 0:38c3afd99aeb 22 msgDTA1=tmpMsg;
formulas 0:38c3afd99aeb 23 break;
formulas 0:38c3afd99aeb 24 case(1):
formulas 0:38c3afd99aeb 25 rx_flag|=(1<<1);
formulas 0:38c3afd99aeb 26 msgDTA2=tmpMsg;
formulas 0:38c3afd99aeb 27 //pc.printf("rx_flag=%x\n",rx_flag);
formulas 0:38c3afd99aeb 28 break;
formulas 0:38c3afd99aeb 29 case(2):
formulas 0:38c3afd99aeb 30 rx_flag|=(1<<2);
formulas 0:38c3afd99aeb 31 msgDTA3=tmpMsg;
formulas 0:38c3afd99aeb 32 break;
formulas 0:38c3afd99aeb 33 case(3):
formulas 0:38c3afd99aeb 34 rx_flag|=(1<<3);
formulas 0:38c3afd99aeb 35 msgDTA4=tmpMsg;
formulas 0:38c3afd99aeb 36 break;
formulas 0:38c3afd99aeb 37 case(4):
formulas 0:38c3afd99aeb 38 rx_flag|=(1<<4);
formulas 0:38c3afd99aeb 39 msgDTA5=tmpMsg;
formulas 0:38c3afd99aeb 40 break;
formulas 0:38c3afd99aeb 41 case(5):
formulas 0:38c3afd99aeb 42 rx_flag|=(1<<5);
formulas 0:38c3afd99aeb 43 msgDTA6=tmpMsg;
formulas 0:38c3afd99aeb 44 break;
formulas 0:38c3afd99aeb 45 case(6):
formulas 0:38c3afd99aeb 46 rx_flag|=(1<<6);
formulas 0:38c3afd99aeb 47 msgLVDTFront=tmpMsg;
formulas 0:38c3afd99aeb 48 break;
formulas 0:38c3afd99aeb 49 case(7):
formulas 0:38c3afd99aeb 50 rx_flag|=(1<<7);
formulas 0:38c3afd99aeb 51 msgLVDTRear=tmpMsg;
formulas 0:38c3afd99aeb 52 break;
formulas 0:38c3afd99aeb 53 case(8):
formulas 0:38c3afd99aeb 54 rx_flag|=(1<<8);
formulas 0:38c3afd99aeb 55 msgBrakes=tmpMsg;
formulas 0:38c3afd99aeb 56 break;
formulas 0:38c3afd99aeb 57 };
formulas 0:38c3afd99aeb 58 //pc.printf("rx_flag=%d\n",rx_flag);
formulas 0:38c3afd99aeb 59 };
formulas 0:38c3afd99aeb 60 };
formulas 0:38c3afd99aeb 61
formulas 0:38c3afd99aeb 62 void UpdateInfo(){ // Update info for DTA values
formulas 0:38c3afd99aeb 63 int noID=9;
formulas 0:38c3afd99aeb 64 uint16_t *newData;
formulas 0:38c3afd99aeb 65 for (int i=0;i<noID;i++){ //For loop goes trough Message IDs
formulas 0:38c3afd99aeb 66 uint16_t flagBit;
formulas 0:38c3afd99aeb 67 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.
formulas 0:38c3afd99aeb 68 if (flagBit) { // =/=0, there is a message, =0, there is no message
formulas 0:38c3afd99aeb 69 switch(i){
formulas 0:38c3afd99aeb 70 case(0):
formulas 0:38c3afd99aeb 71 newData=AllocData(msgDTA1); //Allocate message to a receiver
formulas 0:38c3afd99aeb 72 UpdateInfoDTA1(newData); //Update variables
formulas 0:38c3afd99aeb 73 rx_flag = rx_flag ^ (1<<0); //Set flag of adequate bit to 0
formulas 0:38c3afd99aeb 74 break;
formulas 0:38c3afd99aeb 75 case(1):
formulas 0:38c3afd99aeb 76 newData=AllocData(msgDTA2);
formulas 0:38c3afd99aeb 77 UpdateInfoDTA2(newData);
formulas 0:38c3afd99aeb 78 rx_flag = rx_flag ^ (1<<1);
formulas 0:38c3afd99aeb 79 break;
formulas 0:38c3afd99aeb 80 case(2):
formulas 0:38c3afd99aeb 81 newData=AllocData(msgDTA3);
formulas 0:38c3afd99aeb 82 UpdateInfoDTA3(newData);
formulas 0:38c3afd99aeb 83 rx_flag = rx_flag ^ (1<<2);
formulas 0:38c3afd99aeb 84 break;
formulas 0:38c3afd99aeb 85 case(3):
formulas 0:38c3afd99aeb 86 newData=AllocData(msgDTA4);
formulas 0:38c3afd99aeb 87 UpdateInfoDTA4(newData);
formulas 0:38c3afd99aeb 88 rx_flag = rx_flag ^ (1<<3);
formulas 0:38c3afd99aeb 89 break;
formulas 0:38c3afd99aeb 90 case(4):
formulas 0:38c3afd99aeb 91 newData=AllocData(msgDTA5);
formulas 0:38c3afd99aeb 92 UpdateInfoDTA5(newData);
formulas 0:38c3afd99aeb 93 rx_flag = rx_flag ^ (1<<4);
formulas 0:38c3afd99aeb 94 break;
formulas 0:38c3afd99aeb 95 case(5):
formulas 0:38c3afd99aeb 96 newData=AllocData(msgDTA6);
formulas 0:38c3afd99aeb 97 UpdateInfoDTA6(newData);
formulas 0:38c3afd99aeb 98 rx_flag = rx_flag ^ (1<<5);
formulas 0:38c3afd99aeb 99 break;
formulas 0:38c3afd99aeb 100 case(6):
formulas 0:38c3afd99aeb 101 newData=AllocData(msgLVDTFront);
formulas 0:38c3afd99aeb 102 UpdateInfoLVDTFront(newData);
formulas 0:38c3afd99aeb 103 rx_flag = rx_flag ^ (1<<6);
formulas 0:38c3afd99aeb 104 break;
formulas 0:38c3afd99aeb 105 case(7):
formulas 0:38c3afd99aeb 106 newData=AllocData(msgLVDTRear);
formulas 0:38c3afd99aeb 107 UpdateInfoLVDTRear(newData);
formulas 0:38c3afd99aeb 108 rx_flag = rx_flag ^ (1<<7);
formulas 0:38c3afd99aeb 109 break;
formulas 0:38c3afd99aeb 110 case(8):
formulas 0:38c3afd99aeb 111 newData=AllocData(msgBrakes);
formulas 0:38c3afd99aeb 112 UpdateInfoBrakes(newData);
formulas 0:38c3afd99aeb 113 rx_flag = rx_flag ^ (1<<8);
formulas 0:38c3afd99aeb 114 break;
formulas 0:38c3afd99aeb 115 // for each new id add new case statement
formulas 0:38c3afd99aeb 116 };
formulas 0:38c3afd99aeb 117 };
formulas 0:38c3afd99aeb 118 };
formulas 0:38c3afd99aeb 119 };
formulas 0:38c3afd99aeb 120
formulas 0:38c3afd99aeb 121 uint16_t * AllocData(CANMessage msg){ //Unpack CAN message
formulas 0:38c3afd99aeb 122 uint16_t newData[4];
formulas 0:38c3afd99aeb 123 newData[0]=(msg.data[1]<<8) | msg.data[0];
formulas 0:38c3afd99aeb 124 newData[1]=(msg.data[3]<<8) | msg.data[2];
formulas 0:38c3afd99aeb 125 newData[2]=(msg.data[5]<<8) | msg.data[4];
formulas 0:38c3afd99aeb 126 newData[3]=(msg.data[7]<<8) | msg.data[6];
formulas 0:38c3afd99aeb 127 return newData;
formulas 0:38c3afd99aeb 128 };
formulas 0:38c3afd99aeb 129
formulas 0:38c3afd99aeb 130 void UpdateInfoDTA1(uint16_t newData[4]){ //Update values for DTA1 message
formulas 0:38c3afd99aeb 131 Rpm0=Rpm;
formulas 0:38c3afd99aeb 132 Rpm=newData[0];
formulas 0:38c3afd99aeb 133 TPS0=TPS;
formulas 0:38c3afd99aeb 134 TPS=newData[1];
formulas 0:38c3afd99aeb 135 Water_Temp0=Water_Temp;
formulas 0:38c3afd99aeb 136 Water_Temp=newData[2];
formulas 0:38c3afd99aeb 137 Air_Temp0=Air_Temp;
formulas 0:38c3afd99aeb 138 Air_Temp=newData[3];
formulas 0:38c3afd99aeb 139 };
formulas 0:38c3afd99aeb 140
formulas 0:38c3afd99aeb 141 void UpdateInfoDTA2(uint16_t newData[4]){ //Update values for DTA2 message
formulas 0:38c3afd99aeb 142 MAP0=MAP;
formulas 0:38c3afd99aeb 143 MAP=newData[0];
formulas 0:38c3afd99aeb 144 Lambda0=Lambda;
formulas 0:38c3afd99aeb 145 Lambda=newData[1];
formulas 0:38c3afd99aeb 146 Speed0=Speed;
formulas 0:38c3afd99aeb 147 Speed=newData[2];
formulas 0:38c3afd99aeb 148 Oil_P0=Oil_P;
formulas 0:38c3afd99aeb 149 Oil_P=newData[3];
formulas 0:38c3afd99aeb 150 //pc.printf("MAP=%d, Lambda=%d, Speed=%d, Oil_P=%d\n",MAP,Lambda,Speed,Oil_P);
formulas 0:38c3afd99aeb 151 };
formulas 0:38c3afd99aeb 152
formulas 0:38c3afd99aeb 153 void UpdateInfoDTA3(uint16_t newData[4]){ //Update values for DTA3 message
formulas 0:38c3afd99aeb 154 Oil_Temp0=Oil_Temp;
formulas 0:38c3afd99aeb 155 Oil_Temp=newData[1];
formulas 0:38c3afd99aeb 156 Volts0=Volts;
formulas 0:38c3afd99aeb 157 Volts=newData[2];
formulas 0:38c3afd99aeb 158 };
formulas 0:38c3afd99aeb 159
formulas 0:38c3afd99aeb 160 void UpdateInfoDTA4(uint16_t newData[4]){ //Update values for DTA4 message
formulas 0:38c3afd99aeb 161 Gear0=Gear;
formulas 0:38c3afd99aeb 162 Gear=newData[0];
formulas 0:38c3afd99aeb 163 };
formulas 0:38c3afd99aeb 164
formulas 0:38c3afd99aeb 165 void UpdateInfoDTA5(uint16_t newData[4]){ //Update values for DTA5 message
formulas 0:38c3afd99aeb 166 };
formulas 0:38c3afd99aeb 167
formulas 0:38c3afd99aeb 168 void UpdateInfoDTA6(uint16_t newData[4]){ //Update values for DTA6 message
formulas 0:38c3afd99aeb 169 Crank0=Crank;
formulas 0:38c3afd99aeb 170 Crank=newData[2];
formulas 0:38c3afd99aeb 171 };
formulas 0:38c3afd99aeb 172
formulas 0:38c3afd99aeb 173 void UpdateInfoLVDTFront(uint16_t newData[4]){ //Update values for LVDTFront message
formulas 0:38c3afd99aeb 174 FL_LVDT0=FL_LVDT; //LVDTs are firstly written their voltage value, then overwritten by calculated pertentage value.
formulas 0:38c3afd99aeb 175 FL_LVDT=newData[0];
formulas 0:38c3afd99aeb 176 FR_LVDT0=FR_LVDT;
formulas 0:38c3afd99aeb 177 FR_LVDT=newData[1];
formulas 0:38c3afd99aeb 178 if(!(lvdtref & (1<<3))){ //Check if its the first time LVDT value is received. If yes than store it as Referrent value.
formulas 0:38c3afd99aeb 179 if (FL_LVDT>FL_LVDT_Ref){
formulas 0:38c3afd99aeb 180 FL_LVDT=(FL_LVDT-FL_LVDT_Ref)*100/(LVDT_Max-FL_LVDT_Ref);
formulas 0:38c3afd99aeb 181 }else{
formulas 0:38c3afd99aeb 182 FL_LVDT=(FL_LVDT-FL_LVDT_Ref)*100/FL_LVDT_Ref;
formulas 0:38c3afd99aeb 183 };
formulas 0:38c3afd99aeb 184 pc.printf("Im in 1\n");
formulas 0:38c3afd99aeb 185 }else{
formulas 0:38c3afd99aeb 186 FL_LVDT_Ref=FL_LVDT;
formulas 0:38c3afd99aeb 187 FL_LVDT=0;
formulas 0:38c3afd99aeb 188 lvdtref=lvdtref^(1<<3);
formulas 0:38c3afd99aeb 189 pc.printf("Ref=%d\n",FL_LVDT_Ref);
formulas 0:38c3afd99aeb 190 };
formulas 0:38c3afd99aeb 191 pc.printf("FL_LVDT=%d, FL_LVDT0=%d\n",abs(FL_LVDT),abs(FL_LVDT0));
formulas 0:38c3afd99aeb 192 pc.printf("FR_LVDT=%d, FR_LVDT0=%d\n",abs(FR_LVDT),abs(FR_LVDT0));
formulas 0:38c3afd99aeb 193
formulas 0:38c3afd99aeb 194 if(!(lvdtref & (1<<2))){
formulas 0:38c3afd99aeb 195 if (FR_LVDT>FR_LVDT_Ref){
formulas 0:38c3afd99aeb 196 FR_LVDT=(FR_LVDT-FR_LVDT_Ref)*100/(LVDT_Max-FR_LVDT_Ref);
formulas 0:38c3afd99aeb 197 }else{
formulas 0:38c3afd99aeb 198 FR_LVDT=(FR_LVDT-FR_LVDT_Ref)*100/FR_LVDT_Ref;
formulas 0:38c3afd99aeb 199 };
formulas 0:38c3afd99aeb 200 }else{
formulas 0:38c3afd99aeb 201 FR_LVDT_Ref=FR_LVDT;
formulas 0:38c3afd99aeb 202 FR_LVDT=0;
formulas 0:38c3afd99aeb 203 lvdtref=lvdtref^(1<<2);
formulas 0:38c3afd99aeb 204 };
formulas 0:38c3afd99aeb 205 //printf("FL_Ref=%d, FR_Ref=%d, RL_Ref=%d, RR_Ref=%d\n",FL_LVDT_Dpos,FR_LVDT_Dpos,RL_LVDT_Dpos,RR_LVDT_Dpos);
formulas 0:38c3afd99aeb 206 };
formulas 0:38c3afd99aeb 207
formulas 0:38c3afd99aeb 208 void UpdateInfoLVDTRear(uint16_t newData[4]){ //Update values for LVDTRear message
formulas 0:38c3afd99aeb 209 RL_LVDT0=RL_LVDT;
formulas 0:38c3afd99aeb 210 RL_LVDT=newData[0];
formulas 0:38c3afd99aeb 211 RR_LVDT0=RR_LVDT;
formulas 0:38c3afd99aeb 212 RR_LVDT=newData[1];
formulas 0:38c3afd99aeb 213 if(!(lvdtref & (1<<1))){
formulas 0:38c3afd99aeb 214 if (RL_LVDT>RL_LVDT_Ref){
formulas 0:38c3afd99aeb 215 RL_LVDT=(RL_LVDT-RL_LVDT_Ref)*100/(LVDT_Max-RL_LVDT_Ref);
formulas 0:38c3afd99aeb 216 }else{
formulas 0:38c3afd99aeb 217 RL_LVDT=(RL_LVDT-RL_LVDT_Ref)*100/RL_LVDT_Ref;
formulas 0:38c3afd99aeb 218 };
formulas 0:38c3afd99aeb 219 }else{
formulas 0:38c3afd99aeb 220 RL_LVDT_Ref=RL_LVDT;
formulas 0:38c3afd99aeb 221 RL_LVDT=0;
formulas 0:38c3afd99aeb 222 lvdtref=lvdtref^(1<<1);
formulas 0:38c3afd99aeb 223 };
formulas 0:38c3afd99aeb 224
formulas 0:38c3afd99aeb 225 if(!(lvdtref & (1<<0))){
formulas 0:38c3afd99aeb 226 if (RR_LVDT>RR_LVDT_Ref){
formulas 0:38c3afd99aeb 227 RR_LVDT=(RR_LVDT-RR_LVDT_Ref)*100/(LVDT_Max-RR_LVDT_Ref);
formulas 0:38c3afd99aeb 228 }else{
formulas 0:38c3afd99aeb 229 RR_LVDT=(RR_LVDT-RR_LVDT_Ref)*100/RR_LVDT_Ref;
formulas 0:38c3afd99aeb 230 };
formulas 0:38c3afd99aeb 231 }else{
formulas 0:38c3afd99aeb 232 RR_LVDT_Ref=RR_LVDT;
formulas 0:38c3afd99aeb 233 RR_LVDT=0;
formulas 0:38c3afd99aeb 234 lvdtref=lvdtref^(1<<0);
formulas 0:38c3afd99aeb 235 };
formulas 0:38c3afd99aeb 236 pc.printf("RL_LVDT=%d, RL_LVDT0=%d\n",abs(RL_LVDT),abs(RL_LVDT0));
formulas 0:38c3afd99aeb 237 pc.printf("RR_LVDT=%d, RR_LVDT0=%d\n",abs(RR_LVDT),abs(RR_LVDT0));
formulas 0:38c3afd99aeb 238
formulas 0:38c3afd99aeb 239 };
formulas 0:38c3afd99aeb 240
formulas 0:38c3afd99aeb 241 void UpdateInfoBrakes(uint16_t newData[4]){ //Update values for Brakes message
formulas 0:38c3afd99aeb 242 Brakes0=Brakes;
formulas 0:38c3afd99aeb 243 Brakes=newData[0];
formulas 0:38c3afd99aeb 244 };