KaCanOpen
 All Classes Functions Variables Typedefs Enumerations Pages
entry_publisher.cpp
1 /*
2  * Copyright (c) 2015-2016, Thomas Keh
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "entry_publisher.h"
33 #include "utils.h"
34 #include "logger.h"
35 #include "ros/ros.h"
36 #include "sdo_error.h"
37 
38 #include "std_msgs/UInt8.h"
39 #include "std_msgs/UInt16.h"
40 #include "std_msgs/UInt32.h"
41 #include "std_msgs/Int8.h"
42 #include "std_msgs/Int16.h"
43 #include "std_msgs/Int32.h"
44 #include "std_msgs/Bool.h"
45 #include "std_msgs/String.h"
46 
47 #include <string>
48 
49 namespace kaco {
50 
51 EntryPublisher::EntryPublisher(Device& device, const std::string& entry_name, const ReadAccessMethod access_method)
52  : m_device(device), m_entry_name(entry_name), m_access_method(access_method)
53 {
54 
55  uint8_t node_id = device.get_node_id();
56  m_device_prefix = "device" + std::to_string(node_id) + "/";
57  // no spaces and '-' allowed in ros names
58  m_name = Utils::escape(entry_name);
59  m_type = device.get_entry_type(entry_name);
60 
61 }
62 
64 
65  std::string topic = m_device_prefix+"get_"+m_name;
66  DEBUG_LOG("Advertising "<<topic);
67  ros::NodeHandle nh;
68 
69  switch(m_type) {
70  case Type::uint8:
71  m_publisher = nh.advertise<std_msgs::UInt8>(topic, queue_size);
72  break;
73  case Type::uint16:
74  m_publisher = nh.advertise<std_msgs::UInt16>(topic, queue_size);
75  break;
76  case Type::uint32:
77  m_publisher = nh.advertise<std_msgs::UInt32>(topic, queue_size);
78  break;
79  case Type::int8:
80  m_publisher = nh.advertise<std_msgs::Int8>(topic, queue_size);
81  break;
82  case Type::int16:
83  m_publisher = nh.advertise<std_msgs::Int16>(topic, queue_size);
84  break;
85  case Type::int32:
86  m_publisher = nh.advertise<std_msgs::Int32>(topic, queue_size);
87  break;
88  case Type::boolean:
89  m_publisher = nh.advertise<std_msgs::Bool>(topic, queue_size);
90  break;
91  case Type::string:
92  m_publisher = nh.advertise<std_msgs::String>(topic, queue_size);
93  break;
94  default:
95  ERROR("[EntryPublisher::advertise] Invalid entry type.")
96  }
97 
98 }
99 
101 
102  try {
103 
104  Value value = m_device.get_entry(m_entry_name, m_access_method);
105 
106  switch(m_type) {
107  case Type::uint8: {
108  std_msgs::UInt8 msg;
109  msg.data = value; // auto cast!
110  m_publisher.publish(msg);
111  break;
112  }
113  case Type::uint16: {
114  std_msgs::UInt16 msg;
115  msg.data = value; // auto cast!
116  m_publisher.publish(msg);
117  break;
118  }
119  case Type::uint32: {
120  std_msgs::UInt32 msg;
121  msg.data = value; // auto cast!
122  m_publisher.publish(msg);
123  break;
124  }
125  case Type::int8: {
126  std_msgs::Int8 msg;
127  msg.data = value; // auto cast!
128  m_publisher.publish(msg);
129  break;
130  }
131  case Type::int16: {
132  std_msgs::Int16 msg;
133  msg.data = value; // auto cast!
134  m_publisher.publish(msg);
135  break;
136  }
137  case Type::int32: {
138  std_msgs::Int32 msg;
139  msg.data = value; // auto cast!
140  m_publisher.publish(msg);
141  break;
142  }
143  case Type::boolean: {
144  std_msgs::Bool msg;
145  msg.data = value; // auto cast!
146  m_publisher.publish(msg);
147  break;
148  }
149  case Type::string: {
150  std_msgs::String msg;
151  msg.data = (std::string) value;
152  m_publisher.publish(msg);
153  break;
154  }
155  default: {
156  ERROR("[EntryPublisher::advertise] Invalid entry type.")
157  }
158  }
159 
160  } catch (const sdo_error& error) {
161  // TODO: only catch timeouts?
162  ERROR("Exception in EntryPublisher::publish(): "<<error.what());
163  }
164 
165 }
166 
167 } // end namespace kaco
virtual const char * what() const noexceptoverride
Returns error description.
Definition: sdo_error.cpp:105
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 ...
Definition: device.cpp:102
static std::string escape(const std::string &str)
Converts entry names to lower case and replaces all spaces and '-' by underscores.
Definition: utils.cpp:166
This type of exception is thrown when there are problems while accessing devices via SDO...
Definition: sdo_error.h:48
uint8_t get_node_id() const
Returns the node ID of the device.
Definition: device.cpp:74
void publish() override
This class represents a CanOpen slave device in the network.
Definition: device.h:83
void advertise() override
Type get_entry_type(const std::string &entry_name)
Returns the type of a dictionary entry identified by name as it is defined in the local dictionary...
Definition: device.cpp:87
EntryPublisher(Device &device, const std::string &entry_name, const ReadAccessMethod access_method=ReadAccessMethod::use_default)
Constructor.
This class contains a value to be stored in the object dictionary. The value can have one of the type...
Definition: value.h:53