Skip to content

Instantly share code, notes, and snippets.

@mmurray
Created February 9, 2024 21:26
Show Gist options
  • Save mmurray/03cfb379d54cf4186ee4555a06022148 to your computer and use it in GitHub Desktop.
Save mmurray/03cfb379d54cf4186ee4555a06022148 to your computer and use it in GitHub Desktop.
import rclpy
from rclpy.node import Node
from rclpy.time import Time
from std_msgs.msg import String
from tf2_ros import TransformException
from tf2_ros.buffer import Buffer
from tf2_ros.transform_listener import TransformListener
class FrameListener(Node):
def __init__(self):
super().__init__('stretch_tf_listener')
self.tf_buffer = Buffer()
self.tf_listener = TransformListener(self.tf_buffer, self)
self.cmd_subscriber = self.create_subscription(String, 'demo_commands', self.cmd_callback, 10)
def cmd_callback(self, msg):
from_frame_rel = 'target_object1'
target_frame = 'base_link'
if msg.data == 'record_pose':
self.get_logger().info('Got record message')
now = Time()
trans = self.tf_buffer.lookup_transform(
target_frame,
from_frame_rel,
now)
self.get_logger().info(f'{trans}')
elif msg.data == 'quit':
self.get_logger().info('Got quit message')
self.destroy_node()
rclpy.shutdown()
def main():
rclpy.init()
node = FrameListener()
rclpy.spin(node)
node.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()
from pynput import keyboard
import rclpy
from rclpy.node import Node
from std_msgs.msg import String
class KeyboardInput(Node):
def __init__(self):
super().__init__('keyboard_input')
self.publisher = self.create_publisher(String, 'demo_commands', 10)
def publish_message(self, data):
msg = String()
msg.data = data
self.publisher.publish(msg)
def on_keyboard_press(self, key):
if key == keyboard.Key.esc:
self.publish_message('quit')
self.get_logger().info('quit message sent...')
self.destroy_node()
rclpy.shutdown()
if key == keyboard.Key.space:
self.publish_message('record_pose')
self.get_logger().info('record_pose message sent...')
def get_keyboard_input(self):
with keyboard.Listener(on_press=self.on_keyboard_press) as listener:
listener.join()
if __name__ == '__main__':
rclpy.init()
node = KeyboardInput()
node.get_keyboard_input()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment