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";
41 double period = config.
check(
"thread_period",
Value(default_period),
"Thread period (smaller implies faster charge/discharge)").asFloat64();
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();
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);
60 this->
yarp().attachAsServer(ctrl_port);
61 if (!ctrl_port.open(name +
"/control/rpc:i")) {
62 yCError(FAKEBATTERY,
"Could not open rpc port");
67 PeriodicThread::start();
75 PeriodicThread::stop();
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;
96 std::lock_guard<std::mutex> lock(m_mutex);
97 voltage = battery_voltage;
103 std::lock_guard<std::mutex> lock(m_mutex);
104 current = battery_current;
110 std::lock_guard<std::mutex> lock(m_mutex);
111 charge = battery_charge;
117 std::lock_guard<std::mutex> lock(m_mutex);
118 status = battery_status;
124 std::lock_guard<std::mutex> lock(m_mutex);
125 temperature = battery_temperature;
131 std::lock_guard<std::mutex> lock(m_mutex);
138 std::lock_guard<std::mutex> lock(m_mutex);
139 battery_voltage = voltage;
145 std::lock_guard<std::mutex> lock(m_mutex);
146 battery_current = current;
152 std::lock_guard<std::mutex> lock(m_mutex);
153 battery_charge = charge;
159 std::lock_guard<std::mutex> lock(m_mutex);
165 std::lock_guard<std::mutex> lock(m_mutex);
166 battery_temperature = temperature;
171 std::lock_guard<std::mutex> lock(m_mutex);
172 return battery_voltage;
177 std::lock_guard<std::mutex> lock(m_mutex);
178 return battery_current;
183 std::lock_guard<std::mutex> lock(m_mutex);
184 return battery_charge;
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";
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";
206 return "Invalid battery status";
212 std::lock_guard<std::mutex> lock(m_mutex);
218 std::lock_guard<std::mutex> lock(m_mutex);
219 return battery_temperature;
223 void FakeBattery::updateStatus()
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;
232 battery_status = yarp::dev::IBattery::Battery_status::BATTERY_CRITICAL_WARNING;
234 }
else if (battery_current > -0.1) {
235 battery_status = yarp::dev::IBattery::Battery_status::BATTERY_OK_STANBY;
237 battery_status = yarp::dev::IBattery::Battery_status::BATTERY_OK_IN_CHARGE;