-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbic2200.cpp
457 lines (416 loc) · 18.4 KB
/
bic2200.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//#################################################################################################
// Library to Control a BIC-2200-XX-CAN with a Arduino and a MCP2525
// Uses the Arduino CAN Libary by Sandeep Mistry
// Copyright (c) Dominic Eichinger. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//#################################################################################################
#include "bic2200.h"
int BIC2200::begin(int CS_Pin, byte CAN_Adress) {
//#################################################################################################
// Function: begin
// Access: Public
// Input: CS_Pin (int) Chip Select Pin
// CAN_Adress (byte) [0x00 - 0x07] CAN Bus Adress of BIC-2200
// Output: (int) 0 = CAN Initialisiation unsuccessfull; 1 = CAN Initialisiation successfull
// Description: Initialises the CAN Object and Calculates the CAN IDs
//#################################################################################################
_CS = CS_Pin;
_CAN_ID_SEND = MSG_ID_CAN_SEND_00 + CAN_Adress;
_CAN_ID_RECEIVE = MSG_ID_CAN_RECEIVE_00 + CAN_Adress;
CAN.setPins(_CS);
CAN.setClockFrequency(CAN_CLK_FREQUENCY);
return CAN.begin(CAN_BAUDRATE);
}
float BIC2200::readTemperature() {
//#################################################################################################
// Function: readTemperature
// Access: Public
// Input: -
// Output: (float) -1.0 = Temperature Readout Unsuccessfull; > 0.0 Temperature of BIC-2200
// Description: Read the Internal Temperature of BIC-2200
//#################################################################################################
byte data[2];
float temperature = -1.0;
if (_getRegisterValue(CMD_READ_TEMPERATURE_1, data)) {
temperature = ( ( data[1] << 8 ) + data[0]) * 0.1;
}
return temperature;
}
float BIC2200::readInputVoltage() {
//#################################################################################################
// Function: readInputVoltage
// Access: Public
// Input: -
// Output: (float) -1.0 = Voltage Readout Unsuccessfull; > 0.0 Input Voltage of BIC-2200
// Description: Read the Input Voltage of BIC-2200
//#################################################################################################
byte data[2];
float temperature = -1.0;
if (_getRegisterValue(CMD_READ_VIN, data)) {
temperature = ( ( data[1] << 8 ) + data[0]) * 0.1;
}
return temperature;
}
float BIC2200::readOutputVoltage() {
//#################################################################################################
// Function: readOutputVoltage
// Access: Public
// Input: -
// Output: (float) -1.0 = Voltage Readout Unsuccessfull; > 0.0 Output Voltage of BIC-2200
// Description: Read the Output Voltage of BIC-2200
//#################################################################################################
byte data[2];
float temperature = -1.0;
if (_getRegisterValue(CMD_READ_VOUT, data)) {
temperature = ( ( data[1] << 8 ) + data[0]) * 0.01;
}
return temperature;
}
float BIC2200::readOutputCurrent() {
//#################################################################################################
// Function: readOutputCurrent
// Access: Public
// Input: -
// Output: (float) -1.0 = Current Readout Unsuccessfull; > 0.0 Output Current of BIC-2200
// Description: Read the Output Current of BIC-2200
//#################################################################################################
byte data[2];
float temperature = -1.0;
if (_getRegisterValue(CMD_READ_IOUT, data)) {
temperature = ( ( data[1] << 8 ) + data[0]) * 0.01;
}
return temperature;
}
void BIC2200::setSystemConfig(int config) {
//#################################################################################################
// Function: setSystemConfig
// Access: Public
// Input: (int) config [0-5]
// 0 = 0x000 = CAN Controll disabled; Preset Power OFF
// 1 = 0x001 = CAN Controll enabled; Preset Power OFF
// 2 = 0x010 = CAN Controll disabled; Preset Power ON
// 3 = 0x011 = CAN Controll enabled; Preset Power ON
// 4 = 0x100 = CAN Controll disabled; Preset is previous set value
// 5 = 0x101 = CAN Controll enabled; Preset is previous set value
// Output: -
// Description: Set the System Config Register of BIC-2200
//#################################################################################################
byte data[2];
data[0] = lowByte(config);
data[1] = highByte(config);
_setRegisterValue(CMD_SYSTEM_CONFIG, data, 2);
}
int BIC2200::getSystemConfig() {
//#################################################################################################
// Function: getSystemConfig
// Access: Public
// Input: -
// Output: (int) config [0-5]
// 0 = 0x000 = CAN Controll disabled; Preset Power OFF
// 1 = 0x001 = CAN Controll enabled; Preset Power OFF
// 2 = 0x010 = CAN Controll disabled; Preset Power ON
// 3 = 0x011 = CAN Controll enabled; Preset Power ON
// 4 = 0x100 = CAN Controll disabled; Preset is previous set value
// 5 = 0x101 = CAN Controll enabled; Preset is previous set value
// Description: Get the System Config Register of BIC-2200
//#################################################################################################
byte data[2];
int config = -1;
if ( _getRegisterValue(CMD_SYSTEM_CONFIG, data) ) {
config = ( ( data[1] << 8 ) + data[0]);
}
return config;
}
void BIC2200::setBidirecitonalConfig(int config) {
//#################################################################################################
// Function: setBidirecitonalConfig
// Access: Public
// Input: (int) config [0-1]
// 0 = 0x000 = Bi-direction auto-detect mode. DIR_CTRL and C/D control (analogy) UN-controllable.
// 1 = 0x001 = Bi-direction battery mode. DIR_CTRL and C/D control (analogy) controllable
// Output: -
// Description: Set the Bidirectional Config Register of BIC-2200
//#################################################################################################
byte data[2];
data[0] = lowByte(config);
data[1] = highByte(config);
_setRegisterValue(CMD_BIDIRECTIONAL_CONFIG, data, 2);
}
int BIC2200::getBidirecitonalConfig() {
//#################################################################################################
// Function: getBidirecitonalConfig
// Access: Public
// Input: -
// Output: (int) config [0-1]
// 0 = 0x000 = Bi-direction auto-detect mode. DIR_CTRL and C/D control (analogy) UN-controllable.
// 1 = 0x001 = Bi-direction battery mode. DIR_CTRL and C/D control (analogy) controllable
// Description: Get the Bidirectional Config Register of BIC-2200
//#################################################################################################
byte data[2];
int config = -1;
if ( _getRegisterValue(CMD_BIDIRECTIONAL_CONFIG, data) ) {
config = ( ( data[1] << 8 ) + data[0]);
}
return config;
}
void BIC2200::setOutputVoltage(int voltage) {
//#################################################################################################
// Function: setOutputVoltage
// Access: Public
// Input: (int) voltage (Factor 0.01)
// Output: -
// Description: Sets the Output Voltage of the BIC-2200 (4800 = 48.0V)
//#################################################################################################
byte data[2];
data[0] = lowByte(voltage);
data[1] = highByte(voltage);
_setRegisterValue(CMD_VOUT_SET, data, 2);
}
int BIC2200::getOutputVoltage() {
//#################################################################################################
// Function: getOutputVoltage
// Access: Public
// Input: -
// Output: (int) voltage (Factor 0.01)
// Description: Gets the Output Voltage of the BIC-2200 (4800 = 48.0V)
//#################################################################################################
byte data[2];
int voltage = -1;
if ( _getRegisterValue(CMD_VOUT_SET, data) ) {
voltage = ( ( data[1] << 8 ) + data[0]);
}
return voltage;
}
void BIC2200::setOutputCurrent(int current) {
//#################################################################################################
// Function: setOutputCurrent
// Access: Public
// Input: (int) current (Factor 0.01)
// Output: -
// Description: Sets the Output Current of the BIC-2200 (1000 = 10.0A)
//#################################################################################################
byte data[2];
data[0] = lowByte(current);
data[1] = highByte(current);
_setRegisterValue(CMD_IOUT_SET, data, 2);
}
int BIC2200::getOutputCurrent() {
//#################################################################################################
// Function: getOutputCurrent
// Access: Public
// Input: -
// Output: (int) current (Factor 0.01)
// Description: Gets the Output Current of the BIC-2200 (1000 = 10.0A)
//#################################################################################################
byte data[2];
int current = -1;
if ( _getRegisterValue(CMD_IOUT_SET, data) ) {
current = ( ( data[1] << 8 ) + data[0]);
}
return current;
}
void BIC2200::setReverseOutputVoltage(int voltage) {
//#################################################################################################
// Function: setReverseOutputVoltage
// Access: Public
// Input: (int) voltage (Factor 0.01)
// Output: -
// Description: Sets the Reverse Output Voltage of the BIC-2200 (4800 = 48.0V)
//#################################################################################################
byte data[2];
data[0] = lowByte(voltage);
data[1] = highByte(voltage);
_setRegisterValue(CMD_REVERSE_VOUT_SET, data, 2);
}
int BIC2200::getReverseOutputVoltage() {
//#################################################################################################
// Function: getReverseOutputVoltage
// Access: Public
// Input: -
// Output: (int) voltage (Factor 0.01)
// Description: Gets the Reverse Output Voltage of the BIC-2200 (4800 = 48.0V)
//#################################################################################################
byte data[2];
int voltage = -1;
if ( _getRegisterValue(CMD_REVERSE_VOUT_SET, data) ) {
voltage = ( ( data[1] << 8 ) + data[0]);
}
return voltage;
}
void BIC2200::setReverseOutputCurrent(int current) {
//#################################################################################################
// Function: setReverseOutputCurrent
// Access: Public
// Input: (int) current (Factor 0.01)
// Output: -
// Description: Sets the Reverse Output Current of the BIC-2200 (1000 = 10.0A)
//#################################################################################################
byte data[2];
data[0] = lowByte(current);
data[1] = highByte(current);
_setRegisterValue(CMD_REVERSE_IOUT_SET, data, 2);
}
int BIC2200::getReverseOutputCurrent() {
//#################################################################################################
// Function: getReverseOutputCurrent
// Access: Public
// Input: -
// Output: (int) current (Factor 0.01)
// Description: Gets the Reverse Output Current of the BIC-2200 (1000 = 10.0A)
//#################################################################################################
byte data[2];
int current = -1;
if ( _getRegisterValue(CMD_REVERSE_IOUT_SET, data) ) {
current = ( ( data[1] << 8 ) + data[0]);
}
return current;
}
void BIC2200::setOperation(bool operation) {
//#################################################################################################
// Function: setOperation
// Access: Public
// Input: (bool) operation - false = OFF; true = ON
// Output: -
// Description: Sets the Device ON or OFF
//#################################################################################################
byte data[1];
if (operation) {
data[0] = 0x01;
} else {
data[0] = 0x00;
}
_setRegisterValue(CMD_OPERATION, data, 1);
}
int BIC2200::getOperation() {
//#################################################################################################
// Function: getOperation
// Access: Public
// Input: -
// Output: (bool) operation - false = OFF; true = ON
// Description: Gets the State of the Device (ON/OFF)
//#################################################################################################
byte data[1];
int operation = -1;
if (_getRegisterValue(CMD_OPERATION, data)) {
operation = data[0];
}
return operation;
}
void BIC2200::setDirection(bool direction) {
//#################################################################################################
// Function: setDirection
// Access: Public
// Input: (bool) direction - false = AC->DC; true = DC->AC
// Output: -
// Description: Sets the Power Direction of the BIC-2200
//#################################################################################################
byte data[1];
if (direction) {
data[0] = 0x01;
} else {
data[0] = 0x00;
}
_setRegisterValue(CMD_DIRECTION_CTRL, data, 1);
}
int BIC2200::getDirection() {
//#################################################################################################
// Function: getDirection
// Access: Public
// Input: -
// Output: (bool) direction - false = AC->DC; true = DC->AC
// Description: Gets the Power Direction of the BIC-2200
//#################################################################################################
byte data[1];
int direction = -1;
if (_getRegisterValue(CMD_DIRECTION_CTRL, data)) {
direction = data[0];
}
return direction;
}
int BIC2200::getSystemStatus() {
//#################################################################################################
// Function: getDirection
// Access: Public
// Input: -
// Output: (int) Details: https://www.meanwell.com/upload/pdf/bic-2200-e.pdf
// Description: Gets the System State of the BIC-2200
//#################################################################################################
byte data[2];
int status = -1;
if ( _getRegisterValue(CMD_SYSTEM_STATUS, data) ) {
status = ( ( data[1] << 8 ) + data[0]);
}
return status;
}
int BIC2200::getScalingFactors() {
//#################################################################################################
// Function: getScalingFactors
// Access: Public
// Input: -
// Output: (int) Details: https://www.meanwell.com/upload/pdf/bic-2200-e.pdf
// Description: Gets the Scaling Factors of the BIC-2200
//#################################################################################################
byte data[2];
int factors = -1;
if ( _getRegisterValue(CMD_SCALING_FACTOR, data) ) {
factors = ( ( data[1] << 8 ) + data[0]);
}
return factors;
}
int BIC2200::_getRegisterValue(int reg, byte * data){
//#################################################################################################
// Function: _getRegisterValue
// Access: Private
// Input: reg (int) Register to Read from BIC2200
// data (byte *) Pointer to a Byte Array where the data should be written to
// Output: (int) 0 = data Array not Valid; 1 = data Array is valid
// Description: Reads a Register from BIC-2200
//#################################################################################################
unsigned long startTime;
unsigned int index = 0;
int retVal = 0;
byte data_received[8];
bool blnDataValid = true;
CAN.beginExtendedPacket(_CAN_ID_SEND);
CAN.write(lowByte(reg));
CAN.write(highByte(reg));
CAN.endPacket();
startTime = micros();
while (((micros() - startTime ) < CAN_TIMEOUT ) && blnDataValid ) {
int packetSize = CAN.parsePacket();
if (packetSize || CAN.packetId() != -1) {
while (CAN.available() && index < 8) {
data_received[index] = CAN.read();
++index;
}
if (lowByte(reg) == data_received[0] && highByte(reg) == data_received[1]) {
for (unsigned int i = 0; i < (index - 2); i++) {
data[i] = data_received[i + 2];
}
} else {
blnDataValid = false;
}
if (blnDataValid) {
retVal = 1;
}
}
}
return retVal;
}
void BIC2200::_setRegisterValue(int reg, byte * data, int len){
//#################################################################################################
// Function: _getRegisterValue
// Access: Private
// Input: reg (int) Register to Write to BIC2200
// data (byte *) Pointer to a Byte Array containing the Data to Write
// Output: (int) 0 = Register Value set unsuccessfull; 1 = Register Value sucessfully written
// Description: Writes a Register to BIC-2200
//#################################################################################################
CAN.beginExtendedPacket(_CAN_ID_SEND);
CAN.write(lowByte(reg));
CAN.write(highByte(reg));
for (int i = 0; i < len; i++) {
CAN.write(data[i]);
}
CAN.endPacket();
}