STEM Robotics 4 All

STEM Resources for all

Lesson 6: Variables and MyBlocks (LabView)

The RoboMentors will introduce you to variables and myblocks, gradually enhancing the Go Straight with a Gyro Follower.

What you will learn:

  • What are variables and how to use them
  • What are myblocks, how to make your own

What you need to review:

  • Lesson 1 Learning the basics about the EV3 Brick
  • Lesson 2 Moving Forward without Sensors (Dead Reckoning)
  • Lesson 3 Follow the Edge of a Black Line
  • Lesson 4 Going Straight with Gyro

Recall that the robot can tell how far it has traveled, by using one of the wheel motor sensors (B or C).

Let us consider the follow straight with a gyro example, and try to transform it to use the B motor sensor, for five rotations, or 5*360=1800 degrees:

Now, if we use it in more than once, the second time the motor sensor will show larger and larger values (for instance, from 1800 to 3600 degrees). Otherwise, we have to wait every time for the reset of the motor sensor.

Can we write this better, so we don’t have to remember everything since we started the program ? The answer is yes, if we somehow manage to store the sensor value, right before starting the loop, to remember it:

  • Store the current motor B sensor value as variable iniDist
  • Add up iniDist + distance2Travel and store as variable stopDist
  • Loop
    • follow gyro and adjust steering
    • read current motor B sensor value
    • compare with what we stored in stopDist: if larger or equal, stop the loop

So in the last step, we compare with a stored value, so we need to read it back. What does that mean ?

In general, in order to store values we use what are called variables. You can think of them as a cup with milk, or tea, or water. Only one substance at a time. In a (simple) variable, we only store one number at a time.

If we want to store another number, the previously stored number is thrown away. Of course, once stored, we can read it back, when we need it, and as many times as we need it (reading it does not make it “go away”, unlike drinking from the milk cup — it is more like “looking at the milk from the cup”).

And we can compute other numbers, or ask questions and decide based on the result.

To add variables to your program, you find them in the red tab, next to the math operations.

Yet another improvement of this program could be to not need to reset the gyro every time we use it. We can use the same idea of variables for that purpose. Before we start, instead of resetting, we store the gyro reading, and then use that one inside the loop for steering:

  • Store the current angle read from the gyro as variable angle2keep
  • Add up iniDist + distance2Travel and store as variable stopDist
  • Loop
    • Subtract from angle2keep the value read from gyro
    • Feed the result into the steering part of the drive
    • read current motor B sensor value
    • compare with what we stored in stopDist: if larger or equal, stop the loop

Finally, if we are to use this in several places, or even in different programs, it would be helpful to have a better way than to keep copy-paste-ing it. What if, for instance we realize we need to modify something about it. Then we would have to go find all the other places, and modify the copies as well. Or we want different distances, or speeds.

This can easily become very unwieldy. Thankfully, LabView (and other languagues as well) allows us to bundle up some part of our program and to present it as if it were one of the already existing blocks. This way it can be reused in other parts of our programs, without having to copy-paste it.

We simply invoke it by “its name”. This becomes a “myblock”. Moreover, suppose at one place in the program we need to travel 360 degrees, and at another one, 720. We can tell our myblock to be ready to receive some general value, and to behave accordingly. So here, we can bundle up the go-straight-with-gyro-for-a-given-distance into a myblock, and tell it for what distance. This is called a parameter.

We could actually tell the myblock more than one thing: we can also for instance tell it with what speed to travel. That can be another parameter.

In order to build a myblock, we start with at least some part of the program which is to become the myblock (we can always add things to an existing myblock later on). In the top menu in Tools, there is a “My Block Builder” option. First, we select the blocks to put in the new myblock (without the start block):

Then we request that option:

Then LabView allows us to configure it:

First, we should give it a name, and write a short description.

Then, clicking the ‘+’ at the top allows us to add parameters, one by one, giving them names and saying that they are numbers, etc.

Once we are done, we click Finish, and a new tab with the new myblock content appears along the other programs, together, but also with a new, special box, from where we can read the parameters:

So we can then spool them, like we did for variables, and wire them where we need to read their values, to obtain the final myblock diagram:

Once we consider it good, we can move to a new program tab, and, when we go to the cyan tab at the bottom, which shows all our “myblocks”, we are going to be able to see this new block:

And then, we can use it exactly like any other blocks coming with the Mindstorms EV3 software:

%d bloggers like this: