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