Archive

Monthly Archives: February 2016

Intro

lego-mindstorms-ev3-31313_2

I have been taking a couple of hours each weekend to learn F#. To maintain motivation, I decided to purchase a Lego Mindstorm and use F# to send it instructions. A Lego Mindstorm is a Lego kit for building robots that can receive instructions programmatically. In this article I will provide an overview of what I did to harness F# to power a Lego Mindstorm.

Steps

Precondition: Lego Mindstorm has been acquired and assembled.

  1. Review the first ten minutes of the Lego Mindstorm .NET API by Brian Peek.
  2. The video provides an introduction on how to get started using the .NET API.
  3. Create an F# console solution: FnLegos.sln

Building the Core library

  1. Add a new F# project to your solution and call it Core.
  2. Download the Lego.Ev3 package on Nuget for the Core project.
  3. Add file Arguments.fs to the Core project.
  4. Implement Arguments.fs to reflect the following:
module Arguments

open Lego.Ev3.Core
 
let volume = 100
let frequency = uint16 3000
let duration = uint16 1000
let power = 100
let motors = OutputPort.B ||| OutputPort.C
let breakEnabled = false
  1. Add file Commands.fs to the Core project.

Implement Commands.fs to reflect the following:

module LegoCommands
 
open Lego.Ev3.Core
open Lego.Ev3.Desktop
open System.Threading.Tasks
open Arguments
 
let awaitTask (task: Task) = task |> Async.AwaitIAsyncResult
                                  |> Async.Ignore
let connectAsync (brick:Brick) = async {
    do! brick.ConnectAsync() |> awaitTask }
 
let moveAsync (brick:Brick) = async {
    do! brick.DirectCommand.TurnMotorAtPowerForTimeAsync(motors, power, uint32 duration, breakEnabled) |> awaitTask }
 
let speakAsync (brick:Brick) = async {
    do! brick.DirectCommand.PlayToneAsync(volume, frequency, duration) |> awaitTask }

 

Building the Client Executable

  1. Download the Lego.Ev3 package on Nuget for the (FnLegos) console project.
  2. In (FnLegos) console, add a reference to the Core project that was recently created.
  3. Update Program.fs file to reflect the following:
module LegoCommands
 
open Lego.Ev3.Core
open Lego.Ev3.Desktop
open System.Threading.Tasks
open Arguments
 
let awaitTask (task: Task) = task |> Async.AwaitIAsyncResult
                                  |> Async.Ignore
let connectAsync (brick:Brick) = async {
    do! brick.ConnectAsync() |> awaitTask }
 
let moveAsync (brick:Brick) = async {
    do! brick.DirectCommand.TurnMotorAtPowerForTimeAsync(motors, power, uint32 duration, breakEnabled) |> awaitTask }
 
let speakAsync (brick:Brick) = async {
    do! brick.DirectCommand.PlayToneAsync(volume, frequency, duration) |> awaitTask }

Integration

Integration is fairly simple. We just need to ensure that the firmware is up to date and that we establish communications between our F# program and the Mindstorm.

Firmware

As detailed in the Lego Mindstorm .NET API video, you need to download any necessary firmware that’s available.

Communications

I had issues using the Bluetooth. So instead I just connected the Lego Mindstorm to my computer via USB.

Running the application

  1. Ensure that the console project (i.e. FnLegos) is set as the startup project.
  2. Run the program.
  3. Observe the robot is ALIVE!!!!

Conclusion

In conclusion, I provided an overview of what I did to harness F# to power a Lego Mindstorm.

References:

 

Advertisements