40 m_ibattery_p =
nullptr;
42 memset(m_log_buffer, 0, 1024);
48 m_ibattery_p =
nullptr;
58 if (battery2attach.
size() != 1)
60 yCError(BATTERYWRAPPER,
"Cannot attach more than one device");
68 Idevice2attach->
view(m_ibattery_p);
71 if(
nullptr == m_ibattery_p)
73 yCError(BATTERYWRAPPER,
"Subdevice passed to attach method is invalid");
77 PeriodicThread::setPeriod(m_period);
78 return PeriodicThread::start();
83 if (PeriodicThread::isRunning())
85 PeriodicThread::stop();
87 m_ibattery_p =
nullptr;
96 void BatteryWrapper::detach()
98 m_ibattery_p =
nullptr;
105 bool ok = in.
read(connection);
106 if (!ok)
return false;
128 yCError(BATTERYWRAPPER,
"Invalid vocab received");
133 yCError(BATTERYWRAPPER,
"Invalid vocab received");
143 if (returnToSender !=
nullptr)
145 out.
write(*returnToSender);
160 if (!config.
check(
"period"))
163 yCWarning(BATTERYWRAPPER) <<
"Missing 'period' parameter. Assuming default value 1.0 s";
169 yCInfo(BATTERYWRAPPER) <<
"Using period:" << m_period <<
"s";
171 if (!config.
check(
"quitPortName"))
173 m_quitPortName = config.
find(
"quitPortName").
asString();
176 if (!config.
check(
"name"))
178 yCError(BATTERYWRAPPER) <<
"Missing 'name' parameter. Check you configuration file; it must be like:";
179 yCError(BATTERYWRAPPER) <<
"--name: prefix of the ports opened by the device, e.g. /robotName/battery1";
180 yCError(BATTERYWRAPPER) <<
"/data:o and /rpc:i are automatically appended by the wrapper at the end";
185 m_streamingPortName = config.
find(
"name").
asString() +
"/data:o";
186 m_rpcPortName = config.
find(
"name").
asString() +
"/rpc:i";
189 m_enable_shutdown = config.
check(
"enable_shutdown",
Value(0),
"enable/disable the automatic shutdown").asBool();
190 m_enable_log = config.
check(
"enable_log",
Value(0),
"enable/disable log to file").asBool();
192 if(!initialize_YARP(config))
194 yCError(BATTERYWRAPPER) << m_sensorId <<
"Error initializing YARP ports";
200 yCInfo(BATTERYWRAPPER,
"writing to log file batteryLog.txt");
201 m_logFile = fopen(
"batteryLog.txt",
"w");
204 if (config.
check(
"subdevice"))
210 p.
unput(
"subdevice");
212 p.setMonitor(config.getMonitor(),
"subdevice");
216 yCError(BATTERYWRAPPER) <<
"Failed to open subdevice.. check params";
220 driverlist.
push(&m_driver,
"1");
223 yCError(BATTERYWRAPPER) <<
"Failed to open subdevice.. check params";
234 if (!m_streamingPort.
open(m_streamingPortName.c_str()))
236 yCError(BATTERYWRAPPER) <<
"Error opening port" << m_streamingPortName;
239 if (!m_rpcPort.
open(m_rpcPortName.c_str()))
241 yCError(BATTERYWRAPPER) <<
"Error opening port" << m_rpcPortName;
254 if (m_ibattery_p!=
nullptr)
259 bool ret_sts, ret_chg, ret_vlt, ret_cur, ret_tmp;
263 if (ret_chg) m_battery_charge = tmp;
268 if (ret_vlt) m_battery_voltage = tmp;
273 if (ret_cur) m_battery_current = tmp;
278 if (ret_tmp) m_battery_temperature = tmp;
283 if (ret_sts) m_battery_status = tmp;
288 m_lastStateStamp.
update();
297 m_streamingPort.
write();
300 if (m_battery_status >0.4) check_battery_status(m_battery_charge);
306 struct tm * timeinfo;
308 timeinfo = localtime(&rawtime);
309 char* battery_timestamp = asctime(timeinfo);
310 std::snprintf(m_log_buffer, 1024,
"battery status: %+6.1fA % 6.1fV charge:% 6.1f%% time: %s", m_battery_current, m_battery_voltage, m_battery_charge, battery_timestamp);
311 fprintf(m_logFile,
"%s", m_log_buffer);
316 yCError(BATTERYWRAPPER,
"BatteryWrapper: %s: Sensor returned error", m_sensorId.c_str());
323 yCTrace(BATTERYWRAPPER,
"BatteryWrapper::Close");
324 if (PeriodicThread::isRunning())
326 PeriodicThread::stop();
333 m_streamingPort.
close();
343 PeriodicThread::stop();
348 void BatteryWrapper::notify_message(
string msg)
351 yCWarning(BATTERYWRAPPER,
"%s", msg.c_str());
353 yCWarning(BATTERYWRAPPER,
"%s", msg.c_str());
354 string cmd =
"echo " + msg +
" | wall";
356 retval = system(cmd.c_str());
357 yCDebug(BATTERYWRAPPER) <<
"system executed command" << cmd.c_str() <<
" with return value:" << retval;
361 void BatteryWrapper::emergency_shutdown(
string msg)
365 cmd =
"shutdown /s /t 120 /c " + msg;
366 yCWarning(BATTERYWRAPPER,
"%s", msg.c_str());
371 yCWarning(BATTERYWRAPPER,
"%s", msg.c_str());
372 cmd =
"echo " + msg +
" | wall";
373 retval = system(cmd.c_str());
374 yCDebug(BATTERYWRAPPER) <<
"system executed command" << cmd.c_str() <<
" with return value:" << retval;
376 cmd =
"sudo shutdown -h 2 " + msg;
377 retval = system(cmd.c_str());
378 yCDebug(BATTERYWRAPPER) <<
"system executed command" << cmd.c_str() <<
" with return value:" << retval;
380 #ifdef ICUB_SSH_SHUTDOWN
381 cmd =
"ssh icub@pc104 sudo shutdown -h 2";
382 retval = system(cmd.c_str());
383 yCDebug(BATTERYWRAPPER) <<
"system executed command" << cmd.c_str() <<
" with return value:" << retval;
388 void BatteryWrapper::check_battery_status(
double battery_charge)
390 static bool notify_15 =
true;
391 static bool notify_12 =
true;
392 static bool notify_10 =
true;
393 static bool notify_0 =
true;
395 if (battery_charge > 20)
403 if (battery_charge < 5)
407 if (m_enable_shutdown)
409 emergency_shutdown(
"CRITICAL WARNING: battery charge below critical level 5%. The robot will be stopped and the system will shutdown in 2mins.");
410 if (m_quitPortName !=
"") { stop_robot(m_quitPortName); }
415 notify_message(
"CRITICAL WARNING: battery charge reached critical level 5%, but the emergency shutodown is currently disabled!");
420 else if (battery_charge < 10)
422 if (notify_10) { notify_message(
"WARNING: battery charge below 10%"); notify_10 =
false; }
424 else if (battery_charge < 12)
426 if (notify_12) { notify_message(
"WARNING: battery charge below 12%"); notify_12 =
false; }
428 else if (battery_charge < 15)
430 if (notify_15) { notify_message(
"WARNING: battery charge below 15%"); notify_15 =
false; }
434 void BatteryWrapper::stop_robot(
string quit_port)
441 port_shutdown.
open((m_streamingPortName +
"/shutdown:o").c_str());
445 port_shutdown.
write(bot);
447 port_shutdown.
close();
#define DEFAULT_THREAD_PERIOD
define control board standard interfaces
constexpr yarp::conf::vocab32_t VOCAB_IS
constexpr yarp::conf::vocab32_t VOCAB_FAILED
constexpr yarp::conf::vocab32_t VOCAB_IBATTERY
constexpr yarp::conf::vocab32_t VOCAB_BATTERY_INFO
bool attachAll(const yarp::dev::PolyDriverList &p) override
Attach to a list of objects.
void run() override
Loop function.
bool threadInit() override
Initialization method.
bool detachAll() override
Detach the object (you must have first called attach).
bool close() override
Close the DeviceDriver.
void threadRelease() override
Release method.
bool open(yarp::os::Searchable ¶ms) override
Open the DeviceDriver.
bool view(T *&x)
Get an interface to the device driver.
A generic battery interface.
virtual bool getBatteryCurrent(double ¤t)=0
Get the instantaneous current measurement.
virtual bool getBatteryVoltage(double &voltage)=0
Get the instantaneous voltage measurement.
virtual bool getBatteryInfo(std::string &battery_info)=0
get the battery hardware characteristics (e.g.
virtual bool getBatteryStatus(Battery_status &status)=0
get the battery status
virtual bool getBatteryCharge(double &charge)=0
get the battery status of charge
virtual bool getBatteryTemperature(double &temperature)=0
get the battery temperature
void push(PolyDriver *p, const char *k)
A container for a device driver.
bool close() override
Close the DeviceDriver.
bool isValid() const
Check if device is valid.
bool open(const std::string &txt)
Construct and configure a device by its common name.
A simple collection of objects that can be described and transmitted in a portable way.
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
void addFloat64(yarp::conf::float64_t x)
Places a 64-bit floating point number in the bottle, at the end of the list.
void addVocab(int x)
Places a vocabulary item in the bottle, at the end of the list.
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
void clear()
Empties the bottle of any objects it contains.
bool write(ConnectionWriter &writer) const override
Output a representation of the bottle to a network connection.
void addInt32(std::int32_t x)
Places a 32-bit integer in the bottle, at the end of the list.
void addString(const char *str)
Places a string in the bottle, at the end of the list.
void close() override
Stop port activity.
bool setEnvelope(PortWriter &envelope) override
Set an envelope (e.g., a timestamp) to the next message which will be sent.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
void interrupt() override
Interrupt any current reads or writes attached to the port.
void write(bool forceStrict=false)
Write the current object being returned by BufferedPort::prepare.
T & prepare()
Access the object which will be transmitted by the next call to yarp::os::BufferedPort::write.
An interface for reading from a network connection.
virtual ConnectionWriter * getWriter()=0
Gets a way to reply to the message, if possible.
An interface for writing to a network connection.
static bool connect(const std::string &src, const std::string &dest, const std::string &carrier="", bool quiet=true)
Request that an output port connect to an input port.
An abstraction for a periodic thread.
A mini-server for network communication.
bool write(const PortWriter &writer, const PortWriter *callback=nullptr) const override
Write an object to the port.
void setReader(PortReader &reader) override
Set an external reader for port data.
void interrupt() override
Interrupt any current reads or writes attached to the port.
void close() override
Stop port activity.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
A class for storing options and configuration information.
void fromString(const std::string &txt, bool wipe=true)
Interprets a string as a list of properties.
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
void unput(const std::string &key)
Remove the association from the given key to a value, if present.
A base class for nested structures that can be searched.
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
void update()
Set the timestamp to the current time, and increment the sequence number (wrapping to 0 if the sequen...
A single value (typically within a Bottle).
virtual std::int32_t asVocab() const
Get vocabulary identifier as an integer.
virtual yarp::conf::float32_t asFloat32() const
Get 32-bit floating point value.
virtual std::string asString() const
Get string value.
#define yCInfo(component,...)
#define yCError(component,...)
#define yCTrace(component,...)
#define yCWarning(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
An interface for the device drivers.
An interface to the operating system, including Port based communication.