Skip to content

Commit 55e23ba

Browse files
committed
fix missing python code and a few typos
1 parent 23c88bb commit 55e23ba

1 file changed

Lines changed: 30 additions & 3 deletions

File tree

README.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,9 +1211,34 @@ ros2 run bme_gazebo_sensors_py chase_the_ball
12111211

12121212
As you can see the `process_image()` function is now just a placeholder and we'll start implementing more features within this function later, but first let's extend the node with better handling of the subscription to the image topic. If `process_image()` will take more time to run it will also block the execution of `rclpy.spin_once(self, timeout_sec=0.05)` that is needed to trigger the `image_callback()`. So let's move the spin functionality to a separate thread:
12131213

1214-
Let's change the `__init__()` function first:
1214+
Let's change the `__init__()` constructor first:
12151215
```python
1216+
def __init__(self):
1217+
super().__init__('image_subscriber')
1218+
1219+
# Create a subscriber with a queue size of 1 to only keep the last frame
1220+
self.subscription = self.create_subscription(
1221+
Image,
1222+
'camera/image',
1223+
self.image_callback,
1224+
1 # Queue size of 1
1225+
)
12161226

1227+
self.publisher = self.create_publisher(Twist, 'cmd_vel', 10)
1228+
1229+
# Initialize CvBridge
1230+
self.bridge = CvBridge()
1231+
1232+
# Variable to store the latest frame
1233+
self.latest_frame = None
1234+
self.frame_lock = threading.Lock() # Lock to ensure thread safety
1235+
1236+
# Flag to control the display loop
1237+
self.running = True
1238+
1239+
# Start a separate thread for spinning (to ensure image_callback keeps receiving new frames)
1240+
self.spin_thread = threading.Thread(target=self.spin_thread_func)
1241+
self.spin_thread.start()
12171242
```
12181243

12191244
and then add the `spin_thread_func()` function and also implement a thread lock in `image_callback()`:
@@ -1231,7 +1256,7 @@ and then add the `spin_thread_func()` function and also implement a thread lock
12311256
self.latest_frame = self.bridge.imgmsg_to_cv2(msg, "bgr8")
12321257
```
12331258

1234-
Obviously we don't need `rclpy.spin_once(self, timeout_sec=0.05)` anymore within `display_image()`!
1259+
Obviously, we don't need `rclpy.spin_once(self, timeout_sec=0.05)` anymore within `display_image()`!
12351260

12361261
Let's add a `stop()` function, too, to join the therads when we stop the node:
12371262

@@ -1258,7 +1283,9 @@ def main(args=None):
12581283
rclpy.shutdown()
12591284
```
12601285

1261-
Let's rebuild the workspace and try the node! We shouldn't see any difference at this point, but the image callback is triggered by a separate thread now!
1286+
Let's rebuild the workspace and try the node! We shouldn't see any difference at this point, but the image callback is triggered on a separate thread now!
1287+
1288+
---
12621289

12631290
Now let's work on the image processing:
12641291

0 commit comments

Comments
 (0)