# Robot car better algorithms. (Theory)

I have been playing around with a robot car.
It has a IMU(Not sure how many axles), two motors, tracks, and an ultrasonic distance sensor.

I just wondered if someone could help with psudo code for the following:

Making the robot drive straighter with less drift. With IMU? Sort of like a PID algorithm?

Thanks

edit: got rid of:
This has nothing to do with hardware. I don’t have any problems with anything.

And then you go on to describe a hardware problem! Does your robot know the rpm of each motor?

When you ride a bicycle, if you look at a line below you, your steering will wobble and drift. If you look in the distance, your line will be straighter because you are heading for the distant point. Now, how do you make electronics look farther? I do not know... but if you use your current data and fit it into a "line of best fit" (linear least squares) formula, this might predict a straighter path.

TL;DR: Dampen steering over-correction.

1 Like

Oops.......
No, I don't know the rpm.......

This is just theory? I am perfectly satisfied with the current functionality.

I would just like to know how to do it????
Any way?
preferably using software?
Thank you.

Yes!

1 Like

How to do it? Pick the motor with the lower RPM and make the other motor match it.

1 Like

180Rpm
It drifts to the side a bit

So I slow it down?

How do I measure the actual RPM?

In the accompanying video, and in the page at, " We will see how to use this with an Arduino to measure the motor RPM very soon. Reading Control Encoders..."

https://dronebotworkshop.com/rotary-encoders-arduino

1 Like

You seem to use a Makeblock robot. The build quality is subpar for the motor and encoder. Also, the wheels axles are somewhat crook.

I still managed to use the gyro to make the robot go straight. Instead of using the RPM which works if everything is straight (screws, axles, etc.)

Here's my code :

``````void goStraight(short speed = 100, short firstRun = 0) {
static double zAngleGoal = 0.0;

static double error = 0.0;
static double previousError = 0.0;
static double output = 0;

// PD Controller
// Change these values to suit your needs
// higher kp = more reactive, might have oscillation
// lowewr kp = sluggish, but less oscillation
// higher kd = limit oscillation, the right value stops oscillation
const double kp = 3.0;
const double kd = 1.0;

if (firstRun) {
firstRun = 0;

zAngleGoal = gyro.getAngleZ();
Serial.println ("Setting speed");

encoderLeft.setTarPWM(speed);
encoderRight.setTarPWM(-speed);

return;
}

error = gyro.getAngleZ() - zAngleGoal;

// Astuce web : ELI5 = Explain Like I'm 5
output = kp * error + kd * (error - previousError);

previousError = error;

encoderLeft.setTarPWM(speed - output);
encoderRight.setTarPWM(-speed - output);
}
``````

Here's my repo in which you can find multiple examples (Comments in french).

HTH

1 Like

Can you post a link to that component?

and dont use acronyms without giving them in full first

2 Likes

Thanks!
You are correct.

Ok.
I don't know the link to the component.
It is embedded in the robot. hmm

I could not find a name for the component.
I only found the schematic(does not help, it is in Chinese )

The robot has an MPU-6050 onboard.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.