LEGO Resources
This page describes the EV3 mindstorms robots, and how to program
them.
Background
We use LEGO Mindstorms EV3 robots in COSC343. These robots use the 32-bit ARM9 microprocessor, running Linux. Here's a rundown of the features of the EV3 kits.
- They have four inputs (for sensors) and three outputs (for
motors).
- The motors are servomotors,
which come with position sensors, so you can tell them to rotate by
precise angles.
- There are four sensors:
- A colour sensor, sensitive to red, green and blue light. (This can be configured as a monochrome light sensor as well.)
- A bump sensor. (On/off.)
- A sound sensor. (Sensitive to intensity only - not pitch.)
- A sonar sensor, which computes distance to an object in a certain
direction. (Only works well if the object presents a smooth plane, and there are no confusing sonar reflections in the environment.)
- The EV3 robots have 64MB of RAM, and 16MB of flash memory.
- They have a simple file system, and a simple LCD screen.
- They communicate with a host machine via a USB link. (They can also communicate using bluetooth, though we don't have that enabled in the department.)
- The NXT robots are quite sturdy, being based on the LEGO technics paradigm: lots of struts, and not so many bricks (though regular bricks are still compatible).
- The robots have inboard rechargeable batteries.
You can find out more about the EV3 system
on the EV3 Wikipedia page.
A hierarchy of programming languages
As with any computer system, there are several different levels on
which you can interact with the NXT robots.
- At the lowest level, the ARM9 microprocessor has its own
instruction set: a set of low-level operations for
accessing memory, changing register contents and so on.
- The EV3 robots also come with an operating system, called its firmware, that creates an environment in which to run programs. There are various different varieties of firmware available to use with different programming languages: they are all implemented as extensions of the LINUX operating system. Instructions are given to the firmware in a slightly higher-level set of instructions called bytecode.
The firmware should already be installed on your robot. But if a
robot's batteries ever run out completely, it needs to be
reinstalled: that can be done with the 'firmware download' menu button.
- Programmers use a high-level language to write programs for the EV3: we're using one called ROBOTC (see below). The programmer writes a program in ROBOTC and then compiles it, to generate bytecode, and then downloads the bytecode to the robot, where it can be run.
ROBOTC
The language we'll be using on this course is called
ROBOTC. There are several good references for this language. In particular:
Physical design of your LEGO robots
We have built a number of simple 'turtle' robots for you to use. They have two wheels, which are separately controllable. They have a bumper connected to two touch sensors. They also have a colour sensor, pointing down, and a sonar sensor, pointing forwards.
- The left and right wheels are called motorB and motorC respectively.
- The left and right bumper sensors are called S1 and S2 respectively.
- The colour sensor is called S3.
- The sonar sensor is called S4.
Using the ROBOTC programming environment
The ROBOTC programming environment runs under Windows, so you need to boot up your lab machine under Windows. (If your machine is currently running MacOS, you have to physically turn it off and back on to choose the Windows O/S.)
- Turn your robot on, and connect it to a USB port.
- Then run ROBOTC. This launches a simple editor for writing and compiling ROBOTC code, and downloading the compiled code to the robot.
- The first time you use ROBOTC, you need to tell it you're using an EV3 robot. To do this, follow the menu path 'robot' --> 'platform' --> 'LEGO Mindstorms' --> 'EV3'.
- Now write a program in the editor window. (To start with, it’s a good idea to load up one of the sample programs that come with ROBOTC: see the menu, ‘file’ --> ‘open sample program’. **These programs won’t run by themselves - you need to add #pragma statements, see next bullet point!**)
- Programs normally begin with a few statements beginning #pragma (...). These are preprocessing statements that define what types of sensor and motor are connected to the robot.
- If you’re writing code from scratch, you can create these statements through the menu: ‘robot’ --> ‘motors and sensors setup’ --> ‘motors’ / ‘sensors’.
- The setup actions add preprocessing statements of the form #pragma (. . . ) to the program code. You can also type these #pragma statements directly into the editor.
- You need to create entries for motorB and motorC, and for sensors S1–S4, as described under physical design of your LEGO robots above.
- When you have written a ROBOTC program, compile it (the 'compile program' menu button).
- When the code compiles cleanly, you can download the file to your robot (the 'download to robot' menu button).
- To run the code on the robot, press the right menu button, then 'rc' to get a listing of all the ROBOTC programs. When you select a program, it runs.
- Make sure the robot is not on a table when you run a program!
- To abort, press the top-left grey button.
Good luck!