32 #include "joint_state_subscriber.h"
36 #include "sdo_error.h"
45 int32_t position_360_degree, std::string topic_name)
46 : m_device(device), m_position_0_degree(position_0_degree),
47 m_position_360_degree(position_360_degree), m_topic_name(topic_name),
54 throw std::runtime_error(
"JointStatePublisher can only be used with a CiA 402 device."
55 " You passed a device with profile number "+std::to_string(profile));
58 const Value operation_mode = device.
get_entry(
"Modes of operation display");
63 throw std::runtime_error(
"[JointStatePublisher] Only position mode supported yet."
64 " Try device.set_entry(\"modes_of_operation\", device.get_constant(\"profile_position_mode\"));");
67 if (m_topic_name.empty()) {
69 m_topic_name =
"device" + std::to_string(node_id) +
"/set_joint_state";
76 assert(!m_topic_name.empty());
77 DEBUG_LOG(
"Advertising "<<m_topic_name);
79 m_subscriber = nh.subscribe(m_topic_name, queue_size, &JointStateSubscriber::receive,
this);
84 void JointStateSubscriber::receive(
const sensor_msgs::JointState& msg) {
88 assert(msg.position.size()>0);
89 const int32_t pos = rad_to_pos(msg.position[0]);
91 DEBUG_LOG(
"Received JointState message");
93 DEBUG_DUMP(msg.position[0]);
95 m_device.
execute(
"set_target_position",pos);
97 }
catch (
const sdo_error& error) {
99 ERROR(
"Exception in JointStateSubscriber::receive(): "<<error.what());
104 int32_t JointStateSubscriber::rad_to_pos(
double rad)
const {
106 const double p = rad;
107 const double min = m_position_0_degree;
108 const double max = m_position_360_degree;
109 const double dist = max - min;
110 const double result = ((p/(2*pi()))*dist)+min;
111 return (int32_t) result;
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 ...
uint16_t get_device_profile_number()
Returns the CiA profile number.
uint8_t get_node_id() const
Returns the node ID of the device.
Value execute(const std::string &operation_name, const Value &argument=m_dummy_value)
Executes a convenience operation. It must exist due to a previous load_operations() or add_operation(...
void advertise() override
This class represents a CanOpen slave device in the network.
JointStateSubscriber(Device &device, int32_t position_0_degree, int32_t position_360_degree, std::string topic_name="")
Constructor.
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 > > ...