Rubotz

Rubotz is a Ruby library which generates and compiles NXC programs for Lego Mindstorms NXT robots.

You can program your robot in Ruby, including the compilation phase, and then simply copy the generated executable from your computer to your Mindstorms robot with third-party programs such as NXTBrowser.

Proof-Of-Concept Release Only

Rubotz v0.0.1 is a proof-of-concept release. It implements only a subset of available motor functionality and only one sensor (the ultrasonic sensor). Mindstorms ships with 4 sensors and additional third-party sensors are available as well. NXC supports a multi-threaded model, which is very cool; Rubotz currently only supports one (“main”) thread.

Support for multiple threads is planned; support for all stock sensors and the full range of motor functionality is planned as well.

Documentation Is For Computers To Read

I don’t really believe in documentation; I believe that your documentation should consist entirely of your specs. Specs are documentation which you can run as code, and therefore they are self-verifying documentation; any other kind of documentation is really just a statement of opinion. However, I definitely want people who are interested to be able to use the software!

The specs dir absolutely contains the best documentation, but I’ve also made an effort to augment that with text and RDoc. However, if you’re confused about something, the specs are *by definition* the canonical example of how to do it.

How To Program Your Robot In Ruby

Rubotz.compile takes an options hash, using it as faux keyword arguments in the Rails style. Rubotz.program takes a block, with Ruby code which it then translates into NXC.

The examples directory contains an example as runnable code - logically enough - but I’m repeating it here to demonstrate.

This code is based on a robot I built which aims an ultrasonic sensor at the floor and drives forward, backing away and turning if it encounters a dropoff indicating a table edge.

Check it out on YouTube: www.youtube.com/watch?v=uQ4BwxH7uoY (it’s pretty straightforward).

Rubotz.compile :file => "rubotz",
               :program => (Rubotz.program do
                              the_robot "drives around the table scanning its ultrasonic sensor"
                              initializing do
                                short ultrasonic_sensor
                                activate(UltrasonicSensor.port(4))
                              end
                              looping do
                                clear_screen
                                monitoring(UltrasonicSensor) do |ultrasonic_sensor|
                                  ultrasonic_sensor.> 30 do
                                    display :line => 7, :text => "Danger"
                                    display :line => 8, :number => ultrasonic_sensor
                                    sound "! Attention.rso"
                                    motor :synchronized => true,
                                          :direction => :reverse,
                                          :ports => [:B, :C],
                                          :power => 25,
                                          :turn => 0
                                    wait 1500
                                    motor :synchronized => true,
                                          :direction => :reverse,
                                          :ports => [:B, :C],
                                          :power => 25,
                                          :turn => 100
                                    wait 325
                                  end
                                  ultrasonic_sensor.<= 30 do
                                    display :line => 7, :text => "Safe"
                                    display :line => 8, :number => ultrasonic_sensor
                                    motor :synchronized => true,
                                          :direction => :forwards,
                                          :ports => [:B, :C],
                                          :power => 35,
                                          :turn => 5
                                  end
                                end
                                wait 25
                              end
                            end)

The above code successfully generates NXC code for the table robot, and successfully compiles that code using my NXC install into a format which successfully runs on my Mindstorms robot brain. To transfer the generated code to the robot brain, I use NXTBrowser on OS X:

web.mac.com/carstenm/Lego/NXT/Entries/2006/12/4_NXTBrowser_-_Bluetooth_connectivity_on_an_Intel_Mac.html

I’m very confident that alternatives exist for other platforms. I’m also very confident this will work on other people’s computers and Mindstorms brains as well as my own, but definitely correct me if I got that wrong! (You can reach me by e-mail at [email protected].)