Control Multiple Finch Robots Using Python
This document provides an example of how to control multiple Finch Robots simultaneously using Python. Below are step-by-step instructions and the relevant code snippets.
1. Install the Python Library
First, ensure you have the Finch library installed. Run the following command:
pip install finch
2. Pair All Finch Robots
Pair each Finch Robot with your computer via Bluetooth. Ensure all robots are recognized as separate devices.
3. Python Code Example
Basic Example
The following Python code demonstrates controlling multiple Finch Robots:
from finch import Finch
import time
# Create Finch objects for each robot
robot1 = Finch()
robot2 = Finch()
robot3 = Finch()
# Move each robot in different directions
robot1.wheels(1.0, 0.5) # Robot 1 moves forward and turns slightly
robot2.wheels(-1.0, -1.0) # Robot 2 moves backward
robot3.wheels(0.5, 1.0) # Robot 3 turns in place
# Add a delay to allow them to move
time.sleep(2)
# Stop all robots
robot1.wheels(0, 0)
robot2.wheels(0, 0)
robot3.wheels(0, 0)
# Close connections
robot1.close()
robot2.close()
robot3.close()
Using Threads for Simultaneous Control
To execute commands simultaneously, you can use threading. Here is an example:
import threading
from finch import Finch
import time
# Define a function to control a robot
def control_robot(robot, speed_left, speed_right, duration):
robot.wheels(speed_left, speed_right)
time.sleep(duration)
robot.wheels(0, 0) # Stop the robot
# Create Finch objects
robot1 = Finch()
robot2 = Finch()
# Create threads for each robot
thread1 = threading.Thread(target=control_robot, args=(robot1, 1.0, 0.5, 2))
thread2 = threading.Thread(target=control_robot, args=(robot2, 0.5, 1.0, 2))
# Start the threads
thread1.start()
thread2.start()
# Wait for threads to complete
thread1.join()
thread2.join()
# Close the Finch objects
robot1.close()
robot2.close()
4. Considerations
- Bluetooth Limitations: Ensure your computer supports multiple Bluetooth connections.
- Performance: Optimize your Python program for handling simultaneous communication.
- Testing: Test each robot individually before controlling them together.
With these steps, you can control multiple Finch Robots effectively. Modify the code as needed for your specific use case!