Doing math with a robot
When using sensors to control what your robot must do, you’ll often find that sensor values cannot be used directly. For example, distance sensor values (up to 2000 mm) are too big to plug into a motor power value (0 to 100%) directly.
You can use the light green math blocks from the Data tab to convert numbers into something you can use. In this section, you’ll learn to use each of the following blocks.
blocks
We’ll continue to use Inventor Bot in the following examples, but you can apply these techniques to any design.
Calculations with the Math block
Let’s start with the Basic Math block, which lets you add, subtract, multiply, or divide two numbers. If you type two numbers, it works just like a calculator.
So if you place (1 + 2) in the Print block, it prints 3.
This becomes more useful if you drag another block into it, like the distance measurement. Each time it gets to the Math block in this example, it divides the measured distance by 25.4. The result is passed to the Print block. Because the measurement is in millimeters, this division gives you the distance in inches.
Use the > icon on the Print block to add more fields as in the example. Note
that “mm = “ and “inch” are just extra text you type yourself to make the
output easier to read. The robot treats all numbers like any other; it does
not know anything about units.
Move the sensor around to see different values and verify the result with a calculator. You can expect output such as the following.
182 mm = 7.1653544 inch
74 mm = 2.9133858 inch
40 mm = 1.5748031 inch
71 mm = 2.7952756 inch
137 mm = 5.393701 inch
187 mm = 7.362205 inch
Challenge #6.1.A: Measuring wheel ⸺ Push one motor with a wheel along your desk and print the angle of the motor. Can you turn this into a measuring wheel? Tip ⸺ Use a math block to multiply or divide the measured angle by some other number. What number is this?
Using calculations for decisions
The result of a calculation is just another number. You can compare it to a threshold value to make a decision. The following program expands on what you made in Chapter 5.2. Instead of driving until the distance is less than 500 mm, it drives until the distance is less than 20 inches (which is about the same).
Advanced math operations
The Advanced Math block has all the functions that you would find on a calculator. This includes trigonometric functions like the sine or cosine. We will explain the various functions as they are used throughout examples.
This example demonstrates the max function, which takes two numbers and gives
you the highest one. Here it takes two motor angles. For example, if the left
motor is at 153 degrees and the right is at 273 degrees, it gives 273. The
result is compared to 360, making the robot play a sound when one of the wheels
is manually pushed beyond one rotation, whichever gets there first.
Challenge #6.1.B: One or the other ⸺ Can you make a program that works just like the example above but without any Math blocks? Use the Value Compare blocks and Binary Logic blocks using the techniques from Chapter 5.3. Compare each motor angle to 360. How do you combine both findings to make the program work? Discuss ⸺ Which approach do you find more practical? Does it matter if the results are the same?
Challenge #6.1.C: Absolute max ⸺ The example you just made only works if
you rotate the motor forward. How can you modify it to play the sound even if
you rotate it in reverse? Experiment with the Advanced Math block and choose
absolute (abs). The abs operation takes any number and removes
the minus sign. Where would you place it in this example? Do you need one or
two?
Using the formula block
You can use multiple Math blocks to build bigger expressions. In this example,
we take the sum of both motor angles and divide it by two. This gives the
average of the two angles. If you rotate just one motor by one revolution, the
result is (0 + 360) / 2 = 180. If you rotate both, the average angle is
(360 + 360) / 2 = 360.
Each rounded block is like the parentheses on a calculator. This means that the inner blocks are calculated first.
There is no limit to the number of blocks you can put together, but it quickly
gets unwieldy as you add more blocks. You can achieve the same result with the
Formula block, as shown below. You type a formula with the symbols a,
b, c, and so on. You can use the > icon to add more symbols.
The result is calculated using your values. Here, a is the right motor and
b is the left motor, so the formula is (a + b) / 2.
Your formula can also contain advanced functions such as a * sin(b - c). For
multiplication, use the * symbol. Use / for division.
Challenge #6.1.D: Measuring wheel formula ⸺ Continue with the measuring
wheel example from Challenge #6.1.A. Previously, you have worked out the number
that is needed to scale the wheel degrees to the distance traveled. This time,
use the Formula block. Can you make it work without putting any measurements in
the formula itself? Use input a for the motor angle and input b for the
wheel diameter. Your formula can use pi for the number π.
Proportional control
Math blocks can also be used to control a motor precisely, by gradually changing inputs such as speed. In Section 5.2, you made a program to keep the robot at a fixed 300 mm distance from a wall. It worked, but it would never settle down because it would always jump between forwards and reverse. You mitigated that by making it not move at all if it was close enough to the target, but this can still leave it some way off the intended target.
Instead of changing the speed abruptly as the measurement crosses a threshold, you can change the speed gradually. Let’s say that the distance is 400 mm, so it is 100 mm behind the 300 mm target. We could make it drive at 100 mm/s to slowly approach the target. If it is at 500 mm (so 200 mm away), we could make it drive at 200 mm/s. The following table shows this for distance values from 0 to 600 mm.
| distance (mm) | speed (mm/s) = distance - 300 |
|---|---|
| 0 | -300 (fast reverse from wall) |
| 100 | -200 |
| 200 | -100 |
| 300 | 0 (standstill) |
| 400 | 100 |
| 500 | 200 |
| 600 | 300 (fast forward to wall) |
Notice the pattern here. The speed is always 300 mm less than the distance.
In other words, we have speed = distance - 300. When we measure 200 mm, we’re 100
mm ahead of the target. The formula gives 200 - 300 = -100, which makes the
robot reverse, which is just what we need. Try it out in the following program.
Run the program and let it approach a wall. It should smoothly stop at 300 mm from the wall. Try pushing it forward or pulling it backward. It will keep correcting itself to reject your disturbance.
You can make it respond faster by multiplying the speed, such as by 3. This
makes all speeds higher, but it is still 0 at the 300 mm target, as shown
in the following table.
| distance | speed = (distance - 300) * 3 |
|---|---|
| 0 | -900 (even faster reverse) |
| 100 | -600 |
| 200 | -300 (fast reverse from wall) |
| 300 | 0 (standstill) |
| 400 | 300 (fast forward to wall) |
| 500 | 600 |
| 600 | 900 (even faster forward) |
You can try this as follows. We’ve also added a block to increase the drive acceleration. This lets the drive block reach your desired speed more quickly.
Challenge #6.1.E: Overshoot ⸺ We chose 3 as the multiplication factor
above. This multiplier is sometimes called a gain factor. What happens if you
make it higher? Create a table to record your observations. Try 6, 9, or even
higher. Does it gradually stop, or does it go beyond the target? At what point
does it become unstable? What happens if you choose 0.5, 0.1 or 0? What
happens if you choose a negative gain like -1?
Using random values
The Random block gives a random whole number between two numbers that you choose, those numbers included. The following example makes the robot drive until it sees an object, back up, and turn by a random number of degrees between -90 and 90. If the value happens to be 0, it won’t turn at all. The block gives a new random value every time it is called, so for every turn in this example.
In this example, not only the direction but also the size of the turn was random. On balance, it will steer to the right as often as it will to the left, and sometimes by only small amounts.
Instead of using the random value directly, you can make random decisions, and
decide the turn for each decision specifically. This variation of the program
gets a random number of 1, 2, or 3, with each being equally likely. A
compare block decides what the if-else block will do. If the random value is 1 or 2, the robot steers to the right by 90 degrees. Otherwise it steers left by 90 degrees.
On balance it steers right about two thirds of the time.
Challenge #6.1.F: Stuck in a corner ⸺ Try both of the examples with the random block above and let your robot drive around a room. What happens when it gets to a corner? Do both robots eventually escape the corner? Which program does better, and why?