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 /*****************************************************************************
18 ** Includes
19 *****************************************************************************/
20 
21 #include <ros/ros.h>
22 #include <ros/network.h>
23 #include <string>
24 #include <std_msgs/String.h>
25 #include <sstream>
26 #include "../include/rh_p12_rn_gui/qnode.hpp"
27 
28 /*****************************************************************************
29 ** Namespaces
30 *****************************************************************************/
31 
32 namespace rh_p12_rn_gui {
33 
34 /*****************************************************************************
35 ** Implementation
36 *****************************************************************************/
37 
38 QNode::QNode(int argc, char** argv ) :
39  init_argc(argc),
40  init_argv(argv)
41  {}
42 
44 {
45  if(ros::isStarted())
46  {
47  ros::shutdown(); // explicitly needed since we use ros::start();
49  }
50  wait();
51 }
52 
54 {
55  ros::init(init_argc,init_argv,"rh_p12_rn_gui");
56 
57  ros::start(); // explicitly needed since our nodehandle is going out of scope.
59  // Add your ros communications here.
60  sync_write_item_pub_ = n.advertise<robotis_controller_msgs::SyncWriteItem>("/robotis/sync_write_item", 5);
61  sync_write_position_pub_ = n.advertise<robotis_controller_msgs::SyncWriteItem>("/robotis/direct/sync_write_item", 5);
62 
63  get_item_value_client_ = n.serviceClient<rh_p12_rn_base_module_msgs::GetItemValue>("/robotis/rh_p12_rn_base/get_item_value");
64 
65  start();
66  return true;
67 }
68 
69 void QNode::run() {
70 
71  ros::Rate loop_rate(125);
72 
73  while ( ros::ok() )
74  {
75  ros::spinOnce();
76 
77  Q_EMIT refreshValue(getItemValue("present_position"), (short)(getItemValue("present_current")));
78 
79  loop_rate.sleep();
80  }
81  std::cout << "Ros shutdown, proceeding to close the gui." << std::endl;
82  Q_EMIT rosShutdown(); // used to signal the gui for a shutdown (useful to roslaunch)
83 }
84 
85 void QNode::setCtrlItem(const robotis_controller_msgs::SyncWriteItem &msg)
86 {
88 }
89 
90 void QNode::setPosition(const robotis_controller_msgs::SyncWriteItem &msg)
91 {
93 }
94 
95 uint32_t QNode::getItemValue(std::string item_name)
96 {
97  rh_p12_rn_base_module_msgs::GetItemValue _srv;
98  _srv.request.joint_name = "gripper";
99  _srv.request.item_name = item_name;
100 
101  if(get_item_value_client_.call(_srv))
102  return (uint32_t)_srv.response.value;
103  else
104  return 0;
105 }
106 
107 void QNode::log( const LogLevel &level, const std::string &msg)
108 {
109  logging_model.insertRows(logging_model.rowCount(),1);
110  std::stringstream logging_model_msg;
111  switch ( level )
112  {
113  case(Debug) : {
114  ROS_DEBUG_STREAM(msg);
115  logging_model_msg << "[DEBUG] [" << ros::Time::now() << "]: " << msg;
116  break;
117  }
118  case(Info) : {
119  ROS_INFO_STREAM(msg);
120  logging_model_msg << "[INFO] [" << ros::Time::now() << "]: " << msg;
121  break;
122  }
123  case(Warn) : {
124  ROS_WARN_STREAM(msg);
125  logging_model_msg << "[INFO] [" << ros::Time::now() << "]: " << msg;
126  break;
127  }
128  case(Error) : {
129  ROS_ERROR_STREAM(msg);
130  logging_model_msg << "[ERROR] [" << ros::Time::now() << "]: " << msg;
131  break;
132  }
133  case(Fatal) : {
134  ROS_FATAL_STREAM(msg);
135  logging_model_msg << "[FATAL] [" << ros::Time::now() << "]: " << msg;
136  break;
137  }
138  }
139  QVariant new_row(QString(logging_model_msg.str().c_str()));
140  logging_model.setData(logging_model.index(logging_model.rowCount()-1),new_row);
141  Q_EMIT loggingUpdated(); // used to readjust the scrollbar
142 }
143 
144 } // namespace rh_p12_rn_gui
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
void refreshValue(int pos, int curr)
ROSCPP_DECL void start()
void publish(const boost::shared_ptr< M > &message) const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void setPosition(const robotis_controller_msgs::SyncWriteItem &msg)
Definition: qnode.cpp:90
bool call(MReq &req, MRes &res)
void log(const LogLevel &level, const std::string &msg)
Definition: qnode.cpp:107
ros::ServiceClient get_item_value_client_
Definition: qnode.hpp:91
QNode(int argc, char **argv)
Definition: qnode.cpp:38
QStringListModel logging_model
Definition: qnode.hpp:86
#define ROS_FATAL_STREAM(args)
ros::Publisher sync_write_position_pub_
Definition: qnode.hpp:89
ROSCPP_DECL bool ok()
ros::Publisher sync_write_item_pub_
Definition: qnode.hpp:88
uint32_t getItemValue(std::string item_name)
Definition: qnode.cpp:95
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
#define ROS_WARN_STREAM(args)
#define ROS_DEBUG_STREAM(args)
bool sleep()
char ** init_argv
Definition: qnode.hpp:85
#define ROS_INFO_STREAM(args)
ROSCPP_DECL bool isStarted()
static Time now()
ROSCPP_DECL void shutdown()
#define ROS_ERROR_STREAM(args)
virtual ~QNode()
Definition: qnode.cpp:43
ROSCPP_DECL void spinOnce()
void setCtrlItem(const robotis_controller_msgs::SyncWriteItem &msg)
Definition: qnode.cpp:85
ROSCPP_DECL void waitForShutdown()


rh_p12_rn_gui
Author(s): Zerom
autogenerated on Mon Jun 10 2019 14:42:12