Event-Driven Programming

Summarized using AI

A journey to MRuby on LEGO robots

Torsten Schönebaum • June 01, 2021 • Rotterdam, Netherlands

In the talk titled "A journey to MRuby on LEGO robots," Torsten Schönebaum explores the exciting integration of robotics and programming using MRuby on LEGO EV3 robots. Schönebaum, who works for Sage and has not spoken publicly for a decade, shares his enthusiasm for LEGO and programming. He stresses the importance of play in human development, quoting Friedrich Schiller to illustrate how play fosters creativity and learning. He presents the LEGO Mindstorms Education set, highlighting its components like the LEGO EV3 brick, motors, and various sensors.

Key points covered during the talk include:

- Introduction to TORSTEN SCHÖNEBAUM: Brief background and personal anecdotes about all things LEGO and programming.

- Building Robots: The process involves trial and error, showcasing the fun and challenge in constructing a functional robot.

- LEGO EV3 Capabilities: Discusses the functionalities of the EV3 brick powered by ARM CPU and its constraints affecting complex programming scenarios.

- Programming Challenges: Explains the limitations of the LEGO Education app for complex applications and the exploration of alternative environments like MakeCode and Ignite.

- Adoption of MRuby: Transitioning to MRuby, an embedded implementation of Ruby, which fits the constraints of the EV3 brick. This includes installation challenges and the process of cross-compiling applications.

- Developing Custom Applications: Schönebaum shares the journey of creating custom MRuby gems for easier interaction between Ruby scripts and the robot's motors and sensors.

- Experiments with Concurrency and Input/Output: Discusses handling motor controls, sensor readings, and the possibility of threading for responsive actions.

- Future Aspirations: Mentions integrating LCD display functionality and exemplifying more advanced behavior patterns in future projects.

The conclusion emphasizes the joy and educational value of combining robotics with programming, encouraging attendees to explore LEGO robotics while considering MRuby as a potential programming tool. Schönebaum hopes to see others join in the journey by bringing their LEGO sets to future events, possibly leading to a friendly competition in the form of a Mov Cup.

A journey to MRuby on LEGO robots
Torsten Schönebaum • June 01, 2021 • Rotterdam, Netherlands

Building bricks with MRuby: A journey to MRuby on LEGO robots

Constructing robots with LEGO is fun, programming them using Ruby even more. If you ever wanted to know how to start with MRuby on a device that can be changed into anything you can build with LEGO — this talk is for you.

Torsten Schönebaum - https://twitter.com/radlepunktde
EuRuKo 2019

EuRuKo 2019

