moveit2
The MoveIt Motion Planning Framework for ROS 2.
publish_fake_jog_commands.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2020, PickNik Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of PickNik Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author : Adam Pettinger
36  Desc : Simple node to publish commands to the Servo demo
37  Title : test_servo_parameters.cpp
38  Project : moveit_servo
39  Created : 07/16/2020
40 */
41 
42 #include <geometry_msgs/msg/twist_stamped.hpp>
43 #include <std_srvs/srv/trigger.hpp>
44 #include <rclcpp/client.hpp>
45 #include <rclcpp/executors.hpp>
46 #include <rclcpp/logger.hpp>
47 #include <rclcpp/node.hpp>
48 #include <rclcpp/node_options.hpp>
49 #include <rclcpp/publisher.hpp>
50 #include <rclcpp/qos_event.hpp>
51 #include <rclcpp/time.hpp>
52 #include <rclcpp/utilities.hpp>
53 
54 using namespace std::chrono_literals;
55 
56 int main(int argc, char** argv)
57 {
58  rclcpp::init(argc, argv);
59  static const rclcpp::Logger LOGGER = rclcpp::get_logger("moveit_servo.publish_fake_jog_commands.cpp");
60 
61  // ROS objects
62  rclcpp::NodeOptions node_options;
63  auto node = std::make_shared<rclcpp::Node>("publish_fake_jog_commands", node_options);
64 
65  // Call the start service to init and start the servo component
66  rclcpp::Client<std_srvs::srv::Trigger>::SharedPtr servo_start_client =
67  node->create_client<std_srvs::srv::Trigger>("/servo_node/start_servo");
68  servo_start_client->wait_for_service(1s);
69  servo_start_client->async_send_request(std::make_shared<std_srvs::srv::Trigger::Request>());
70 
71  // Create a publisher for publishing the jog commands
72  auto pub = node->create_publisher<geometry_msgs::msg::TwistStamped>("/servo_node/delta_twist_cmds", 10);
73  std::weak_ptr<std::remove_pointer<decltype(pub.get())>::type> captured_pub = pub;
74  std::weak_ptr<std::remove_pointer<decltype(node.get())>::type> captured_node = node;
75  auto callback = [captured_pub, captured_node]() -> void {
76  auto pub_ptr = captured_pub.lock();
77  auto node_ptr = captured_node.lock();
78  if (!pub_ptr || !node_ptr)
79  {
80  return;
81  }
82  auto msg = std::make_unique<geometry_msgs::msg::TwistStamped>();
83  msg->header.stamp = node_ptr->now();
84  msg->header.frame_id = "panda_link0";
85  msg->twist.linear.x = 0.3;
86  msg->twist.angular.z = 0.5;
87  pub_ptr->publish(std::move(msg));
88  };
89  auto timer = node->create_wall_timer(50ms, callback);
90 
91  rclcpp::spin(node);
92  rclcpp::shutdown();
93  return 0;
94 }
int main(int argc, char **argv)