ホストベースなirbでmrubyとラジコンと戯れる

@bovensiepenさんという方がArduino Due上でmirbを動かしています。

http://blog.mruby.sh/201305201003.html

自分も以前chipKITMax32で試したのですが、メモリが厳しすぎてまともに動きませんでした。どうもコンパイルするときにメモリをかなり使ってしまうみたいなので、こんどはホストベースのmirbというのをやってみました。

動画

デモ動画。無駄に長いので、暇な人以外は後半だけどうぞ。無謀にも英語に挑戦してます。

説明

https://cacoo.com/diagrams/EmmKpYRK6YEvRwcE-44F09.png
図のまんまですが、ホスト側とボード側両方でmrubyを動かし、ホスト側でコンパイルしてボードに送ってます。ホストが(も)頑張るからホストベースと読んでいます。ボード側のメモリ消費量は多少抑えられたはず。
動画でタイピングは激しくミスってますが、文法エラーはホスト上のコンパイラで、実行時エラー(例外)はボード上で一応ちゃんと検知されます。

いじったところ

ちなみにmrubyのソースコードは本日最新版に一行だけ変更してます(バイトコードを読み込む部分ですが、いまいち自信なし)。→(2013/6/25追記:)mruby本体に同様の変更が取り込まれたので、修正なしで動くようになりました。

それ以外はbuild_config.rbでいくつかの機能をDisableにしました。

MRuby::CrossBuild.new("chipKitMax32") do |conf|
  toolchain :gcc

  # Mac OS X
  MPIDE_PATH = '/Applications/Mpide.app/Contents/Resources/Java'
  # GNU Linux
  # MPIDE_PATH = '/opt/mpide-0023-linux-20120903'

  PIC32_PATH = "#{MPIDE_PATH}/hardware/pic32"  

  conf.cc do |cc|
    cc.command = "#{PIC32_PATH}/compiler/pic32-tools/bin/pic32-gcc"
    cc.include_paths << ["#{PIC32_PATH}/cores/pic32",
                        "#{PIC32_PATH}/variants/Max32",
                        "#{PIC32_PATH}/libraries"]
    cc.flags = %w(-O2 -mno-smart-io -w -ffunction-sections -fdata-sections -g -mdebugger -Wcast-align 
                -fno-short-double -mprocessor=32MX795F512L -DF_CPU=80000000L -DARDUINO=23 -D_BOARD_MEGA_ 
                -DMPIDEVER=0x01000202 -DMPIDE=23)

    #some adjustment for mruby to reduce heap usage.
    #cc.flags << %w(-DMRB_USE_FLOAT) #動かん
    cc.flags << %w(-DMRB_HEAP_PAGE_SIZE=64)
    #cc.flags << %w(-DMRB_WORD_BOXING) #動かん
    cc.flags << %w(-DMRB_USE_IV_SEGLIST)
    cc.flags << %w(-DKHASH_DEFAULT_SIZE=8)
    cc.flags << %w(-DMRB_STR_BUF_MIN_SIZE=20)
    #cc.flags << %w(-DDISABLE_STDIO) #don't disable STDIO, to use mrb_read_irep_file()
    cc.flags << %w(-DMRB_GC_STRESS)   #no document
    cc.flags << %w(-DPOOL_PAGE_SIZE=1000) #effective only for use with mruby-eval
    cc.compile_options = "%{flags} -o %{outfile} -c %{infile}"
  end

  conf.cxx do |cxx|
    cxx.command = conf.cc.command.dup
    cxx.include_paths = conf.cc.include_paths.dup
    cxx.flags = conf.cc.flags.dup
    cxx.compile_options = conf.cc.compile_options.dup
  end

  conf.archiver do |archiver|
    archiver.command = "#{PIC32_PATH}/compiler/pic32-tools/bin/pic32-ar"
    archiver.archive_options = 'rcs %{outfile} %{objs}'
  end

  #no executables
  conf.bins = []

  conf.gem :core => "mruby-print" 
  conf.gem :core => "mruby-math"
  conf.gem :core => "mruby-random"
  #conf.gem :core => "mruby-eval"   #一応動いた
  conf.gem :github => "masamitsu-murase/mruby-hs-regexp", :branch => "master" #動いた。素晴らしい 

  conf.gem :github => "kyab/mruby-arduino", :branch => "master"

  conf.build_mrbtest_lib_only

end

MRuby::Build.new do |conf|
  # load specific toolchain settings
  toolchain :clang

  # include the default GEMs
  conf.gembox 'default'

  conf.cc.flags << [ENV['CFLAGS'] || %w(-DENABLE_READLINE -g)]   #-arch i386 -arch x86_64
  conf.cc.compile_options = "%{flags} -o %{outfile} -c %{infile}"  #building universal binary dont allow -M

  conf.linker.libraries << "edit"   #OSX readline compatible library
  
  conf.gem :github => "kyab/mruby-bin-mirb-hostbased", :branch => "master"

end

ソース

動画の中で紹介しているmirb(mruby-bin-mirb-hostbased)とmruby-arduinoは以下に置いています。

https://github.com/kyab/mruby-bin-mirb-hostbased (MPIDE用のスケッチ付き)
https://github.com/kyab/mruby-arduino

*

いろいろやってるとそのうちメモリ不足で止まってしまいますが、どうもmrb_funcall()で返ってきたmrb_valueGCに開放させる方法がわかりません。。

あと、USBでつながったままでどうやってラジコンを走らせるんだという話もありますが、そこはちょっと工夫しています。それは次回。