Download presentation
Presentation is loading. Please wait.
1
Building robots Spring 20031 Mindstorms Programming Java + leJOS, NQC, and others
2
Building Robots Spring 20032 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.
3
Building Robots Spring 20033 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
4
Building Robots Spring 20034 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
5
Building Robots Spring 20035 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
6
Building Robots Spring 20036 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
7
Building Robots Spring 20037 A simple class to control the RCX import josx.platform.rcx.*; // leJOS API class dumb { 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 ( ); }
8
Building Robots Spring 20038 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
9
Building Robots Spring 20039 Adding a random change of direction private static 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
10
Building Robots Spring 200310 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 ( );
11
Building Robots Spring 200311 Interfaces An abstraction to describe functionality independent of implementation An object can have different interfaces depending on context An interface can have multiple implementations, depending on the object public interface MouseListener { void mouseClicked (MouseEvent event); void mouseEntered (MouseEvent event); void mouseExited (MouseEvent event); …. }
12
Building Robots Spring 200312 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 ( ); }
13
Building Robots Spring 200313 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 }
14
Building Robots Spring 200314 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
15
Building Robots Spring 200315 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.