This Arduino project provides a modularized approach for controlling a robot using PID-based line following with sensor feedback.
-
Attributes:
normal_speed
: Normal speed of the motors.min_speed
: Minimum allowable speed.max_speed
: Maximum allowable speed.- Pins for left and right motor control (
left_in1
,left_in2
,left_en
,right_in1
,right_in2
,right_en
).
-
Methods:
initialize()
: Sets up the motor control pins.set_motor(int in1, int in2, int en, int speed)
: Controls individual motors.get_direction(int speed)
: Determines motor direction.normalize_speed(int speed)
: Ensures speed stays within defined limits.drive(int speed_difference, bool debug)
: Drives the robot with a speed difference between left and right motors.
-
Attributes:
KP
: Proportional gain.KI
: Integral gain.KD
: Derivative gain.last_proportional
,integral
: Trackers for PID calculations.
-
Methods:
calculate_speed_difference(int proportional)
: Computes the speed difference using PID control.
-
Attributes:
threshold
: Threshold value for line detection.sensors_count
: Number of sensors.sensor_pins
: Array of pins for individual sensors.errors
: Array of error values corresponding to different line positions.previous_error
: Tracks the previous error value.
-
Methods:
calculate_error()
: Calculates the error based on sensor readings.on_line(int sensor_pin)
: Checks if a sensor is on the line.get_line_position()
: Determines the position of the line based on sensor readings.
User-configurable settings for threshold, sensor count, sensor pins, errors, motor speeds, PID gains, debugging, and delay time.
-
Initialize Components:
- Set up motor control and serial communication.
- Adjust PID and sensor settings.
-
Main Loop:
- Continuously calculate sensor error.
- Use PID controller to determine speed difference.
- Drive the motors accordingly.
-
Debugging:
- Optionally print error and speed difference values to Serial for debugging purposes.