Tutorial 2: Reading data from a break beam sensor
This tutorial shows how to subscribe to a break beam sensor topic to count parts on the conveyor belt.
Starting the environment for Tutorial 2
To start the environment, use this command:
ros2 launch ariac_gazebo ariac.launch.py competitor_pkg:=ariac_tutorials trial_name:=tutorial dev_mode:=True
Running tutorial 2
To start tutorial 2, open a new terminal and use this command:
ros2 launch ariac_tutorials tutorial.launch.py tutorial:=2
Expected output of tutorial 2
Code explanation for Tutorial 2
This is the node used for tutorial 2. The functions from competition_interface.py which are used are highlighted.
#!/usr/bin/env python3
import rclpy
import threading
from rclpy.executors import MultiThreadedExecutor
from ariac_tutorials.competition_interface import CompetitionInterface
def main(args=None):
rclpy.init(args=args)
interface = CompetitionInterface(enable_moveit=False)
executor = MultiThreadedExecutor()
executor.add_node(interface)
spin_thread = threading.Thread(target=executor.spin)
spin_thread.start()
interface.start_competition()
count = 0
while rclpy.ok():
try:
if interface.conveyor_part_count > count:
interface.get_logger().info(f"Part detected on conveyor. Count: {interface.conveyor_part_count}")
count+= 1
except KeyboardInterrupt:
break
interface.end_competition()
spin_thread.join()
if __name__ == '__main__':
main()
The purpose of this tutorial is to log the number of parts that have been counted by the break beam sensor over the course of the competition. Whenever the break beam sensor senses a part and that part has not already been counted, interface._breakbeam_cb
is run and interface.conveyor_part_count
is incremented by one. Inside the node, whenever interface.conveyor_part_count
is increased, the count is logged.