|
YARP
Yet Another Robot Platform
|
|
Go to the documentation of this file.
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)
257 double battery_charge = 0;
258 double battery_voltage = 0;
259 double battery_current = 0;
260 double battery_temperature = 0;
272 m_lastStateStamp.
update();
281 m_streamingPort.
write();
284 if (battery_current>0.4) check_battery_status(battery_charge);
290 struct tm * timeinfo;
292 timeinfo = localtime(&rawtime);
293 char* battery_timestamp = asctime(timeinfo);
294 std::snprintf(m_log_buffer, 1024,
"battery status: %+6.1fA % 6.1fV charge:% 6.1f%% time: %s", battery_current, battery_voltage, battery_charge, battery_timestamp);
295 fprintf(m_logFile,
"%s", m_log_buffer);
300 yCError(BATTERYWRAPPER,
"BatteryWrapper: %s: Sensor returned error", m_sensorId.c_str());
307 yCTrace(BATTERYWRAPPER,
"BatteryWrapper::Close");
308 if (PeriodicThread::isRunning())
310 PeriodicThread::stop();
317 m_streamingPort.
close();
327 PeriodicThread::stop();
332 void BatteryWrapper::notify_message(
string msg)
335 yCWarning(BATTERYWRAPPER,
"%s", msg.c_str());
337 yCWarning(BATTERYWRAPPER,
"%s", msg.c_str());
338 string cmd =
"echo " + msg +
" | wall";
340 retval = system(cmd.c_str());
341 yCDebug(BATTERYWRAPPER) <<
"system executed command" << cmd.c_str() <<
" with return value:" << retval;
345 void BatteryWrapper::emergency_shutdown(
string msg)
349 cmd =
"shutdown /s /t 120 /c " + msg;
350 yCWarning(BATTERYWRAPPER,
"%s", msg.c_str());
355 yCWarning(BATTERYWRAPPER,
"%s", msg.c_str());
356 cmd =
"echo " + msg +
" | wall";
357 retval = system(cmd.c_str());
358 yCDebug(BATTERYWRAPPER) <<
"system executed command" << cmd.c_str() <<
" with return value:" << retval;
360 cmd =
"sudo shutdown -h 2 " + msg;
361 retval = system(cmd.c_str());
362 yCDebug(BATTERYWRAPPER) <<
"system executed command" << cmd.c_str() <<
" with return value:" << retval;
364 #ifdef ICUB_SSH_SHUTDOWN
365 cmd =
"ssh icub@pc104 sudo shutdown -h 2";
366 retval = system(cmd.c_str());
367 yCDebug(BATTERYWRAPPER) <<
"system executed command" << cmd.c_str() <<
" with return value:" << retval;
372 void BatteryWrapper::check_battery_status(
double battery_charge)
374 static bool notify_15 =
true;
375 static bool notify_12 =
true;
376 static bool notify_10 =
true;
377 static bool notify_0 =
true;
379 if (battery_charge > 20)
387 if (battery_charge < 5)
391 if (m_enable_shutdown)
393 emergency_shutdown(
"CRITICAL WARNING: battery charge below critical level 5%. The robot will be stopped and the system will shutdown in 2mins.");
394 if (m_quitPortName !=
"") { stop_robot(m_quitPortName); }
399 notify_message(
"CRITICAL WARNING: battery charge reached critical level 5%, but the emergency shutodown is currently disabled!");
404 else if (battery_charge < 10)
406 if (notify_10) { notify_message(
"WARNING: battery charge below 10%"); notify_10 =
false; }
408 else if (battery_charge < 12)
410 if (notify_12) { notify_message(
"WARNING: battery charge below 12%"); notify_12 =
false; }
412 else if (battery_charge < 15)
414 if (notify_15) { notify_message(
"WARNING: battery charge below 15%"); notify_15 =
false; }
418 void BatteryWrapper::stop_robot(
string quit_port)
425 port_shutdown.
open((m_streamingPortName +
"/shutdown:o").c_str());
429 port_shutdown.
write(bot);
431 port_shutdown.
close();
void close() override
Stop port activity.
A simple collection of objects that can be described and transmitted in a portable way.
virtual std::int32_t asVocab() const
Get vocabulary identifier as an integer.
bool threadInit() override
Initialization method.
virtual bool getBatteryInfo(std::string &battery_info)=0
get the battery hardware characteristics (e.g.
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
void threadRelease() override
Release method.
void clear()
Empties the bottle of any objects it contains.
A base class for nested structures that can be searched.
virtual bool getBatteryStatus(Battery_status &status)=0
get the battery status
#define yCWarning(component,...)
virtual bool getBatteryTemperature(double &temperature)=0
get the battery temperature
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
constexpr yarp::conf::vocab32_t VOCAB_BATTERY_INFO
void fromString(const std::string &txt, bool wipe=true)
Interprets a string as a list of properties.
bool isValid() const
Check if device is valid.
#define YARP_LOG_COMPONENT(name,...)
void interrupt() override
Interrupt any current reads or writes attached to the port.
bool setEnvelope(PortWriter &envelope) override
Set an envelope (e.g., a timestamp) to the next message which will be sent.
virtual yarp::conf::float32_t asFloat32() const
Get 32-bit floating point value.
bool view(T *&x)
Get an interface to the device driver.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
define control board standard interfaces
void addFloat64(yarp::conf::float64_t x)
Places a 64-bit floating point number in the bottle, at the end of the list.
bool open(const std::string &txt)
Construct and configure a device by its common name.
An interface for the device drivers.
bool detachAll() override
Detach the object (you must have first called attach).
void push(PolyDriver *p, const char *k)
T & prepare()
Access the object which will be transmitted by the next call to yarp::os::BufferedPort::write.
A mini-server for network communication.
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
bool write(ConnectionWriter &writer) const override
Output a representation of the bottle to a network connection.
An interface for writing to a network connection.
virtual std::string asString() const
Get string value.
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.
A container for a device driver.
constexpr yarp::conf::vocab32_t VOCAB_FAILED
bool close() override
Close the DeviceDriver.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
constexpr yarp::conf::vocab32_t VOCAB_IBATTERY
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
void addInt32(std::int32_t x)
Places a 32-bit integer in the bottle, at the end of the list.
virtual ConnectionWriter * getWriter()=0
Gets a way to reply to the message, if possible.
virtual bool getBatteryCurrent(double ¤t)=0
Get the instantaneous current measurement.
void addString(const char *str)
Places a string in the bottle, at the end of the list.
void setReader(PortReader &reader) override
Set an external reader for port data.
An abstraction for a periodic thread.
void addVocab(int x)
Places a vocabulary item in the bottle, at the end of the list.
An interface for reading from a network connection.
#define yCError(component,...)
virtual bool getBatteryVoltage(double &voltage)=0
Get the instantaneous voltage measurement.
bool write(const PortWriter &writer, const PortWriter *callback=nullptr) const override
Write an object to the port.
void update()
Set the timestamp to the current time, and increment the sequence number (wrapping to 0 if the sequen...
void write(bool forceStrict=false)
Write the current object being returned by BufferedPort::prepare.
A generic battery interface.
#define yCInfo(component,...)
An interface to the operating system, including Port based communication.
#define yCDebug(component,...)
#define DEFAULT_THREAD_PERIOD
void close() override
Stop port activity.
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
void interrupt() override
Interrupt any current reads or writes attached to the port.
void run() override
Loop function.
#define yCTrace(component,...)
constexpr yarp::conf::vocab32_t VOCAB_IS
bool attachAll(const yarp::dev::PolyDriverList &p) override
Attach to a list of objects.
A single value (typically within a Bottle).
bool close() override
Close the DeviceDriver.
void unput(const std::string &key)
Remove the association from the given key to a value, if present.
bool open(yarp::os::Searchable ¶ms) override
Open the DeviceDriver.
A class for storing options and configuration information.
virtual bool getBatteryCharge(double &charge)=0
get the battery status of charge