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 
25 /*****************************************************************************
26 ** Includes
27 *****************************************************************************/
28 
29 #include <ros/ros.h>
30 #include <ros/network.h>
31 #include <string>
32 #include <sstream>
34 
35 /*****************************************************************************
36 ** Namespaces
37 *****************************************************************************/
38 
39 namespace manipulator_h_gui {
40 
41 /*****************************************************************************
42 ** Implementation
43 *****************************************************************************/
44 
45 QNode::QNode(int argc, char** argv ) :
46  init_argc_(argc),
47  init_argv_(argv)
48 {}
49 
51  if(ros::isStarted()) {
52  ros::shutdown(); // explicitly needed since we use ros::start();
54  }
55  wait();
56 }
57 
58 bool QNode::init() {
59  ros::init(init_argc_,init_argv_,"manipulator_h_gui");
60 
61  ros::start(); // explicitly needed since our nodehandle is going out of scope.
63 
64  // Add your ros communications here.
65  ini_pose_msg_pub_ = n.advertise<std_msgs::String>("/robotis/base/ini_pose_msg", 0);
66  set_mode_msg_pub_ = n.advertise<std_msgs::String>("/robotis/base/set_mode_msg", 0);
67 
68  joint_pose_msg_pub_ = n.advertise<manipulator_h_base_module_msgs::JointPose>("/robotis/base/joint_pose_msg", 0);
69  kinematics_pose_msg_pub_ = n.advertise<manipulator_h_base_module_msgs::KinematicsPose>("/robotis/base/kinematics_pose_msg", 0);
70 
71  get_joint_pose_client_ = n.serviceClient<manipulator_h_base_module_msgs::GetJointPose>("/robotis/base/get_joint_pose", 0);
72  get_kinematics_pose_client_ = n.serviceClient<manipulator_h_base_module_msgs::GetKinematicsPose>("/robotis/base/get_kinematics_pose", 0);
73 
74  status_msg_sub_ = n.subscribe("/robotis/status", 10, &QNode::statusMsgCallback, this);
75 
76  start();
77  return true;
78 }
79 
80 void QNode::run() {
81 
82  ros::Rate loop_rate(50);
83 
84  while ( ros::ok() )
85  {
86  ros::spinOnce();
87 
88  loop_rate.sleep();
89  }
90  std::cout << "Ros shutdown, proceeding to close the gui." << std::endl;
91  Q_EMIT rosShutdown(); // used to signal the gui for a shutdown (useful to roslaunch)
92 }
93 
94 
95 void QNode::statusMsgCallback(const robotis_controller_msgs::StatusMsg::ConstPtr &msg)
96 {
97  log((LogLevel) msg->type, msg->status_msg, msg->module_name);
98 }
99 
100 void QNode::log( const LogLevel &level, const std::string &msg, std::string sender)
101 {
102  logging_model_.insertRows(logging_model_.rowCount(),1);
103  std::stringstream logging_model_msg;
104 
105  std::stringstream _sender;
106  _sender << "[" << sender << "] ";
107 
108  switch ( level ) {
109  case(Debug) : {
110  ROS_DEBUG_STREAM(msg);
111  logging_model_msg << "[DEBUG] [" << ros::Time::now() << "]: " << _sender.str() << msg;
112  break;
113  }
114  case(Info) : {
115  ROS_INFO_STREAM(msg);
116  logging_model_msg << "[INFO] [" << ros::Time::now() << "]: " << _sender.str() << msg;
117  break;
118  }
119  case(Warn) : {
120  ROS_WARN_STREAM(msg);
121  logging_model_msg << "[WARN] [" << ros::Time::now() << "]: " << _sender.str() <<msg;
122  break;
123  }
124  case(Error) : {
125  ROS_ERROR_STREAM(msg);
126  logging_model_msg << "<ERROR> [" << ros::Time::now() << "]: " << _sender.str() <<msg;
127  break;
128  }
129  case(Fatal) : {
130  ROS_FATAL_STREAM(msg);
131  logging_model_msg << "[FATAL] [" << ros::Time::now() << "]: " << _sender.str() <<msg;
132  break;
133  }
134  }
135  QVariant new_row(QString(logging_model_msg.str().c_str()));
136  logging_model_.setData(logging_model_.index(logging_model_.rowCount()-1),new_row);
137  Q_EMIT loggingUpdated(); // used to readjust the scrollbar
138 }
139 
140 void QNode::sendJointPoseMsg( manipulator_h_base_module_msgs::JointPose msg )
141 {
143 
144  log( Info , "Send Joint Pose Msg" );
145 }
146 
147 void QNode::sendKinematicsPoseMsg( manipulator_h_base_module_msgs::KinematicsPose msg )
148 {
150 
151  log( Info , "Send Kinematics Pose Msg" );
152 }
153 
154 void QNode::sendIniPoseMsg( std_msgs::String msg )
155 {
156  ini_pose_msg_pub_.publish ( msg );
157 
158  log( Info , "Go to Manipulator Initial Pose" );
159 }
160 
161 void QNode::sendSetModeMsg( std_msgs::String msg )
162 {
163  set_mode_msg_pub_.publish ( msg );
164 
165  log( Info , "Set BaseModule" );
166 }
167 
168 void QNode::getJointPose( std::vector<std::string> joint_name )
169 {
170  log( Info , "Get Current Joint Pose" );
171 
172  manipulator_h_base_module_msgs::GetJointPose _get_joint_pose;
173 
174  // request
175  for ( int _id = 0; _id < joint_name.size(); _id++ )
176  _get_joint_pose.request.joint_name.push_back( joint_name[ _id ] );
177 
178  // response
179  if ( get_joint_pose_client_.call ( _get_joint_pose ) )
180  {
181  manipulator_h_base_module_msgs::JointPose _joint_pose;
182 
183  for ( int _id = 0; _id < _get_joint_pose.response.joint_name.size(); _id++ )
184  {
185  _joint_pose.name.push_back( _get_joint_pose.response.joint_name[ _id ] );
186  _joint_pose.value.push_back( _get_joint_pose.response.joint_value[ _id ]);
187  }
188 
189  Q_EMIT updateCurrentJointPose( _joint_pose );
190  }
191  else
192  log(Error, "fail to get joint pose.");
193 }
194 
195 void QNode::getKinematicsPose ( std::string group_name )
196 {
197  log( Info , "Get Current Kinematics Pose" );
198 
199  manipulator_h_base_module_msgs::GetKinematicsPose _get_kinematics_pose;
200 
201  // request
202  _get_kinematics_pose.request.group_name = group_name;
203 
204  // response
205  if ( get_kinematics_pose_client_.call( _get_kinematics_pose ) )
206  {
207  manipulator_h_base_module_msgs::KinematicsPose _kinematcis_pose;
208 
209  _kinematcis_pose.name = _get_kinematics_pose.request.group_name;
210  _kinematcis_pose.pose = _get_kinematics_pose.response.group_pose;
211 
212  Q_EMIT updateCurrentKinematicsPose( _kinematcis_pose );
213  }
214  else
215  log(Error, "fail to get kinematcis pose.");
216 }
217 
218 } // namespace manipulator_h_gui
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
void sendSetModeMsg(std_msgs::String msg)
Definition: qnode.cpp:161
ROSCPP_DECL void start()
void publish(const boost::shared_ptr< M > &message) const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
void sendJointPoseMsg(manipulator_h_base_module_msgs::JointPose msg)
Definition: qnode.cpp:140
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool call(MReq &req, MRes &res)
ros::Publisher set_mode_msg_pub_
Definition: qnode.hpp:122
void getKinematicsPose(std::string group_name)
Definition: qnode.cpp:195
void getJointPose(std::vector< std::string > joint_name)
Definition: qnode.cpp:168
QStringListModel logging_model_
Definition: qnode.hpp:119
void sendKinematicsPoseMsg(manipulator_h_base_module_msgs::KinematicsPose msg)
Definition: qnode.cpp:147
ros::Subscriber status_msg_sub_
Definition: qnode.hpp:130
ros::Publisher ini_pose_msg_pub_
Definition: qnode.hpp:121
#define ROS_FATAL_STREAM(args)
QNode(int argc, char **argv)
Definition: qnode.cpp:45
void statusMsgCallback(const robotis_controller_msgs::StatusMsg::ConstPtr &msg)
Definition: qnode.cpp:95
void updateCurrentKinematicsPose(manipulator_h_base_module_msgs::KinematicsPose)
ROSCPP_DECL bool ok()
ros::Publisher kinematics_pose_msg_pub_
Definition: qnode.hpp:125
ros::Publisher joint_pose_msg_pub_
Definition: qnode.hpp:124
ros::ServiceClient get_kinematics_pose_client_
Definition: qnode.hpp:128
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
void log(const LogLevel &level, const std::string &msg, std::string sender="GUI")
Definition: qnode.cpp:100
#define ROS_WARN_STREAM(args)
#define ROS_DEBUG_STREAM(args)
ros::ServiceClient get_joint_pose_client_
Definition: qnode.hpp:127
bool sleep()
#define ROS_INFO_STREAM(args)
ROSCPP_DECL bool isStarted()
Communications central!
static Time now()
ROSCPP_DECL void shutdown()
#define ROS_ERROR_STREAM(args)
ROSCPP_DECL void spinOnce()
ROSCPP_DECL void waitForShutdown()
void sendIniPoseMsg(std_msgs::String msg)
Definition: qnode.cpp:154
void updateCurrentJointPose(manipulator_h_base_module_msgs::JointPose)


manipulator_h_gui
Author(s): SCH
autogenerated on Mon Jun 10 2019 14:02:56