41 #include <sensor_msgs/msg/joy.hpp>
42 #include <geometry_msgs/msg/twist_stamped.hpp>
43 #include <control_msgs/msg/joint_jog.hpp>
44 #include <std_srvs/srv/trigger.hpp>
45 #include <moveit_msgs/msg/planning_scene.hpp>
46 #include <rclcpp/client.hpp>
47 #include <rclcpp/experimental/buffers/intra_process_buffer.hpp>
48 #include <rclcpp/node.hpp>
49 #include <rclcpp/publisher.hpp>
50 #include <rclcpp/qos.hpp>
51 #include <rclcpp/qos_event.hpp>
52 #include <rclcpp/subscription.hpp>
53 #include <rclcpp/time.hpp>
54 #include <rclcpp/utilities.hpp>
59 const std::string
TWIST_TOPIC =
"/servo_node/delta_twist_cmds";
60 const std::string
JOINT_TOPIC =
"/servo_node/delta_joint_cmds";
107 std::unique_ptr<geometry_msgs::msg::TwistStamped>& twist,
108 std::unique_ptr<control_msgs::msg::JointJog>& joint)
112 if (buttons[
A] || buttons[
B] || buttons[
X] || buttons[
Y] || axes[
D_PAD_X] || axes[
D_PAD_Y])
115 joint->joint_names.push_back(
"panda_joint1");
116 joint->velocities.push_back(axes[
D_PAD_X]);
117 joint->joint_names.push_back(
"panda_joint2");
118 joint->velocities.push_back(axes[
D_PAD_Y]);
121 joint->joint_names.push_back(
"panda_joint7");
122 joint->velocities.push_back(buttons[
B] - buttons[
X]);
123 joint->joint_names.push_back(
"panda_joint6");
124 joint->velocities.push_back(buttons[
Y] - buttons[
A]);
134 twist->twist.linear.x = lin_x_right + lin_x_left;
140 double roll_negative = -1 * (buttons[
LEFT_BUMPER]);
141 twist->twist.angular.z = roll_positive + roll_negative;
167 joy_sub_ = this->create_subscription<sensor_msgs::msg::Joy>(
169 [
this](
const sensor_msgs::msg::Joy::ConstSharedPtr& msg) {
return joyCB(msg); });
171 twist_pub_ = this->create_publisher<geometry_msgs::msg::TwistStamped>(
TWIST_TOPIC, rclcpp::SystemDefaultsQoS());
172 joint_pub_ = this->create_publisher<control_msgs::msg::JointJog>(
JOINT_TOPIC, rclcpp::SystemDefaultsQoS());
174 this->create_publisher<moveit_msgs::msg::PlanningScene>(
"/planning_scene", rclcpp::SystemDefaultsQoS());
177 servo_start_client_ = this->create_client<std_srvs::srv::Trigger>(
"/servo_node/start_servo");
178 servo_start_client_->wait_for_service(std::chrono::seconds(1));
179 servo_start_client_->async_send_request(std::make_shared<std_srvs::srv::Trigger::Request>());
182 collision_pub_thread_ = std::thread([
this]() {
183 rclcpp::sleep_for(std::chrono::seconds(3));
185 moveit_msgs::msg::CollisionObject collision_object;
186 collision_object.header.frame_id =
"panda_link0";
187 collision_object.id =
"box";
189 shape_msgs::msg::SolidPrimitive table_1;
190 table_1.type = table_1.BOX;
191 table_1.dimensions = { 0.4, 0.6, 0.03 };
193 geometry_msgs::msg::Pose table_1_pose;
194 table_1_pose.position.x = 0.6;
195 table_1_pose.position.y = 0.0;
196 table_1_pose.position.z = 0.4;
198 shape_msgs::msg::SolidPrimitive table_2;
199 table_2.type = table_2.BOX;
200 table_2.dimensions = { 0.6, 0.4, 0.03 };
202 geometry_msgs::msg::Pose table_2_pose;
203 table_2_pose.position.x = 0.0;
204 table_2_pose.position.y = 0.5;
205 table_2_pose.position.z = 0.25;
207 collision_object.primitives.push_back(table_1);
208 collision_object.primitive_poses.push_back(table_1_pose);
209 collision_object.primitives.push_back(table_2);
210 collision_object.primitive_poses.push_back(table_2_pose);
211 collision_object.operation = collision_object.ADD;
213 moveit_msgs::msg::PlanningSceneWorld psw;
214 psw.collision_objects.push_back(collision_object);
216 auto ps = std::make_unique<moveit_msgs::msg::PlanningScene>();
219 collision_pub_->publish(std::move(ps));
225 if (collision_pub_thread_.joinable())
226 collision_pub_thread_.join();
229 void joyCB(
const sensor_msgs::msg::Joy::ConstSharedPtr& msg)
232 auto twist_msg = std::make_unique<geometry_msgs::msg::TwistStamped>();
233 auto joint_msg = std::make_unique<control_msgs::msg::JointJog>();
242 twist_msg->header.frame_id = frame_to_publish_;
243 twist_msg->header.stamp = this->now();
244 twist_pub_->publish(std::move(twist_msg));
249 joint_msg->header.stamp = this->now();
250 joint_msg->header.frame_id =
"panda_link3";
251 joint_pub_->publish(std::move(joint_msg));
256 rclcpp::Subscription<sensor_msgs::msg::Joy>::SharedPtr joy_sub_;
257 rclcpp::Publisher<geometry_msgs::msg::TwistStamped>::SharedPtr twist_pub_;
258 rclcpp::Publisher<control_msgs::msg::JointJog>::SharedPtr joint_pub_;
259 rclcpp::Publisher<moveit_msgs::msg::PlanningScene>::SharedPtr collision_pub_;
260 rclcpp::Client<std_srvs::srv::Trigger>::SharedPtr servo_start_client_;
262 std::string frame_to_publish_;
264 std::thread collision_pub_thread_;
270 #include <rclcpp_components/register_node_macro.hpp>
~JoyToServoPub() override
JoyToServoPub(const rclcpp::NodeOptions &options)
void joyCB(const sensor_msgs::msg::Joy::ConstSharedPtr &msg)
std::map< Button, double > BUTTON_DEFAULTS
const std::string JOY_TOPIC
const std::string EEF_FRAME_ID
std::map< Axis, double > AXIS_DEFAULTS
void updateCmdFrame(std::string &frame_name, const std::vector< int > &buttons)
// This should update the frame_to_publish_ as needed for changing command frame via controller
const std::string BASE_FRAME_ID
bool convertJoyToCmd(const std::vector< float > &axes, const std::vector< int > &buttons, std::unique_ptr< geometry_msgs::msg::TwistStamped > &twist, std::unique_ptr< control_msgs::msg::JointJog > &joint)
// This converts a joystick axes and buttons array to a TwistStamped or JointJog message
const std::string TWIST_TOPIC
const std::string JOINT_TOPIC