150 likes | 218 Views
Mindstorms Programming. Java + leJOS, NQC, and others. Hardware and firmware. The RCX is a Hitachi 8-bit microcontroller 16 Mhz clock rate 16Kb of ROM 32 kB of RAM Firmware includes basic executive User program (what’s left over)
E N D
Mindstorms Programming Java + leJOS, NQC, and others Building robots Spring 2003
Hardware and firmware • The RCX is a Hitachi 8-bit microcontroller • 16 Mhz clock rate • 16Kb of ROM • 32 kB of RAM • Firmware includes basic executive • User program (what’s left over) • About 1/1000 of a bottom-line PC, but more computing power than the Lunar Lander. Building Robots Spring 2003
Communicating with the environment • 3 input ports • Various sensors: touch, light, rotation, temperature • Read 10-bit values from environment • 3 output ports • Control actuators: motors, pneumatic valves, switches • Sound • Counters and interval timers • Infrared communication port • Download program, upload data to host Building Robots Spring 2003
The Firmware • Built-in executive time-slices between activities (Threads, tasks) • Interrupt handler for asynchronous events detected by sensors • Sufficient to support synchronization primitives from high-level languages: Java, Ada95 • Graphics programming interface for iconic programming • NQC (not quite C) for C-like programming with threads Building Robots Spring 2003
Java and LejOS • LejOS is a pared-down JVM that executes bytecodes on the RCX • Download from lejos.sourceforge.net • Round-Robin scheduling: 128 instructions / time slice • Event model: asynchronous event handling described by means of listeners • Exception handling for synchronous abnormal conditions • Single-precision floating-point arithmetic • No garbage collection: 12 kB for user programs Building Robots Spring 2003
Instant Java • Imperative language, object-oriented, multithreaded • Class is basic unit of encapsulation: • Methods and data members • Private / public attributes to control visibility • New classes created by: • Composition: data members are instances of other classes • Inheritance: new class overrides and extends attributes of parent • Classes grouped into packages to define API’s • Package-class visibility described by import clauses Building Robots Spring 2003
A simple class to control the RCX import josx.platform.rcx.*; // leJOS API classdumb { private static final int MAX = 1500; public static void main (String [] args) { // required form for main program Motor.A.forward ( ); Motor.C.forward ( ); // defined in josx.platform.rcx.Motor while (true) { move ( ); spin ( ); } Building Robots Spring 2003
Methods for simple motion private static void move ( ) { // only callable from within the class // static method exists independently of any object int delay1 = (int)Math.random ( ) * MAX_DELAY); Try { Thread.sleep (delay1); // in miliseconds // the sleep method may throw an exception // therefore call must be written to handle it // (or at leaste acknowledge that it may happen } catch InterruptedException e) ( ); // all the while, motors keep running Building Robots Spring 2003
Adding a random change of direction privatestatic void spin ( ) { // change direction by reversing one motor Int delay2 = (int) (Math.random ( ) * MAX_DELAY; Motor.C.reversedirection ( ); try { Thread.sleep (delay2); } catch (InterruptedException e) { } Motor.C.reversedirection ( ); // go forward again } ; // end of class dumb Building Robots Spring 2003
Threads and concurrency • Instances of built-in class Thread execute concurrently (interleaved on uniprocessor) • Object with independent behavior extends Thread and overrides run method class Rover extends Thread { public void explore ( ) {.. }; public void find_base ( ) { .. }; public int SOS ( ) {..}; put void run ( ) {… whatever a Rover does }; } Rover MarsRover = new Rover ( ); MarsRover.start ( ); Building Robots Spring 2003
Interfaces • An abstraction to describe functionality independentof implementation • An object can have different interfaces depending on context • An interface can have multiple implementations, depending on the object publicinterface MouseListener { void mouseClicked (MouseEvent event); void mouseEntered (MouseEvent event); void mouseExited (MouseEvent event); …. } Building Robots Spring 2003
The event model • Events are triggered by the environment (touch sensor collides, camera detects motion, etc.) • The event source is an object that interacts with the environment (e.g. a button in an Applet) • Listener interfaces describe handlers for events • An instance of a class that implements the interface can be a concrete handler for the event import josx.platform.rcx.*; class Beep implements ButtonListener { public void buttonPressed (Button b ) { Sound.beepSequence ( ); } Building Robots Spring 2003
Attaching a listener import josc.platform.rcx.* ; class ButtonTest { public static void main (String [ ] args) { Button.RUN.addButtonListener (new Beep ()); // Button.RUN is a predefined singleton // addButtonListener attaches a handler to an // event that may be triggered by the RUN button while (true ( )); // watch for RUN being pressed } Building Robots Spring 2003
The leJOS API : Robotics primitives • josx : root package • Josx.platform.rcx • All static methods : cannot create additional objects for hardware (motors, sensors). • josx.platform.rcs.motor • Void forward ( ) • void backward ( ) • int getPower ( ) • void setPower • (int aPower • josx.platform.rcx.sensor • josx.platform.rcx.Sound • josx.util • Josx.robotics.Behavior Building Robots Spring 2003
Programming Behaviors • Can describe each behavior as a thread, and describe activation, suspension, preemption, etc. • Complex and error-prone: synchronization primitive are low-level, interactions are difficult to program • Expensive in memory • Alternative: simple executive (Arbitrator) and active interface (Behavior) with simple coordination operations. • Each class that implements Behavior must provide: • takeControl, action, suppress • The constructor for an Abitrator takes an array of behaviors, position in array is priority Building Robots Spring 2003