XRP Line-Following Robot Using an AI Vision Camera (feat. HuskyLens)
by christyc2 in Circuits > Robots
39 Views, 0 Favorites, 0 Comments
XRP Line-Following Robot Using an AI Vision Camera (feat. HuskyLens)
Build a line-following robot using an XRP robot and a DFRobot HuskyLens (SEN0305) AI vision sensor. The HuskyLens provides visual data, and the XRP board (with the RP2350 microcontroller) reads this data and drives the motors using PID controllers for stable line tracking.
Learning Objectives:
- How to read sensor data from the HuskyLens over I²C
- How differential-drive robots steer
- How PID control stabilizes robot motion
- How to tune a real controller through experimentation
You’ll end with a robot that:
- Detects and tracks a line using computer vision
- Computes steering corrections using PID control
- Adjusts motor speeds to follow the path accurately
All code uploaded to GitHub. Feel free to let us know if you encounter any issues!
Supplies
- HuskyLens AI Camera (SEN0305)
- An XRP robot or kit. If you do not have one yet, you can purchase the entire kit through Sparkfun Electronics. Alternatively, you would buy only the electronics through Sparkfun and 3D print your own XRP chassis!
- We'll be using:
- Two XRP DC motors
- XRP board with an RP2350 microcontroller
- Battery pack (4 AA batteries)
- Any XRP Chassis with rails for mounting the camera
- Quick-connect jumper cables (for I2C and motors)
- USB cable for flashing/programming
- Some sort of line path (printed or made with tape)
- A mount for the HuskyLens to attach to the XRP rails (I have attached the one I used, but feel free to design your own!)
Downloads
Setup
- Install Thonny, or use the browser-based editor at https://xrpcode.wpi.edu/ (MicroPython).
- Update the HuskyLens firmware following these instructions (optional): https://wiki.dfrobot.com/HUSKYLENS_V1.0_SKU_SEN0305_SEN0336#4.%20Upgrade%20Firmware
Mechanical Mounting
- Prepare your assembled XRP chassis
- Attach the HuskyLens to the XRP rails using your HuskyLens mount
- Angle the camera downward so it clearly sees the floor 1-2 inches ahead of the robot.
- Make sure nothing blocks the camera’s view (wires, battery pack, etc.).
Tip: A higher camera angle sees more of the line, but the data is noisier. A lower camera angle sees less of the line, but the data is more consistent.
Wiring the HuskyLens
I²C Wiring:
- HuskyLens SDA → XRP SDA
- HuskyLens SCL → XRP SCL
- HuskyLens VCC → 5V
- HuskyLens GND → GND
Teach the HuskyLens the Line
- Power on the HuskyLens.
- Rotate the dial to Line Tracking mode. Note: The HuskyLens has a built-in Line Tracking mode and can return path information (x and y coordinates of the arrow head and tail) to a microcontroller over I²C. For more information on the HuskyLens, refer to the DFRobot wiki.
- Put the robot over the line.
- Aim the crosshair at the line, centered on the screen.
- Press the top-right “Learn” button until the line becomes highlighted on the display.
Communication With the XRP Board
- Design a custom I2C protocol, name it i2c.py, and save it in the root of your directory. An example code is attached below.
- Request frames from the HuskyLens and print the detected line’s x- and y-positions to verify communication. An example main.py is also attached below.
- You should see the values change as you move the robot side-to-side over the line.
If you see no data:
- Re-check that the wiring is correct (Try flipping SDA and SCL)
- Make sure the HuskyLens is in Line Tracking mode
- Relearn the line sample
Implement a PID Controller
- Set the desired arrow position and orientation. We want the arrow to:
- Stay centered horizontally on the screen
- Point forward (upward)
- The XRP library includes built-in differential drive and PID controller modules. Initialize PID controllers for x- and y-direction steering.
3. Convert arrow position data into error values for the PID controllers. Positive error means the arrow is too far to the left, so the robot should turn right. Negative error means the arrow is too far to the right, so the robot should turn left.
4. Write a control loop that drives the XRP robot based on the PID outputs and line position
I have attached the full main.py (with comments), ready for running the XRP robot.
Downloads
Drive Test and PID Tuning (Low Speed)
- Place the robot on the line.
- Start with Ki = 0, Kd = 0, and Kp = 0.1.
- Run the main script and expect to spend 10–20 minutes tuning.
- Observe the robot’s behavior:
- If it does nothing → communication error
- If it veers sharply → Kp too high
- If it oscillates widely → increase Kd or lower Kp
- If it drifts permanently to one side → increase Ki slightly
- [TODO: Embed sample videos of the robot with varying PID values]
Tuning Process:
- Increase Kp slowly until the robot responds and begins oscillating around the line.
- Reduce Kp slightly.
- Add a small Kd to damp oscillations and improve responsiveness.
- Add a small Ki only if the robot shows steady bias (for example, due to imperfect mounting or motor asymmetry).
- Keep the base speed low while tuning. Increase speed afterward and re-tune minimally.
Congratulations on completing the challenge! Feel free to extend your project and share it with the community!