Ruby Community
mruby/c: Running on Less Than 64KB RAM Microcontroller

Summarized using AI

mruby/c: Running on Less Than 64KB RAM Microcontroller

HASUMI Hitoshi • November 18, 2019 • Nashville, TN

In the talk titled "mruby/c: Running on Less Than 64KB RAM Microcontroller" presented by HASUMI Hitoshi at RubyConf 2019, the speaker explores the capabilities of mruby/c, a lightweight implementation of Ruby designed for microcontrollers with less than 64KB of RAM.

Key Points Discussed:

  • Introduction to mruby/c: The speaker defines mruby/c as a compact Ruby implementation suited for embedded systems, emphasizing its efficiency in handling tasks that traditional Ruby implementations cannot due to memory constraints.
  • Microcontroller Benefits: Various advantages of using microcontrollers in applications, particularly in production environments, are presented. Key points include:
    • Immediate startup on power supply.
    • Reduced security vulnerabilities due to lack of operating systems.
    • Low energy consumption, suitable for battery or solar power.
  • Case Study – IoT for Sake Brewing: Hitoshi showcases an IoT system he developed for Asahi Shuzo sake brewery to monitor temperature and CO2 levels during sake brewing. This system features:
    • Use of Cypress PSoC 5 LP microcontroller for real-time tracking of brewing conditions.
    • A mobile app interface displaying critical environmental data.
  • Technical Details of mruby/c: The talk covers the structure and operation of mruby/c, explaining its lack of certain dynamic features found in standard Ruby implementations, while still being practical for microcontroller applications.
  • Development Tools: Hitoshi introduces several tools he has developed for enhancing the mruby/c programming experience:
    • UBC Utils for application setup.
    • MRuby Test for unit testing.
    • mruby/c Debugger for monitoring execution.
  • Demonstration and Real-time Data: A demonstration involving the measurement of CO2 levels is provided, emphasizing the importance of maintaining safe atmospheric conditions during the brewing process.

Conclusion and Takeaways:

Hitoshi concludes the presentation by reiterating the practicality and versatility of mruby/c for embedded programming. He encourages developers to explore the unique opportunities that mruby/c offers, especially in environments constrained by hardware limitations. The entire talk is infused with a personal touch, reflecting Hitoshi's passion for both brewing sake and developing innovative technology solutions using Ruby.

The audience is motivated to learn more about both Ruby and embedded system programming, particularly within the context of IoT applications.

mruby/c: Running on Less Than 64KB RAM Microcontroller
HASUMI Hitoshi • November 18, 2019 • Nashville, TN

RubyConf 2019 - mruby/c: Running on Less Than 64KB RAM Microcontroller by HASUMI Hitoshi

I will show you an actual IoT product which uses one-chip-microcontroller-based hardwares for a Japanese Sake Brewery. Brewing Sake requires workers to take temperature of the ingredients very frequently. mruby/c is a small implementation of Ruby and runs on microcontroller which has less than 64KB RAM. You will see a new Ruby World.

#confreaks #rubyconf2019

RubyConf 2019

