| Class | Rubotz::Generator |
| In: |
lib/rubotz/generator.rb
|
| Parent: | Object |
Pretty much all the methods that you program Rubotz robots with live in this class. (It may be wiser to isolate those methods in an API module and then mix that in, since that would make the documentation clearer, but this is how it works for now.)
Instantiating an object of this class should never be necessary unless you‘re rewriting the main module or doing similar development. In the course of actually programming a robot, you just use the methods of the class as if they were regular Ruby keywords.
| code | [RW] | |
| comment | [RW] | |
| initialization_section | [RW] | |
| loop_body | [RW] |
Example: activate(UltrasonicSensor.port(4)). You need to do this in the initialization section of your program, before going into the loop body and actually accessing the sensor.
The display method takes a hash of symbols and strings, and uses that to write to the Mindstorms NXT display. For example:
display :line => 7, :text => "Hello World" display :line => 8, :number => ultrasonic_sensor
(Although support for variable interpolation is currently imperfect, it works perfectly for this particular method.)
The NXT brain has 8 lines on its display, and you probably have to call clear_screen before you update it. Numbering starts at 1, not 0.
The current Rubotz model is very limited; you have one constantly-running loop, and an initialization section beforehand, which is where you declare variables and activate sensors. Use initializing to wrap code which goes in the initialization section of a loop, rather than in the loop itself.
This method_missing assumes missing methods are actually references to as-yet-undefined variables. Although variable interpolation is currently imperfect in Rubotz, this method causes all its successes.
Use monitoring to wrap blocks which check a sensor for data and then make decisions based on that data. Its args are a sensor class, and the block you‘re wrapping in it.
The motor method simply passes a MotorGenerator the options you specify; see the MotorGenerator RDoc for details on how to use this method, and what those options should be.
This method pauses program execution. It doesn‘t cause the robot to wait, but the program. This makes it a vital method. After you turn your motors on and start moving in a given direction, you need to wait an arbitrary amount of time before you do anything else; otherwise you‘ll simply turn on your motors and the program will immediately jump to the next instruction without actually driving anywhere.
I think wait works in terms of milliseconds, but honestly I don‘t know. The units are some kind of fraction of a second which appears to work in some order of magnitude. In practical terms, just use round numbers and experiment to see what works for you.