We work with Lego Mindstorms EV3 from .NET. Connecting two or more EV3 Bricks Connecting via Wi-Fi

USB connection

LEGO Mindstorms EV3 can connect to a PC or other EV3 via a USB connection. The connection speed and stability in this case are better than with any other method, including Bluetooth.

LEGO Mindstorms EV3 has two USB ports.

Communication between LEGO EV3 and other LEGO EV3 blocks in daisy chain mode.

Daisy chain mode is used to connect two or more LEGO EV3 blocks.

This mode:

  • designed to connect more than one LEGO Mindstorms EV3;
  • serves to connect more sensors, motors and other devices;
  • allows communication between several LEGO Mindstorms EV3 (up to 4), which gives us up to 16 external ports and the same number of internal ports;
  • makes it possible to control the entire chain from the main LEGO Mindstorms EV3;
  • cannot function when Wi-Fi or Bluetooth is active.

To enable the daisy chain connection mode, go to the project settings window and check the box.

When this mode is selected, then for any motor we can select the EV3 block that will be used and the necessary sensors.

The table shows options for using EV3 blocks:

Action

Medium motor

Big motor

Steering

Independent management

Gyroscopic

Infrared

Ultrasonic

Motor rotation

Temperatures

Energy meter

Sound

Connection via Bluetooth

Bluetooth allows LEGO Mindstorms EV3 to connect to a PC, other LEGO Mindstorms EV3, smartphones and other Bluetooth devices. Communication range via Bluetooth is up to 25 m.

You can connect up to 7 blocks to one LEGO Mindstorms EV3. The EV3 Master Brick allows you to send and receive messages to each EV3 Slave. EV3 Slaves can only send messages to the EV3 Master Brick, not to each other.

EV3 connection sequence via Bluetooth

In order to connect two or more EV3 blocks to each other via Bluetooth, you need to perform the following steps:

1. Open a tab Settings.

2. Select Bluetooth and press the center button.

3. We put Checkbox visibility bluetooth.

4. Check that the Bluetooth sign ("<") виден на верхней левой стороне.

5. Do the above procedure for the required number of EV3 Bricks.

6. Go to the Connection tab:

7. Click on the Search button:

8. Select the EV3 you want to connect to (or to which you want to connect) and press the center button.

9. We connect the first and second blocks with the access key.

If you do everything correctly, the icon " will appear in the upper left corner<>", connect other EV3 blocks in the same way if there are more than two of them.

If you turn off LEGO EV3, then the connection will be lost and you will need to repeat all the steps.

Important: each block must have its own program written.

Example program:

First Block: When the touch sensor is pressed, the first EV3 Brick transmits text to the second block with a 3 second delay (the main block).

Example program for block 2:

The second block waits for the text from the first block to be received, and once it has received it, it will display a word (in our example, the word "Hello") for 10 seconds (subblock).

Connect via Wi-Fi

Longer range communication is possible by connecting the Wi-Fi Dongle to the USB port on EV3.

