PowerShell Demo Extravaganza: Lego Mindstorms Robot, Part 1

One of PowerShell Magazine’s editors is asking me for quite some time to write an article about how to move Lego “bricks” from PowerShell. This demo is part of PowerShell Demo Extravaganza session that I’ve presented at different Microsoft conferences (TechEd Australia 2013 session is recorded here). This will be the first part in PowerShell demo extravaganza series of posts, so let’s start.

To move Lego from command line you need one special Lego “brick”. It comes with only few sets (8547 or 31313), and it’s called “intelligent brick”. This is the newest model (version 3).

In the past I used previous model to move different sets (like Crane and R2D2 (at 1:14)). To move them I used .Net library from MindSqualls site. Last week I finally received new set but when I tried to use the same approach it didn’t work. Luckily, a new .NET library was published last year on CodePlex (LEGO MINDSTORMS EV3 API). Great things about this new .NET library is that it comes with documentation too.

First, we need to download .NET library, and expand files (don’t forget to unblock the archive before unpacking). It comes with three flavors:

  • Desktop
  • Phone
  • WinRT

We will concentrate on the Desktop version. The steps are similar for other two platforms.

Step 1. Load DLL into your PowerShell session


Step 2. Create a “brick” object

Before we continue, we need to connect “brick” and a laptop. The following connection types are available:

  • USB
  • Bluetooth
  • WiFi (it requires USB WiFi adapter to be inserted into “brick” )

In this example we will use Bluetooth. Please ensure that Bluetooth is enabled on “brick” and iPhone/iPad/iPod is not selected, and then pair it with a computer and note outbound COM port.

$btc = New-Object -TypeName Lego.Ev3.Desktop.BluetoothCommunication COM3
$brick = New-Object -TypeName Lego.Ev3.Core.Brick $btc

Step 3. Connect a laptop and a “brick”


And that’s it–the connection is established with just three commands. Now we have full control of our Lego Mindstorms robot!

We can connect motors and sensors to the “brick”. Motors are connected to ports named with letters (“A”, “B”, “C”, “D”) and sensors that are named with numbers (“1”, “2”, “3”, “4”). Brick object that we have created have the “Ports” property. It allows us to get information about what is connected to each port.

Display motor connected on port “B”


Note: When motor is connected to a port, *Value properties are number of steps (full circle is 360 steps) that motor has done.

Display sensor connected on port “3”:


Note: In case of infrared sensor *Value properties represent a distance between sensor and nearest obstacle. To move the “brick” we need first to assemble an EV3 robot that has some motors. For start we will use the TRACK3R. It has three motors and one IR sensor that we can use to measure distance.

To move motors we can use different methods “under” $brick.DirectCommand property:

$brick.DirectCommand.StepMotorAtPowerAsync(@("B","C"), 50, 360, $false)
  • @(“B”,”C”)- turn on two motors
  • 50 – represtens Power (1 – 100)
  • 360 – number of steps
  • $false – engage (parking) brake after command is completed

This is the complete code to move motors for one full circle:

$btc = New-Object -TypeName Lego.Ev3.Desktop.BluetoothCommunication COM3
$brick = New-Object -TypeName Lego.Ev3.Core.Brick $btc

$brick.DirectCommand.StepMotorAtSpeedAsync(@("B","C"), 50, 360, $false)

How hard is to move Lego “Brick” using PowerShell? Well, just four lines of code! I really like PowerShell!

In the next post we will take care that this robot avoids obstacles. Stay tuned!

Share on: