KEMBAR78
mruby/c and data-flow programming for small devices | PDF
mruby/c and data-flow
programming for small devices
Kazuaki Tanaka
About me
• Kazuaki Tanaka
• Kyushu Institute of Technology
• Lectures:
Data Structure, Image Processing
• Research:
Embedded Systems, IoT, Wireless
communication
Activities
• Ruby Association, mruby Forum, Ruby
Business Council(Ruby協)
• mruby, mruby/c committer
Research on Wireless Communication
• LPWA (Low Power Wide Area network)
– Ultra-narrow band communication
– Protocol design, Firmware
– Application, Pilot experiment
– Low-cost network for local government
Disaster Prevention
Land slides in Fukuoka
2020
Pilot experiment
• Rain/Wind measurement
– Flood alert, land slide alert
Topic today
• Programming environment for small devices
– Using mruby/c
– For IoT applications
DEMO
DEMO
• Blinking two LEDs
• Talk about backside of this DEMO
– What’s mruby/c ?
– Data-flow programming
– Integrating mruby/c and data-flow
mruby/c
Ruby for small devices
• Programming language Ruby
– Object oriented programming language
• mruby, mruby/c
– Apply Ruby features for embedded software
– Small memory footprint
Concept of mruby
• Reduce memory footprint in execution
– mruby Compiler + mruby VM
– Compile source code in PC
– Execute compiled code in target device
• Extensible libraries “mrbgems”
– Easy to add new features to mruby
Ruby, mruby, mruby/c
Ruby
1 MB~ 128KB~ 20KB~
Memory
Language
Target
device
Targeting: Small devices
• Low power, Low cost
• Long-time operation
• mruby/c provides,
– Memory management, object reference counter
– Power management
– (Semi-) real-time operation
RBoard
• mruby/c ready microcontroller board
– PIC32MX170F256B
SRAM 64KB
Flash 256KB
– I/O
Digital In, Out
ADC, PWM, UART, I2C
You can order @ chip1stop
島根情報処理センター
mruby VM
ary = [2, 4.1, 0.5, 3]
ary.sort.each do |data|
puts data
end
00000000: 52 49 54 45 30 33 30 30 00 00 00 d6 4d 41 54 5a RITE0300....MATZ
00000010: 30 30 30 30 49 52 45 50 00 00 00 98 30 33 30 30 0000IREP....0300
00000020: 00 00 00 64 00 03 00 08 00 01 00 00 00 00 00 2f ...d.........../
00000030: 0b 01 51 04 00 01 05 01 09 06 40 05 52 04 2d 03 ..Q.......@.R.-.
00000040: 00 01 08 03 0a 04 07 05 09 06 48 02 03 04 01 03 ..........H.....
00000050: 02 2f 03 01 00 57 04 00 30 03 02 00 38 03 69 00 ./...W..0...8.i.
00000060: 01 00 00 08 61 20 2a 20 33 20 3d 20 00 00 03 00 ....a * 3 = ....
00000070: 04 70 75 74 73 00 00 04 73 6f 72 74 00 00 04 65 .puts...sort...e
00000080: 61 63 68 00 00 00 00 28 00 03 00 06 00 00 00 00 ach....(........
00000090: 00 00 00 0d 34 04 00 00 01 04 01 2d 03 00 01 38 ....4......-...8
000000a0: 03 00 00 00 01 00 04 70 75 74 73 00 4c 56 41 52 .......puts.LVAR
000000b0: 00 00 00 22 00 00 00 03 00 01 61 00 03 61 72 79 ..."......a..ary
000000c0: 00 04 64 61 74 61 00 00 00 01 00 02 ff ff 45 4e ..data........EN
000000d0: 44 00 00 00 00 08 D.....
Ruby source code
Compile
mruby bytecode
mruby VM
on target device
Execute
mruby compiler
Inside RBoard microcontroller
RBoard Hardware
HAL: Hardware Abstraction Layer
mruby/c VM
Application Bytecode
Transfer from PC
Firmware
mruby/c on github
• VM
• HAL
– ESP32
– PIC24
– POSIX
– PSoC5LP
– RP2040 (Raspberry Pi Pico)
– STM32
https://github.com/mrubyc/mrubyc
Data-flow programming
Visual Programming
Block Programming
Data-flow Programming
Block Programming
Smalruby
• Education
• Entry to procedural programming
loop do
puts( "mruby" )
sleep(1)
end
Block Programming
Node-RED
• IoT
?
Inject node
Process nodes
Nodes
Wires
Inject and Process
QUEUE
Inject: Create a packet, store into Queue
Process: Retrieve a packet from Queue, process, store into Queue
Packet = {
destination node,
payload
}
Integrating mruby/c and data-flow
How to ?
nodered2mruby
• Input= Data-flow description
– Node-RED JSON
• Output= mruby/c
– Bytecode (or mruby source)
[
{
"id": "ef2b6bb33e489cef",
"type": "inject",
"z": "f6f2187d.f17ca8",
"name": "",
"props": [],
"repeat": "1",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"x": 130,
"y": 100,
"wires": [
[
"7438f9109319b2e9"
]
]
},
・・・・・
]
DEMO Program
Injection 1.0 [s]
Injection 2.5 [s]
Green LED
Red LED
$queue = []
# main loop
while true do
# inject
injects.each_index do |idx|
inject_node injects[idx]
end
# process queue
msg = $queue.first
if msg then
$queue.delete_at 0
idx = find_target(nodes, msg)
if idx then
process_node nodes[idx], msg
else
puts "node not found: #{msg[:id]}"
end
end
# next
sleep LoopInterval
end
Pseudo code
Conclusion
• This work is continuing,
• Future work (or just idea)
– Dispatching performance
– Distributed system, IoT
– Deploying
Acknowledgements
• murby/c developers
• itoc 「しまねソフト研究開発センター」
• Kyutech students
• National Institute of Technology Matsue
College students
References
• https://github.com/mrubyc/
• https://github.com/nodered-mrubyc/
• K. Tanaka, S. Ogura and R. Krishnamoorthy,
"Hybrid IoT Programming with Data Flow
Diagrams and mruby," 2024 IEEE Conference on
Dependable, Autonomic and Secure Computing
(DASC), Boracay Island, Philippines, 2024, pp. 78-
82, doi: 10.1109/DASC64200.2024.00017.

mruby/c and data-flow programming for small devices

  • 1.
    mruby/c and data-flow programmingfor small devices Kazuaki Tanaka
  • 3.
    About me • KazuakiTanaka • Kyushu Institute of Technology • Lectures: Data Structure, Image Processing • Research: Embedded Systems, IoT, Wireless communication
  • 4.
    Activities • Ruby Association,mruby Forum, Ruby Business Council(Ruby協) • mruby, mruby/c committer
  • 5.
    Research on WirelessCommunication • LPWA (Low Power Wide Area network) – Ultra-narrow band communication – Protocol design, Firmware – Application, Pilot experiment – Low-cost network for local government
  • 6.
  • 7.
    Pilot experiment • Rain/Windmeasurement – Flood alert, land slide alert
  • 8.
    Topic today • Programmingenvironment for small devices – Using mruby/c – For IoT applications
  • 9.
  • 10.
    DEMO • Blinking twoLEDs • Talk about backside of this DEMO – What’s mruby/c ? – Data-flow programming – Integrating mruby/c and data-flow
  • 11.
  • 12.
    Ruby for smalldevices • Programming language Ruby – Object oriented programming language • mruby, mruby/c – Apply Ruby features for embedded software – Small memory footprint
  • 13.
    Concept of mruby •Reduce memory footprint in execution – mruby Compiler + mruby VM – Compile source code in PC – Execute compiled code in target device • Extensible libraries “mrbgems” – Easy to add new features to mruby
  • 14.
    Ruby, mruby, mruby/c Ruby 1MB~ 128KB~ 20KB~ Memory Language Target device
  • 15.
    Targeting: Small devices •Low power, Low cost • Long-time operation • mruby/c provides, – Memory management, object reference counter – Power management – (Semi-) real-time operation
  • 16.
    RBoard • mruby/c readymicrocontroller board – PIC32MX170F256B SRAM 64KB Flash 256KB – I/O Digital In, Out ADC, PWM, UART, I2C You can order @ chip1stop 島根情報処理センター
  • 17.
    mruby VM ary =[2, 4.1, 0.5, 3] ary.sort.each do |data| puts data end 00000000: 52 49 54 45 30 33 30 30 00 00 00 d6 4d 41 54 5a RITE0300....MATZ 00000010: 30 30 30 30 49 52 45 50 00 00 00 98 30 33 30 30 0000IREP....0300 00000020: 00 00 00 64 00 03 00 08 00 01 00 00 00 00 00 2f ...d.........../ 00000030: 0b 01 51 04 00 01 05 01 09 06 40 05 52 04 2d 03 ..Q.......@.R.-. 00000040: 00 01 08 03 0a 04 07 05 09 06 48 02 03 04 01 03 ..........H..... 00000050: 02 2f 03 01 00 57 04 00 30 03 02 00 38 03 69 00 ./...W..0...8.i. 00000060: 01 00 00 08 61 20 2a 20 33 20 3d 20 00 00 03 00 ....a * 3 = .... 00000070: 04 70 75 74 73 00 00 04 73 6f 72 74 00 00 04 65 .puts...sort...e 00000080: 61 63 68 00 00 00 00 28 00 03 00 06 00 00 00 00 ach....(........ 00000090: 00 00 00 0d 34 04 00 00 01 04 01 2d 03 00 01 38 ....4......-...8 000000a0: 03 00 00 00 01 00 04 70 75 74 73 00 4c 56 41 52 .......puts.LVAR 000000b0: 00 00 00 22 00 00 00 03 00 01 61 00 03 61 72 79 ..."......a..ary 000000c0: 00 04 64 61 74 61 00 00 00 01 00 02 ff ff 45 4e ..data........EN 000000d0: 44 00 00 00 00 08 D..... Ruby source code Compile mruby bytecode mruby VM on target device Execute mruby compiler
  • 18.
    Inside RBoard microcontroller RBoardHardware HAL: Hardware Abstraction Layer mruby/c VM Application Bytecode Transfer from PC Firmware
  • 19.
    mruby/c on github •VM • HAL – ESP32 – PIC24 – POSIX – PSoC5LP – RP2040 (Raspberry Pi Pico) – STM32 https://github.com/mrubyc/mrubyc
  • 20.
  • 21.
  • 22.
    Block Programming Smalruby • Education •Entry to procedural programming loop do puts( "mruby" ) sleep(1) end
  • 23.
  • 24.
  • 25.
    Inject and Process QUEUE Inject:Create a packet, store into Queue Process: Retrieve a packet from Queue, process, store into Queue Packet = { destination node, payload }
  • 26.
  • 27.
  • 28.
    nodered2mruby • Input= Data-flowdescription – Node-RED JSON • Output= mruby/c – Bytecode (or mruby source)
  • 29.
    [ { "id": "ef2b6bb33e489cef", "type": "inject", "z":"f6f2187d.f17ca8", "name": "", "props": [], "repeat": "1", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "x": 130, "y": 100, "wires": [ [ "7438f9109319b2e9" ] ] }, ・・・・・ ]
  • 30.
    DEMO Program Injection 1.0[s] Injection 2.5 [s] Green LED Red LED
  • 31.
    $queue = [] #main loop while true do # inject injects.each_index do |idx| inject_node injects[idx] end # process queue msg = $queue.first if msg then $queue.delete_at 0 idx = find_target(nodes, msg) if idx then process_node nodes[idx], msg else puts "node not found: #{msg[:id]}" end end # next sleep LoopInterval end Pseudo code
  • 32.
    Conclusion • This workis continuing, • Future work (or just idea) – Dispatching performance – Distributed system, IoT – Deploying
  • 33.
    Acknowledgements • murby/c developers •itoc 「しまねソフト研究開発センター」 • Kyutech students • National Institute of Technology Matsue College students
  • 34.
    References • https://github.com/mrubyc/ • https://github.com/nodered-mrubyc/ •K. Tanaka, S. Ogura and R. Krishnamoorthy, "Hybrid IoT Programming with Data Flow Diagrams and mruby," 2024 IEEE Conference on Dependable, Autonomic and Secure Computing (DASC), Boracay Island, Philippines, 2024, pp. 78- 82, doi: 10.1109/DASC64200.2024.00017.