00:00:12.769 Hello everybody! It is a great honor to have such an opportunity at RubyConf. I'm really, really nervous because I didn't expect such a great hall.
00:00:20.900 I should give a talk. The title of my talk is 'mruby/c: Running on Less Than 64KB RAM Microcontroller'.
00:00:28.289 All right, I'm Hasumi. You can call me Hitoshi. I live in Matsue City, which is the holy place of Ruby because Matz lives there.
00:00:37.550 As you may know, I love sake, and I also enjoy coffee and soba. Although many foreign people—especially Americans—are familiar with soba, not everyone seems to know it.
00:01:02.209 I'm currently working at a company called Aha, which has many branches around the world. One of them is based in the U.S. and has created a restaurant called Shake Shack. I haven't tried it yet, but I'm planning to.
00:01:16.950 I usually participate in Matsue Ruby events. This castle, which I mentioned earlier, was built in 1611, and now let's move on to my subject, Chapter 1 – Introduction.
00:01:40.080 Let me explain some terms we should know first. mruby/c is a language implementation that I will talk about today. I will refer to it as 'mruby/c' or 'mrb/c' for convenience.
00:01:52.050 A microcontroller is a small computer that contains a CPU, memory, and programmable peripherals. RTOS stands for Real-Time Operating System, which is typically used for microcontrollers, but I won’t be using it in my discussion today.
00:02:11.000 The term 'task' is almost equivalent to 'thread' in Linux. Keep this in mind, as it is very important. Asahi Shuzo is one of the best Japanese sake breweries.
00:02:39.500 They are not only famous for their sake but also have no association with Asahi Beer. I've developed an IoT system using mruby/c, and now let me explain why I chose to use microcontrollers.
00:02:59.200 I didn’t opt for single-board computers like Raspberry Pi for production environments. My motivation for using microcontrollers is that they start immediately after power is supplied. In the context of users—brewery workers—they can operate these devices very simply, like home electrical appliances.
00:03:37.340 All they need to do is turn them off and turn them on—that's it! Microcontrollers can run without an operating system, including RTOS, Linux, or Windows. This is often referred to as 'bare metal' programming.
00:03:51.050 By using microcontrollers, we can narrow down the list of security issues since most vulnerabilities are associated with operating systems like Linux or Windows. You won’t have to deal with unnecessary downtime or app degradation after updates. This is an important factor.
00:04:34.010 Moreover, microcontrollers are low-energy and can be powered by small batteries or solar cells, making them suitable for mass production. You can select the appropriate chipset for your application to minimize costs.
00:05:01.180 There are many different models of microcontrollers available. I will introduce two of them. The first is the Cypress PSoC 5 LP, which I used for the sake brewery. It has 64 KB of RAM, which is the target size for the mruby/c implementation.
00:05:40.290 This can be represented with a 16-bit integer. The second one you may know is the USB 32, which has recently gained popularity. Later, I will show you a demonstration of my IoT project, which I implemented for sake brewing.
00:06:06.090 I used the PSoC 5 LP for this project, and it was delivered to our actual brewery in January 2018. Our devices track the temperature of sake ingredients during brewing, as well as the surrounding temperature and humidity.
00:06:21.820 The server displays this data on a mobile app. Right now, we are steaming rice in a thermostat-controlled room. The rice can absorb natural lactic acid from the air and the wooden surfaces.
00:06:51.500 Currently, this is the first version of an IoT device we built to sense temperature. This is the second version, which looks much cooler. This is the smartphone app that shows the temperature, indicating that the ingredient is above 42 degrees Celsius.
00:07:12.850 Forty-two degrees Celsius is approximately equivalent to 107.6 degrees Fahrenheit, which is very hot. By the way, working in the field of IoT can be urgent and stressful. Just imagine needing to travel between a dark, cold storage cellar and a humid, very hot rice-drying room.
00:07:36.690 It's crucial to have efficient processes because brewing sake is a race against time. What if you had to mend your firmware using a small laptop on-site? Just imagine how Ruby’s expressiveness and agility could help in these situations!
00:08:01.370 I'm excited to share with you that this is the first—and only—sake labeled with Ruby. This sake was brewed using Ruby! I am not sure if you'll have a chance to taste it, but I hope you can try it if you come to Ruby Kaigi 2020.
00:08:14.300 I hope you all can come to Japan in April next year, as it's the best season to visit.
00:08:25.600 Now, let me show you a demo regarding CO2 concentration. The CO2 concentration of 4400 ppm is considered atmospheric level, and when it reaches 1000 ppm, your cognitive speed decreases.
00:08:44.400 Tomatoes require CO2 for photosynthesis, and levels above 2000 ppm can make you feel sleepy or give you a headache, reducing your activity levels. If it reaches 40,000 ppm, it could be dangerous.
00:09:09.360 Here you can see the CO2 sensor, which is showing our current atmospheric CO2 level.
00:09:36.820 It currently reports 1294 ppm. My device will continue measuring CO2 concentration, as levels can increase with breathing.
00:10:02.090 I can prove that CO2 levels will rise if you fall asleep right now.
00:10:11.360 By the way, there are many delightful challenges in IoT development, such as working with peripheral equipment, circuit wiring, and housing problems. It also includes issues related to printed circuit boards and soldering.
00:10:40.590 Additionally, programming with C or Ruby or MicroPython can be necessary, as well as network programming. I will cover a bit of all of these today.
00:10:57.090 Peripheral equipment is key to making sure all components work correctly according to their spec sheets. Always check whether the equipment is functioning before you begin writing your application.
00:11:24.740 Combining components with careful consideration is crucial to making everything work together properly. I often build prototypes just to experiment with Raspberry Pi, and I see whether the concepts hold true for my needs.
00:11:48.560 This is a service created to facilitate communication with hardware via a serial protocol. Using Ruby on mruby, we can communicate easily with our devices.
00:12:12.200 For example, if I ask the module something, it will respond with a relevant output based on our coded commands. For instance, if I say '80', it understands the command and can execute it.
00:12:32.280 If I add an ID to that command, it applies a unique identifier, and if I send a command that does not exist, it will return an error.
00:12:50.900 Soldering can be one of the most challenging aspects of making these devices. The parts often have very small connections.
00:13:08.950 Discovering hardware bugs can be much more difficult than software ones. I remember my teacher used to say that all electronic designs must be very precise.
00:13:43.120 Now, let’s move to Chapter 2: mruby/c. What is mruby/c? It is another implementation of Ruby that’s particularly suited for embedded systems. It is easily combined with other system programming languages like C, C++, Rust, and Go.
00:14:11.199 The beauty of mruby/c is that it is compact and capable, specially developed for single-chip microcontrollers.
00:14:36.480 This slide shows a comparison between mruby and mruby/c from the viewpoint of processing stacks.
00:14:52.540 If we compare the bytecode produced, you’ll find that they are quite similar, as we compile them with mrbc. However, the VMs (Virtual Machines) differ, and the way tasks are managed varies.
00:15:31.520 The mruby/c VM has its own mechanism for managing multitasking. It is important to note that mruby/c is designed to consume considerably less RAM compared to the general mruby implementation.
00:15:52.030 This efficiency allows mruby/c to run on devices with as little as 64KB or even 32KB of RAM.
00:16:06.130 It also has fewer functionalities compared to mruby. For instance, mruby/c does not support modules, and therefore does not have kernel modules.
00:16:23.030 You might wonder how specific features are implemented within mruby/c despite these limitations.
00:16:39.740 It is interesting to note that mruby/c lacks dynamic features such as method missing or test point functionalities commonly found in higher-level Ruby implementations.
00:17:05.430 In conclusion, despite limited features, mruby/c remains practical for microcontroller applications. I believe it is sufficient for most developers' needs.
00:17:30.240 Now, I will illustrate the basics of using mruby/c. We write our application logic in Ruby, and the compiler converts those Ruby files into C.
00:17:56.050 This code is primarily boilerplate that initializes the environment. After initializing, we can start working on multitasking capabilities.
00:18:22.520 The mruby/c runtime functions allow us to execute tasks concurrently. You might be a little disappointed to learn that we still have to write some C code.
00:18:56.210 However, don't worry—this C code is mostly boilerplate, and anyone can manage it.
00:19:22.470 In Chapter 3, we will discuss applications and tools. You can view the repository of today’s demo later for reference.
00:19:43.570 Now, let’s check how the actual code looks; it contains infinite loops with three models: CO2, LED, and thermistor.
00:20:20.570 The main loop checks if the CO2 level is above a certain threshold, and if so, it triggers the LED to flash.
00:20:36.880 The LED module's operation is easy: we set a pin high to turn it on and low to turn it off.
00:21:00.570 The secret to the simplicity of the LED's operation lies in the C code implementation behind it.
00:21:27.060 Embedding Ruby functions within C code grants powerful features for controlling hardware.
00:21:45.320 Let’s explore how to retrieve values from the CO2 sensor correctly.
00:22:04.440 For CO2 data, we write commands specific to the microcontroller, allowing us to retrieve values seamlessly.
00:22:25.810 On the Ruby side, I made use of the array object to work with the retrieved data, which makes the process effective and efficient.
00:22:52.670 As we can see, the CO2 model calculates CO2 concentration based on the data that was processed from the C code.
00:23:30.460 The next step is to check if the CO2 sensor is functioning correctly, which is a simple tip to reduce memory usage.
00:23:59.020 C functions can return a string object instead of an array object to optimize performance when memory is limited.
00:24:35.390 The Ruby model works effectively, thanks to the flexible nature of Ruby itself.
00:25:15.220 In the infinite loop, data that is retrieved from the sensors is sent to the server by concatenating string objects.
00:25:40.980 This approach is significantly easier than processing data in C.
00:26:06.670 Now let me introduce several tools that can aid in developing applications on mruby/c.
00:26:38.220 I created a few tools to streamline mruby/c development.
00:26:56.830 UBC Utils is a command-line utility tool I created to help set up boilerplate applications.
00:27:18.500 It features various subcommands that provide users access to class lists and methods specific to mruby/c.
00:27:45.460 Next is 'MRuby Test,' an official unit testing framework for mruby/c, offering a simple way to perform unit tests.
00:28:13.040 It gathers information from the code and allows users to write and run tests easily, compiling everything down to one executable.
00:28:43.420 Lastly, 'mruby/c Debugger' provides a graphical interface for monitoring execution flow, global variables, and tasks.
00:29:06.080 You can also set breakpoints to analyze local variables effectively.
00:29:22.490 Now, let’s go back to the demo and observe the CO2 level.
00:29:59.680 Currently, the CO2 concentration is around 1231 ppm.
00:30:24.640 This indicates that ventilation needs to be reviewed since fresh air is essential for optimal conditions.
00:31:02.760 In conclusion, thank you for your attention and for being a wonderful audience!
Explore all talks recorded at RubyConf 2019
+88