25 memset(m_log_buffer, 0, 1024);
31 m_ibattery_p =
nullptr;
34bool Battery_nws_yarp::attach(
PolyDriver* driver)
42 driver->
view(m_ibattery_p);
43 if (
nullptr == m_ibattery_p)
48 m_msgsImpl = std::make_unique<IBatteryMsgsImpl>(m_ibattery_p);
55bool Battery_nws_yarp::detach()
61 m_ibattery_p =
nullptr;
67 if (!connection.
isValid()) {
return false;}
68 if (!m_msgsImpl) {
return false;}
70 bool b = m_msgsImpl->read(connection);
91 m_streamingPortName =
m_name +
"/data:o";
92 m_rpcPortName =
m_name +
"/rpc:i";
94 if(!initialize_YARP(config))
103 m_logFile =
fopen(
"batteryLog.txt",
"w");
111 if (!m_streamingPort.
open(m_streamingPortName.c_str()))
116 if (!m_rpcPort.
open(m_rpcPortName.c_str()))
131 if (m_ibattery_p!=
nullptr)
141 m_battery_charge =
tmp;
148 m_battery_voltage =
tmp;
155 m_battery_current =
tmp;
162 m_battery_temperature =
tmp;
169 m_battery_status =
tmp;
175 m_lastStateStamp.
update();
179 b.
charge = m_battery_charge;
181 b.
status = m_battery_status;
183 m_streamingPort.
write();
186 if (m_battery_status > 0.4) {
187 check_battery_status(m_battery_charge);
198 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);
199 fprintf(m_logFile,
"%s", m_log_buffer);
217 m_streamingPort.
close();
231void Battery_nws_yarp::notify_message(std::string msg)
237 std::string cmd =
"echo " + msg +
" | wall";
239 retval =
system(cmd.c_str());
240 yCDebug(
BATTERYWRAPPER) <<
"system executed command" << cmd.c_str() <<
" with return value:" << retval;
244void Battery_nws_yarp::emergency_shutdown(std::string msg)
248 cmd =
"shutdown /s /t 120 /c " + msg;
255 cmd =
"echo " + msg +
" | wall";
256 retval =
system(cmd.c_str());
257 yCDebug(
BATTERYWRAPPER) <<
"system executed command" << cmd.c_str() <<
" with return value:" << retval;
259 cmd =
"sudo shutdown -h 2 " + msg;
260 retval =
system(cmd.c_str());
261 yCDebug(
BATTERYWRAPPER) <<
"system executed command" << cmd.c_str() <<
" with return value:" << retval;
263#ifdef ICUB_SSH_SHUTDOWN
264 cmd =
"ssh icub@pc104 sudo shutdown -h 2";
265 retval =
system(cmd.c_str());
266 yCDebug(
BATTERYWRAPPER) <<
"system executed command" << cmd.c_str() <<
" with return value:" << retval;
271void Battery_nws_yarp::check_battery_status(
double battery_charge)
278 if (battery_charge > 20)
286 if (battery_charge < 5)
292 emergency_shutdown(
"CRITICAL WARNING: battery charge below critical level 5%. The robot will be stopped and the system will shutdown in 2mins.");
298 notify_message(
"CRITICAL WARNING: battery charge reached critical level 5%, but the emergency shutodown is currently disabled!");
303 else if (battery_charge < 10)
305 if (
notify_10) { notify_message(
"WARNING: battery charge below 10%");
notify_10 =
false; }
307 else if (battery_charge < 12)
309 if (
notify_12) { notify_message(
"WARNING: battery charge below 12%");
notify_12 =
false; }
311 else if (battery_charge < 15)
313 if (
notify_15) { notify_message(
"WARNING: battery charge below 15%");
notify_15 =
false; }
317void Battery_nws_yarp::stop_robot(std::string
quit_port)
335 std::lock_guard <std::mutex>
lg(m_mutex);
#define DEFAULT_THREAD_PERIOD
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
std::string m_quitPortName
bool open(yarp::os::Searchable ¶ms) override
Open the DeviceDriver.
void threadRelease() override
Release method.
bool threadInit() override
Initialization method.
void run() override
Loop function.
bool close() override
Close the DeviceDriver.
IBatteryMsgsImpl(yarp::dev::IBattery *iBattery)
return_get_BatteryInfo getBatteryInfoRPC() override
yarp::dev::ReturnValue result
double charge
Battery charge percentage [%].
double voltage
Battery Voltage [V].
double current
Battery Current [A].
std::int32_t status
the status of the device.
double temperature
Battery Temperature [degC].
bool view(T *&x)
Get an interface to the device driver.
A generic battery interface.
virtual yarp::dev::ReturnValue getBatteryTemperature(double &temperature)=0
get the battery temperature
virtual yarp::dev::ReturnValue getBatteryInfo(std::string &battery_info)=0
get the battery hardware characteristics (e.g.
virtual yarp::dev::ReturnValue getBatteryCharge(double &charge)=0
get the battery status of charge
virtual yarp::dev::ReturnValue getBatteryStatus(Battery_status &status)=0
get the battery status
virtual yarp::dev::ReturnValue getBatteryCurrent(double ¤t)=0
Get the instantaneous current measurement.
virtual yarp::dev::ReturnValue getBatteryVoltage(double &voltage)=0
Get the instantaneous voltage measurement.
A container for a device driver.
bool detachAll() final
Detach the object (you must have first called attach).
A simple collection of objects that can be described and transmitted in a portable way.
void addString(const char *str)
Places a string in the bottle, at the end of the list.
A mini-server for performing network communication in the background.
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 bool isValid() const =0
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.
bool setPeriod(double period)
Set the (new) period of the thread.
bool isRunning() const
Returns true when the thread is started, false otherwise.
bool start()
Call this to start the thread.
void stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
A mini-server for network communication.
void setReader(PortReader &reader) override
Set an external reader for port data.
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 base class for nested structures that can be searched.
void update()
Set the timestamp to the current time, and increment the sequence number (wrapping to 0 if the sequen...
#define yCInfo(component,...)
#define yCError(component,...)
#define yCTrace(component,...)
#define yCWarning(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
For streams capable of holding different kinds of content, check what they actually have.
An interface to the operating system, including Port based communication.