Konacan kod
Dependencies: BSP_DISCO_F469NIa EEPROM_DISCO_F469NI LCD_DISCO_F469NIa SD_DISCO_F469NI mbed
Fork of IntegrationCAN by
Revision 2:f54f01527ebb, committed 2018-06-28
- Comitter:
- formulas
- Date:
- Thu Jun 28 16:33:23 2018 +0000
- Parent:
- 1:ed6a402be3e3
- Commit message:
- a
Changed in this revision
diff -r ed6a402be3e3 -r f54f01527ebb CAN_library.cpp --- a/CAN_library.cpp Wed May 16 14:55:13 2018 +0000 +++ b/CAN_library.cpp Thu Jun 28 16:33:23 2018 +0000 @@ -2,8 +2,12 @@ extern CAN can; extern Serial pc; -extern CANMessage msgDTA1, msgDTA2, msgDTA3, msgDTA4, msgDTA5, msgDTA6, msgLVDTFront, msgLVDTRear, msgBrakes; +extern SD_DISCO_F469NI sd; +//extern EEPROM_DISCO_F469NI eep; +extern Timer t_int,t_store; +extern CANMessage msgDTA1, msgDTA2, msgDTA3, msgDTA4, msgDTA5, msgDTA6, msgLVDTFront, msgLVDTRear, msgBrakes, msgDistance; extern uint16_t rx_flag; +extern uint8_t distance_flag; extern uint8_t lvdtref; extern uint16_t Rpm0, Speed0, Gear0, Water_Temp0, Oil_Temp0, TPS0, Brakes0, Oil_P0, MAP0, Air_Temp0, Lambda0, Volts0, Crank0; @@ -11,6 +15,7 @@ extern int FL_LVDT0, FR_LVDT0, RL_LVDT0, RR_LVDT0, FL_LVDT, FR_LVDT, RL_LVDT, RR_LVDT; extern int FL_LVDT_Ref,FR_LVDT_Ref,RL_LVDT_Ref,RR_LVDT_Ref; int LVDT_Max=150; +extern float Meter_counter,Meter_counter0; void CANMsgReceive(){ // CAN RX Interrupt Function CANMessage tmpMsg; @@ -54,13 +59,16 @@ rx_flag|=(1<<8); msgBrakes=tmpMsg; break; + case(9): + rx_flag|=(1<<9); + msgDistance=tmpMsg; }; //pc.printf("rx_flag=%d\n",rx_flag); }; }; -void UpdateInfo(){ // Update info for DTA values - int noID=9; +void UpdateInfo(){ // Update info for received values + int noID=10; uint16_t *newData; for (int i=0;i<noID;i++){ //For loop goes trough Message IDs uint16_t flagBit; @@ -112,10 +120,18 @@ UpdateInfoBrakes(newData); rx_flag = rx_flag ^ (1<<8); break; + case(9): + newData=AllocData(msgDistance); + if(distance_flag==0){ + InitDistanceMeter(newData); + distance_flag=1; + }; + rx_flag = rx_flag ^ (1<<9); // for each new id add new case statement }; }; }; + UpdateMeterCounter(); }; uint16_t * AllocData(CANMessage msg){ //Unpack CAN message @@ -127,6 +143,33 @@ return newData; }; +void InitDistanceMeter(uint16_t newData[4]){ + uint32_t DistanceBuffer; + DistanceBuffer=(uint32_t)((newData[1]<<16) | (newData[0])); + Meter_counter=float(DistanceBuffer)*10; + pc.printf("Init success, start km=%d,%f\n",DistanceBuffer,Meter_counter); + t_int.start(); + t_store.start(); +}; +/*void InitDistanceMeter(){ + uint32_t distance_buffer; + if(sd.ReadBlocks((uint32_t*)distance_buffer,DISTANCE_MEMORY_ADDR ,1,SD_DATATIMEOUT)){ + Meter_counter=(float(distance_buffer))/100; + + +};*/ +/*void InitDistanceMeter(){ + + uint8_t DistanceBuffer8[4]; + uint32_t DistanceBuffer32; + if(eep.ReadBuffer(DistanceBuffer8, 0, (uint16_t*)4)){ + pc.printf("Read success\n"); + } + DistanceBuffer32=(DistanceBuffer8[0]<<24) | (DistanceBuffer8[1]<<16) | (DistanceBuffer8[2]<<8) | DistanceBuffer8[3]; + Meter_counter=(float(DistanceBuffer32))/100; + pc.printf("Meter_counter=%f\n",Meter_counter); +};*/ + void UpdateInfoDTA1(uint16_t newData[4]){ //Update values for DTA1 message Rpm0=Rpm; Rpm=newData[0]; @@ -170,6 +213,59 @@ Crank=newData[2]; }; +void UpdateMeterCounter(){ + float ti=t_int.read(); + if(ti>0.1){ + Meter_counter0=Meter_counter; + Meter_counter=Meter_counter+(float)(Speed*0.0277778); // 0.1*1000/3600 + t_int.reset(); + }; + if(t_store>2){ + char DistanceBuffer8[8]; + uint32_t DistanceBuffer32=uint32_t(Meter_counter); + DistanceBuffer32=uint32_t(Meter_counter); + DistanceBuffer8[0]=uint8_t(DistanceBuffer32>>24); + DistanceBuffer8[1]=uint8_t((DistanceBuffer32 & (uint32_t)0x00FF0000)>>16); + DistanceBuffer8[2]=uint8_t((DistanceBuffer32 & (uint32_t)0x0000FF00)>>8); + DistanceBuffer8[3]=uint8_t((DistanceBuffer32 & (uint32_t)0x000000FF)); + CANMessage msg; + if(can.write(CANMessage((uint16_t)DISTANCE,DistanceBuffer8, 8))) { + pc.printf("%d\n",DistanceBuffer32); + }; + t_store.reset(); + + /*uint32_t distance_buffer; + //distance_buffer=uint32_t(Meter_counter) ; + pc.printf("distance=%d\n",distance_buffer); + distance_buffer=0xFF000000; + if(sd.WriteBlocks((uint32_t*)distance_buffer,DISTANCE_MEMORY_ADDR,1,SD_DATATIMEOUT)){ + pc.printf("Storage success\n"); + }else{ + pc.printf("Storage failed\n"); + };*/ + + /*uint32_t DistanceBuffer32; + uint8_t DistanceBuffer8[4]; + DistanceBuffer32=uint32_t(Meter_counter); + DistanceBuffer8[0]=DistanceBuffer32>>24; + DistanceBuffer8[1]=(DistanceBuffer32 & (uint32_t)0x00FF0000)>>16; + DistanceBuffer8[2]=(DistanceBuffer32 & (uint32_t)0x0000FF00)>>8; + DistanceBuffer8[3]=(DistanceBuffer32 & (uint32_t)0x000000FF); + if(eep.WriteBuffer(DistanceBuffer8,0,4)){ + pc.printf("Write success,Distance=%d\n",DistanceBuffer32); + };*/ + }; +}; + +void send(uint16_t ID, char dta[8]) { + if(can.write(CANMessage((uint16_t)ID,dta, 8))) { + printf("%x\n",ID); + 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]); + }else{ + printf("Shit\n"); + } +}; + void UpdateInfoLVDTFront(uint16_t newData[4]){ //Update values for LVDTFront message FL_LVDT0=FL_LVDT; //LVDTs are firstly written their voltage value, then overwritten by calculated pertentage value. FL_LVDT=newData[0]; @@ -181,15 +277,15 @@ }else{ FL_LVDT=(FL_LVDT-FL_LVDT_Ref)*100/FL_LVDT_Ref; }; - pc.printf("Im in 1\n"); + //pc.printf("Im in 1\n"); }else{ FL_LVDT_Ref=FL_LVDT; FL_LVDT=0; lvdtref=lvdtref^(1<<3); - pc.printf("Ref=%d\n",FL_LVDT_Ref); + //pc.printf("Ref=%d\n",FL_LVDT_Ref); }; - pc.printf("FL_LVDT=%d, FL_LVDT0=%d\n",abs(FL_LVDT),abs(FL_LVDT0)); - pc.printf("FR_LVDT=%d, FR_LVDT0=%d\n",abs(FR_LVDT),abs(FR_LVDT0)); + //pc.printf("FL_LVDT=%d, FL_LVDT0=%d\n",abs(FL_LVDT),abs(FL_LVDT0)); + //pc.printf("FR_LVDT=%d, FR_LVDT0=%d\n",abs(FR_LVDT),abs(FR_LVDT0)); if(!(lvdtref & (1<<2))){ if (FR_LVDT>FR_LVDT_Ref){ @@ -233,8 +329,8 @@ RR_LVDT=0; lvdtref=lvdtref^(1<<0); }; - pc.printf("RL_LVDT=%d, RL_LVDT0=%d\n",abs(RL_LVDT),abs(RL_LVDT0)); - pc.printf("RR_LVDT=%d, RR_LVDT0=%d\n",abs(RR_LVDT),abs(RR_LVDT0)); + //pc.printf("RL_LVDT=%d, RL_LVDT0=%d\n",abs(RL_LVDT),abs(RL_LVDT0)); + //pc.printf("RR_LVDT=%d, RR_LVDT0=%d\n",abs(RR_LVDT),abs(RR_LVDT0)); };
diff -r ed6a402be3e3 -r f54f01527ebb CAN_library.h --- a/CAN_library.h Wed May 16 14:55:13 2018 +0000 +++ b/CAN_library.h Thu Jun 28 16:33:23 2018 +0000 @@ -1,4 +1,7 @@ #include "mbed.h" +#include "SD_DISCO_F469NI.h" +#include "EEPROM_DISCO_F469NI.h" + //CAN Identificators #define DTA_1 0x2000 @@ -6,21 +9,27 @@ #define DTA_3 0x2002 #define DTA_4 0x2003 #define DTA_5 0x2004 -#define DTA_6 0x2006 +#define DTA_6 0x2005 // This IDs can be changed as they are our own IDs -#define LVDT_FRONT 0x1000 -#define LVDT_REAR 0x1001 -#define BRAKES 0x1002 +#define LVDT_FRONT 0x1006 +#define LVDT_REAR 0x1007 +#define BRAKES 0x1008 +#define DISTANCE 0x0009 // If we add some other IDs in the vehicle add them here +#define DISTANCE_MEMORY_ADDR 3592960 //Start address in SD card memory reserved for Distance Statistics. + //List of functions -void CANMsgReceive(); // CAN RX Interrupt Function + +void CANMsgReceive(); // CAN RX Interrupt Function void UpdateInfo(); // Update info for DTA values uint16_t * AllocData(CANMessage msg); // Unpack CAN message +void InitDistanceMeter(uint16_t newData[4]); // Initialise Distance Meter + //Update values void UpdateInfoDTA1(uint16_t newData[4]); // Update values for DTA1 message void UpdateInfoDTA2(uint16_t newData[4]); // Update values for DTA2 message @@ -28,6 +37,8 @@ void UpdateInfoDTA4(uint16_t newData[4]); // Update values for DTA4 message void UpdateInfoDTA5(uint16_t newData[4]); // Update values for DTA5 message void UpdateInfoDTA6(uint16_t newData[4]); // Update values for DTA6 message +void UpdateMeterCounter(); +void send(uint16_t ID, char dta[8]); void UpdateInfoLVDTFront(uint16_t newData[4]); // Update values for LVDTFront message void UpdateInfoLVDTRear(uint16_t newData[4]); // Update values for LVDTRear message void UpdateInfoBrakes(uint16_t newData[4]); // Update values for Brakes message \ No newline at end of file
diff -r ed6a402be3e3 -r f54f01527ebb EEPROM_DISCO_F469NI.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EEPROM_DISCO_F469NI.lib Thu Jun 28 16:33:23 2018 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/teams/ST/code/EEPROM_DISCO_F469NI/#cd22055cbc4b
diff -r ed6a402be3e3 -r f54f01527ebb draw_print_library.cpp --- a/draw_print_library.cpp Wed May 16 14:55:13 2018 +0000 +++ b/draw_print_library.cpp Thu Jun 28 16:33:23 2018 +0000 @@ -363,7 +363,7 @@ }; void ChangeCrank(int Crank){ - PrintString(" ",50,350,400,LCD_COLOR_GREEN); + PrintString(" ",50,350,400,LCD_COLOR_GREEN); if (Crank){ PrintString("ERROR",50,CrankXPos,CrankYPos,LCD_COLOR_RED); lcd.SetTextColor(LCD_COLOR_BLACK);
diff -r ed6a402be3e3 -r f54f01527ebb draw_print_library.h --- a/draw_print_library.h Wed May 16 14:55:13 2018 +0000 +++ b/draw_print_library.h Thu Jun 28 16:33:23 2018 +0000 @@ -1,7 +1,7 @@ #include "LCD_DISCO_F469NI.h" #include "SD_DISCO_F469NI.h" -#define PI 3.14159265358979323846 +#define PI 3.14159265358979323846 #define BYTE_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c" //Pattern which converts uint8_t to binary(array of 8 chars) #define BYTE_TO_BINARY(byte)\ (byte & 0x80 ? '1' : '0'),\ @@ -28,12 +28,13 @@ const uint16_t LogoSmallXPos=635,LogoSmallYPos=5; //Small Logo, all three screens const uint16_t WaterTempXPos=15,WaterTempYPos=350; //Water Temperature in Main const uint16_t TPSXPos=569,TPSYPos=200; //TPS in Main -const uint16_t OilPXPos=330,OilPYPos=25; //Oil Pressure in First Auxiliary -const uint16_t MAPXPos=330,MAPYPos=100; //MAP in First Auxiliary -const uint16_t AirTempXPos=330,AirTempYPos=175; //Air Temperature in First Auxiliary -const uint16_t LambdaXPos=330,LambdaYPos=250; //Lambda Sensor value in First Auxiliary -const uint16_t VoltsXPos=330,VoltsYPos=325; //Volts in Accumulator in First Auxiliary -const uint16_t CrankXPos=330,CrankYPos=400; //Crank Error Message in First Auxiliary +const uint16_t OilPXPos=330,OilPYPos=30; //Oil Pressure in First Auxiliary +const uint16_t MAPXPos=330,MAPYPos=90; //MAP in First Auxiliary +const uint16_t AirTempXPos=330,AirTempYPos=150; //Air Temperature in First Auxiliary +const uint16_t LambdaXPos=330,LambdaYPos=210; //Lambda Sensor value in First Auxiliary +const uint16_t VoltsXPos=330,VoltsYPos=270; //Volts in Accumulator in First Auxiliary +const uint16_t CrankXPos=330,CrankYPos=330; //Crank Error Message in First Auxiliary +const uint16_t Meter_counterXPos=330,Meter_counterYPos=390; //Kilometer counter in First Auxiliary //YPos in bottom position for LVDTs const uint16_t FLLVDTBarXPos=280,FLLVDTBarYPos=200; //Front Left Bar LVDT in Second Auxiliary const uint16_t FRLVDTBarXPos=440,FRLVDTBarYPos=200; //Front Right Bar LVDT in Second Auxiliary @@ -45,18 +46,19 @@ const uint16_t RRLVDTXPos=534,RRLVDTYPos=310; //Rear Right LVDT in Second Auxiliary -typedef struct BWImage{ //Black-White Image Structure. These are stored on the controller +typedef struct BWImage { //Black-White Image Structure. These are stored on the controller char name; uint16_t width; uint16_t height; uint8_t *bitmap; - } GEAR,CHAR; +} GEAR,CHAR; -typedef struct RGBImage{ //RGB Coloured Image Structure. These are stored on SD Card +typedef struct RGBImage { //RGB Coloured Image Structure. These are stored on SD Card uint16_t width; uint16_t height; uint32_t START_ADDR; - }IMAGE; +} IMAGE; + void DrawSpeedMeter(); //Draw Speedmeter function void PrintChar(CHAR Char,uint16_t StartXPos,uint16_t StartYPos,uint32_t TextColor); //Print Char function
diff -r ed6a402be3e3 -r f54f01527ebb main.cpp --- a/main.cpp Wed May 16 14:55:13 2018 +0000 +++ b/main.cpp Thu Jun 28 16:33:23 2018 +0000 @@ -1,11 +1,15 @@ #include "CAN_library.h" #include "screen_library.h" +#include "EEPROM_DISCO_F469NI.h" + LCD_DISCO_F469NI lcd; //Initialize LCD Display SD_DISCO_F469NI sd; //Initialize SD Card -Serial pc(USBTX, USBRX); //Initialize Serial. This is used only while debuging code. +//EEPROM_DISCO_F469NI eep; //Initialize EEPROM Internal memory +Serial pc(USBTX, USBRX); //Initialize Serial. This is used only while debuging code. CAN can(PB_5, PB_13); //Initialize CAN. InterruptIn change_screen_input(D0); //Initialize Digital input for Change screen button +Timer t_int,t_store; // Variables received from DTA, LVDTs and brakes uint16_t Rpm0=0,Speed0=0,Gear0=0,Water_Temp0=0,Oil_Temp0=0,TPS0=0,Brakes0=0,Oil_P0=0,MAP0=0,Air_Temp0=0,Lambda0=0,Volts0=0,Crank0=0; @@ -13,10 +17,12 @@ int FL_LVDT0=0,FR_LVDT0=0,RL_LVDT0=0,RR_LVDT0=0,FL_LVDT=0,FR_LVDT=0,RL_LVDT=0,RR_LVDT=0; //Referrent LVDT values. First received value is referrent. int FL_LVDT_Ref,FR_LVDT_Ref,RL_LVDT_Ref,RR_LVDT_Ref; +float Meter_counter=0,Meter_counter0=0; uint8_t change_flag=0; uint16_t rx_flag=0x0000; // Receive specific CAN data message -uint8_t lvdtref=0x0F; // Flag if refferent LVDT value is received (first received LVDT value, 1=no, 0=yes). From highest to lowest bit: LL,LR,RL,RR. +uint8_t distance_flag=0; +uint8_t lvdtref=0x0F; // Flag if refferent LVDT value is received (first received LVDT value, 1=no, 0=yes). From highest to lowest bit: LL,LR,RL,RR. uint8_t screen_flag=0x01; // Current screen flag. 1=Main, 2=First Aux, 3=Second Aux. uint8_t ft_main_flag=1; // Detect first time Main screne in loop uint8_t ft_2aux_flag=1; // Detect first time Second Aux screne in loop @@ -32,6 +38,7 @@ CANMessage msgLVDTFront; // Left, Right, Steering Wheel CANMessage msgLVDTRear; // Left, Right CANMessage msgBrakes; // Brake system preassure, Braking On/Off +CANMessage msgDistance; // Total distance int main(){ @@ -39,6 +46,7 @@ change_screen_input.rise(&ChangeCommand); //Attach interrupt function to rising edge of DigitalIn for changing screen. SetIntro(); // Display logo when starting display SetMain(); // First screen is main by default + //sd.Init(); while(1){ if(change_flag){ // Check if screen is changed ChangeScreen();
diff -r ed6a402be3e3 -r f54f01527ebb screen_library.cpp --- a/screen_library.cpp Wed May 16 14:55:13 2018 +0000 +++ b/screen_library.cpp Thu Jun 28 16:33:23 2018 +0000 @@ -13,6 +13,7 @@ extern int FL_LVDT0, FR_LVDT0, RL_LVDT0, RR_LVDT0, FL_LVDT, FR_LVDT, RL_LVDT, RR_LVDT; extern int FL_LVDT_Ref,FR_LVDT_Ref,RL_LVDT_Ref,RR_LVDT_Ref; extern int LVDT_Max; +extern float Meter_counter,Meter_counter0; void ChangeCommand(){ //Interrupt function for registering change command change_flag=1; @@ -86,17 +87,19 @@ lcd.FillRect(0,0,800,480); lcd.SetTextColor(LCD_COLOR_BLACK); //Draw First Auxiliary screen - PrintString("Oil P",50,20,25,LCD_COLOR_BLACK); - PrintString("MAP",50,20,100,LCD_COLOR_BLACK); - PrintString("Air Temp",50,20,175,LCD_COLOR_BLACK); - PrintString("Lambda",50,20,250,LCD_COLOR_BLACK); - PrintString("Volts",50,20,325,LCD_COLOR_BLACK); - PrintString("Crank",50,20,400,LCD_COLOR_BLACK); - PrintString("kPa",50,470,25,LCD_COLOR_BLACK); - PrintString("kPa",50,470,100,LCD_COLOR_BLACK); - PrintString("C",50,470,175,LCD_COLOR_BLACK); - PrintString("Ratio",50,470,250,LCD_COLOR_BLACK); - PrintString("V",50,470,325,LCD_COLOR_BLACK); + PrintString("Oil P",50,20,30,LCD_COLOR_BLACK); + PrintString("MAP",50,20,90,LCD_COLOR_BLACK); + PrintString("Air Temp",50,20,150,LCD_COLOR_BLACK); + PrintString("Lambda",50,20,210,LCD_COLOR_BLACK); + PrintString("Volts",50,20,270,LCD_COLOR_BLACK); + PrintString("Crank",50,20,330,LCD_COLOR_BLACK); + PrintString("Distance:",50,20,390,LCD_COLOR_BLACK); + PrintString("kPa",50,470,30,LCD_COLOR_BLACK); + PrintString("kPa",50,470,90,LCD_COLOR_BLACK); + PrintString("C",50,470,150,LCD_COLOR_BLACK); + PrintString("Ratio",50,470,210,LCD_COLOR_BLACK); + PrintString("V",50,470,270,LCD_COLOR_BLACK); + PrintString("km",50,550,390,LCD_COLOR_BLACK); DrawRGBImage(LogoSmall,LogoSmallXPos,LogoSmallYPos); //Display initial values SetNumber(Oil_P,50,OilPXPos,OilPYPos,3,0,0); @@ -104,7 +107,8 @@ SetNumber(Air_Temp,50,AirTempXPos,AirTempYPos,3,0,0); SetNumber(Lambda,50,LambdaXPos,LambdaYPos,3,0,0); SetNumber(Volts,50,VoltsXPos,VoltsYPos,3,0,0); - ChangeCrank(Crank); + ChangeCrank(Crank); + SetNumber((int)(Meter_counter/10),50,Meter_counterXPos,Meter_counterYPos,5,3,0); }; @@ -184,6 +188,9 @@ if (Crank!=Crank0){ ChangeCrank(Crank); }; + if (Meter_counter!=Meter_counter0){ + ChangeNumber((int)(Meter_counter/10),(int)(Meter_counter0/10),50,Meter_counterXPos,Meter_counterYPos,5,3,0); + }; }; void SecondAuxUpdate(){ // Update information in Second Auxiliary screen