00:00:05.920 Let's start by introducing our speaker, Torsten Schönebaum. It's worth noting that Torsten hasn't given a talk in 10 years, making this almost feel like a new experience for him. His children are quite jealous that he is here in Rotterdam while they are at home, especially since they cannot even play with their own LEGO. So, let's give a big round of applause for Torsten!
00:00:34.400 Thank you! My name is Torsten Schönebaum, and I work for Sage, a global business software company. I am involved with their accounting product, which is cloud-based software for small businesses. However, today I won't be discussing my work; instead, I want to talk about what I do at home.
00:01:06.479 Let’s start with a quote, as I've heard that it’s a great way to begin a talk. Friedrich Schiller, a German poet and philosopher, once said, 'Men only play when in the full meaning of the word he is a man and he is only completely a man when he plays.' Before anyone calls the code of conduct team, remember that Friedrich Schiller lived in the 18th century, and equal rights weren't a topic back then. The translation could be improved, so you should read it as 'humans only play.' What he means is that humans develop their cultural abilities during childhood through play, even enhancing these abilities later in life. Playing is crucial; it helps you learn new things and fosters creativity while engaging in activities without a direct goal or target.
00:01:58.560 This understanding provides a great excuse for receiving a rather large Christmas present—a box of LEGO Mindstorms Education. It's an even better excuse to buy another box of components. Inside the box, you will find numerous LEGO parts and, perhaps most importantly, the LEGO EV3 brick, which functions like a computer. You also get three motors—two large ones and a medium one—along with a collection of sensors. These include an ultrasonic sensor for measuring distances with precision, a geometry sensor for measuring angles, and a color sensor for detecting colors or measuring reflected light, which is particularly useful when programming a robot to follow a line.
00:02:28.239 Additionally, the set includes two touch sensors, which are essentially just buttons that you can press. Now, let's talk about building a robot. I brought a robot with me, and you can see it in the pictures. Building a robot often involves a trial-and-error process, which is a form of play. If you don't follow the instructions, you need to try various ideas until you find something that works; most of the time, it doesn't work on the first attempt.
00:03:02.800 For instance, the robot I have here took several attempts to get into a usable shape, but I was satisfied enough with the result to set it aside for later use. Now, what can robots built with LEGO do? In essence, you can create virtually anything, although some restrictions apply, such as the motors not being very powerful. You can build a robot that merely moves around, one that follows a line, or even one that navigates a maze. There is a competition known as RoboCup, where participants design robots that can rescue marbles from a maze, which is an impressive challenge.
00:03:49.920 Now, let's get back to the computer aspect since we're at a Ruby conference. The EV3 brick is based on an ARM CPU running at 300 MHz with 64 MB of RAM, which is ample for a small robot. It also includes a micro SD card slot and, while it has a small display, it isn’t particularly helpful. Let’s move on to our first demo. I've programmed the robot with a simple program.
00:05:06.240 As we switch to the browser, you can see a webpage loaded from the robot, displaying sensor measurements in real time. For instance, when I check the ultrasonic sensor readings, you’ll notice the values change as expected. Now, let's see the robot on the screen. Great! I can control it through my browser, and it even has sound functionality. Let's try moving it.
00:06:06.440 As you can hear, I need to stop the robot before it falls off the stage and move it back because I'll be doing another demo later. Now, how do we program this computer? For that, LEGO provides the EV3 Education app, which is free to download. It features a graphical programming interface based on LabVIEW, making it intuitive to use with live debugging and data logging capabilities. However, while it’s great for beginners, it's challenging to create complex applications, such as web services running on the robot, using that software.
00:06:56.640 The application is often slow and cumbersome for seasoned developers, making it less enjoyable for learners, too. This led me to search for alternatives. One option is the MakeCode environment from Microsoft, which offers browser-based programming, with the flexibility of graphical programming or text file editing in TypeScript. Unfortunately, it lacks live debugging features when used in tandem with the brick. I also came across Ignite, a Ruby gem, but it runs on your computer and produces bytecode understood by the EV3's default operating system.
00:07:51.120 However, I wanted to run Ruby directly on the brick, so I started exploring other options. I found EV3 Dev, which is a Debian-based Linux distribution for the EV3 brick. You can install it on an SD card without flashing the firmware, allowing you to easily remove it later. This is a big plus, as I can give the brick back to my kids without altering its original firmware. EV3 Dev includes drivers for almost all sensors and motors that come packaged with the brick, and it even has an SSH server, so I can access a console on the device.
00:08:33.520 I was particularly interested in leveraging Mov, a Ruby implementation designed for embedded devices, fitting the purpose perfectly. The 'm' in Mov suggests it is embeddable and lightweight, making it suitable for this small computer. Although it doesn’t implement every class and method from CRuby, it has a smaller footprint, leading to potentially quicker performance in certain tasks, an essential aspect for running a LEGO robot effectively.
00:09:21.840 I attempted to install Mov on the EV3 brick, initially expecting the simple command 'apt-get install mov' to work since the brick runs on a Debian-based operating system, and it succeeded. I was able to create a simple 'Hello World' program. However, I quickly discovered that the CPU doesn’t handle floating-point operations well, so I aimed to utilize the Rational class for integer operations. Unfortunately, I found that the Rational class wasn't available in Mov, prompting the need for further adjustments.
00:10:03.680 Given that Mov doesn't support Ruby gems directly due to the nature of being compiled and statically linked, all dependencies must be pre-defined in the build configuration. Instead, I turned my attention to MRuby gems, which function differently from traditional Ruby gems. To use them, you need to fetch the Mov source, set your build configuration, and specify the gems you want to include in your app.
00:10:47.760 In my build configuration example, I stated the dependencies I needed, like the Rational gem, and used a simple command to compile everything. However, the building process took a considerable amount of time on the EV3 brick—much longer than I had anticipated, leading to an eventual halt after around an hour of waiting.
00:11:29.360 Fortunately, there is an easier method; cross-compiling applications for the EV3 robot is a viable option. The excellent folks behind EV3 Dev provided a Docker image that simplifies this process. By booting up a Docker container on my local machine, I was able to install the necessary Debian packages, make slight adjustments to the build configuration, and compile cross-compiled applications efficiently.
00:12:31.600 I wrote a series of scripts to access the compiler shell to facilitate this process. Eventually, I managed to build a MRuby interpreter, alongside some necessary libraries, and transferred the compiled binaries back to the robot using SCP. SSHing into the EV3 brick console allowed me to execute the commands, and successfully confirmed that the Rational class was operational within the environment.
00:13:40.640 I was quite impressed with my progress up to this point. However, I wanted to take things further. For my simple Ruby scripts, I decided to create a tailored application. Mov allows you to work with bytecode, which the interpreter generates and executes. Additionally, there are tools that can convert this bytecode into object files in a C format, allowing you to yield a binary from your Ruby code.
00:14:54.160 The boilerplate required for this is straightforward, involving rarely more than 50 lines of code, which initializes the MRuby state and executes the bytecode. My ultimate goal was to automate the build process, so I set up a makefile that would generate boilerplate code for any Ruby application designed for the robot while also compiling it into a binary.
00:15:58.160 Now that I could produce binaries, I needed to handle how to interact with the robot's motors and sensors. The EV3 Dev setup relies on the sysfs system in Linux for communication. This implies each sensor and motor has a dedicated directory containing files for sensor values and motor states, allowing simple read/write operations for respective components.
00:17:31.760 Utilizing the Mov input/output capabilities, implementing reads and writes is straightforward, such as obtaining distance values from the ultrasonic sensor or sending commands to control the motors. For instance, you can read the maximum speed of a motor, set your desired speed, and dispatch a command to the motor to run indefinitely until a stop command triggers.
00:18:15.360 Having accomplished sensor input and motor control, I didn't want only to create one program for the robot; I also sought to continually build new configurations, rather than repeating code for accessing sensors and motors. So, I developed an MRuby gem of my own, simplifying my future development process.
00:19:04.080 Creating a custom gem is relatively simple; you create a Rakefile and a specification directory, list your dependencies, and even define build instructions. After setting the parameters for compiling, I formatted my Ruby files within an 'mrblib' directory and C files within a source directory. Following this structure allows me to reference my custom gem during building.
00:20:05.920 To control the robot, I can instantiate a motor by specifying its corresponding port. I developed a movement controller to calculate the distance a robot travels based on wheel rotations. For a wheel with a circumference of 173 millimeters, I could formulate the distance moved by the robot effectively. I also integrated sound capabilities and LED controls into the setup, using the various sensors available to facilitate interactions.
00:21:09.360 Reflecting on my experience with developing this Mov setup, I found that it wasn't always easy to identify which methods from C were supported within Mov. However, through accessing API documentation and examining the source code, I could grasp the mechanics behind its functions.
00:22:31.840 I encountered challenges while constructing my own gem, as utilizing different classes for sensors and motors escalated to an overwhelming number of files. I learned that order is paramount when referencing classes to avoid undefined errors, leading me to structure my files carefully to maintain the desired sequence during loading.
00:23:56.480 Running into operational difficulties within movie on the robot showed some limitations in terms of speed, particularly regarding recognition of altered sensor values. Yet, most operational aspects performed sufficiently well, primarily due to the EV3 hardware constraints.
00:25:09.760 Looking forward, I want to delve into concepts like concurrency within my projects, as I aim to trigger actions based on button presses without blocking the program. Furthermore, I'm eager to integrate display functionality, despite its small size, as it's powered by a Linux framebuffer needing a C code interface.
00:27:00.640 As we wrap up, let's take a look at the results of one of the robot's tasks. Thank you all for your attention. I hope some of you will bring your own LEGO set next year, and we might even host a Mov Cup!
00:27:17.520 Thank you very much! I appreciate your kind words and support.
Explore all talks recorded at EuRuKo 2019
+12