going straight not stable
Opened this issue · 3 comments
Hello i'm trying to use your code in python but the robot not going straight stable
moving left -> right -> left -> right ......etc
my code
import Motion
from motors import Motors
MOTOR_NOMINAL = 50
MOTOR_DELTAMAX = 50
MOTORS = None
def motor_forward(delta):
if delta > +MOTOR_DELTAMAX:
delta = +MOTOR_DELTAMAX
if delta < -MOTOR_DELTAMAX:
delta = -MOTOR_DELTAMAX
RIGHT_SPEED = MOTOR_NOMINAL-delta
LEFT_SPEED = MOTOR_NOMINAL+delta
#print(MOTOR_NOMINAL-delta)
#print(MOTOR_NOMINAL+delta)
# (right, right, left, left)
MOTORS.forword(RIGHT_SPEED,RIGHT_SPEED,LEFT_SPEED,LEFT_SPEED)
# PID ===================================================
PID_K_p = 30.0
PID_K_i = 0.2
PID_K_d = 1.0
i_input = float(0.0)
d_last = float(0.0)
def constrain(val, min_val, max_val):
return min(max_val, max(min_val, val))
def pid(error):
p_input = float(0.0)
d_input = float(0.0)
p_input = error
global i_input, d_last
i_input= constrain(i_input + error, -50, +50)
d_input = error-d_last
d_last = error
return p_input * PID_K_p + i_input * PID_K_i + d_input * PID_K_d
def motion_callback(current, tareget):
steer= pid( tareget - current )
#print("steer = %s" % steer)
motor_forward(steer)
def start(motors):
global MOTORS
MOTORS = motors
Motion.start(motion_callback)
Hi @ahmedAlmasri, I had the same :-)
In my first runs I had kp positive, but the other two (ki, kd) at 0. The car went pretty straight, but had a small drift to the right.
This is where ki helps. So I set ki to 5 or so, and the car went zig zag. The more I decreased ki, the less it zig zagged.
So, I would suggest you try ki 0 first. Then set it to a bigger value, but half that after each run that you believe is still too much zig zag.
This is tuning the PID, and that is hard.
Good luck.
@maarten-pennings Thank you for quick answer
but i have a question what's the best solution to make a robot going straight , i'm tried encoder and mpu but all it do not a get a perfect result do you have any idea ?
and i will try your answer :)
Navigating ("driving straight") is simply a hard problem. Best would be to have (a) beacon(s): absolute references.
The MPU is semi ok because it is using semi absolute references. But it drifts.
The encoders are even worse. They accurately track the rotations of the motor axle, but the wheel diameters vary and there is wheel to floor slip. So not good for driving straight.
Irrespective of the sensor you are using, you need a control mechanism for the motors. The PID feedback loop is fairly adequate and popular.