YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Battery_nws_ros2.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2023 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "Battery_nws_ros2.h"
8#include <yarp/os/LogStream.h>
9#include <yarp/os/Stamp.h>
10#include <Ros2Utils.h>
11
12YARP_LOG_COMPONENT(BATTERY_NWS_ROS2, "yarp.devices.Battery_nws_ros2")
13
17
19{
20 m_battery_interface = nullptr;
21}
22
23
25{
26 if (driver->isValid())
27 {
28 driver->view(m_battery_interface);
29 } else {
30 yCError(BATTERY_NWS_ROS2) << "not valid driver";
31 }
32
33 if (m_battery_interface == nullptr)
34 {
35 yCError(BATTERY_NWS_ROS2, "Subdevice passed to attach method is invalid");
36 return false;
37 }
38 PeriodicThread::setPeriod(m_period);
39 return PeriodicThread::start();
40}
41
42
44{
45 if (PeriodicThread::isRunning())
46 {
47 PeriodicThread::stop();
48 }
49 m_battery_interface = nullptr;
50 return true;
51}
52
54{
55 return true;
56}
57
59{
60 parseParams(config);
61 rclcpp::NodeOptions node_options;
62 node_options.allow_undeclared_parameters(true);
63 node_options.automatically_declare_parameters_from_overrides(true);
64 m_node = NodeCreator::createNode(m_node_name, node_options);
65 if (m_node == nullptr) {
66 yCError(BATTERY_NWS_ROS2) << " opening " << m_node_name << " Node, check your yarp-ROS2 network configuration\n";
67 return false;
68 }
69
70 m_ros2Publisher = m_node->create_publisher<sensor_msgs::msg::BatteryState>(m_topic_name, 10);
71 return true;
72}
73
77
79{
80 if (m_battery_interface==nullptr)
81 {
82 yCError(BATTERY_NWS_ROS2) << "the interface is not valid";
83 }
84 else if (!m_ros2Publisher)
85 {
86 yCError(BATTERY_NWS_ROS2) << "the publisher is not ready";
87 }
88 else
89 {
90 double voltage=0;
91 double current=0;
92 double charge=0;
93 double temperature=0;
95 std::string battery_info;
96 m_battery_interface->getBatteryVoltage(voltage);
97 m_battery_interface->getBatteryCurrent(current);
98 m_battery_interface->getBatteryCharge(charge);
99 m_battery_interface->getBatteryStatus(status);
100 m_battery_interface->getBatteryTemperature(temperature);
101 m_battery_interface->getBatteryInfo(battery_info);
102
103 m_timeStamp.update(yarp::os::Time::now());
104
105 battMsg.voltage = voltage;
106 battMsg.current = current;
107 battMsg.temperature = temperature;
108 battMsg.charge = std::numeric_limits<double>::quiet_NaN();//std::nan("");
109 // battMsg.capacity = std::nan("");
110 // battMsg.design_capacity = std::nan("");
111 battMsg.percentage = charge;
112 battMsg.power_supply_status = 0;
113 battMsg.power_supply_health = 0;
114 battMsg.power_supply_technology = 0;
115 battMsg.present=true;
116 battMsg.location="";
117 battMsg.serial_number="";
118
119 battMsg.header.frame_id = "" ;
120 battMsg.header.stamp.sec = int(m_timeStamp.getTime());
121 battMsg.header.stamp.nanosec = int(1000000000UL * (m_timeStamp.getTime() - int(m_timeStamp.getTime())));
122
123 m_ros2Publisher->publish(battMsg);
124 }
125}
126
128{
130 if (PeriodicThread::isRunning())
131 {
132 PeriodicThread::stop();
133 }
134
135 detach();
136 return true;
137}
const yarp::os::LogComponent & BATTERY_NWS_ROS2()
#define DEFAULT_THREAD_PERIOD
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
Battery_nws_ros2: A ros2 nws to get the status of a battery and publish it on a ros2 topic....
bool close() override
Close the DeviceDriver.
void run() override
Loop function.
bool detach() override
Detach the object (you must have first called attach).
bool attach(yarp::dev::PolyDriver *driver) override
Attach to another object.
void threadRelease() override
Release method.
bool open(yarp::os::Searchable &params) override
Open the DeviceDriver.
bool threadInit() override
Initialization method.
static rclcpp::Node::SharedPtr createNode(std::string name)
Definition Ros2Utils.cpp:9
bool view(T *&x)
Get an interface to the device driver.
virtual bool getBatteryCurrent(double &current)=0
Get the instantaneous current measurement.
virtual bool getBatteryVoltage(double &voltage)=0
Get the instantaneous voltage measurement.
virtual bool getBatteryInfo(std::string &battery_info)=0
get the battery hardware characteristics (e.g.
virtual bool getBatteryStatus(Battery_status &status)=0
get the battery status
virtual bool getBatteryCharge(double &charge)=0
get the battery status of charge
virtual bool getBatteryTemperature(double &temperature)=0
get the battery temperature
A container for a device driver.
Definition PolyDriver.h:23
bool isValid() const
Check if device is valid.
A base class for nested structures that can be searched.
Definition Searchable.h:31
void update()
Set the timestamp to the current time, and increment the sequence number (wrapping to 0 if the sequen...
Definition Stamp.cpp:124
double getTime() const
Get the time stamp.
Definition Stamp.cpp:34
#define yCError(component,...)
#define yCTrace(component,...)
#define YARP_LOG_COMPONENT(name,...)
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition Time.cpp:121
The main, catch-all namespace for YARP.
Definition dirs.h:16