Advertisement
microrobotics

Pololu 3Pi Robot follow a line

Apr 21st, 2023
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Here's an example code for making the Pololu 3Pi Robot follow a line using an Arduino:
  3.  
  4. Note that this code assumes a black line on a white background. If your line is white on a black background, you will need to modify the read_line function call to use IR_EMITTERS_OFF instead of IR_EMITTERS_ON. Additionally, you may need to adjust the calibration values and the proportional_control function to optimize the line following behavior for your specific line.
  5. */
  6.  
  7. #include <Pololu3pi.h>
  8.  
  9. void setup() {
  10.   // Set up the 3Pi Robot
  11.   initialize();
  12.  
  13.   // Set the maximum speed for the motors
  14.   set_max_speed(60);
  15.  
  16.   // Calibrate the sensors for line following
  17.   calibrate_line_sensors(IR_EMITTERS_ON);
  18. }
  19.  
  20. void loop() {
  21.   // Read the sensor values and determine the motor speeds
  22.   unsigned int sensors[5];
  23.   read_line(sensors, IR_EMITTERS_ON);
  24.   int position = line_position(sensors);
  25.   int speedDifference = proportional_control(position);
  26.  
  27.   // Set the motor speeds based on the sensor readings
  28.   int leftSpeed = 60 + speedDifference;
  29.   int rightSpeed = 60 - speedDifference;
  30.   set_motors(leftSpeed, rightSpeed);
  31. }
  32.  
  33. // Calculate the proportional control signal based on the sensor readings
  34. int proportional_control(int position) {
  35.   int error = position - 2000; // Center position is 2000
  36.   int speedDifference = error / 4;
  37.   return speedDifference;
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement