Implementing PID distance control for ping/dc motor

How would I go about using PID control for the purpose of stopping a 4wd car within a certain distance from a wall? My preferred arrangement would be to have the ping sensor fixed forward and the 4 motors set to only move forward and backward. It doesn't seem to be a difficult task but I have not seen anything like it anywhere. Any ideas?

The PID control loop has two inputs and an output:

Input: The value from your ultrasonic distance sensor
Setpoint: The distance you want to stop at.
Output: The signal to the motors

Connect them all up and you just have to tune the loop.

Thank you, that should help me with getting this started.

I wrote something similar to that. But I controlled speed, regarding to the distance to the wall. That made it more precise.

Check out the attachment, I tried to comment most of it.
It drives forward, when the battery voltage is OK ( Little extra safety for my LiPo ) and when plug off a jumpercable from ground.
It drives until the wall is close; drives backwards for 1 meter, then does a randomized spin left or right and statrs all over again.
The voids "curve left" and "curve right" are not used here and dont work very well.

robot_L298_ping100.ino (6.6 KB)

johnwasser how can i implement the input with a ultrasonic sensor??
do you state the measured distance in input or you state a PIN of sensor(trigger or echo?) ?

FrankBonaparta:
johnwasser how can i implement the input with a ultrasonic sensor??
do you state the measured distance in input or you state a PIN of sensor(trigger or echo?) ?

There are tons of examples on the web. Google can help you find them.
You pulse the 'trigger' pin and measure the echo time pulse on the 'echo' pin, usually with pulseIn(). That gives you a time in microseconds that is proportional to the distance. If you want you can convert from time to distance by multiplying by the speed of sound in inches/CM/feet per microsecond. Then divide by 2 since the time you measure is round-trip time.

yes i know how convert the time to distance...but that distance would be the value of my Input??
for example
Input= DistanceCm intead of Input=analogRead(PinEcchoo));?
SetPoint= 40 cm or if sensor give me the value like 940 that i recieve when it is a 40 cm... (it is an example).. it is my doubt