integrating ROS2
I am working on a pan and tilt project using Humble on Raspberry pi 4 & Raspbian. I have managed to successfully migrate https://bitbucket.org/theconstructcore/pan_and_tilt_morpheus_chair/src/master/pan_and_tilt_control/scripts/pan_and_tilt_server.py from ROS1 to ROS2.
Now I am trying adapt to this code and integrate ROS2 to work with the /panandtilt_server.py KeyboardPanTilt.py https://core-electronics.com.au/guides/pan-tilt-hat-raspberry-pi/#Down
Can you please have a look and let me know if there is a chance it may work or are any obvious errors caused by misunderstanding(s) of ROS2 concepts.
Thanks Zahid
import rclpy from rclpy.node import Node
from tutorial_interfaces.msg import PanAndTilt
importing required libraries
import curses import os import time import picamera
class KeyBoardPressPanTilt(Node):
def __init__(self):
super().__init__('KeyBoard_Press_PanTilt')
self.publisher_ = self.create_publisher(PanAndTilt, 'pan_and_tilt', 10)
# Initialise camera
self.camera = picamera.PiCamera()
self.camera.resolution = (1024, 768)
self.camera.start_preview(fullscreen=False, window = (100,20,640,480))
# flipping the camera for so its not upside down
self.camera.vflip = True
self.camera.hflip = True
def ProcessActiveKeyBoardPress(self):
# Process active key presses:
# -- Letter p will take a picture and store file name image[picNum].jpg,
# where [number] increments over a picture taking session.
# -- Arrow keys will control the Pan Tilt Camera (deltaPan/deltaTilt Degree angles)
# -- Letter q will quit the application,
a = 0.0
b = 0.0
# initialise pan and tilt positions and process increments driven by arrow keys
# set start up serrvo positions
msg = PanAndTilt()
msg.pan = a
msg.tilt = b
self.publisher_.publish(msg)
# Set up key mappings and curses for arrow key responses
screen = curses.initscr() # get the curses screen window
curses.noecho() # turn off input echoing
curses.cbreak() # respond to keys immediately (don't wait for enter)
screen.keypad(True) # map arrow keys to special values
# set arrow key delta
deltaPan=1.0
deltaTilt=1.0
picNum = 1 # Initialise picture number
try:
while True:
char = self.screen.getch()
if char == ord('q'):
#if q is pressed quit
break
if char == ord('p'):
#if p is pressed take a photo!
self.camera.capture('image%s.jpg' % self.picNum)
picNum = picNum 1
screen.addstr(0, 0, 'picture taken! ')
elif char == curses.KEY_RIGHT:
screen.addstr(0, 0, 'right ')
if (b - deltaTilt ) > -90:
b = b - deltaTilt
msg.pan = b
self.publisher_.publish(msg)
time.sleep(0.005)
elif char == curses.KEY_LEFT:
screen.addstr(0, 0, 'left ')
if (b deltaTilt) < 90:
b = b deltaTilt
msg.pan = b
self.publisher_.publish(msg)
time.sleep(0.005)
elif char == curses.KEY_DOWN:
screen.addstr(0, 0, 'down ')
if (a deltaPan) < 90:
a = a deltaPan
msg.tilt = a
self.publisher_.publish(msg)
time.sleep(0.005)
elif char == curses.KEY_UP:
screen.addstr(0, 0, 'up ')
if (a - deltaPan) > -90:
a = a - deltaPan
msg.tilt = a
self.publisher_.publish(msg)
time.sleep(0.005)
finally:
# shut down cleanly
curses.nocbreak(); screen.keypad(0); curses.echo()
curses.endwin()
def main(args=None): rclpy.init(args=args)
node = KeyBoardPressPanTilt()
node.get_logger().info("Starting KeyBoard Press node, shut down with CTRL-C")
rclpy.spin(node)
ProcessActiveKeyBoardPress()
node.destroy_node()
rclpy.shutdown()
if name == 'main': main()
import sys import rclpy from rclpy.node import Node from pypantilt.panandtiltmove import PanAndTiltMove
from tutorial_interfaces.msg import PanAndTilt
class PanAndTiltServer(Node):
def __init__(self):
super().__init__('Pan_And_Tilt_Server')
self._pan_and_tilt_move_object = PanAndTiltMove()
self.pan_and_tilt_sub = self.create_subscription(
PanAndTilt,
'pan_and_tilt',
self.pan_and_tilt_callback,
10)
self.pan_and_tilt_sub # unused variable warning
def pan_and_tilt_callback(self, msg):
self.get_logger().info("Received PandAndTilt New msg: " + str(msg))
self._pan_and_tilt_move_object.move_to_pitch_yaw(yaw=msg.pan, pitch=msg.tilt)
def main(args=None): rclpy.init(args=args)
node = PanAndTiltServer()
node.get_logger().info("Starting server node, shut down with CTRL-C")
rclpy.spin(node)
node.destroy_node()
rclpy.shutdown()
if name == 'main': main()
Asked by zahidr on 2023-07-21 15:54:13 UTC
Comments