To use Wi-Fi, you need to install a special module on the EV3 Brick using a USB connector (Wi-Fi adapter (Netgear N150 Wireless Adapter (WNA1100)), and you can also connect a Wi-Fi Dongle.

Select screen mode

Mode selection
Block text field
Inputs
Preview button

Select the type of text or graphics you want to see using the mode selector. After selecting the mode, you can select the input values. Available inputs will vary depending on the mode. Modes and inputs are described below.

You can click on the "Preview" button to preview what the "Screen" block will display on the EV3 screen. You can leave the view open while selecting the input values ​​for the block.

Screen coordinates

Many of the Screen block modes use X and Y coordinates to determine the location of an element. The coordinates determine the position of the pixels on the EV3 Brick screen. The position (0, 0) is at the top left corner of the screen as shown in the image below.

Screen dimensions: 178 pixels wide and 128 pixels high. The range of X coordinate values ​​is from 0 on the screen on the left to 177 on the right. The range of Y coordinate values ​​is from 0 at the top to 127 at the bottom.

Tips and tricks

You can use the Preview button in the top left corner of the Screen block to help you find the correct screen coordinates.

Text - Pixels

Text - Pixels mode allows you to display text anywhere on the EV3 Brick screen.

Reset window

Reset Window mode returns the EV3 Brick screen to the standard information screen shown while the program is running. This screen shows the program name and other feedback information. When you run a program on the EV3 Brick, this screen appears before the first Screen block of the program runs.

Ensuring visibility of displayed elements

When the EV3 Program completes, the EV3 Brick screen clears and returns to the EV3 Brick Menu screen. Any text or graphics displayed by the program will be erased. If, for example, your program has one "Screen" block and nothing else, then the screen will be cleared so quickly immediately after the program ends that you will not see the results of the "Screen" block.

If you want the screen to remain visible after the program has completed, you must add a block to the end of the program to prevent the program from ending immediately, as shown in the following examples.

Displaying multiple items

If you want to display multiple text or graphics elements on the screen at the same time, it is important not to clear the EV3 Brick screen between elements. Each mode of the Screen block has a Clear Screen input. If Clear Screen is true, the entire screen will be cleared before the element is displayed. This means that to display multiple items, you must set Clear Screen to False for every Screen block except the first one.

Displaying numbers

To display a numeric value in your program, connect the data bus to the Text input of the Text Display block. The numeric data bus will be automatically converted to text using data bus type conversion (for more information, see

The idea of ​​replacing the microcomputer in the constructor with a Beaglebone or something else is not new. But with the release of EV3, it became possible not only to get a 100% analogue, but also to increase the performance of your Lerobot.

Video presentation of the project:

E VB fully supports the Lego Mindstorms Ev3 system in both hardware and software, and is 100% compatible with all Lego sensors and motors. The block works the same as the Lego Mindstorms EV3 block:

BeagleBone Black a single board Linux computer. It is a competitor to Raspberry Pi. Features a powerful AM335x 720MHz ARM® processor, large the number of inputs / outputs, the possibilities can be expanded with additional boards.

Lego Mindstorms EV3 has an ARM9 (TI Sitara AM180x) 300MHz processor, so moving to an ARM Cortex-A8 (TI Sitara AM335x) 1GHz BeagleBone Black processor improves performance, plus it becomes possible to connect additional expansion cards!

The most important thing is that Lego Mindstorms EV3 has an open description of all software and hardware!

For example, the famous Rubik's cube solving robot has been assembled and demonstrated. Only instead of EV3 they installed the developed EVB. We suggest watching the video:

The authors of the project are already releasing and selling EVB. They plan to significantly expand production by the end of April 2015. In addition, they have developed and are producing several compatible sensors.

Traditionally, robots built on a platform Lego Mindstorms EV3, are programmed using the LabVIEW graphical environment. In this case, the programs run on the EV3 controller and the robot operates autonomously. Here I will talk about an alternative way to control the robot - using the .NET platform running on a computer.

But before we get into programming, let's look at some cases where this might be useful:

  • Requires remote control of the robot from a laptop (for example, by pressing buttons)
  • It is required to collect data from the EV3 controller and process it on an external system (for example, for IoT systems)
  • Any other situations when you want to write a control algorithm in .NET and run it from a computer connected to the EV3 controller

LEGO MINDSTORMS EV3 API for .NET

The EV3 controller is controlled from an external system by sending commands to the serial port. The command format itself is described in the Communication Developer Kit.

But implementing this protocol manually is boring. Therefore, you can use the ready-made .NET wrapper, which Brian Peek carefully wrote. The source code for this library is hosted on Github, and the ready-to-use package can be found on Nuget.

Connecting to the EV3 Controller

The Brick class is used to communicate with the EV3 Controller. When creating this object, you need to pass an implementation of the ICommunication interface to the constructor - an object that describes how to connect to the EV3 controller. UsbCommunication, BluetoothCommunication and NetworkCommunication (WiFi connection) implementations are available.

The most popular connection method is via Bluetooth. Let's take a closer look at this connection method.

Before we can programmatically connect to the controller via Bluetooth, the controller must be connected to the computer using the operating system settings.

After the controller is connected, go to the Bluetooth settings and select the COM ports tab. We find our controller, we need outgoing port. We will specify it when creating the BluetoothCommunication object.

The code to connect to the controller will look like this:

Public async Task Connect(ICommunication communication) ( var communication = new BluetoothCommunication("COM9"); var brick = _brick = new Brick(communication); await _brick.ConnectAsync(); )

Optionally, you can specify a connection timeout to the controller:

Await _brick.ConnectAsync(TimeSpan.FromSeconds(5));

Connecting to the unit via USB or WiFi is carried out in the same way, with the exception that the UsbCommunication and NetworkCommunication objects are used.

All further actions performed on the controller are done through the Brick object.

Let's spin the engines

To execute commands on the EV3 controller, we access the DirectCommand property of the Brick object. First, let's try to start the engines.

Let's assume that our motor is connected to port A of the controller, then running this motor at 50% power will look like this:

Await _brick.DirectCommand.TurnMotorAtPowerAsync(OutputPort.A, 50);

There are other methods to control the motor. For example, you can rotate a motor to a specified angle using the StepMotorAtPowerAsync() and StepMotorAtSpeedAsync() methods. There are several methods available, which are variations on the modes of turning on the motors - by time, speed, power, etc.

Forced stop is carried out using the StopMotorAsync() method:

Await _brick.DirectCommand.StopMotorAsync(OutputPort.A, true);

The second parameter indicates the use of the brake. If you set it to false, the motor will coast to a stop.

Reading values ​​from sensors

The EV3 controller has four ports for connecting sensors. In addition to this, the motors also have built-in encoders, which allows them to be used as sensors. As a result, we have 8 ports from which values ​​can be read.

The ports for reading values ​​can be accessed through the Ports property of the Brick object. Ports is a collection of ports available on the controller. Therefore, to work with a specific port, you need to select it. InputPort.One ... InputPort.Four are the sensor ports, and InputPort.A ... InputPort.D are the motor encoders.

Var port1 = _brick.Ports;

Sensors in EV3 can operate in different modes. For example, the EV3 Color Sensor can be used to measure ambient light, measure reflected light, or detect color. Therefore, in order to “tell” the sensor exactly how we want to use it, we need to set its mode:

Brick.Ports.SetMode(ColorMode.Reflective);

Now that the sensor is connected and its operating mode is set, you can read data from it. You can get “raw” data, processed value and percentage value.

Float si = _brick.Ports.SIValue; int raw = _brick.Ports.RawValue; byte percent = _brick.Ports.PercentValue;

The SIValue property returns the processed data. It all depends on which sensor is used and in what mode. For example, when measuring reflected light, we will get values ​​from 0 to 100 depending on the intensity of the reflected light (black/white).

The RawValue property returns the raw value obtained from the ADC. Sometimes it is more convenient to use it for subsequent processing and use. By the way, in the EV3 development environment it is also possible to obtain “raw” values ​​- for this you need to use the block from the blue panel.

If the sensor you are using expects to receive values ​​in percentages, then you can also use the PercentValue property.

Executing commands in batches

Let's say we have a robot cart with two wheels and we want to deploy it in place. In this case, the two wheels must rotate in the opposite direction. If we use DirectCommand and send two commands sequentially to the controller, some time may pass between their execution:

Await _brick.DirectCommand.TurnMotorAtPowerAsync(OutputPort.A, 50); await _brick.DirectCommand.TurnMotorAtPowerAsync(OutputPort.B, -50);

In this example, we send a command to rotate motor A at a speed of 50, after sending this command successfully, we repeat the same with the motor connected to port B. The problem is that sending commands does not happen instantly, so the motors may start to spin at different times - while the command is being transmitted for port B, motor A already will start spinning.

If it is critical for us to make the motors spin at the same time, we can send commands to the controller in a “bundle”. In this case, you should use the BatchCommand property instead of DirectCommand:

Brick.BatchCommand.TurnMotorAtPower(OutputPort.A, 50); _brick.BatchCommand.TurnMotorAtPower(OutputPort.B, -50); await _brick.BatchCommand.SendCommandAsync();

Now two commands are prepared at once, after which they are sent to the controller in one packet. The controller, having received these commands, will begin to rotate the motors simultaneously.

What else can you do

In addition to rotating motors and reading sensor values, you can perform a number of other actions on the EV3 controller. I will not go into detail about each of them, I will only list a list of what can be done:

  • CleanUIAsync(), DrawTextAsync(), DrawLineAsync(), etc. - manipulation of the built-in screen of the EV3 controller
  • PlayToneAsync() and PlaySoundAsync() - use the built-in speaker to play sounds
  • WriteFileAsync() , CopyFileAsync() , DeleteFileAsync() (from SystemCommand) - working with files

Conclusion

Using .NET to control Mindstorms EV3 robots demonstrates well how technologies from “different worlds” can work together. As a result of researching the EV3 API for .NET, a small application was created that allows you to control the EV3 robot from a computer. Unfortunately, similar applications exist for NXT, but EV3 bypassed them. At the same time, they are useful in controlled robot competitions, such as robot football.

The application can be downloaded and installed from this link: