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 
27 #include <yarp/os/Network.h>
28 #include <yarp/os/Port.h>
29 #include <yarp/os/BufferedPort.h>
30 #include <yarp/os/Bottle.h>
31 #include <yarp/os/Time.h>
32 #include <yarp/os/Property.h>
33 
34 #include <yarp/os/PeriodicThread.h>
35 #include <yarp/os/BufferedPort.h>
36 #include <yarp/os/Stamp.h>
37 
38 #include <yarp/sig/Vector.h>
39 
40 #include <yarp/dev/IBattery.h>
41 #include <yarp/dev/PolyDriver.h>
42 #include <yarp/dev/DeviceDriver.h>
44 #include <yarp/dev/api.h>
45 
46 #define DEFAULT_THREAD_PERIOD 0.02 //s
47 
104  public yarp::os::PortReader
105 {
106 public:
107  BatteryWrapper();
108  ~BatteryWrapper();
109 
110  bool open(yarp::os::Searchable &params) override;
111  bool close() override;
112 
113  bool attachAll(const yarp::dev::PolyDriverList &p) override;
114  bool detachAll() override;
115 
116  bool threadInit() override;
117  void threadRelease() override;
118  void run() override;
119 
120 private:
121 
122  //driver stuff
123  yarp::dev::PolyDriver m_driver;
124  yarp::dev::IBattery *m_ibattery_p; // the battery read from
125 
126  //ports stuff
127  std::string m_streamingPortName;
128  std::string m_rpcPortName;
129  std::string m_quitPortName;
130  yarp::os::Port m_rpcPort;
132 
133  //data
134  yarp::os::Stamp m_lastStateStamp; // the last reading time stamp
135  double m_period;
136  bool m_ownDevices;
137  std::string m_sensorId;
138 
139  //behavior controls
140  bool m_enable_shutdown;
141  bool m_enable_log;
142 
143  //log stuff
144  char m_log_buffer[1024];
145  FILE *m_logFile;
146 
147  //public methods
148  bool read(yarp::os::ConnectionReader& connection) override;
149 
150 private:
151  //private methods
152  void attach(yarp::dev::IBattery *s);
153  void detach();
154 
155  //internal methods to handle particular statuses of the battery
156  bool initialize_YARP(yarp::os::Searchable &config);
157  void notify_message(std::string msg);
158  void emergency_shutdown(std::string msg);
159  void check_battery_status(double battery_charge);
160  void stop_robot(std::string quit_port);
161 };
162 
163 #endif // YARP_DEV_BATTERYWRAPPER_H
BatteryWrapper::threadInit
bool threadInit() override
Initialization method.
Definition: BatteryWrapper.cpp:150
BatteryWrapper::threadRelease
void threadRelease() override
Release method.
Definition: BatteryWrapper.cpp:248
Network.h
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:69
Vector.h
contains the definition of a Vector type
BatteryWrapper
batteryWrapper: Device that expose a battery sensor (using the IBattery interface) on the YARP networ...
Definition: BatteryWrapper.h:105
Port.h
yarp::dev::DeviceDriver
Interface implemented by all device drivers.
Definition: DeviceDriver.h:38
yarp::dev::PolyDriverList
Definition: PolyDriverList.h:22
BatteryWrapper::detachAll
bool detachAll() override
Detach the object (you must have first called attach).
Definition: BatteryWrapper.cpp:81
yarp::os::Port
A mini-server for network communication.
Definition: Port.h:50
yarp::os::BufferedPort< yarp::os::Bottle >
yarp::os::PortReader
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:28
Property.h
PolyDriver.h
Stamp.h
yarp::dev::PolyDriver
A container for a device driver.
Definition: PolyDriver.h:27
BatteryWrapper::BatteryWrapper
BatteryWrapper()
Definition: BatteryWrapper.cpp:37
BufferedPort.h
IMultipleWrapper.h
yarp::os::PeriodicThread
An abstraction for a periodic thread.
Definition: PeriodicThread.h:25
PeriodicThread.h
yarp::os::ConnectionReader
An interface for reading from a network connection.
Definition: ConnectionReader.h:40
yarp::os::Stamp
An abstraction for a time stamp and/or sequence number.
Definition: Stamp.h:25
yarp::dev::IBattery
A generic battery interface.
Definition: IBattery.h:33
BatteryWrapper::~BatteryWrapper
~BatteryWrapper()
Definition: BatteryWrapper.cpp:45
IBattery.h
yarp::dev::IMultipleWrapper
Interface for an object that can wrap/attach to to another.
Definition: IMultipleWrapper.h:30
Time.h
BatteryWrapper::run
void run() override
Loop function.
Definition: BatteryWrapper.cpp:252
BatteryWrapper::attachAll
bool attachAll(const yarp::dev::PolyDriverList &p) override
Attach to a list of objects.
Definition: BatteryWrapper.cpp:51
BatteryWrapper::close
bool close() override
Close the DeviceDriver.
Definition: BatteryWrapper.cpp:305
api.h
Bottle.h
BatteryWrapper::open
bool open(yarp::os::Searchable &params) override
Open the DeviceDriver.
Definition: BatteryWrapper.cpp:155
DeviceDriver.h