I have a mapping robot , 3 ultra sound sensors HC SR04 , arduino uno board, two motors MG-6-120, power source 2 5v 4000 mAh power banks, driver L298N.

It uses a wall follower algorithm (here is the problem), if it finds a wall it should go parallel to it until it finishes the room, but as it follows the wall is oscillates heavily left and right so when it has to turn it is not parallel to the wall and sometimes misses the next wall after the turn.

I will post a picture of the problem.

P.S. the robot only has to make 90 deg turns.

Wall following function in a few words:

if (dist < minDist) turnSoftLeft();

if (dist > maxDist) turnSoftRight();

but this causes it to oscillate a lot and never go smooth parallel to the wall.

I read that I could use a PID proportional–integral–derivative, but I have no idea how to do that.

enter image description here


In a broad understanding of control systems, we use a sensor reading as input. we set a target for that reading and try to minimize the difference between target and input reading by performing some action and taking the input again. IN PID control,we use additional parameters(constants) to reduce the difference between input and target faster.

You can follow the following algorithm without PID control and yet achieve a proper wall following For your problem :
Input : Distance from Ultrasound sensors.
Output : Motor

Assuming your placement of sensors on front(1),
right(2) and left(3) face of robot.

The simple algorithm can be modelled as follows:

  1. set constants i.e min dist to move forward,min dist to make right turn,min dist to make left turn.(min dist = minimum distance)

Loop 1
2. read from sensors 1,2,3.
3. if(distance from 1 > min dist to move forward) : actuate both motors.
4. if(distance from 2 < min dist to make right turn) : actuate left motor.
5. if(distance from 3 < min dist to make left turn) : actuate right motor.
end loop1

There is a 2nd loop in actuate function where
1. Set target distance from sensor as min distance + some constant.

loop 2
2. compute error = target - read from the interested sensor.
3. output:using PWM technique control motor ON-OFF actuation
until error is zero.
end loop2

If you are interested in PID control you can follow the link

It is a tutorial on matlab for PID control of robot.
you can change few scripts in the SIMIAM controller to suit your needs It even has a implementation of ultrasound sensor.

hope this helps

I personally fell implementing a simple PID controller on arduino is tough
as it needs a lot of fine tuning to make the robot move parallel to wall. I suggest you use additional sensors like odometry or IMU to get exact parallel following

  • $\begingroup$ can you please tell me more about loop 2: what constant should I put? and 3. output:using PWM technique control motor ON-OFF actuation until error is zero. How can I do this? $\endgroup$ Nov 30 '17 at 7:26
  • $\begingroup$ follow up question: loop 2 locks the control until error = 0 , but how do i treat the case if I have obstacle in front while robot is adjusting loop 2 for left distance $\endgroup$ Nov 30 '17 at 7:57
  • $\begingroup$ I used trail error method with constants. Initially i set random values then corrected them in iterative fashion to get desired output.It is not an efficient way but it worked for me. $\endgroup$
    – Pradeep BV
    Nov 30 '17 at 8:21
  • $\begingroup$ For just wall following along outer boundry you can modify the above code $\endgroup$
    – Pradeep BV
    Nov 30 '17 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.