qnode.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright 2018 ROBOTIS CO., LTD.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *******************************************************************************/
16 
17 /* Authors: Kayman Jung, SCH */
18 
19 /*****************************************************************************
20  ** Includes
21  *****************************************************************************/
22 
23 #include <string>
24 #include <sstream>
25 
26 #include <ros/ros.h>
27 #include <ros/network.h>
28 #include <std_msgs/String.h>
29 
30 #include "../include/thormang3_offset_tuner_client/qnode.hpp"
31 
32 /*****************************************************************************
33  ** Namespaces
34  *****************************************************************************/
35 
37 {
38 
39 /*****************************************************************************
40  ** Implementation
41  *****************************************************************************/
42 
43 QNode::QNode(int argc, char** argv)
44  : init_argc_(argc),
45  init_argv_(argv),
46  is_refresh_(false)
47 {
48 }
49 
51 {
52  if (ros::isStarted())
53  {
54  ros::shutdown(); // explicitly needed since we use ros::start();
56  }
57  wait();
58 }
59 
61 {
62  ros::init(init_argc_, init_argv_, "thormang3_offset_tuner_client");
63 
64  ros::start(); // explicitly needed since our nodehandle is going out of scope.
65  ros::NodeHandle nh;
66 
67  // Add your ros communications here
68  joint_offset_data_pub_ = nh.advertise<thormang3_offset_tuner_msgs::JointOffsetData>(
69  "/robotis/offset_tuner/joint_offset_data", 0);
70  torque_enable_pub_ = nh.advertise<thormang3_offset_tuner_msgs::JointTorqueOnOffArray>(
71  "/robotis/offset_tuner/torque_enable", 0);
72  command_pub_ = nh.advertise<std_msgs::String>("/robotis/offset_tuner/command", 0);
73 
74  get_present_joint_offset_data_client_ = nh.serviceClient<thormang3_offset_tuner_msgs::GetPresentJointOffsetData>(
75  "/robotis/offset_tuner/get_present_joint_offset_data");
76 
77  std::string _config_path = ros::package::getPath("thormang3_offset_tuner_client") + "/config/joint_data.yaml";
78  ParseOffsetGroup(_config_path);
79 
80  start();
81  return true;
82 }
83 
84 void QNode::run()
85 {
86 
87  ros::Rate loop_rate(125);
88 
89  while (ros::ok())
90  {
91  ros::spinOnce();
92  loop_rate.sleep();
93  }
94  std::cout << "Ros shutdown, proceeding to close the gui." << std::endl;
95  Q_EMIT rosShutdown(); // used to signal the gui for a shutdown (useful to roslaunch)
96 }
97 
98 void QNode::send_torque_enable_msg(thormang3_offset_tuner_msgs::JointTorqueOnOffArray msg)
99 {
101 
102  log(Info, "Joint Torque On/Off");
103 }
104 
105 void QNode::send_joint_offset_data_msg(thormang3_offset_tuner_msgs::JointOffsetData msg)
106 {
108 
109  log(Info, "Send Joint Offset Data");
110 }
111 
112 void QNode::send_command_msg(std_msgs::String msg)
113 {
114  command_pub_.publish(msg);
115 
116  std::stringstream log_msg;
117  log_msg << "Send Command : " << msg.data;
118 
119  log(Info, log_msg.str());
120 }
121 
123 {
124  is_refresh_ = true;
125  thormang3_offset_tuner_msgs::GetPresentJointOffsetData get_present_joint_offset_data;
126 
127  //request
128 
129  //response
130  if (get_present_joint_offset_data_client_.call(get_present_joint_offset_data))
131  {
132  for (int id = 0; id < get_present_joint_offset_data.response.present_data_array.size(); id++)
133  {
134  thormang3_offset_tuner_msgs::JointOffsetPositionData present_joint_data = get_present_joint_offset_data.response
135  .present_data_array[id];
136 
137  Q_EMIT update_present_joint_offset_data(present_joint_data);
138  }
139  }
140  else
141  log(Error, "Fail to get joint offset data");
142 
143  is_refresh_ = false;
144 }
145 
146 void QNode::log(const LogLevel &level, const std::string &msg)
147 {
148  logging_model_.insertRows(logging_model_.rowCount(), 1);
149  std::stringstream logging_model_msg;
150  switch (level)
151  {
152  case (Debug):
153  {
154  ROS_DEBUG_STREAM(msg);
155  logging_model_msg << "[DEBUG] [" << ros::Time::now() << "]: " << msg;
156  break;
157  }
158  case (Info):
159  {
160  ROS_INFO_STREAM(msg);
161  logging_model_msg << "[INFO] [" << ros::Time::now() << "]: " << msg;
162  break;
163  }
164  case (Warn):
165  {
166  ROS_WARN_STREAM(msg);
167  logging_model_msg << "[INFO] [" << ros::Time::now() << "]: " << msg;
168  break;
169  }
170  case (Error):
171  {
172  ROS_ERROR_STREAM(msg);
173  logging_model_msg << "[ERROR] [" << ros::Time::now() << "]: " << msg;
174  break;
175  }
176  case (Fatal):
177  {
178  ROS_FATAL_STREAM(msg);
179  logging_model_msg << "[FATAL] [" << ros::Time::now() << "]: " << msg;
180  break;
181  }
182  }
183  QVariant new_row(QString(logging_model_msg.str().c_str()));
184  logging_model_.setData(logging_model_.index(logging_model_.rowCount() - 1), new_row);
185  Q_EMIT loggingUpdated(); // used to readjust the scrollbar
186 }
187 
188 void QNode::ParseOffsetGroup(const std::string &path)
189 {
190  YAML::Node doc;
191  try
192  {
193  // load yaml
194  doc = YAML::LoadFile(path.c_str());
195  } catch (const std::exception& e)
196  {
197  ROS_ERROR("Fail to load offset config yaml.");
198  return;
199  }
200 
201  // parse right_arm
202  YAML::Node right_arm_node = doc["right_arm"];
203  for (YAML::iterator yaml_it = right_arm_node.begin(); yaml_it != right_arm_node.end(); ++yaml_it)
204  {
205  int index;
206  std::string joint_name;
207 
208  index = yaml_it->first.as<int>();
209  joint_name = yaml_it->second.as<std::string>();
210 
211  right_arm_offset_group[index] = joint_name;
212  }
213 
214  YAML::Node left_arm_node = doc["left_arm"];
215  for (YAML::iterator yaml_it = left_arm_node.begin(); yaml_it != left_arm_node.end(); ++yaml_it)
216  {
217  int index;
218  std::string joint_name;
219 
220  index = yaml_it->first.as<int>();
221  joint_name = yaml_it->second.as<std::string>();
222 
223  left_arm_offset_group[index] = joint_name;
224  }
225 
226  YAML::Node legs_node = doc["legs"];
227  for (YAML::iterator yaml_it = legs_node.begin(); yaml_it != legs_node.end(); ++yaml_it)
228  {
229  int index;
230  std::string joint_name;
231 
232  index = yaml_it->first.as<int>();
233  joint_name = yaml_it->second.as<std::string>();
234 
235  legs_offset_group[index] = joint_name;
236  }
237 
238  YAML::Node body_node = doc["body"];
239  for (YAML::iterator yaml_it = body_node.begin(); yaml_it != body_node.end(); ++yaml_it)
240  {
241  int index;
242  std::string joint_name;
243 
244  index = yaml_it->first.as<int>();
245  joint_name = yaml_it->second.as<std::string>();
246 
247  body_offset_group[index] = joint_name;
248  }
249 }
250 } // namespace thormang3_offset_tuner_client
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
ROSCPP_DECL void start()
void publish(const boost::shared_ptr< M > &message) const
void send_command_msg(std_msgs::String msg)
Definition: qnode.cpp:112
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool call(MReq &req, MRes &res)
QNode(int argc, char **argv)
Definition: qnode.cpp:43
void log(const LogLevel &level, const std::string &msg)
Definition: qnode.cpp:146
std::map< int, std::string > right_arm_offset_group
Definition: qnode.hpp:91
void ParseOffsetGroup(const std::string &path)
Definition: qnode.cpp:188
#define ROS_FATAL_STREAM(args)
ROSCPP_DECL bool ok()
std::map< int, std::string > body_offset_group
Definition: qnode.hpp:94
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
#define ROS_WARN_STREAM(args)
#define ROS_DEBUG_STREAM(args)
ROSLIB_DECL std::string getPath(const std::string &package_name)
bool sleep()
std::map< int, std::string > left_arm_offset_group
Definition: qnode.hpp:92
#define ROS_INFO_STREAM(args)
ROSCPP_DECL bool isStarted()
void send_torque_enable_msg(thormang3_offset_tuner_msgs::JointTorqueOnOffArray msg)
Definition: qnode.cpp:98
ros::ServiceClient get_present_joint_offset_data_client_
Definition: qnode.hpp:117
static Time now()
ROSCPP_DECL void shutdown()
#define ROS_ERROR_STREAM(args)
void send_joint_offset_data_msg(thormang3_offset_tuner_msgs::JointOffsetData msg)
Definition: qnode.cpp:105
std::map< int, std::string > legs_offset_group
Definition: qnode.hpp:93
ROSCPP_DECL void spinOnce()
#define ROS_ERROR(...)
ros::Publisher joint_offset_data_pub_
Definition: qnode.hpp:113
ROSCPP_DECL void waitForShutdown()
void update_present_joint_offset_data(thormang3_offset_tuner_msgs::JointOffsetPositionData msg)


thormang3_offset_tuner_client
Author(s): Kayman
autogenerated on Mon Jun 10 2019 15:38:36