Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
BMS.cpp
00001 #include "BMS.h" 00002 #include "mbed.h" 00003 00004 //Constructor 00005 BMS::BMS(PinName rd, PinName td): _bms(rd,td) { 00006 00007 frequency=250000; //Set frequency param 00008 } 00009 00010 void BMS::init_bms(){ 00011 _bms.frequency(frequency); 00012 offset_T=40; 00013 } 00014 void BMS::data_request(){ 00015 00016 req_msg.id=0x200; 00017 req_msg.data[0] = 0x10; 00018 req_msg.data[1] = 0x1A; 00019 req_msg.data[2] = 0x00; 00020 req_msg.data[3] = 0x02; 00021 req_msg.data[4] = 0x00; 00022 req_msg.data[5] = 0x00; 00023 req_msg.data[6] = 0x00; 00024 req_msg.data[7] = 0x03; 00025 00026 _bms.write(req_msg); 00027 } 00028 void BMS::int2bin(int value, char* buffer, int bufferSize) { 00029 char *nextChar = buffer + bufferSize-2; // location to write the least significant bit 00030 00031 for (int i = 0; i<(bufferSize-1); i++) { // for each bit 00032 (value & (1<<i)) ? *nextChar = '1' : *nextChar = '0'; // if set set to '1' else '0' 00033 nextChar --; 00034 } 00035 00036 *(buffer + bufferSize-1) = 0; // add the null terminator 00037 } 00038 void BMS::read_data(){ 00039 Serial pc(USBTX,USBRX); 00040 while(_bms.read(read_msg)){ 00041 if (read_msg.id == 1){ 00042 00043 //Tension min 00044 pc.printf("Message received: %d\n", read_msg.id); 00045 V_min_l = read_msg.data[0]; 00046 V_min_h = read_msg.data[1]; 00047 V_min = V_min_l*256 + V_min_h; 00048 pc.printf("V min: %f\n\r", V_min); 00049 00050 //Tension max 00051 V_max_l = read_msg.data[2]; 00052 V_max_h = read_msg.data[3]; 00053 V_max = V_max_l*256 + V_max_h; 00054 pc.printf("V max: %f\n\r", V_max); 00055 00056 //Temperatura min 00057 T_min = read_msg.data[4]-offset_T; 00058 pc.printf("T min: %d\n\r", T_min); 00059 00060 //Temperatura min 00061 T_max = read_msg.data[5]-offset_T; 00062 // float V_max = V_max_l*256 + V_max_h; 00063 pc.printf("T max: %d\n\r", T_max); 00064 00065 00066 //Precharged cmd 00067 precharged = read_msg.data[6]; 00068 int2bin(precharged, binaryBuffer, 9) ; 00069 precharged_c=binaryBuffer[0]; 00070 pc.printf("Precharged: %d\r\n", precharged); 00071 pc.printf("Precharged: %c \r\n ", binaryBuffer[0]); 00072 00073 //Rely 00074 rely_c=binaryBuffer[1]; 00075 pc.printf("Rely: %c \r\n", binaryBuffer[1]); 00076 00077 //Alarm 00078 alarm_c =binaryBuffer[2]; 00079 pc.printf("Alarm: %c \r\n ", binaryBuffer[2]); 00080 00081 } 00082 00083 } 00084 } 00085 float BMS::get_min_V(){ 00086 return V_min; 00087 } 00088 00089 float BMS::get_max_V(){ 00090 return V_max; 00091 } 00092 float BMS::get_voltage(){ 00093 voltage = (V_min + V_max)/2; 00094 return voltage; 00095 } 00096 int BMS::get_T_min(){ 00097 return T_min; 00098 } 00099 int BMS::get_T_max(){ 00100 return T_max; 00101 } 00102 char BMS::get_precharged_value(){ 00103 return precharged_c; 00104 } 00105 char BMS::get_rely_value(){ 00106 return rely_c; 00107 } 00108 char BMS::get_alarm(){ 00109 return alarm_c; 00110 }
Generated on Thu Aug 25 2022 14:00:38 by
1.7.2