gazebo_bridge.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: Darby Lim */
18 
19 #include <ros/ros.h>
20 #include <std_msgs/Float64.h>
21 #include <std_msgs/Float64MultiArray.h>
22 
23 std::vector<ros::Publisher> gazebo_goal_joint_position_pub_;
25 std_msgs::Float64MultiArray joint_trajectory_point_;
27 
28 const float CONTROL_PERIOD = 0.001f;
29 
30 void jointTrajectoryPointCallback(const std_msgs::Float64MultiArray::ConstPtr& msg)
31 {
32  if (is_moving_ == false)
33  {
35  is_moving_ = true;
36  }
37 }
38 
39 void gripperPositionCallback(const std_msgs::Float64MultiArray::ConstPtr& msg)
40 {
41  std_msgs::Float64 positions;
42  positions.data = msg->data[0];
43  gazebo_goal_joint_position_pub_[4].publish(positions);
44 }
45 
47 {
48  const uint8_t POINT_SIZE = 4 + 1; //Joint num + Time
49  static uint32_t points = 0;
50 
51  static uint8_t wait_for_pub = 0;
52  static uint8_t loop_cnt = 0;
53 
54  if (is_moving_ == true)
55  {
56  uint32_t all_points_cnt = joint_trajectory_point_.data.size();
57  uint8_t pub_cnt = 0;
58 
59  if (loop_cnt < wait_for_pub)
60  {
61  loop_cnt++;
62  return;
63  }
64  else
65  {
66  for (uint32_t positions = points + 1; positions < (points + POINT_SIZE); positions++)
67  {
68  std_msgs::Float64 joint_position;
69  joint_position.data = joint_trajectory_point_.data[positions];
70 
71  gazebo_goal_joint_position_pub_.at(pub_cnt).publish(joint_position);
72  pub_cnt++;
73  }
74 
75  points = points + POINT_SIZE;
76  wait_for_pub = (joint_trajectory_point_.data[points] - joint_trajectory_point_.data[points - POINT_SIZE]) / CONTROL_PERIOD;
77 
78  loop_cnt = 0;
79 
80  if (points >= all_points_cnt)
81  {
82  joint_trajectory_point_.data.clear();
83  points = 0;
84  wait_for_pub = 0;
85  is_moving_ = false;
86  }
87  }
88  }
89 }
90 
92 {
93  ros::Publisher pb;
94  pb = nh.advertise<std_msgs::Float64>("joint1_position/command", 10);
95  gazebo_goal_joint_position_pub_.push_back(pb);
96 
97  pb = nh.advertise<std_msgs::Float64>("joint2_position/command", 10);
98  gazebo_goal_joint_position_pub_.push_back(pb);
99 
100  pb = nh.advertise<std_msgs::Float64>("joint3_position/command", 10);
101  gazebo_goal_joint_position_pub_.push_back(pb);
102 
103  pb = nh.advertise<std_msgs::Float64>("joint4_position/command", 10);
104  gazebo_goal_joint_position_pub_.push_back(pb);
105 
106  pb = nh.advertise<std_msgs::Float64>("gripper_position/command", 10);
107  gazebo_goal_joint_position_pub_.push_back(pb);
108 }
109 
111 {
112  joint_trajectory_point_sub_ = nh.subscribe("joint_trajectory_point", 1000, jointTrajectoryPointCallback);
113  gripper_position_sub_ = nh.subscribe("gripper_position", 10, gripperPositionCallback);
114 }
115 
116 int main(int argc, char **argv)
117 {
118  ros::init(argc, argv, "gazebo_bridge");
119  ros::NodeHandle node_handle("");
120 
121  initPublisher(node_handle);
122  initSubscriber(node_handle);
123 
124  ros::Timer publish_timer = node_handle.createTimer(ros::Duration(CONTROL_PERIOD), publishCallback);
125 
126  ros::spin();
127  return 0;
128 }
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
int main(int argc, char **argv)
std_msgs::Float64MultiArray joint_trajectory_point_
ros::Subscriber joint_trajectory_point_sub_
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void initSubscriber(ros::NodeHandle nh)
void jointTrajectoryPointCallback(const std_msgs::Float64MultiArray::ConstPtr &msg)
void initPublisher(ros::NodeHandle nh)
ROSCPP_DECL void spin(Spinner &spinner)
void publishCallback(const ros::TimerEvent &)
void gripperPositionCallback(const std_msgs::Float64MultiArray::ConstPtr &msg)
std::vector< ros::Publisher > gazebo_goal_joint_position_pub_
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
const float CONTROL_PERIOD
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool is_moving_
ros::Subscriber gripper_position_sub_


open_manipulator_with_tb3_tools
Author(s): Darby Lim
autogenerated on Thu Sep 10 2020 03:52:23