1
$\begingroup$

I'm trying to implement a PID based line follower. The robot has 2 line sensors with ranges from 0-200, with 0 being black and 200 being white. I'm trying to follow a black line.

I'm struggling to get the robot to do anything but erratically turn and confuse itself on the spot, getting wildly out of control. I've tried adjusting the setpoint value, the operators in the equations and the kp/kd values to no avail.

Is there something wrong with the algorithm I'm using? (I'm a beginner to this so it's likely)

Thank You

My code is below:

enter image description here

$\endgroup$

1 Answer 1

1
$\begingroup$

You seem to only be using a PD controller, but that should be sufficient. The biggest typo I could spot is that you multiple instead of add the proportional and derivative terms. Namely it should be:

motorSpeed = kp*error + kd*(error - lastError)

Usually one would also divide the error difference by the time that has passed, however this would only change the effective kd gain by a constant factor, assuming that the time between samples is constant. Also normally the error would defined as setpoint minus position, which effectively would multiply motorSpeed by minus one.

$\endgroup$
1
  • 1
    $\begingroup$ Thank you, after fixing the operand it was still doing the same, it turns out the issue was the kp and kd values just being far too high. $\endgroup$ Commented Jan 22, 2019 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.