moveit2
The MoveIt Motion Planning Framework for ROS 2.
ps4_dualshock.py
Go to the documentation of this file.
1 # Software License Agreement (BSD License)
2 #
3 # Copyright (c) 2022, Peter David Fagan
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
16 # * Neither the name of Willow Garage, Inc. nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 #
33 # Author: Peter David Fagan
34 """ PS4 dualshock teleop device implementation. """
35 
36 import rclpy
37 from multiprocessing import Process
38 
39 from geometry_msgs.msg import TwistStamped
40 from sensor_msgs.msg import Joy
41 from std_srvs.srv import Trigger
42 
43 from dataclasses import dataclass
44 from moveit.servo_client.teleop import TeleopDevice
45 
46 
47 
50 
51 
52 @dataclass
54  LEFT_STICK_X: int = 0
55  LEFT_STICK_Y: int = 1
56  LEFT_TRIGGER: int = 2
57  RIGHT_STICK_X: int = 3
58  RIGHT_STICK_Y: int = 4
59  RIGHT_TRIGGER: int = 5
60  D_PAD_X: int = 6
61  D_PAD_Y: int = 7
62 
63 
64 @dataclass
66  X: int = 0
67  O: int = 1
68  TRIANGLE: int = 2
69  SQUARE: int = 3
70  L1: int = 4
71  R1: int = 5
72  L2: int = 6
73  R2: int = 7
74  SHARE: int = 8
75  OPTIONS: int = 9
76  HOME: int = 10
77  LEFT_STICK_TRIGGER: int = 11
78  RIGHT_STICK_TRIGGER: int = 12
79 
80 
81 @dataclass
83  """
84  A dataclass to store device config. This class follows the conventions of Joy message (http://docs.ros.org/en/noetic/api/sensor_msgs/html/msg/Joy.html)
85  """
86 
87  Axes: DualShockAxes = DualShockAxes()
88 
89  Buttons: DualShockButtons = DualShockButtons()
90 
91 
92 
95 
96 
98  "A class which encapsulates teleoperation functionalities for ps4 dualshock device."
99 
100  def __init__(
101  self,
102  ee_frame_name: str,
103  node_name: str = "ps4_dualshock_teleop",
104  device_name: str = "ps4_dualshock",
105  device_config: PS4DualShock = PS4DualShock(),
106  ):
107  super().__init__(
108  node_name=node_name,
109  device_name=device_name,
110  device_config=device_config,
111  ee_frame_name=ee_frame_name,
112  )
113  self.loggerlogger = rclpy.logging.get_logger("ps4_dualshock_teleop")
114 
115  def publish_command(self, data):
116  """
117  Publishes the teleop command.
118  """
119  try:
120  # convert joy data to twist command
121  twist = TwistStamped()
122  twist.twist.linear.z = data.axes[self.device_configdevice_config.Axes.RIGHT_STICK_Y]
123  twist.twist.linear.y = data.axes[self.device_configdevice_config.Axes.RIGHT_STICK_X]
124 
125  lin_x_right = -0.5 * (data.axes[self.device_configdevice_config.Axes.RIGHT_TRIGGER])
126  lin_x_left = 0.5 * (data.axes[self.device_configdevice_config.Axes.LEFT_TRIGGER])
127  twist.twist.linear.x = lin_x_right + lin_x_left
128 
129  twist.twist.angular.y = data.axes[self.device_configdevice_config.Axes.LEFT_STICK_Y]
130  twist.twist.angular.x = data.axes[self.device_configdevice_config.Axes.LEFT_STICK_X]
131 
132  roll_positive = data.buttons[self.device_configdevice_config.Buttons.R1]
133  roll_negative = -1 * data.buttons[self.device_configdevice_config.Buttons.L1]
134  twist.twist.angular.z = float(roll_positive + roll_negative)
135 
136  twist.header.frame_id = self.ee_frame_nameee_frame_name
137  twist.header.stamp = self.get_clock().now().to_msg()
138  self.twist_publishertwist_publisher.publish(twist)
139  except Exception as e:
140  self.loggerlogger.info.error(e)
141  print(e)
142 
143  def record():
144  pass
def __init__(self, str ee_frame_name, str node_name="ps4_dualshock_teleop", str device_name="ps4_dualshock", PS4DualShock device_config=PS4DualShock())
void print(PropagationDistanceField &pdf, int numX, int numY, int numZ)