YARP
Yet Another Robot Platform
BatteryWrapper.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #ifndef YARP_DEV_BATTERYWRAPPER_H
20 #define YARP_DEV_BATTERYWRAPPER_H
21 
22 #include <vector>
23 #include <iostream>
24 #include <string>
25 #include <sstream>
26 #include <cmath>
27 
28 #include <yarp/os/Network.h>
29 #include <yarp/os/Port.h>
30 #include <yarp/os/BufferedPort.h>
31 #include <yarp/os/Bottle.h>
32 #include <yarp/os/Time.h>
33 #include <yarp/os/Property.h>
34 
35 #include <yarp/os/PeriodicThread.h>
36 #include <yarp/os/BufferedPort.h>
37 #include <yarp/os/Stamp.h>
38 
39 #include <yarp/sig/Vector.h>
40 
41 #include <yarp/dev/IBattery.h>
42 #include <yarp/dev/PolyDriver.h>
43 #include <yarp/dev/DeviceDriver.h>
45 #include <yarp/dev/api.h>
46 
101 #define DEFAULT_THREAD_PERIOD 0.02 //s
102 
107  public yarp::os::PortReader
108 {
109 public:
110  BatteryWrapper();
111  ~BatteryWrapper();
112 
113  bool open(yarp::os::Searchable &params) override;
114  bool close() override;
115 
116  bool attachAll(const yarp::dev::PolyDriverList &p) override;
117  bool detachAll() override;
118 
119  bool threadInit() override;
120  void threadRelease() override;
121  void run() override;
122 
123 private:
124 
125  //driver stuff
126  yarp::dev::PolyDriver m_driver;
127  yarp::dev::IBattery *m_ibattery_p; // the battery read from
128 
129  //ports stuff
130  std::string m_streamingPortName;
131  std::string m_rpcPortName;
132  std::string m_quitPortName;
133  yarp::os::Port m_rpcPort;
135 
136  //data
137  double m_battery_charge = std::nan("");
138  double m_battery_voltage = std::nan("");
139  double m_battery_current = std::nan("");
140  double m_battery_temperature = std::nan("");
142 
143  yarp::os::Stamp m_lastStateStamp; // the last reading time stamp
144  double m_period;
145  bool m_ownDevices;
146  std::string m_sensorId;
147 
148  //behavior controls
149  bool m_enable_shutdown;
150  bool m_enable_log;
151 
152  //log stuff
153  char m_log_buffer[1024];
154  FILE *m_logFile;
155 
156  //public methods
157  bool read(yarp::os::ConnectionReader& connection) override;
158 
159 private:
160  //private methods
161  void attach(yarp::dev::IBattery *s);
162  void detach();
163 
164  //internal methods to handle particular statuses of the battery
165  bool initialize_YARP(yarp::os::Searchable &config);
166  void notify_message(std::string msg);
167  void emergency_shutdown(std::string msg);
168  void check_battery_status(double battery_charge);
169  void stop_robot(std::string quit_port);
170 };
171 
172 #endif // YARP_DEV_BATTERYWRAPPER_H
const int BATTERY_TIMEOUT
Definition: BatteryClient.h:37
contains the definition of a Vector type
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:38
A generic battery interface.
Definition: IBattery.h:33
Interface for an object that can wrap/attach to to another.
A container for a device driver.
Definition: PolyDriver.h:27
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:28
A mini-server for network communication.
Definition: Port.h:50
A base class for nested structures that can be searched.
Definition: Searchable.h:69
An abstraction for a time stamp and/or sequence number.
Definition: Stamp.h:25