12 #include <yarp/os/Log.h>
13 #include <yarp/os/LogStream.h>
16 #include <string_view>
21 #include "EoProtocol.h"
22 #include "EoProtocolAS.h"
23 #include "EoProtocolMN.h"
24 #include "EoAnalogSensors.h"
26 #include "embot_core_binary.h"
29 #pragma warning(once : 4355)
33 using namespace yarp::os;
38 temperature_ =
data->temperature / 10;
39 voltage_ = std::trunc(10 *
data->voltage) / 10;
40 current_ = std::trunc(10 *
data->current) / 10;
41 charge_ =
data->charge;
42 status_ =
data->status;
43 timeStamp_ = timestamp;
46 embObjBattery::embObjBattery()
48 yInfo() <<
"CanBatterySensors has been created";
49 device_ = std::make_shared<yarp::dev::embObjDevPrivData>(
"embObjBattery");
52 embObjBattery::embObjBattery(std::shared_ptr<yarp::dev::embObjDevPrivData> device) : device_(device)
68 yInfo() <<
"embObjBattery::open(): preparing ETH resource";
69 if (!
device_->prerareEthService(config,
this))
72 yInfo() <<
device_->getBoardInfo() <<
" embObjBattery::open(): browsing xml files which describe the service";
74 if (!parser.
parse(config))
76 yError() <<
device_->getBoardInfo() <<
"open() fails to parse xml... cannot continue ";
80 yInfo() <<
device_->getBoardInfo() <<
" embObjBattery::open(): verify the presence of the board and if its protocol version is correct";
81 if (!
device_->res->verifyEPprotocol(eoprot_endpoint_analogsensors))
83 yError() <<
device_->getBoardInfo() <<
" open() fails to verifyEPprotocol... cannot continue ";
88 yInfo() <<
device_->getBoardInfo() <<
" embObjBattery::open(): verify and activate the FT service";
89 eOmn_serv_parameter_t canBatteryData;
90 canBatteryData.configuration.type = eomn_serv_AS_battery;
91 canBatteryData.configuration.diagnosticsmode = eomn_serv_diagn_mode_NONE;
92 canBatteryData.configuration.diagnosticsparam = 0;
93 parser.
toEomn(canBatteryData.configuration.data.as.battery);
94 if (!
device_->res->serviceVerifyActivate(eomn_serv_category_battery, &canBatteryData, 5.0))
96 yError() <<
device_->getBoardInfo() <<
" open() fails to serviceVerifyActivate... cannot continue ";
101 yInfo() <<
device_->getBoardInfo() <<
" embObjBattery::open(): configure the FT service";
104 yError() <<
device_->getBoardInfo() <<
" open() fails to sendConfig2boards... cannot continue";
109 yInfo() <<
device_->getBoardInfo() <<
" embObjBattery::open(): impose the network variable which the ETH bord must stream up";
112 yError() <<
device_->getBoardInfo() <<
" open() fails to initRegulars... cannot continue";
117 yInfo() <<
device_->getBoardInfo() <<
" embObjBattery::open(): start the FT service";
118 if (!
device_->res->serviceStart(eomn_serv_category_battery))
120 yError() <<
device_->getBoardInfo() <<
" open() fails to serviceStart... cannot continue";
128 yDebug() <<
device_->getBoardInfo() <<
" open() correctly starts service";
132 yInfo() <<
device_->getBoardInfo() <<
" embObjBattery::open(): start streaming of FT data";
135 yError() <<
device_->getBoardInfo() <<
" open() fails to sendStart2boards... cannot continue";
150 eOprotID32_t id32 = eo_prot_ID32dummy;
151 eOas_battery_config_t cfg{0, 0};
153 id32 = eoprot_ID_get(eoprot_endpoint_analogsensors, eoprot_entity_as_battery, 0, eoprot_tag_as_battery_config);
157 yError() <<
device_->getBoardInfo() <<
" sendConfig2boards() while try to configure fperiod=" << cfg.period;
163 yDebug() <<
device_->getBoardInfo() <<
" sendConfig2boards() correctly configured boards with period=" << cfg.period;
170 eOprotID32_t id32 = eo_prot_ID32dummy;
176 id32 = eoprot_ID_get(eoprot_endpoint_analogsensors, eoprot_entity_as_battery, 0, eoprot_tag_as_battery_cmmnds_enable);
180 yError() <<
device_->getBoardInfo() <<
" sendStart2boards() while try to enable the boards transmission";
186 yDebug() <<
device_->getBoardInfo() <<
" sendStart2boards() correctly enabled the boards transmission";
195 vector<eOprotID32_t> id32v;
196 eOprotID32_t id32 = eo_prot_ID32dummy;
199 id32 = eoprot_ID_get(eoprot_endpoint_analogsensors, eoprot_entity_as_battery, 0, eoprot_tag_as_battery_status_timedvalue);
200 id32v.push_back(id32);
204 yError() <<
device_->getBoardInfo() <<
" initRegulars() fails to add its variables to regulars: cannot proceed any further";
210 yDebug() <<
device_->getBoardInfo() <<
" initRegulars() added" << id32v.size() <<
"regular rops ";
212 for (
size_t r = 0; r < id32v.size(); r++)
214 uint32_t item = id32v.at(r);
215 eoprot_ID2information(item, nvinfo,
sizeof(nvinfo));
216 yDebug() <<
device_->getBoardInfo() <<
"\t it added regular rop for" << nvinfo;
232 eOprotIndex_t eoprotIndex = eoprot_ID2index(id32);
235 yError() <<
device_->getBoardInfo() <<
" update() index too big";
238 eOprotEntity_t entity = eoprot_ID2entity(id32);
239 if (entity != eoprot_entity_as_battery)
241 yError() <<
device_->getBoardInfo() <<
" update() wrong entity";
245 eOprotTag_t tag = eoprot_ID2tag(id32);
246 if (tag != eoprot_tag_as_battery_status_timedvalue)
248 yError() <<
device_->getBoardInfo() <<
" update() wrong tag";
252 eOas_battery_timedvalue_t *
data = (eOas_battery_timedvalue_t *)rxdata;
260 yDebug(
"CAN DATA NOT YET AVAILABLE");
277 std::unique_lock<std::shared_mutex> lck(
mutex_);
304 yError() <<
device_->getBoardInfo() <<
" update timeout for index:" << eoprot_ID2index(id32);
306 masStatus_[eoprot_ID2index(id32)] = MAS_TIMEOUT;
317 static const std::array<std::pair<eOas_battery_alarm_status_t, std::string_view>, eoas_battery_alarm_status_numberof> s_boards_map_of_battery_alarm_status =
319 {{eoas_bms_general_alarm_lowvoltage,
"eoas_bms_general_alarm_lowvoltage"},
320 {eoas_bms_general_alarm_highvoltage,
"eoas_bms_general_alarm_highvoltage"},
321 {eoas_bms_general_alarm_overcurrent_discharge,
"eoas_bms_general_alarm_overcurrent_discharge"},
322 {eoas_bms_general_alarm_overcurrent_charge,
"eoas_bms_general_alarm_overcurrent_charge"},
323 {eoas_bms_general_alarm_lowSOC,
"eoas_bms_general_alarm_lowSOC"},
324 {eoas_bms_general_alarm_lowtemperature,
"eoas_bms_general_alarm_lowtemperature"},
325 {eoas_bms_general_alarm_hightemperature,
"eoas_bms_general_alarm_hightemperature"},
326 {eoas_bat_status_hsm_mosfet_broken,
"eoas_bat_status_hsm_mosfet_broken"},
327 {eoas_bat_status_hsm_mosfet_normal,
"eoas_bat_status_hsm_mosfet_normal"},
328 {eoas_bat_status_hsm_overcurrent_overvoltage,
"eoas_bat_status_hsm_overcurrent_overvoltage"},
329 {eoas_bat_status_hsm_normal,
"eoas_bat_status_hsm_normal"},
330 {eoas_bat_status_hsm_voltage_power_good,
"eoas_bat_status_hsm_voltage_power_good"},
331 {eoas_bat_status_hsm_voltage_not_guaranteed,
"eoas_bat_status_hsm_voltage_not_guaranteed"},
332 {eoas_bat_status_hsm_status_on,
"eoas_bat_status_hsm_status_on"},
333 {eoas_bat_status_hsm_status_off,
"eoas_bat_status_hsm_status_off"},
334 {eoas_bat_status_motor_regulator_overcurrent,
"eoas_bat_status_motor_regulator_overcurrent"},
335 {eoas_bat_status_motor_regulator_normal,
"eoas_bat_status_motor_regulator_normal"},
336 {eoas_bat_status_motor_on,
"eoas_bat_status_motor_on"},
337 {eoas_bat_status_motor_off,
"eoas_bat_status_motor_off"},
338 {eoas_bat_status_board_regulator_overcurrent,
"eoas_bat_status_board_regulator_overcurrent"},
339 {eoas_bat_status_board_regulator_normal,
"eoas_bat_status_board_regulator_normal"},
340 {eoas_bat_status_board_on,
"eoas_bat_status_board_on"},
341 {eoas_bat_status_board_off,
"eoas_bat_status_board_off"},
342 {eoas_bat_status_btn_2_start_up_phase,
"eoas_bat_status_btn_2_start_up_phase"},
343 {eoas_bat_status_btn_2_stable_op,
"eoas_bat_status_btn_2_stable_op"},
344 {eoas_bat_status_btn_1_start_up_phase,
"eoas_bat_status_btn_1_start_up_phase"},
345 {eoas_bat_status_btn_1_stable_op,
"eoas_bat_status_btn_1_stable_op"}}
349 std::string statusstring = {};
350 statusstring.reserve(512);
354 for (uint8_t i = 0; i < eoas_bms_alarm_numberof; i++)
356 if((embot::core::binary::bit::check(currStatus, i)))
358 statusstring.append(
"\t");
359 statusstring.append(s_boards_map_of_battery_alarm_status.at(i).second);
360 statusstring.append(
"\n");
367 for (uint8_t i = eoas_bms_alarm_numberof; i < eoas_battery_alarm_status_numberof; i = i+2)
369 if ((embot::core::binary::bit::check(currStatus, bit_pos) != embot::core::binary::bit::check(prevStatus, bit_pos)) || isFirstLoop)
371 statusstring.append(
"\t");
372 if((embot::core::binary::bit::check(currStatus, bit_pos)))
374 statusstring.append(s_boards_map_of_battery_alarm_status.at(i).second);
378 statusstring.append(s_boards_map_of_battery_alarm_status.at(i+1).second);
380 statusstring.append(
"\n");
387 if(statusstring.empty())
389 statusstring.append(
"\tNo Faults Detected. All Alarms Bit Down\n");
400 return yarp::os::Time::now();
445 std::stringstream ss;
449 battery_info = ss.str();
457 if ((
int)(voltage_ * 10) != (
int)(other.
voltage_ * 10))
459 if ((
int)(current_ * 10) != (
int)(other.
current_ * 10))
bool operator==(const BatteryInfo &right, const BatteryInfo &left)
bool operator==(const CanBatteryData &other) const
void decode(eOas_battery_timedvalue_t *data, double timestamp)
bool operator!=(const CanBatteryData &other) const
bool parse(const yarp::os::Searchable &config)
BatteryInfo & getBatteryInfo()
bool toEomn(eOmn_serv_config_data_as_battery_t &out) const
virtual bool setcheckRemoteValue(const eOprotID32_t id32, void *value, const unsigned int retries=10, const double waitbeforecheck=0.001, const double timeout=0.050)=0
virtual bool serviceSetRegulars(eOmn_serv_category_t category, vector< eOprotID32_t > &id32vector, double timeout=0.500)=0
eth::iethresType_t type() override
bool open(yarp::os::Searchable &config)
eOabstime_t firstCanTimestamp_
std::string updateStatusStringStream(const uint16_t &currStatus, const uint16_t &prevStatus, bool isFirstLoop)
bool initialised() override
std::map< eOprotID32_t, eOabstime_t > timeoutUpdate_
double firstYarpTimestamp_
bool sendConfig2boards(ServiceParserCanBattery &parser, eth::AbstractEthResource *deviceRes)
bool getBatteryVoltage(double &voltage) override
static constexpr bool useBoardTimeFlag_
static constexpr eOabstime_t updateTimeout_
virtual double calculateBoardTime(eOabstime_t current)
bool getBatteryStatus(Battery_status &status) override
bool getBatteryCurrent(double ¤t) override
bool checkUpdateTimeout(eOprotID32_t id32, eOabstime_t current)
std::shared_ptr< yarp::dev::embObjDevPrivData > device_
bool getBatteryInfo(std::string &battery_info) override
bool sendStart2boards(ServiceParserCanBattery &parser, eth::AbstractEthResource *deviceRes)
std::vector< yarp::dev::MAS_status > masStatus_
bool initRegulars(ServiceParserCanBattery &parser, eth::AbstractEthResource *deviceRes)
bool update(eOprotID32_t id32, double timestamp, void *rxdata) override
static constexpr bool checkUpdateTimeoutFlag_
CanBatteryData canBatteryData_
bool getBatteryTemperature(double &temperature) override
bool getBatteryCharge(double &charge) override
Copyright (C) 2008 RobotCub Consortium.