Visual Storms Tools Another Brick in the Robot
Visual. Storms Tools Another Brick in the Robot. . . Antonio Cisternino & Diego Colombo Università degli Studi di Pisa
Introduction • Lego Mind. Storms allow building small robots based on the popular Lego bricks • A programmable brick allows controlling up to three devices (motors) and test up to three input sensors • Programs are expressed in a bytecode interpreted by an interpreter in the brick’s ROM • The execution model is concurrent (up to ten threads) and there is a (very) small amount of memory representing the state of the execution Antonio Cisternino & Diego Colombo
Programming the Brick • Lego provides a visual programming system to program the brick • Other tools have been developed to program Mind. Storms: a tiny JVM is being written (to replace the Lego VM), NQC is a C-Like language for programming the Brick • We have written a library to program Mind. Storms with. NET and Visual Studio • The library compiles Intermediate Language (IL) into Lego bytecode, in this way (almost) any. NET compiler can be used to program the Brick. Antonio Cisternino & Diego Colombo
Compilation Scheme C# bc. I File VB SML IL Visual. Storms Brick Downloader . . . Antonio Cisternino & Diego Colombo
Memory Organization Global variables (0 -31) sp 46 47 . . . Task 9 (32 -47) V 1 V 2. . Stack Antonio Cisternino & Diego Colombo Task 1 (32 -47) Task 0 (Main) 32 -47 • The compilation process should map a stack based machine into a register based machine • Class fields are mapped to global variables • Stack of a method is mapped to local variables starting from index 47 towards 32 • Local variables are mapped starting from 32 • Thus maxstack + #locals < 32
Global variables are expressed as fields Compilation: an Example Inheritance to indicate the type of Brick using System; namespace Storm. Test. Advanced { public class Aracno. Brick. Top : Visual. Storms. Types. RCX 2 { public int x; public int y; [Function. Type(Function. Task, 0)] public void main() { Custom attribute to indicate tasks, int i = Sensor 2(); subs and functions while(Sensor 1() == 0) if (i != 0) Set. Motors. Power(Visual. Storms. Types. Motor. A, Sensor 3()); else Set. Motors. Power(Visual. Storms. Types. Motor. C, Sensor 3()); }}} Inherited methods for I/O Antonio Cisternino & Diego Colombo
Compilation Example: IL IL_0000: IL_0001: IL_0006: IL_0007: IL_0009: IL_000 a: IL_000 c: IL_000 d: IL_000 e: IL_000 f: IL_0014: IL_0019: IL_001 b: IL_001 c: IL_001 d: IL_001 e: IL_0023: IL_0028: IL_0029: IL_002 e: IL_0030: ldarg. 0 : 20 32 9 1 0 call instance. Set. Var int 32 Visual. Storms. Types. Lego. Brick: : Sensor 2() stloc. 0 br. s IL_0028 LJump : 114 26 0 (26 f) ldloc. 0 Set. Var : 20 47 0 32 0 brfalse. s IL_001 b LCheck. Do : 149 192 2 47 0 0 9 0 (9 f) ldarg. 0 ldc. i 4. 1 ldarg. 0 Set. Power : 19 1 9 2 call instance int 32 Visual. Storms. Types. Lego. Brick: : Sensor 3() call instance void Lego. Brick: : Set. Motors. Power(Motor, int 32) LJump : 114 6 0 (6 f) br. s IL_0028 ldarg. 0 ldc. i 4. 4 Set. Power : 19 4 9 2 ldarg. 0 call instance int 32 Visual. Storms. Types. Lego. Brick: : Sensor 3() call instance void Lego. Brick: : Set. Motors. Power(Motor, int 32) ldarg. 0 Set. Var : 20 47 9 0 0 call instance int 32 Visual. Storms. Types. Lego. Brick: : Sensor 1() brfalse. s IL_0009 LCheck. Do : 149 192 2 47 0 0 221 255 (35 b) ret Antonio Cisternino & Diego Colombo
Compilation Example: Output Set. Var : 20 32 9 1 0 LJump : 114 26 0 (26 f) Set. Var : 20 47 0 32 0 LCheck. Do : 149 192 2 47 0 0 9 0 (9 f) Set. Power : 19 1 9 2 LJump : 114 6 0 (6 f) Set. Power : 19 4 9 2 Set. Var : 20 47 9 0 0 LCheck. Do : 149 192 2 47 0 0 221 255 (35 b) Antonio Cisternino & Diego Colombo
Compilation Example: NQC int x; int y; task main () { int i = SENSOR_2; while(SENSOR_1 == 0) { if (i != 0) { Set. Power (OUT_A, SENSOR_3); } else { Set. Power (OUT_C, SENSOR_3); } } } Antonio Cisternino & Diego Colombo
Compilation Example: NQC’s output *** *** pwr dir setv jmpl chkl pwr jmpl pwr chkl Var 0 = x Var 1 = y Var 47 = i Task 0 = main Set. Var : 20 32 9 1 0 LJump : 114 26 0 (26 f) Set. Var : 20 47 0 32 0 LCheck. Do : 149 192 2 47 0 0 9 0 (9 f) Set. Power : 19 1 9 2 LJump : 114 6 0 (6 f) Set. Power : 19 4 9 2 Set. Var : 20 47 9 0 0 LCheck. Do : 149 192 2 47 0 0 221 255 (35 b) ABC, 7 ABC, Fwd var[47], Input(1) 33 0 == var[47], 29 A, Input(2) 33 C, Input(2) 0 == Input(0), 14 Antonio Cisternino & Diego Colombo
Structure of the System Visual. Studio User Visual. Storms Add. In Assembly. Loader Visual. Storms. Compiler Sharp. Storms. Lib Cli. File. Reader Sharp. Serial Antonio Cisternino & Diego Colombo Reflection
Demo Antonio Cisternino & Diego Colombo
Applications • Teaching: the ability of programming simple robots with real world languages allow a gentle introduction to programming • Use of Lego to prototype robots controlled by remote programs running on a full fledged PC and radio controlled • Application of extensible reflection provided by. NET • It is Possibile to implement emulators and debug programs in Visual Studio (or guidbg) for. NET Antonio Cisternino & Diego Colombo
Conclusions and Future Work • IL is a rich binary format that can be exploited for purposes different than execution • The ability of compiling a subset of C# programs to the robot allows a neat environment for experimenting with easy-to-build robots • In the future the compiler will recognize call to external methods generating RPC code and distributing a computation between the robot and the PC automatically Antonio Cisternino & Diego Colombo
- Slides: 14