bms_interface.h
Go to the documentation of this file.
1 //
2 // Created by sub on 22/10/17.
3 //
4 
5 #ifndef BMS_BMS_INTERFACE_H
6 #define BMS_BMS_INTERFACE_H
7 
8 #include <cerrno>
9 #include <cstring>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <termios.h>
13 #include <errno.h>
14 #include <string>
15 
16 #include <fcntl.h>
17 #include <unistd.h> // UNIX standard function definitions
18 #include <stdexcept>
19 #include <stdint.h>
20 #include <vector>
21 
22 #define BUFF_SIZE 500
23 #define MAX_BAD_READS 200
24 #define BAUD_RATE B9600
25 
26 #define BMS_PKG_START_DELIM ':'
27 #define BMS_PKG_START_DELIM_LEN 1
28 #define BMS_PKG_END_DELIM '~'
29 #define BMS_PKG_CRC_BACK_OFFSET 3
30 #define BMS_PKG_CRC_SIZE 2
31 #define BMS_PKG_BASE 16 //hexa
32 #define BMS_PKG_CMD_SUCCESS 130
33 #define BMS_PKG_PROTOCOL_VERSION 82
34 
35 #define BMS_PKG_ADDR_INDX 1
36 #define BMS_PKG_ADDR_SIZE 2
37 
38 #define BMS_PKG_CMD_INDX 3
39 #define BMS_PKG_CMD_SIZE 2
40 
41 #define BMS_PKG_VER_INDX 5
42 #define BMS_PKG_VER_SIZE 2
43 
44 #define BMS_PKG_LEN_INDX 7
45 #define BMS_PKG_LEN_SIZE 4
46 
47 #define BMS_PKG_TIME_INDX 11
48 #define BMS_PKG_TIME_SIZE 14
49 
50 #define BMS_PKG_VBAT_INDX 25
51 #define BMS_PKG_VBAT_SIZE 4
52 
53 #define BMS_PKG_CELL_NUM_INDX 29
54 #define BMS_PKG_CELL_NUM_SIZE 2
55 
56 #define BMS_PKG_CELLS_ARR_INDX 31
57 #define BMS_PKG_CELL_SIZE 4
58 
59 #define BMS_PKG_CHRG_CURR_INDX 31
60 #define BMS_PKG_CHRG_CURR_SIZE 4
61 
62 #define BMS_PKG_DCHRG_CURR_INDX 35
63 #define BMS_PKG_DCHRG_CURR_SIZE 4
64 
65 /* assuming cell size is 0 (for other values, multiply) */
66 #define BMS_PKG_TEMP_NUM_INDX 39
67 #define BMS_PKG_TEMP_NUM_SIZE 2
68 
69 #define BMS_PKG_TEMPS_ARRAY_INDX 41
70 #define BMS_PKG_TEMP_SIZE 2
71 
72 /* assuming cell size is 0 (for other values, multiply) */
73 #define BMS_PKG_VSTATE_INDX 41
74 #define BMS_PKG_VSTATE_SIZE 4
75 
76 #define BMS_PKG_CSTATE_INDX 45
77 #define BMS_PKG_CSTATE_SIZE 4
78 
79 #define BMS_PKG_TSTATE_INDX 49
80 #define BMS_PKG_TSTATE_SIZE 4
81 
82 #define BMS_PKG_ALARM_INDX 53
83 #define BMS_PKG_ALARM_SIZE 4
84 
85 #define BMS_PKG_FET_STATE_INDX 57
86 #define BMS_PKG_FET_STATE_SIZE 2
87 
88 #define BMS_PKG_WARN_VOV_INDX 59
89 #define BMS_PKG_WARN_VOV_SIZE 4
90 
91 #define BMS_PKG_WARN_VUV_INDX 63
92 #define BMS_PKG_WARN_VUV_SIZE 4
93 
94 #define BMS_PKG_NUM_WARN_VHIGH_INDX 67
95 #define BMS_PKG_NUM_WARN_VHIGH_SIZE 4
96 
97 #define BMS_PKG_NUM_WARN_VLOW_INDX 71
98 #define BMS_PKG_NUM_WARN_VLOW_SIZE 4
99 
100 #define BMS_PKG_BALANCE_STATE_INDX 75
101 #define BMS_PKG_BALANCE_STATE_SIZE 4
102 
103 #define BMS_PKG_DCHG_NUM_INDX 79
104 #define BMS_PKG_DCHG_NUM_SIZE 4
105 
106 #define BMS_PKG_CHG_NUM_INDX 83
107 #define BMS_PKG_CHG_NUM_SIZE 4
108 
109 #define BMS_PKG_SOC_INDX 87
110 #define BMS_PKG_SOC_SIZE 2
111 
112 #define BMS_PKG_CAP_NOW_INDX 89
113 #define BMS_PKG_CAP_NOW_SIZE 4
114 
115 #define BMS_PKG_CAP_FULL_INDX 93
116 #define BMS_PKG_CAP_FULL_SIZE 4
117 
118 
119 namespace bms
120 {
121  struct data
122  {
123  bool valid = false;
124  uint8_t addr = 0; //Device Address
125  uint8_t cmd = 0; //Command for BMS
126  uint8_t ver = 0; //Protocol Version
127  uint16_t len = 0; //Length of the entire posted data
128  ulong time = 0;
129  float vbat = 0; //battery total voltage (V)
130  uint8_t cell_num = 0; //Number of cells 1~16
131  std::vector<float> vcells; //all voltages by order (V)
132  float chrg_current = 0; //CHG current
133  float dchrg_current = 0; //DSG current
134  uint8_t temp_num = 0; //Number of temp sensors (m)
135  std::vector<uint8_t> temps; //temp sensors array
136  uint8_t temp_max = 0;
137  uint16_t vstate = 0;
138 
139  /* raw cstate_state and bits flags fetched from it */
140  uint16_t cstate = 0;
141  uint8_t is_dchrg = 0; //1 if discharging, 0 if not
142  uint8_t is_chrg = 0; //1 if charging, 0 if not
143 
144  uint16_t tstate = 0;
145  uint16_t alarm = 0;
146 
147  uint8_t fet_state = 0;
148 
149  uint16_t warn_vov = 0; //single cell high voltage warning value mV
150  uint16_t warn_vuv = 0; //single cell undervoltage warning value
151  uint16_t num_warn_vhigh = 0; //battery pack high voltage warning value
152  uint16_t num_warn_vlow = 0; //battery pack undervoltage warning value
153  uint16_t balance_state = 0; //Balance state for V0 to V15
154  uint16_t dchg_num = 0; //Number of discharge events
155  uint16_t chg_num = 0; //Number of charge events
156  uint8_t soc = 0; //Battery SOC [%]
157  uint16_t cap_now = 0; //Current capacity
158  uint16_t cap_full = 0; //Full Charge Capacity [Ah]
159  };
160 
161 
162  class BMSException : public std::runtime_error
163  {
164  public:
165  BMSException(std::string err_msg) : std::runtime_error(err_msg){}
166  };
167 
169  {
170  public:
171  BMSErrorException(std::string err_msg) : BMSException(err_msg){}
172  };
173 
175  {
176  public:
177  BMSWarnException(std::string err_msg) : BMSException(err_msg){}
178  };
179 
180 
182  {
183  private:
185  const std::string SEND_ME_DATA_ = ":000200000ee8~";
186 
187  void setAttributes();
188  int readPkgFromBMS(char *buff, size_t buff_size_limit);
189  data decodePkg(char buff[], size_t buff_size);
190  int fetchParam(char buff[], int param_indx, size_t param_size);
191 
192  public:
193  void connect(std::string port);
194  data read();
196  ~BMSInterface();
197  uint8_t getBitInByte(uint8_t byte, uint8_t position)
198  {
199  return (byte >> position) & 0x1;
200  }
201 
202  uint8_t getBitInWord(uint16_t byte, uint8_t position)
203  {
204  return (byte >> position) & 0x1;
205  }
206 
207  };
208 }
209 
210 
211 #endif //BMS_BMS_INTERFACE_H
uint8_t getBitInByte(uint8_t byte, uint8_t position)
uint8_t is_chrg
std::vector< uint8_t > temps
uint16_t warn_vuv
uint8_t temp_max
uint16_t chg_num
uint8_t getBitInWord(uint16_t byte, uint8_t position)
uint16_t tstate
float chrg_current
uint16_t num_warn_vhigh
uint16_t len
std::vector< float > vcells
uint16_t balance_state
uint8_t temp_num
uint16_t alarm
uint8_t cell_num
uint16_t vstate
BMSErrorException(std::string err_msg)
uint8_t soc
uint16_t cap_full
uint16_t dchg_num
uint16_t num_warn_vlow
uint8_t is_dchrg
uint8_t cmd
uint8_t ver
uint16_t cap_now
uint8_t fet_state
float dchrg_current
BMSException(std::string err_msg)
#define BMS_PKG_CAP_FULL_INDX
uint16_t cstate
uint16_t warn_vov
uint8_t addr
BMSWarnException(std::string err_msg)


bms_interface
Author(s): Jane Doe
autogenerated on Wed Jan 3 2018 03:47:54