I'm making a robot that uses two DC motors to move, and has two distance sensors on its right side. I want to make a PID controller to control the angle of the robot (using a wall as reference) and also its distance to the wall.

I made a PID controller that can minimize the difference between both sensors' measurements, making the robot go always parallel to the wall. I also made a controller for controlling the distance one sensor measures, for the robot to go always at the same distance to the wall.

What I want to do is combine those two controllers into one. I know there's something called MISO PID controller (multiple inputs single output) but I don't know how to use it or even design it. I thought of adding those two variables (the difference and one sensor's measurement) and using that as the controller's input. Is that right? How can I achieve this?

PS: I also thought of adding the difference between each sensor's measurement and the distance I want, or using those two differences as the variables of my MISO controller.

  • $\begingroup$ I have a question regarding your PID-Wall-Follower. I have already developed a simple version without PID controller that measures the distance in a 45 degree angle and by using the arcos, a correction angle is avaialble to set the robot parallel to the wall. However, I have now found a PID controller that could help me to ensure the parallel distance to the wall. My question now is if you can share your code regarding the wall-follower? Even if I only have one sensor for measuring the distance to the wall, I guess it would help me. Thanks a lot $\endgroup$
    – burki0611
    Commented Dec 1, 2020 at 18:00

4 Answers 4


If I'm interpreting your description correctly (the two sensors are measuring the distance to the wall in a direction roughly perpendicular to the direction of travel and the two sensors are displaced so that one is more "forward" in the direction of travel) , you need to change the angle of the robot relative to the wall in order to change the distance to the wall.

This is a good use case for a cascaded or backstepping controller where an outer control loop's output is a reference for an inner loop. Your inner loop will control the angle of the robot relative to the wall and the outer loop will control the distance to the wall.

So the outer loop will look like: desired_angle = -k_p * (distance_to_wall - desired_distance)

Note that only a P (or PI) controller should be sufficient since if moving at a constant velocity, a constant angle will be a constant velocity towards or away from the wall.

Since the outer loop outputs an angle, you can convert that into a desired sensor reading difference, given the geometry of the robot and the placement of the sensors. Or more simply, you can just interpret the output of the outer loop as a sensor reading difference, as long as you keep the output to a reasonable limit.

The inner loop will look like: motor_control = -k_p * (current_sensor_difference - desired_sensor_difference) - k_d * (...)

This inner loop will be the same inner loop you used before to control the angle to 0, but with a non zero set point.

Hope this helps!


The principle is called linguistic multivariable fuzzy pid control. The idea is to create a domain specific language and create with that a domain specific controller. The existing sensor variables and existing motor primitives for starting and stopping the servo motors are taken as a base for constructing a higher level behavior system on top. Rodney Brooks described this principle first. In terms of programming it means, to add new methods in the controller class. For example, “stayaway-fromwall”, “move-onthe-line”, “rotate-until” and so on. The motion primitives are forming together with the sensor variables a domain specific language and the controller is formulated in that language.

I know, it sounds very theoretically. A practical example can be tested out in the famous “Karel the robot” game. The user has the task to define new subfunction and combines them into a program. This gets started in the Karel simulator. Programming a real robot in a maze works similar.

  • $\begingroup$ This doesn't address OP's question at all. The Karel game doesn't show or explain how to combine PID controllers. Also, I find zero results online when I search "linguistic multivariable fuzzy pid control" or "linguistic multivariable" pid. Can you please provide links to the methods you mention and/or an example of how it works? $\endgroup$
    – Chuck
    Commented Jan 2, 2019 at 16:49
  • $\begingroup$ @Chuck Which kind of of academic search engine do you have? Google Scholar shows over 1000 hits for “wall following fuzzy controller multivariable”, Additionally, the “Flakey the robot” project by SRI gave a good introduction into linguistic grounded robot control. $\endgroup$ Commented Jan 2, 2019 at 18:39
  • $\begingroup$ You said The principle is called linguistic multivariable fuzzy pid control, so I searched that phrase and found nothing. Without quotes turns up results,but then I can't seem to find a paper that has something to covers linguistic and multivariable and fuzzy pid. You mention "Flakey the robot" as a good example, but the wikipedia entry has 6 sentences on it, none of which mention PID. The SRI page has even less information. $\endgroup$
    – Chuck
    Commented Jan 2, 2019 at 18:58
  • $\begingroup$ My point is that you state "the principle is called ____" and then I personally can't find anything that features that phrase. The project you mention in the answer doesn't seem to relate, at least that I can find, to multivariable PID control at all, and again, none of this seems to address the question which is, in essence, "How do I combine two PID controllers?" If your answer is "fuzzy PID control," then please cite a source that shows how fuzzy PID combines multiple PID controllers and/or write a small example here. $\endgroup$
    – Chuck
    Commented Jan 2, 2019 at 19:04

The angle of the wall can be measured using only one sensor but with two readings over a short time period. One PID could then be used to keep the distance to the wall the same over time.

  • $\begingroup$ That does not answer my question though, I'm looking for a way to implement two PID loops at the same time and using the same actuators as output for both. The wall thing is just to provide context on to why I was trying to achieve that $\endgroup$
    – Iaka Noe
    Commented Jul 18, 2021 at 22:56

A (possible) way that I could think of is to use a (resource-aware) switching PID controller in your feedback loop. The operation is as follows:

[1] The controller is of the class single-input-single-output.

[2] The input to the controller is the (desired) error i.e., the error measued between the reference position and the location of the wall-1/wall-2 that is being measued along the motion. I omit the discussion on the control output which is redundant.

[3] The fun part starts here: In short the controller will act as a switching PID controller which switches between its two error inputs. Thereby, reducing the error when certain limit exceeds. This limit can be in terms of performance, for instance, how the ideall the robot should maneuver parallel to the wall.

[4] This switching action can be based on algorithims like roll-out policy. Thereby, switching between the inputs whenever it is necessary. Of course, this depends upon your performance specificationsn and your processor's capabilities. Nevertheless, you can dig into topics such as resource aware controller design per se to look more in detail about this.

Disclaimer: Following this procedure might have impact on the stability, hence always verify the stability argument before going forward with these techniques.

  • $\begingroup$ The domain of a wall following robot is too complex for pid switching control. PID switching has it's roots in a mathematical description which can't be provided in case of robot control. So called “roll-out policies” are an optimization technique. If no model is available, the optimization will fail. $\endgroup$ Commented Jan 7, 2019 at 9:26
  • $\begingroup$ @Manuel Rodriguez could you elaborate on being too complex and can't be provided for robot control. Also, roll-out policies can also be tweaked to behave based on measured data. $\endgroup$ Commented Jan 7, 2019 at 9:33
  • $\begingroup$ Sure, according to the problem description the systems contains of 2 actuators, 2 sensors and a wall which is the reference line to follow. Is it a toy problem in the form of f(x)=y? No, it's an np-hard problem which contains of a non-linear complex statespace which gets divided by project's requirements. It's very likely that the robot is in a maze like environment and has to win a game against other robots. $\endgroup$ Commented Jan 7, 2019 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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