Here's a sample Python code for a LEGO EV3 line follower using two color sensors and a PID (Proportional-Integral-Derivative) controller:
python
from ev3dev2.motor import MoveSteering, OUTPUT_B, OUTPUT_C
from ev3dev2.sensor.lego import ColorSensor
from time import sleep
# Initialize the motors
steering_drive = MoveSteering(OUTPUT_B, OUTPUT_C)
# Initialize the color sensors
left_sensor = ColorSensor('in1')
right_sensor = ColorSensor('in2')
# PID constants
Kp = 1.0 # Proportional gain
Ki = 0.0 # Integral gain
Kd = 0.0 # Derivative gain
# Initialize PID variables
integral = 0
previous_error = 0
# Target value for line following (assuming black line on white surface)
target_value = (left_sensor.reflected_light_intensity() + right_sensor.reflected_light_intensity()) / 2
while True:
# Read the reflected light intensity from the color sensors
left_value = left_sensor.reflected_light_intensity()
right_value = right_sensor.reflected_light_intensity()
# Calculate the error
error = target_value - (left_value + right_value) / 2
# Calculate the proportional term
proportional = Kp * error
# Calculate the integral term
integral += error
integral = max(min(integral, 100), -100) # Limit the integral term to prevent windup
integral_term = Ki * integral
# Calculate the derivative term
derivative = error - previous_error
derivative_term = Kd * derivative
# Calculate the steering value
steering = proportional + integral_term + derivative_term
steering = max(min(steering, 100), -100) # Limit the steering value to the range [-100, 100]
# Set the motor speed and steering
steering_drive.on(steering, 20) # Adjust the speed (20) as needed
# Update the previous error for the next iteration
previous_error = error
# Sleep for a short duration to control the loop speed
sleep(0.01)
This code uses the ev3dev2 library to control the LEGO EV3 motors and sensors. Here's how it works:
The code initializes the motors and color sensors.
It defines the PID constants (Kp, Ki, Kd) and initializes the PID variables (integral, previous_error).
The target value for line following is set as the average of the initial reflected light intensity from both sensors.
Inside the main loop, the code reads the reflected light intensity from the color sensors.
It calculates the error by subtracting the average sensor value from the target value.
The proportional term is calculated by multiplying the error by the proportional gain (Kp).
The integral term is calculated by accumulating the error over time and multiplying it by the integral gain (Ki). The integral term is limited to prevent windup.
The derivative term is calculated by taking the difference between the current error and the previous error, and multiplying it by the derivative gain (Kd).
The steering value is calculated by summing the proportional, integral, and derivative terms. The steering value is limited to the range [-100, 100].
The motor speed and steering are set using the on() method of the MoveSteering object.
The previous error is updated for the next iteration.
The loop sleeps for a short duration (0.01 seconds) to control the loop speed.
Note: Make sure to adjust the PID constants (Kp, Ki, Kd) and the motor speed to achieve optimal line following performance based on your specific setup and environment.