# Arduino Forum

## Topics => Robotics => Topic started by: matteov on Jun 03, 2018, 10:20 pm

Title: Pid control
Post by: matteov on Jun 03, 2018, 10:20 pm
Hi , i want to ask if someone can explain how to use PID in order to control the speed of motors of a drone... Maybe with a small sketch. Thank you.
Title: Re: Pid control
Post by: jremington on Jun 04, 2018, 12:36 am
Lots of tutorials on line. Google "arduino pid motor speed control".
Title: Re: Pid control
Post by: makersecrets on Jun 13, 2018, 01:02 am
Hi Matteov,

The basic Idea of a PID controller is exactly the same thing that happens when you want to drive a car and stay in the middle of the road. If the car is moving to the right, you steer left in order to make it come back to the center of the lane and if the car is moving left you steer right.

This idea is just expressed in a mathematical form which means that you will compute an error depending on your desired position and the current observed position. So let's say that staying at the center of the road corresponds to 0. Your error would be error = 0 - actual position.

If the car is on the right "actual position" > 0 => error < 0. If you imagine that sending a negative value to the steering wheel steers the car to the left, and  apply the error directly to the steering wheel, your car will automatically correct it's position to come back at the center of the lane and the same thing will happen if the car drifts to the right.

In mathematical term for the simple version, you compute:
Error = Kp*(Desired Position - Actual Position)

With Kp is a gain that encodes how fas you would like your car to come back to the desired position.
There equation above doesn't really work in real life because your system will never converge to your desired position, so you will need to add a derivative term to stabilise the system and also you can add an integral term to correct for what is called the static error.

If you manage to implement the above, you are already well on track and can implement the complete PID controller!

Makersecrets