32 #include "joint_state_publisher.h"
36 #include "sdo_error.h"
39 #include "sensor_msgs/JointState.h"
47 int32_t position_360_degree,
const std::string& position_actual_field,
const std::string& topic_name)
48 : m_device(device), m_position_0_degree(position_0_degree),
49 m_position_360_degree(position_360_degree), m_position_actual_field(position_actual_field), m_topic_name(topic_name), m_initialized(false)
55 throw std::runtime_error(
"JointStatePublisher can only be used with a CiA 402 device."
56 " You passed a device with profile number "+std::to_string(profile));
59 const Value operation_mode = device.
get_entry(
"Modes of operation display");
64 throw std::runtime_error(
"[JointStatePublisher] Only position mode supported yet."
65 " Try device.set_entry(\"modes_of_operation\", device.get_constant(\"profile_position_mode\"));");
69 if (m_topic_name.empty()) {
71 m_topic_name =
"device" + std::to_string(node_id) +
"/get_joint_state";
78 assert(!m_topic_name.empty());
79 DEBUG_LOG(
"Advertising "<<m_topic_name);
81 m_publisher = nh.advertise<sensor_msgs::JointState>(m_topic_name, queue_size);
91 throw std::runtime_error(
"[JointStatePublisher] publish() called before advertise().");
94 sensor_msgs::JointState js;
97 js.position.resize(1);
100 js.velocity.resize(0);
103 js.name[0] = m_topic_name;
105 const int32_t pos = m_device.
get_entry(m_position_actual_field);
106 js.header.stamp = ros::Time::now();
107 js.position[0] = pos_to_rad(pos);
109 DEBUG_LOG(
"Sending JointState message");
111 DEBUG_DUMP(js.position[0]);
113 m_publisher.publish(js);
117 ERROR(
"Exception in JointStatePublisher::publish(): "<<error.
what());
122 double JointStatePublisher::pos_to_rad(int32_t pos)
const {
123 const double p = pos;
124 const double min = m_position_0_degree;
125 const double max = m_position_360_degree;
126 const double dist = max - min;
127 const double result = ((p-min)/dist)*2*pi();
virtual const char * what() const noexceptoverride
Returns error description.
const Value & get_entry(const std::string &entry_name, const ReadAccessMethod access_method=ReadAccessMethod::use_default)
Gets the value of a dictionary entry by name internally. If there is no cached value or the entry is ...
JointStatePublisher(Device &device, int32_t position_0_degree, int32_t position_360_degree, const std::string &position_actual_field="Position actual value", const std::string &topic_name="")
Constructor.
This type of exception is thrown when there are problems while accessing devices via SDO...
uint16_t get_device_profile_number()
Returns the CiA profile number.
void advertise() override
uint8_t get_node_id() const
Returns the node ID of the device.
This class represents a CanOpen slave device in the network.
This class contains a value to be stored in the object dictionary. The value can have one of the type...
static const std::map< uint16_t, std::map< std::string, const Value > > constants
Constants for CiA profiles. Type: map < profile number , map < operation name , constant value > > ...