pi_cam_node.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 """
3 Pi Camera ROS Node
4 
5 Written by: Zahi Kakish (zmk5)
6 License: BSD 3-Clause
7 
8 """
9 import sys
10 import rospy
11 import picamera
12 import picamera.array
13 from sensor_msgs.msg import Image
14 from cv_bridge import CvBridge
15 
16 
17 def main():
18  """
19  Sample Pi Camera ROS Node
20 
21  This simple ROS node script takes images from a Pi Camera attached to a
22  Pheeno's Raspberry Pi (or other Robot that uses the Pi Camera) and
23  Publishes the images as a ROS topic.
24 
25  NOTE: This method of publishing images is very computationally intensive,
26  especially on a Raspberry Pi. Use when needed or try to do image processing
27  within a node and only publish the results. For example, checking if a
28  color is located within an image and then publishing True or False instead
29  of the image.
30 
31  """
32  # Select camera for use.
33  with picamera.PiCamera() as camera:
34  with picamera.array.PiRGBArray(camera) as stream:
35  # Initialize Node
36  sys.stdout.write("Initializing 'pi_cam' node...")
37  rospy.init_node("pi_cam")
38  sys.stdout.write("done!\n")
39 
40  # Create Publisher to publish pi_cam images.
41  sys.stdout.write("Creating Publisher...")
42  pub = rospy.Publisher("/pi_cam/image_raw",
43  Image,
44  queue_size=307200)
45  sys.stdout.write("done!\n")
46 
47  # Other important variables
48  camera.resolution = (640, 480)
49  bridge = CvBridge()
50  rate = rospy.Rate(10)
51 
52  # Starting node
53  sys.stdout.write("Starting node!\n")
54  while not rospy.is_shutdown():
55  try:
56  # Capture and publish
57  camera.capture(stream, 'bgr', use_video_port=True)
58  image_message = bridge.cv2_to_imgmsg(
59  stream.array, encoding="bgr8")
60  pub.publish(image_message)
61 
62  # Reset stream for next capture
63  stream.truncate(0)
64  rate.sleep()
65 
66  except KeyboardInterrupt:
67  sys.exit("Closing node!\n")
68 
69 
70 
71 if __name__ == "__main__":
72  try:
73  main()
74 
75  except rospy.ROSInterruptException:
76  pass
77 
78  finally:
79  rospy.loginfo("Exiting 'pi_cam' node.")
def main()
Definition: pi_cam_node.py:17


pheeno_ros
Author(s): Zahi Kakish, Sean Wilson
autogenerated on Mon Jun 10 2019 14:10:48