YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
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>
10#include <yarp/os/LogStream.h>
11#include <yarp/os/Time.h>
12
13#include <iostream>
14#include <cstring>
15
16using namespace yarp::os;
17using namespace yarp::dev;
18
19namespace {
20YARP_LOG_COMPONENT(FAKEBATTERY, "yarp.device.fakeBattery")
21constexpr double default_period = 0.02;
22constexpr double default_charge = 50.0;
23constexpr double default_voltage = 30.0;
24constexpr double default_current = 3.0;
25constexpr double default_temperature = 20.0;
27}
28
33
34
36{
37 if (!this->parseParams(config)) {return false;}
38
40
41 std::lock_guard<std::mutex> lock(m_mutex);
46 battery_info = std::move(m_info);
47 updateStatus();
48
50 yCError(FAKEBATTERY, "Could not open rpc port");
51 close();
52 return false;
53 }
55
57
58 return true;
59}
60
62{
63 // Stop the thread
65
66 // Close the RPC port
68
69 return true;
70}
71
73{
74 std::lock_guard<std::mutex> lock(m_mutex);
75 if (battery_current > 0.1) {
76 battery_charge -= 0.001;
77 } else if (battery_current < -0.1) {
78 battery_charge += 0.001;
79 }
80 updateStatus();
81}
82
84{
85 std::lock_guard<std::mutex> lock(m_mutex);
86 voltage = battery_voltage;
87 return ReturnValue_ok;
88}
89
91{
92 std::lock_guard<std::mutex> lock(m_mutex);
93 current = battery_current;
94 return ReturnValue_ok;
95}
96
98{
99 std::lock_guard<std::mutex> lock(m_mutex);
100 charge = battery_charge;
101 return ReturnValue_ok;
102}
103
105{
106 std::lock_guard<std::mutex> lock(m_mutex);
107 status = battery_status;
108 return ReturnValue_ok;
109}
110
112{
113 std::lock_guard<std::mutex> lock(m_mutex);
114 temperature = battery_temperature;
115 return ReturnValue_ok;
116}
117
119{
120 std::lock_guard<std::mutex> lock(m_mutex);
122 return ReturnValue_ok;
123}
124
125void FakeBattery::setBatteryVoltage(const double voltage)
126{
127 std::lock_guard<std::mutex> lock(m_mutex);
128 battery_voltage = voltage;
130 updateStatus();
131}
132
133void FakeBattery::setBatteryCurrent(const double current)
134{
135 std::lock_guard<std::mutex> lock(m_mutex);
136 battery_current = current;
138 updateStatus();
139}
140
141void FakeBattery::setBatteryCharge(const double charge)
142{
143 std::lock_guard<std::mutex> lock(m_mutex);
144 battery_charge = charge;
146 updateStatus();
147}
148
150{
151 std::lock_guard<std::mutex> lock(m_mutex);
152 battery_status = status;
154}
155
156void FakeBattery::setBatteryInfo(const std::string& info)
157{
158 std::lock_guard<std::mutex> lock(m_mutex);
160}
161
162void FakeBattery::setBatteryTemperature(const double temperature)
163{
164 std::lock_guard<std::mutex> lock(m_mutex);
165 battery_temperature = temperature;
166}
167
169{
170 std::lock_guard<std::mutex> lock(m_mutex);
171 return battery_voltage;
172}
173
175{
176 std::lock_guard<std::mutex> lock(m_mutex);
177 return battery_current;
178}
179
181{
182 std::lock_guard<std::mutex> lock(m_mutex);
183 return battery_charge;
184}
185
187{
188 std::lock_guard<std::mutex> lock(m_mutex);
189 yCInfo(FAKEBATTERY, "getBatteryStatus() -> %d", static_cast<int>(battery_status));
190 return battery_status;
191}
192
194{
195 std::lock_guard<std::mutex> lock(m_mutex);
196 switch (battery_status) {
198 return "0: BATTERY_OK_STANDBY";
200 return "1: BATTERY_OK_IN_CHARGE";
202 return "2: BATTERY_OK_IN_USE";
204 return "3: BATTERY_GENERAL_ERROR";
205 case BATTERY_TIMEOUT:
206 return "4: BATTERY_TIMEOUT";
208 return "5: BATTERY_LOW_WARNING";
210 return "6: BATTERY_CRITICAL_WARNING";
211 default:
212 return "Invalid battery status";
213 };
214}
215
217{
218 std::lock_guard<std::mutex> lock(m_mutex);
219 return battery_info;
220}
221
223{
224 std::lock_guard<std::mutex> lock(m_mutex);
225 return battery_temperature;
226}
227
228
229void FakeBattery::updateStatus()
230{
232 return;
233 }
235 if (battery_current > 0.1) {
236 if (battery_charge > 15.0) {
238 } else if (battery_charge > 5.0) {
240 } else {
242 }
243 } else if (battery_current > -0.1) {
245 } else {
247 }
248}
constexpr double default_period
#define ReturnValue_ok
Definition ReturnValue.h:77
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
fakeBattery: Documentation to be added
Definition FakeBattery.h:33
void setBatteryTemperature(const double temperature) override
double getBatteryCharge() override
std::string getBatteryInfo() override
std::string battery_info
Definition FakeBattery.h:40
double battery_temperature
Definition FakeBattery.h:39
void setBatteryStatus(const yarp::dev::IBattery::Battery_status status) override
void setBatteryInfo(const std::string &info) override
Battery_status getBatteryStatus() override
yarp::os::RpcServer ctrl_port
Definition FakeBattery.h:46
void setBatteryCurrent(const double current) override
void run() override
Loop function.
Battery_status battery_status
Definition FakeBattery.h:41
bool suspend_battery_status_update
Definition FakeBattery.h:42
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
void setBatteryVoltage(const double voltage) override
bool close() override
Close the DeviceDriver.
double battery_charge
Definition FakeBattery.h:36
double battery_voltage
Definition FakeBattery.h:37
void setBatteryCharge(const double charge) override
double getBatteryVoltage() override
std::mutex m_mutex
Definition FakeBattery.h:35
double battery_current
Definition FakeBattery.h:38
double getBatteryCurrent() override
std::string getBatteryStatusString() override
double getBatteryTemperature() override
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
void close() override
Stop port activity.
A mini-server for performing network communication in the background.
An abstraction for a periodic thread.
bool setPeriod(double period)
Set the (new) period of the 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:31
yarp::os::WireLink & yarp()
Get YARP state associated with this object.
Definition Wire.h:28
#define yCInfo(component,...)
#define yCError(component,...)
#define YARP_LOG_COMPONENT(name,...)
constexpr const T & clamp(const T &v, const T &lo, const T &hi)
Definition numeric.h:91
For streams capable of holding different kinds of content, check what they actually have.
An interface to the operating system, including Port based communication.