Programming logic is a set of principles the determine how a task will be carried out in a computer program. In order for a robot to follow our instructions, we must incorporate a list of commands and tests that can be easily answered in our logic. A common example would be where we tell a robot, “if you are 20cm from a wall, turn to the left.” Logic is a tool to develop reasonable conclusions based on a given set of data. Logic is free of emotion and deals very specifically with information in its purest form. Typically, logic can determine statements that are TRUE or FALSE, YES or NO, or do a comparison to test for specific results.

People use words like “logic” and “logical” a lot, often without really understanding what they mean. Logic is the science of how to evaluate arguments and reasoning. It’s not a matter of opinion, it’s a science of how arguments must be formed in order to be reasonable or correct. Obviously, a better understanding is critical for helping us reason and think better. Without it, it’s too easy for us to fall into error.

Simple Robot Logic Examples


Maze Navigation logic would look like this:

  1. Move ahead and then stop after a short distance.
  2. Measure the distance to the wall.
  3. Is it more than 20cm?
    Yes, then move ahead a short distance again.
    No, then turn to the Left.
  4. Go to Step 1 again and repeat.

Another example for Line Following would be:

  1. Do you see the black line to follow
    Yes, then move ahead a short distance.
    No, then look Left or Right.
  2. Do you see the line yet?
    Yes, then go to Step 1.
    No, then Stop.

Deductive Reasoning

One type of logical reasoning is deductive. Deductive reasoning uses information from a large set and applies that information to any member of that set.

For example:
All Computer Programmers are boring (major evidence or premise)
Brad is a Computer Programmer (minor evidence or premise) Therefore, Brad is boring (conclusion)
The conclusion declares that since Brad is a member of a profession, then he must have the characteristics attributed to programming as a whole.

Inductive Reasoning

Another type of logical reasoning is inductive. Inductive reasoning uses specific data to form a larger, generalized conclusion. It is considered the opposite of deductive reasoning.

For example:
Yesterday, Brad left for work at 7:15 a.m. and arrived at work on time.
Today, Brad left for work at 7:15 a.m. and arrived at work on time.Therefore, if you leave at 7:15 a.m., you will always arrive at work on time.
In this example, we have a small set of data (two days of arriving on time), and we have concluded that this will always be the case.


You may have noticed some problems with these examples. ALL Computer Programmers are certainly not boring and traffic patterns are not always the same (especially if you have to drive past a major shopping area at Christmas time to get to work). Since we all know Brad is NOT boring, we may need to find a new way to test or have a method of testing that has more validity.

Using words like ‘all’ will generally falsify your statement (only one example of the contrary is needed). However, if you use words like ‘many’ or ‘some,’ it is less likely that your premises will be rejected. So, when using this type of reasoning, make sure your premises are verifiable.

Inductive reasoning requires lots of data. The more data you present, the easier it is for your reader to make the necessary leap to your conclusion. The more data, the shorter the leap. Two data points regarding driving time are not enough. Those days happened to be sunny and clear. There was gas in the vehicle. There were no accidents blocking the road.

Logical Fallacies

Logical fallacies are incorrectly reasoned facts. There are many logical fallacies, but the more popular ones are as follows:

Ad Hominem: The literal translation of this term is ‘to the person.’ This is when we attack people instead of attacking the argument. Instead of saying that you are stupid for believing that if you leave for work every day at the same time you will arrive on time, your lack of data should be attacked.

Stereotyping: We use stereotypes all of the time, sometimes without even knowing it. Stating that all Computer Programmers are boring is a stereotype.

Faulty Sampling: Our argument concerning arriving on time for work uses a faulty sample. We have only two data points that are not representative of the whole. (Both days were sunny, you didn’t have to shovel your car out of the snow.)

False Dilemma: Oftentimes we oversimplify. A false dilemma implies that there are only two options. For example, the statement ‘You are either with us or against us’ is a false dilemma. The third option is indifference.

Post Hoc/Ergo Propter Hoc: The Latin translation is ‘It happened before this, therefore it happened because of this.’ Statements like, ‘Every time I wash my car, it rains,’ is committing the post hoc fallacy.


Fuzzy Logic

Computers define EVERYTHING in binary, a simple 0 or 1. Its either on, or off. But the world isn’t simply black and white, and everything is not always simply true or false. Fuzzy Logic is a numeric representation where the answer isn’t just Yes or No, but is a value somewhere in between. It isn’t where something is just very hot or very cold, but instead could be luke warm, slightly chilly, etc. Humans operate much better when describing things in a scale or range. If we describe a temperature, we might say 21 degrees Celcius. Describing a food we might say its a little spicy or bland. Naming a color we might say it is dark green or light green. To not have that range of descriptions would really hamper our descriptive abilities. Your robot need this ability too.

Disadvantages of Binary Logic

If a robot can only make decisions based on two extremes, its actions will also be two extremes. Its like saying a robot can only go two speeds, 1 km/hour and 10 km/hour, and no speed in between. If suppose your sensors give you values right between both extremes, your robot would become very jerky bouncing between both those speeds. This is referred to as oscillation, or when sensor data quickly bounces between several different predefined sets of actions.

Having this wide variation of actions ca be very bad for your robot – jerky motions can quickly wear and break mechanical parts, and will waste huge amounts of energy in acceleration/deceleration. You could perhaps write a long list of sensor to speed conversions, but that would be painfully long, and will waste memory space and processing time.

fuzzy logic example:

Sensor data can range between 0 and 100. So the motor at 0 means stopped and 100 means 100% power.
sensor value bounces between 3 and 4 (so not consistent a whole number):

speed = sensor / 10

so on a scale of 0 km/h to 10 km/h

if say your sensor = 30% power, speed = 3 km/h
and if sensor = 40% power, speed = 4 km/h
and if sensor = 47% power, speed = 4.7 km/h

This is where fuzzy logic comes in, as fuzzy control allows very smooth transitions between actions. Not only is there a smooth transition between speeds, but the fuzzy program was much shorter and easier to write. So this above example isn’t exactly fuzzy logic, but an oversimplified equation so that you can have a basic of understanding.


You can argue that the above description is a very poor description of true fuzzy logic, and they are correct. Fuzzy logic was never developed or intended for use on robots. Its use was for describing things that didn’t fit a binary description. As such, this is not true fuzzy logic. However . . . The point of this tutorial is to adapt fuzzy logic into a functional, practical, shortened control algorithm that you may use on your robot.