Line Follower Robot

by xprobyhimself in Circuits > Robots

965 Views, 11 Favorites, 0 Comments

Line Follower Robot

20240306_200042.jpg
Line Follower Robot2
20240305_222411.jpg
Screenshot 2024-08-25 161801.png
20240306_200125.jpg

I’m excited to share my PID line follower robot, designed with 7 IR sensors, capable of following any line with precision at speeds of up to 2 m/s. I built this robot to compete in a national competition between universities, where it demonstrated both speed and accuracy. While this design has proven effective, there's always room for improvement. Now, I’m offering you the steps to create your own version and take your robotics skills to the next level. Whether you're aiming to compete or simply push the boundaries of your engineering capabilities, this project is a great way to challenge yourself and showcase your talent.

(The videos in the project showcase the robot's functionality during testing. While these clips highlight its early performance, the final version has seen significant improvements. A real map is planned for a more polished demonstration.)

Supplies

20240306_200125.jpg
efsfsf.jpg
20240826_141508.jpg
20240826_141247.jpg
20240826_141603.jpg
20240404_194005.jpg
  1. Arduino Mega (or any compatible microcontroller)
  2. DC Motors × 2 (I repurposed toy motors, but any suitable DC motors will work)
  3. IR Reflective Sensors TCRT5000 × 7 (alternative IR sensors can also be used)
  4. Jumper Wires for all connections
  5. 3D Printer for custom parts
  6. Motor Driver L298N (or any compatible motor driver)
  7. Hot Glue for securing components
  8. 7V+ Power Supply (I used two 3.7V LiPo batteries)

3D Printing

20240305_183448.jpg
20240305_190930.jpg
20230301_172515.jpg
20230301_140905.jpg
last.png


Download the 3D Model Files

Prepare the 3D Printer

  1. Check Printer Settings: Ensure your 3D printer is set up and ready with the appropriate filament (e.g., PLA or ABS).
  2. Preheat the Printer: Preheat the printer to the correct temperature for your chosen filament.

Load Files into Slicing Software

  1. Import the Models: Open your slicing software (e.g., Cura, PrusaSlicer) and import the body part files.
  2. Arrange the Models: Position the body parts optimally on the print bed to maximize space.

Configure Print Settings

  1. Set Layer Height: Choose a layer height that balances speed and detail (e.g., 0.2mm).
  2. Adjust Infill Density: Set the infill percentage (20-30%) based on your desired strength and material usage.
  3. Disable Supports: No supports needed.

Start 3D Printing

  1. Begin Printing: Once everything is set, start the printing process. Monitor the first few layers to ensure proper adhesion and print quality.


Assembling

20230301_202029.jpg
20230301_113950.jpg
20230301_154236.jpg
20230302_134733.jpg
20230302_121554.jpg

1. Disassemble the Toy:

  1. Extract Motors and Wheels: Begin by carefully taking apart the toy vehicle. Your goal is to retrieve the motors and wheels without damaging them.
  2. Tools: Use small screwdrivers or prying tools to disassemble the toy. If necessary, cut away plastic casings around the motors with a sharp blade or small saw, being careful not to damage the motors or their wiring.
  3. Label Wires: If the motors have attached wires, label them to ensure correct reconnection later.

2. Prepare the Main Body:

  1. Mark Motor Placement: Before attaching anything, mark the positions where the motors and wheels will be placed to ensure proper alignment.

3. Attach Motors and Wheels:

  1. Apply Glue: Apply hot glue generously around the motor mounts and press them onto the main body. Hold them in place until the glue sets. Ensure the motors are aligned so that the wheels will be parallel to each other.
  2. Wire Clearance: As you position the motors, ensure there is enough space for the wires to pass through the body without being pinched or strained. You may need to pass them in small notches or holes in the body to route the wires safely.

4. Attach the Front Section:

  1. Lower Front Placement: The front section of the robot, often housing the sensors, should be positioned slightly lower than the main body. This setup improves ground clearance, allowing the sensors to be closer to the surface and more effective at detecting lines.
  2. Use a Spacer: If needed, use an object or spacer between the front section and the main body to achieve the desired height. This could be a piece of plastic, wood
  3. Glue in Place: Secure the front section with hot glue, ensuring it is firmly attached and correctly angled. Double-check that the sensor mounting points are level and that the sensors will have an unobstructed view of the surface.

5. Sensor Mounting:

  1. Position Sensors: Place the sensors on the front section, arranging them in a straight line or slightly angled to improve detection accuracy on curves.
  2. Secure with Glue: Use hot glue to fix the sensors in place. Ensure they are securely attached but avoid excess glue that might interfere with their operation.
  3. Check Alignment: After gluing, check that all sensors are aligned and at an even height relative to the ground. Adjust if necessary before the glue sets.

6. Use the 3D Model as a Guide:

  1. Follow the 3D Model: use the 3D model of the robot as a reference during assembly. The model will help ensure that all components are positioned correctly and that the robot’s structure is balanced.
  2. Check Dimensions: Compare the physical assembly to the 3D model, especially focusing on the spacing and alignment of components like the motors, wheels, and sensors.
  3. Adjust as Needed: If the assembled robot differs from the 3D model, make necessary adjustments to align with the design specifications.

7. Final Checks and Adjustments:

  1. Test the Mechanisms: Before finalizing the assembly, manually rotate the wheels to ensure they spin freely and are not obstructed by the body or wiring.
  2. Wire Management: Route all wires neatly, securing them with small zip ties or additional glue if needed. This prevents them from getting tangled or caught in moving parts.
  3. Component Stability: Ensure all glued components are stable and secure. Reapply glue if any part feels loose.

8. Power and Electronics:

  1. Connect Electronics: Once the mechanical assembly is complete, connect the motors to the motor driver, attach the sensors to the control board, and connect the power supply.

By following these steps and using the 3D model as a guide, you'll ensure that your robot is well-constructed, with all components properly aligned and securely attached. This approach will contribute to the robot's performance in following lines efficiently.

Wiring

20230301_200603.jpg
Screenshot 2024-04-13 165943.png
20240306_200104.jpg

Wiring Steps:

1. Connect the Motor Driver to the Motors:

  1. Motor 1:
  2. Connect one terminal of Motor 1 to the OUT1 pin on the motor driver.
  3. Connect the other terminal of Motor 1 to the OUT2 pin on the motor driver.
  4. Motor 2:
  5. Connect one terminal of Motor 2 to the OUT3 pin on the motor driver.
  6. Connect the other terminal of Motor 2 to the OUT4 pin on the motor driver.

2. Connect the Motor Driver to the Arduino:

  1. Motor 1 Control (ENA and IN Pins):
  2. Connect ENA on the motor driver to pin 3 on the Arduino.
  3. Connect IN1 on the motor driver to pin 15 on the Arduino.
  4. Connect IN2 on the motor driver to pin 17 on the Arduino.
  5. Motor 2 Control (ENB and IN Pins):
  6. Connect ENB on the motor driver to pin 2 on the Arduino.
  7. Connect IN3 on the motor driver to pin 18 on the Arduino.
  8. Connect IN4 on the motor driver to pin 19 on the Arduino.

3. Power Connections:

  1. Battery:
  2. Connect the positive terminal of the 9V battery to the VCC pin on the motor driver.
  3. Connect the negative terminal of the 9V battery to the GND pin on the motor driver.
  4. Motor Driver Power:
  5. Connect the GND pin from the motor driver to the GND pin on the Arduino.

4. Connect the Line Sensors to the Arduino:

  1. Sensor Array (5 Sensors):
  2. Connect the OUT pin of each sensor to the following analog pins on the Arduino:
  3. Sensor 1 (far left): A0
  4. Sensor 2: A1
  5. Sensor 3 (center): A2
  6. Sensor 4: A3
  7. Sensor 5 (far right): A4
  8. Connect the VCC pin of each sensor to the 5V pin on the Arduino.
  9. Connect the GND pin of each sensor to the GND pin on the Arduino.
  10. Front Sensor:
  11. Connect the OUT pin of the front sensor to the A5 pin on the Arduino.
  12. Connect the VCC and GND pins as described for the sensor array.
  13. Back Sensor:
  14. Connect the OUT pin of the back sensor to the A6 pin on the Arduino.
  15. Connect the VCC and GND pins similarly.

5. Additional Digital Sensors:

  1. Right Side Sensor:
  2. Connect the OUT pin of the right sensor to the digital pin 24 on the Arduino.
  3. Left Side Sensor:
  4. Connect the OUT pin of the left sensor to the digital pin 22 on the Arduino.
  5. Power the sensors using the 5V and GND connections from the Arduino.

Final Checks:

  1. Ensure All Connections Are Secure: Double-check that each wire is properly connected and that the pins correspond to the correct assignment in the code.
  2. Test the Wiring: Before running the full code, verify that each motor responds to the control inputs and that the sensors are providing the expected signals.


Programing / Testing

Screenshot 2024-08-25 215017.png
LineFollowerRobot2-test


Code Explanation:


Setup and Initialization:

  1. Motor Control Pins: The motor driver is controlled through specific pins (ENA, IN1, IN2, ENB, IN3, IN4) assigned to the motors.
  2. Sensor Pins: Seven sensors are used to detect the line and surroundings (sensorPins[5], f, b, r, l).
  3. PID Constants: These values (Kp, Kd, Ki) are used to fine-tune the robot's movement, ensuring it follows the line accurately change it based on your robot behavior.
  4. Variables: Several variables are initialized to store sensor readings, PID error calculations, and motor speeds.

Calibration:

  1. The calibrate() function runs the robot in a circle to detect the minimum and maximum values of the sensors, determining the threshold for line detection.

Main Loop:

  1. Initial Calibration and Movement: Once, the robot calibrates itself, it makes a slight turn to align with the line and then starts moving forward.
  2. Sensor Readings: The robot continuously reads the sensors to determine the position of the line.
  3. PID Calculation: The error between the desired position and the actual position is calculated. The PID controller uses this error to adjust the speed of the motors (leftSpeed, rightSpeed) to keep the robot on the line.
  4. Movement Logic: Depending on sensor inputs:
  5. Forward: If the robot is on track, it moves forward.
  6. Turns: If the robot detects a turn, it adjusts its direction (left or right) accordingly.
  7. Special Cases: There are also provisions for handling complex situations, such as sharp turns or U-turns, but some are commented out.

Functions:

  1. Movement Functions (forward, turnRight, turnLeft): These control the motors based on the speed and direction needed.
  2. Error Calculation (calculateError): Computes the error by averaging sensor values to keep the robot centered on the line.

After uploading the code to your line-following robot, it's crucial to test and fine-tune the code based on how the robot behaves in real-world conditions.

1. Initial Testing:

Once the code is uploaded, observe the robot's behavior as it follows the line:

  1. Does the robot stay on the line? Check if the robot maintains a consistent path along the line.
  2. How smooth is the movement? Notice if the robot is jerky or if it smoothly follows the curve of the line.
  3. Speed vs. Control: Assess whether the robot can maintain its speed without losing track of the line.

2. Analyzing the Behavior:

Based on your observations, you can identify areas that might need adjustment:

  1. Overshooting or Undershooting Turns: If the robot overshoots or undershoots turns, the PID constants (Kp, Ki, Kd) may need to be adjusted.
  2. Oscillations: If the robot wobbles or oscillates while following a straight line, it could be due to the Kd value. Decreasing Kd might help reduce this effect.
  3. Slow Response: If the robot is too slow to correct its path, increasing Kp can make the robot more responsive.
  4. Integral Windup: If the robot’s corrections become too aggressive over time, you might need to adjust the integral limit or Ki value to prevent integral windup.

3. Fine-Tuning the PID Constants:

  1. Proportional Control (Kp): Start by adjusting Kp. This controls how aggressively the robot reacts to the error. If Kp is too high, the robot might oscillate or become unstable. If it's too low, the robot might be too sluggish.
  2. Integral Control (Ki): Fine-tune Ki to address any steady-state errors where the robot consistently drifts off the line. Be cautious, as too high of a Ki can cause the robot to overcorrect and oscillate.
  3. Derivative Control (Kd): Kd helps to dampen the oscillations by predicting future errors. Adjust this value if your robot is too jittery or oscillates when following curves.

4. Adjusting Threshold Values:

  1. During testing, if the robot struggles to detect the line correctly, revisit the threshold values set during the calibrate() function. You might need to run the calibration multiple times under different lighting conditions to ensure reliable detection.

5. Real-World Testing:

  1. Track Complexity: Test the robot on tracks with different complexities—sharp turns, intersections, or varying line widths. This will help ensure that the robot performs well in various scenarios.
  2. Speed Adjustments: If the robot performs well at lower speeds but struggles at higher speeds, you may need to adjust the motor speeds or further refine the PID constants.
  3. Environmental Factors: Test under different lighting conditions and surface textures to ensure the robot can consistently detect the line and adjust its path accordingly.

6. Iteration and Refinement:

  1. Document Changes: Keep track of the changes you make to the PID constants, thresholds, and other parameters. This will help you understand what adjustments lead to better performance.
  2. Iterate: Testing and tweaking should be an iterative process. After each adjustment, retest the robot and refine as necessary.
  3. Hardware Considerations: If the robot still struggles despite code adjustments, consider whether hardware changes (like repositioning sensors or adjusting motor power) might be necessary.

7. Final Optimization:

  1. Once the robot consistently follows the line with the desired speed and accuracy, you can finalize the code.
  2. Test the robot in different environments and with various track layouts to ensure it’s ready for competition.

8. Prepare for the Unexpected:

  1. Always expect the unexpected in competitions. Prepare to make quick adjustments if the track conditions differ from your testing environment.





Downloads