YARP
Yet Another Robot Platform
fakeBattery.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #include "fakeBattery.h"
10 
11 #include <yarp/os/Log.h>
12 #include <yarp/os/LogComponent.h>
13 #include <yarp/os/LogStream.h>
14 #include <yarp/os/Time.h>
15 
16 #include <iostream>
17 #include <cstring>
18 
19 using namespace std;
20 using namespace yarp::os;
21 using namespace yarp::dev;
22 
23 namespace {
24 YARP_LOG_COMPONENT(FAKEBATTERY, "yarp.device.fakeBattery")
25 constexpr double default_period = 0.02;
26 constexpr double default_charge = 50.0;
27 constexpr double default_voltage = 30.0;
28 constexpr double default_current = 3.0;
29 constexpr double default_temperature = 20.0;
30 constexpr const char* default_info = "Fake battery system v2.0";
31 }
32 
34  PeriodicThread(default_period)
35 {
36 }
37 
38 
40 {
41  double period = config.check("thread_period", Value(default_period), "Thread period (smaller implies faster charge/discharge)").asFloat64();
42  setPeriod(period);
43 
44  double charge = config.check("charge", Value(default_charge), "Initial charge (%)").asFloat64();
45  double voltage = config.check("voltage", Value(default_voltage), "Initial voltage (V)").asFloat64();
46  double current = config.check("current", Value(default_current), "Initial current (A)").asFloat64();
47  double temperature = config.check("temperature", Value(default_temperature), "Initial temperature (°C)").asFloat64();
48  std::string info = config.check("info", Value(default_info), "Initial battery information").asString();
49  {
50  std::lock_guard<std::mutex> lock(m_mutex);
51  battery_charge = charge;
52  battery_voltage = voltage;
53  battery_current = current;
54  battery_temperature = temperature;
55  battery_info = std::move(info);
56  updateStatus();
57  }
58 
59  std::string name = config.find("name").asString();
60  this->yarp().attachAsServer(ctrl_port);
61  if (!ctrl_port.open(name + "/control/rpc:i")) {
62  yCError(FAKEBATTERY, "Could not open rpc port");
63  close();
64  return false;
65  }
66 
67  PeriodicThread::start();
68 
69  return true;
70 }
71 
73 {
74  // Stop the thread
75  PeriodicThread::stop();
76 
77  // Close the RPC port
78  ctrl_port.close();
79 
80  return true;
81 }
82 
84 {
85  std::lock_guard<std::mutex> lock(m_mutex);
86  if (battery_current > 0.1) {
87  battery_charge -= 0.001;
88  } else if (battery_current < -0.1) {
89  battery_charge += 0.001;
90  }
91  updateStatus();
92 }
93 
94 bool FakeBattery::getBatteryVoltage(double& voltage)
95 {
96  std::lock_guard<std::mutex> lock(m_mutex);
97  voltage = battery_voltage;
98  return true;
99 }
100 
101 bool FakeBattery::getBatteryCurrent(double& current)
102 {
103  std::lock_guard<std::mutex> lock(m_mutex);
104  current = battery_current;
105  return true;
106 }
107 
108 bool FakeBattery::getBatteryCharge(double& charge)
109 {
110  std::lock_guard<std::mutex> lock(m_mutex);
111  charge = battery_charge;
112  return true;
113 }
114 
116 {
117  std::lock_guard<std::mutex> lock(m_mutex);
118  status = battery_status;
119  return true;
120 }
121 
122 bool FakeBattery::getBatteryTemperature(double& temperature)
123 {
124  std::lock_guard<std::mutex> lock(m_mutex);
125  temperature = battery_temperature;
126  return true;
127 }
128 
129 bool FakeBattery::getBatteryInfo(string& info)
130 {
131  std::lock_guard<std::mutex> lock(m_mutex);
132  info = battery_info;
133  return true;
134 }
135 
136 void FakeBattery::setBatteryVoltage(const double voltage)
137 {
138  std::lock_guard<std::mutex> lock(m_mutex);
139  battery_voltage = voltage;
140  updateStatus();
141 }
142 
143 void FakeBattery::setBatteryCurrent(const double current)
144 {
145  std::lock_guard<std::mutex> lock(m_mutex);
146  battery_current = current;
147  updateStatus();
148 }
149 
150 void FakeBattery::setBatteryCharge(const double charge)
151 {
152  std::lock_guard<std::mutex> lock(m_mutex);
153  battery_charge = charge;
154  updateStatus();
155 }
156 
157 void FakeBattery::setBatteryInfo(const std::string& info)
158 {
159  std::lock_guard<std::mutex> lock(m_mutex);
160  battery_info = info;
161 }
162 
163 void FakeBattery::setBatteryTemperature(const double temperature)
164 {
165  std::lock_guard<std::mutex> lock(m_mutex);
166  battery_temperature = temperature;
167 }
168 
170 {
171  std::lock_guard<std::mutex> lock(m_mutex);
172  return battery_voltage;
173 }
174 
176 {
177  std::lock_guard<std::mutex> lock(m_mutex);
178  return battery_current;
179 }
180 
182 {
183  std::lock_guard<std::mutex> lock(m_mutex);
184  return battery_charge;
185 }
186 
188 {
189  std::lock_guard<std::mutex> lock(m_mutex);
190  switch (battery_status) {
191  case BATTERY_OK_STANBY:
192  return "0: BATTERY_OK_STANBY";
193  case BATTERY_OK_IN_CHARGE:
194  return "1: BATTERY_OK_IN_CHARGE";
195  case BATTERY_OK_IN_USE:
196  return "2: BATTERY_OK_IN_USE";
197  case BATTERY_GENERAL_ERROR:
198  return "3: BATTERY_GENERAL_ERROR";
199  case BATTERY_TIMEOUT:
200  return "4: BATTERY_TIMEOUT";
201  case BATTERY_LOW_WARNING:
202  return "5: BATTERY_LOW_WARNING";
203  case BATTERY_CRITICAL_WARNING:
204  return "6: BATTERY_CRITICAL_WARNING";
205  default:
206  return "Invalid battery status";
207  };
208 }
209 
211 {
212  std::lock_guard<std::mutex> lock(m_mutex);
213  return battery_info;
214 }
215 
217 {
218  std::lock_guard<std::mutex> lock(m_mutex);
219  return battery_temperature;
220 }
221 
222 
223 void FakeBattery::updateStatus()
224 {
225  battery_charge = yarp::conf::clamp(battery_charge, 0.0, 100.0);
226  if (battery_current > 0.1) {
227  if (battery_charge > 15.0) {
228  battery_status = yarp::dev::IBattery::Battery_status::BATTERY_OK_IN_USE;
229  } else if (battery_charge > 5.0) {
230  battery_status = yarp::dev::IBattery::Battery_status::BATTERY_LOW_WARNING;
231  } else {
232  battery_status = yarp::dev::IBattery::Battery_status::BATTERY_CRITICAL_WARNING;
233  }
234  } else if (battery_current > -0.1) {
235  battery_status = yarp::dev::IBattery::Battery_status::BATTERY_OK_STANBY;
236  } else {
237  battery_status = yarp::dev::IBattery::Battery_status::BATTERY_OK_IN_CHARGE;
238  }
239 }
LogStream.h
yarp::dev::IBattery::Battery_status
Battery_status
Definition: IBattery.h:36
FakeBattery::close
bool close() override
Close the DeviceDriver.
Definition: fakeBattery.cpp:72
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:69
FakeBattery::open
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Definition: fakeBattery.cpp:39
YARP_LOG_COMPONENT
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:80
yarp::conf::clamp
constexpr const T & clamp(const T &v, const T &lo, const T &hi)
Definition: numeric.h:65
FakeBattery::run
void run() override
Loop function.
Definition: fakeBattery.cpp:83
FakeBattery::getBatteryVoltage
double getBatteryVoltage() override
Definition: fakeBattery.cpp:169
FakeBattery::getBatteryTemperature
double getBatteryTemperature() override
Definition: fakeBattery.cpp:216
yarp::dev
An interface for the device drivers.
Definition: audioBufferSizeData.cpp:17
FakeBattery::getBatteryCharge
double getBatteryCharge() override
Definition: fakeBattery.cpp:181
fakeBattery.h
FakeBattery::getBatteryInfo
std::string getBatteryInfo() override
Definition: fakeBattery.cpp:210
FakeBattery::setBatteryCharge
void setBatteryCharge(const double charge) override
Definition: fakeBattery.cpp:150
Log.h
yarp::os::Value::asString
virtual std::string asString() const
Get string value.
Definition: Value.cpp:237
yarp::os::Searchable::check
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
BATTERY_TIMEOUT
const int BATTERY_TIMEOUT
Definition: BatteryClient.h:37
yarp::os::Searchable::find
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
yarp::os::PeriodicThread
An abstraction for a periodic thread.
Definition: PeriodicThread.h:25
LogComponent.h
FakeBattery::setBatteryVoltage
void setBatteryVoltage(const double voltage) override
Definition: fakeBattery.cpp:136
FakeBattery::setBatteryCurrent
void setBatteryCurrent(const double current) override
Definition: fakeBattery.cpp:143
FakeBattery
fakeBattery: Documentation to be added
Definition: fakeBattery.h:32
yCError
#define yCError(component,...)
Definition: LogComponent.h:157
FakeBattery::getBatteryCurrent
double getBatteryCurrent() override
Definition: fakeBattery.cpp:175
FakeBattery::setBatteryInfo
void setBatteryInfo(const std::string &info) override
Definition: fakeBattery.cpp:157
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
FakeBattery::getBatteryStatus
std::string getBatteryStatus() override
Definition: fakeBattery.cpp:187
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
Time.h
yarp::os::Value
A single value (typically within a Bottle).
Definition: Value.h:47
FakeBattery::setBatteryTemperature
void setBatteryTemperature(const double temperature) override
Definition: fakeBattery.cpp:163