Skip to main content
InSource KnowledgeCenter

TN AppSvr163 Lego Mindstorms API and ArchestrA

insource logo large.jpg

 

Description

This article will show how the Lego Mindstorms API was used to build an AppServer object to command and control a Lego Mindstorms EV3.

 

  • Author: Lewis Talley    
  • Published: 11/12/2015
  • Applies to: Application Server 2014 R2

 

 

Details

The API was obtained from (https://github.com/BrianPeek/legoev3).  There is also documentation on how to use the methods and properties in the link.  First you will need to import the downloaded script library into a galaxy.  Read this primer first https://github.com/BrianPeek/legoev3/wiki 

due to the nature of how application server scripting works (ie does not support .NET event hooks at this time), the choice was made to "poll" the device at each engine scan to see when data had changed, and to also issue commands in this way.  All of the object source code will be included, but I will only work through a few examples for the sake of simplicity.  unless otherwise indicated, all scripts run as async. 

To use the color sensor you can use either a twister game or I used rubber color tiles from Lowe's.

There is also a document attached in this content that is a walk through what was used and built for this demo.

 

the script library.png

 

First an object onscan script was created to connect to the EV3 and place the connection into the AppDomain as follows:

*******************connect********************************

--declarations section

dim myEV3 as Lego.Ev3.Core.Brick;
dim myEV3Connection as Lego.Ev3.Desktop.NetworkCommunication;
dim myPort as Lego.Ev3.Core.Port;
dim myEV3Port as Lego.Ev3.Core.InputPort;

 

--onscan

myEV3Connection = new Lego.Ev3.Desktop.NetworkCommunication("your ip address here");
LogMessage ("***************created USB object ************");

myEV3 = new Lego.Ev3.Core.Brick(myEV3Connection);
LogMessage ("***************created lego brick object on Network connection************");

myEV3.ConnectAsync();
LogMessage ("***************Created an Async connection to EV3************");


System.AppDomain.CurrentDomain.SetData("EV3", myEV3);
LogMessage ("***************App Domain SetData************");

 

 

 

*****************poll for data**********************************************

me.triggerConnectAsync (this script runs async every scan)

 

myEV3Port = Lego.Ev3.Core.InputPort.Four;

''LogMessage ("**************SI Value: " + myEV3.Ports[myEV3Port].SIValue.ToString() +"****" );
Me.Distance = myEV3.Ports[myEV3Port].SIValue;

myEV3Port = Lego.Ev3.Core.InputPort.Three;

Me.myColorInt = myEV3.Ports[myEV3Port].SIValue;

myEV3Port = Lego.Ev3.Core.InputPort.Two;


Me.Heading = myEV3.Ports[myEV3Port].SIValue;

myEV3Port = Lego.Ev3.Core.InputPort.One;
Me.TouchSensor = myEV3.Ports[myEV3Port].SIValue;

 

****************************************sending commands to motors*******************

dim myEV3 as Lego.Ev3.Core.Brick;
dim myEV3Connection as Lego.Ev3.Desktop.NetworkCommunication;

 

--this script runs with a button called drive forward that sets a bit

dim motorB as Lego.Ev3.Core.OutputPort;
dim motorC as Lego.Ev3.Core.OutputPort;

motorB = Lego.Ev3.Core.OutputPort.B;
motorC = Lego.Ev3.Core.OutputPort.C;


myEV3 = System.AppDomain.CurrentDomain.GetData("EV3");
LogMessage ("******App Domain Get Data Called*****");

if myEV3 <> null then
    System.Threading.Monitor.Enter(myEV3);
    myEV3.BatchCommand.TurnMotorAtPowerForTime(motorB,Me.MotorBPower,0,Me.MotorBTime ,0,0);
    myEV3.BatchCommand.TurnMotorAtPowerForTime(motorC,Me.MotorCPower,0,Me.MotorBTime ,0,0);
    
    myEV3.BatchCommand.SendCommandAsync();
    
    System.Threading.Monitor.Exit(myEV3);

endif;

Me.trgDriveForward = false;

 

Those are the fundamentals, you can download the object and each of the scripts to test.