cpp_interface_example.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * Title : cpp_interface_example.cpp
3  * Project : moveit_jog_arm
4  * Created : 11/20/2019
5  * Author : Andy Zelenak
6  *
7  * BSD 3-Clause License
8  *
9  * Copyright (c) 2019, Los Alamos National Security, LLC
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  *
15  * * Redistributions of source code must retain the above copyright notice, this
16  * list of conditions and the following disclaimer.
17  *
18  * * Redistributions in binary form must reproduce the above copyright notice,
19  * this list of conditions and the following disclaimer in the documentation
20  * and/or other materials provided with the distribution.
21  *
22  * * Neither the name of the copyright holder nor the names of its
23  * contributors may be used to endorse or promote products derived from
24  * this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE
30  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 *******************************************************************************/
38 
41 
42 static const std::string LOGNAME = "cpp_interface_example";
43 
49 int main(int argc, char** argv)
50 {
51  ros::init(argc, argv, LOGNAME);
52 
53  // Load the planning scene monitor
54  planning_scene_monitor::PlanningSceneMonitorPtr planning_scene_monitor;
55  planning_scene_monitor = std::make_shared<planning_scene_monitor::PlanningSceneMonitor>("robot_description");
56  if (!planning_scene_monitor->getPlanningScene())
57  {
58  ROS_ERROR_STREAM_NAMED(LOGNAME, "Error in setting up the PlanningSceneMonitor.");
59  exit(EXIT_FAILURE);
60  }
61 
62  planning_scene_monitor->startSceneMonitor();
63  planning_scene_monitor->startWorldGeometryMonitor(
66  false /* skip octomap monitor */);
67  planning_scene_monitor->startStateMonitor();
68 
69  // Run the jogging C++ interface in a new thread to ensure a constant outgoing message rate.
70  moveit_jog_arm::JogCppInterface jog_interface(planning_scene_monitor);
71  std::thread jogging_thread([&]() { jog_interface.startMainLoop(); });
72 
73  // Make a Cartesian velocity message
74  geometry_msgs::TwistStamped velocity_msg;
75  velocity_msg.header.frame_id = "base_link";
76  velocity_msg.twist.linear.y = 0.01;
77  velocity_msg.twist.linear.z = -0.01;
78 
79  ros::Rate cmd_rate(100);
80  uint num_commands = 0;
81 
82  // Send a few Cartesian velocity commands
83  while (ros::ok() && num_commands < 200)
84  {
85  ++num_commands;
86  velocity_msg.header.stamp = ros::Time::now();
87  jog_interface.provideTwistStampedCommand(velocity_msg);
88  cmd_rate.sleep();
89  }
90 
91  // Leave plenty of time for the jogger to halt its previous motion.
92  // For a faster response, adjust the incoming_command_timeout yaml parameter
93  ros::Duration(2).sleep();
94 
95  // Make a joint command
96  control_msgs::JointJog base_joint_command;
97  base_joint_command.joint_names.push_back("elbow_joint");
98  base_joint_command.velocities.push_back(0.2);
99  base_joint_command.header.stamp = ros::Time::now();
100 
101  // Send a few joint commands
102  num_commands = 0;
103  while (ros::ok() && num_commands < 200)
104  {
105  ++num_commands;
106  base_joint_command.header.stamp = ros::Time::now();
107  jog_interface.provideJointCommand(base_joint_command);
108  cmd_rate.sleep();
109  }
110 
111  // Retrieve the current joint state from the jogger
112  sensor_msgs::JointState current_joint_state = jog_interface.getJointState();
113  ROS_INFO_STREAM_NAMED(LOGNAME, "Current joint state:");
114  ROS_INFO_STREAM_NAMED(LOGNAME, current_joint_state);
115 
116  // Retrieve the current status of the jogger
117  moveit_jog_arm::StatusCode status = jog_interface.getJoggerStatus();
118  ROS_INFO_STREAM_NAMED(LOGNAME, "Jogger status:\n" << status);
119 
120  jog_interface.stopMainLoop();
121  jogging_thread.join();
122  return 0;
123 }
#define ROS_ERROR_STREAM_NAMED(name, args)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
int main(int argc, char **argv)
#define ROS_INFO_STREAM_NAMED(name, args)
static const std::string DEFAULT_PLANNING_SCENE_WORLD_TOPIC
void provideJointCommand(const control_msgs::JointJog &joint_command)
Send joint position(s) commands.
ROSCPP_DECL bool ok()
void provideTwistStampedCommand(const geometry_msgs::TwistStamped &velocity_command)
Provide a Cartesian velocity command to the jogger. The units are determined by settings in the yaml ...
static const std::string LOGNAME
bool sleep()
static const std::string DEFAULT_COLLISION_OBJECT_TOPIC
static Time now()
bool sleep() const
sensor_msgs::JointState getJointState()


moveit_jog_arm
Author(s): Brian O'Neil, Andy Zelenak , Blake Anderson, Alexander Rössler
autogenerated on Fri Jun 5 2020 03:53:46