YARP
Yet Another Robot Platform
BatteryWrapper.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: LGPL-2.1-or-later
4  */
5 
6 #ifndef YARP_DEV_BATTERYWRAPPER_H
7 #define YARP_DEV_BATTERYWRAPPER_H
8 
9 #include <vector>
10 #include <iostream>
11 #include <string>
12 #include <sstream>
13 #include <cmath>
14 
15 #include <yarp/os/Network.h>
16 #include <yarp/os/Port.h>
17 #include <yarp/os/BufferedPort.h>
18 #include <yarp/os/Bottle.h>
19 #include <yarp/os/Time.h>
20 #include <yarp/os/Property.h>
21 
22 #include <yarp/os/PeriodicThread.h>
23 #include <yarp/os/BufferedPort.h>
24 #include <yarp/os/Stamp.h>
25 
26 #include <yarp/sig/Vector.h>
27 
28 #include <yarp/dev/IBattery.h>
29 #include <yarp/dev/PolyDriver.h>
30 #include <yarp/dev/DeviceDriver.h>
32 #include <yarp/dev/api.h>
33 
34 #define DEFAULT_THREAD_PERIOD 0.02 //s
35 
93 {
94 public:
97 
98  bool open(yarp::os::Searchable &params) override;
99  bool close() override;
100 
101  bool attachAll(const yarp::dev::PolyDriverList &p) override;
102  bool detachAll() override;
103 
104  bool threadInit() override;
105  void threadRelease() override;
106  void run() override;
107 
108 private:
109 
110  //driver stuff
111  yarp::dev::PolyDriver m_driver;
112  yarp::dev::IBattery *m_ibattery_p; // the battery read from
113 
114  //ports stuff
115  std::string m_streamingPortName;
116  std::string m_rpcPortName;
117  std::string m_quitPortName;
118  yarp::os::Port m_rpcPort;
120 
121  //data
122  double m_battery_charge = std::nan("");
123  double m_battery_voltage = std::nan("");
124  double m_battery_current = std::nan("");
125  double m_battery_temperature = std::nan("");
127 
128  yarp::os::Stamp m_lastStateStamp; // the last reading time stamp
129  double m_period;
130  bool m_ownDevices;
131  std::string m_sensorId;
132 
133  //behavior controls
134  bool m_enable_shutdown;
135  bool m_enable_log;
136 
137  //log stuff
138  char m_log_buffer[1024];
139  FILE *m_logFile;
140 
141  //public methods
142  bool read(yarp::os::ConnectionReader& connection) override;
143 
144 private:
145  //private methods
146  void attach(yarp::dev::IBattery *s);
147  void detach();
148 
149  //internal methods to handle particular statuses of the battery
150  bool initialize_YARP(yarp::os::Searchable &config);
151  void notify_message(std::string msg);
152  void emergency_shutdown(std::string msg);
153  void check_battery_status(double battery_charge);
154  void stop_robot(std::string quit_port);
155 };
156 
157 #endif // YARP_DEV_BATTERYWRAPPER_H
const int BATTERY_TIMEOUT
Definition: BatteryClient.h:24
contains the definition of a Vector type
batteryWrapper: Device that expose a battery sensor (using the IBattery interface) on the YARP networ...
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 &params) override
Open the DeviceDriver.
Interface implemented by all device drivers.
Definition: DeviceDriver.h:35
A generic battery interface.
Definition: IBattery.h:30
Interface for an object that can wrap/attach to to another.
A container for a device driver.
Definition: PolyDriver.h:24
An interface for reading from a network connection.
An abstraction for a periodic thread.
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:25
A mini-server for network communication.
Definition: Port.h:47
A base class for nested structures that can be searched.
Definition: Searchable.h:66
An abstraction for a time stamp and/or sequence number.
Definition: Stamp.h:22