car following white lane

hi,
im using chasis 4-wd famous car.

i have two sensors which detect white or black at the front:
one at front left
and one at front right

the white strip is around half of a4 paper, almost exactly at width of the car so a little bit to the right, the right sensor will detect black, or a little bit left, the left sensor will detect black.

im not sure how to do the algorithm, so far i made somthing like this:
assuming under 150 its white

if(right_sensor > WHITE_LIMIT) {
setSpeedLeftWheels(0);
setSpeedLeftWheels(curr_right_speed);
}

and the same way for the left sensor.

sometimes its work but its not look good that everytime it gets black detection , one side is stopping and waiting for the other to side to come.

thanks for suggestion,
i will mention its my first project so im not a proffesional :slight_smile:

Add some debug prints.

it dosen't look contious,
its like the car doing zig zag

See reply #1.
Without seeing code and a drawing of the vehicle and schematic, it's the best anyone can suggest.

constexpr double kp = 0.07;
constexpr double ki = 0.05;
constexpr double kd = 0.03;

int e_speed_left = 0;
int pv_speed_left = 0;
int pwm_left = 0;
int e_speed_left_sum = 0;
int e_speed_left_pre = 0;

int e_speed_right = 0;
int pv_speed_right = 0;
int pwm_right = 0;
int e_speed_right_sum = 0;
int e_speed_right_pre = 0;

void loop()
{

car.readWhiteSensors();

if (car.curr_left_white > white_limit || car.curr_right_white > white_limit) {
car.fix();
}
else {
car.balanceSpeed();
}

}

void CarClass::balanceSpeed()
{

e_speed_left = set_speed - current_speed_left;
e_speed_right = set_speed - current_speed_right;

pwm_left = e_speed_left * kp + e_speed_left_sum * ki + (e_speed_left - e_speed_left_pre)*kd;
pwm_right = e_speed_right * kp + e_speed_right_sum * ki + (e_speed_right - e_speed_right_pre)*kd;

e_speed_left_pre = e_speed_left;
e_speed_right_pre = e_speed_right;

e_speed_left_sum += e_speed_left;
e_speed_right_sum += e_speed_right;

if (e_speed_left_sum >4000) e_speed_left_sum = 4000;
if (e_speed_right_sum >4000) e_speed_right_sum = 4000;

if (e_speed_left_sum <-4000) e_speed_left_sum = -4000;
if (e_speed_right_sum <-4000) e_speed_right_sum = -4000;

if (pwm_left < 0) pwm_left = 0;
if (pwm_right < 0) pwm_right = 0;

if (pwm_left > 255) pwm_left = 255;
if (pwm_right > 255) pwm_right = 255;

setSpeed(pwm_left, pwm_right);

delay(100);
}

void CarClass::fix() {

if (curr_left_white > white_limit) {
setSpeed(0, pwm_left);
}

if (curr_right_white > white_limit) {
setSpeed(pwm_right, 0);
}
}

Needs more code tag.
And comments.

Setup-deficient too.

...and not all the code is posted.

aarg:
...and not all the code is posted.

that goes without saying

What would happen if I was driving my car and applied full brakes to only the right or left side every time I veer a little off course? It looks like that's your algorithm.

Google, "feedback loop stability"

you are right, thats why i asked for help

Then don't set your motor speed to 0 in fix(). For example, you could set it to a fraction of the speed of the other wheel.