Programming with Lejos Part 1. What is lejos? Cut-down version of Java for the RCX platform Includes: –Lejos API –JVM API includes functionality needed.

Slides:



Advertisements
Similar presentations
LeJOS Java for Lego RCX CJ Chung Associate Professor of Computer Science Director of Robofest Assistants Maurice Tedder Steven Kryskalla.
Advertisements

Socket Programming ENTERPRISE JAVA. 2 Content  Sockets  Streams  Threads  Readings.
III. Streams. Introduction Often a program needs to bring in information from an external source or to send out information to an external destination.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Robotics Invention System 2.0
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Getting Ready to go with LeJos and an IDE NSF Workshop November 19-21, 2004 The University of Mississippi Pam Lawhead.
Building robots Spring Mindstorms Programming Java + leJOS, NQC, and others.
The Turtle Laboratory Sequence Myles McNally LMICSE Workshop November , 2004 University of Mississippi.
Introduction to Lego Mindstorms LMICSE Workshop June , 2005 Alma College.
The Turtle Laboratory Sequence LMICSE Workshop August , 2006 Villanova University.
The Turtle Laboratory Sequence LMICSE Workshop June , 2005 Alma College.
Using LeJOS LMICSE Workshop August , 2006 Villanova University.
Using LeJOS LMICSE Workshop June , 2005 Alma College.
LEGO Mindstorms Hitachi H8-based RCX brick B.A. Juliano, R.S. Renner, F. Jauregui January 2004 California State University, Chico Intelligent Systems Laboratory.
Web Proxy Server. Proxy Server Introduction Returns status and error messages. Handles http CGI requests. –For more information about CGI please refer.
Copyright © 2003 ProsoftTraining. All rights reserved. Sun Certified Java Programmer Exam Preparation Guide.
Io package as Java’s basic I/O system continue’d.
LEGO NXT Robot Programming Introduction to Programming a Lego NXT robot in Java.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Patterns, frameworks & Lejos
Interfacing the LEGO RCX to the outside world John M. Larkin Whitworth College Spokane, WA.
Department of Computing and Information Sciences Kansas State University Design Methodology for State based Embedded Systems Case Study: Robot Controller.
[Low-level] Programming of NXT Robots Pavel Petrovič Department of Applied Informatics, Faculty of Mathematics, Physics and Informatics
LeJos Kyle Adams Rob Boorman. Overview of leJos leJos stands for LEGO Java Operating System. leJos was originally developed by Jose Solorzano, and is.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
CIS 644 Aug. 25, 1999 tour of Java. First … about the media lectures… we are experimenting with the media format please give feedback.
The Java Programming Language
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Robotics Overview of NXT-G Actuators in Mindstorms. Touch sensor Labwork: Right turn. Touch/bump. [Explore move versus Motor Move mini & motor mini. Motor*.]
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
Mindscript Presented by Dean Thomas Matthew Horoszowski.
1 Introduction to Java. 2 What is Java? A programming language. A platform –A virtual machine (JVM) definition. –Runtime environments in diverse hardware.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Li Tak Sing COMPS311F. Case study: consumers and producers A fixed size buffer which can hold at most certain integers. A number of producers which generate.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
The Handy Board Bryan Valentini General Robotics 2003.
Event Driven Programming Minus the GUI Plus a Robot By Charlie Vick.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Copyright Curt Hill Variables What are they? Why do we need them?
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
Building robots Spring Building Braitenberg Vehicles From Abstraction to Realization.
Minds and Computers 2.1 The RCX l Hitachi H8/3297 series processor l 3 inputs/sensors (1, 2, 3) l 3 outputs/motors (A, B, C) l 32k RAM/ 16k ROM ä 12 kB.
PROG Mobile Java Application Development PROG Mobile Java Application Development Data Storage, Continued.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Parallel Processing (CS526) Spring 2012(Week 8).  Shared Memory Architecture  Shared Memory Programming & PLs  Java Threads  Preparing the Environment.
Mindstorms 1.1 Today’s topics l AI l History of Robotics l Uses of robots l The RCX l ROBOLAB l Upcoming ä Basic control ä Kinematics ä Robot architectures.
Decisions, Looping, and input sensors Mindstorms, Eclipse and Java.
I/O Basics 26 January Aside from print( ) and println( ), none of the I/O methods have been used significantly. The reason is simple: most real.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
LeJOS Protocols. Serial Class Serial class found in josx.platform.rcx package Serial class found in josx.platform.rcx package RCX – IR, RCX – RCX communication.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
Lecture 6 Arrays and Interfaces.
Yanal Alahmad Java Workshop Yanal Alahmad
IT Basic Wednesday 14 April 2010
I/O Basics.
Introduction to Robots and the Mind - Methods -
Object Oriented Programming in java
Programming Robots using Java
Programming Robots using Java
LEGO Mindstorms Robot and Java
Presentation transcript:

Programming with Lejos Part 1

What is lejos? Cut-down version of Java for the RCX platform Includes: –Lejos API –JVM API includes functionality needed to control motors, sensors, buttons as well as many components from the standard Java API

How it works Lego firmware is replaced with lejos JVM (only has to be done once) Program source code is written & compiled on PC Compiled code (Java byte code) is downloaded to & runs on RCX

JVM & RCX technical specifications RCX has 32k of RAM –4K unusable –16K for Lejos – 12K free memory for user code! Built-in support for: –Floating-point numbers (useful for trig/navigation) –Limited multithreading –2-dimensional Arrays –Limited recursion

Events & the lejos API Objects & their listeners: –Timer: TimerListener –Button: ButtonListener –Sensor: SensorListener Object acts as event source; when event occurs, all registered listeners we notified & can execute appropriate code

Exception Handling & Garbage Collection Lejos follows standard Java exception- handling model Lejos does NOT have automatic garbage collection –Important to re-use objects rather the just creating new ones –Forces good memory “hygiene”

josx.platfom.rcx: includes classes for controlling –3 output ports (A, B & C) –3 input ports (1, 2 & 3) –3 buttons (Prgm, Run, View) –LCD display –Speaker output –IR communication There are also classes for accessing system time & monitoring battery power Packages in the Lejos API

java.lang: Includes some of the most commonly used standard classes –Math –String –StringBuffer: mutable version of String –System –Thread –Throwable

Packages in the Lejos API java.util: like java.lang, includes some familiar and/or useful classes –Random –Vector: dynamic array –BitSet: series of bits

Packages in the Lejos API java.io: provides support for transmitting data streams via the IR port –Serializable interface –InputStream, OutputStream, DataInputStream, DataOutputStream and IOException classes

Packages in the Lejos API josx.robotics: generic classes & methods for robot-specific tasks –Behavior Control classes: includes Behavior interface & Arbitrator class –Navigation classes

Classes in josx.platform.rcx Sound class contains a set of static methods that can produce predefined or custom sounds: –Sound.beep() // beeps once –Sound.twoBeeps() // beeps twice –Sound. buzz() // emits almost inaudible tone –Sound. beepSequence() // series of // downward tones –Sound. playTone (int frequency, int duration) MinSound class is a cut-down version - contains only the (deprecated) playTone method

Classes in josx.platform.rcx Unlike most Java environments, robots running Lejos are physical objects The RCX brick has 3 programmable buttons, 3 sensor connections and 3 power output ports When programming for the RCX, these components are represented by predefined static objects; it wouldn’t make sense to instantiate more sensor ports, for example, because physically there are just 3

Classes in josx.platform.rcx The LCD, MinLCD and TextLCD classes relate to various aspects of the RCX’s LCD display MinLCD, like MinSound, is a cut-down version primarily used to conserve memory LCD is used for number display, while TextLCD can display Strings

LCD class methods (all are static) void refresh() void setNumber (int code, int value, int point) - parameters: –code: value Indicates use of signed or unsigned number, use of 5 th digit –value: number to display –point: code indicating position of decimal point These 2 methods are also found in MinLCD

LCD class methods (all are static) void clear(): clears display void showNumber(int i): displays an unsigned, up to 4-digit number void setSegment (int code): activates the display segment indicated by code; codes are defined by static constants in the Segment interface

Example 1: Sounds & Display import josx.platform.rcx.*; public class MakeNoise { public static void main(String[] args) throws InterruptedException { int freq;// frequency of tone for (freq = 440; freq < 10000; freq = 110 * freq / 100) { // ascending tones LCD.showNumber(freq); Sound.playTone(freq, 50); Thread.sleep(500); } for (freq = 440; freq > 20; freq = 90 * freq / 100) { // descending tones LCD.showNumber(freq); Sound.playTone(freq, 50); Thread.sleep(500); }

Classes in josx.platform.rcx Button class methods include: –public void addButtonListener (ButtonListener b) –public boolean isPressed() –public static int readButtons() Returns int with bits set: 0x02 (view button pressed) 0x04 (prgm button pressed), 0x01 (run button pressed). If all buttons are released, this method returns 0. –void waitForPressAndRelease () throws InterruptedException stops execution until button pressed & released

Classes in josx.platform.rcx Button class also contains static fields that represent the RCX buttons: –Button.PRGM –Button.RUN –Button.VIEW ButtonListener interface specifies 2 methods: –public void buttonPressed (Button b) –public void buttonReleased (Button b)

Classes in josx.platform.rcx ROM class: provides access to embedded routines in the RCX ROM; includes static method int getBatteryPower() Motor class: includes methods controlling DC output to ports A, B and C Sensor class: includes methods to communicate with sensor controllers 1, 2 and 3

Motor class methods: mutators void forward(): motor rotates forward void backward(): motor rotates backward void stop(): stops with braking action void flt(): motor floats to a stop void reverseDirection(): changes from forward to backward, or vice-versa void setPower(int power): sets motor power; 7 is high, 0 low

Motor class methods: accessors int getPower(): returns current power setting char getID(): returns motor ID (A, B or C) All of the following methods return a boolean value –isBackward() –isFloating() –isForward() –isMoving() –isStopped()

Example import josx.platform.rcx.*; public class Patrol { public static void main(String[] args) throws InterruptedException { Motor.A.forward(); while (true) { Motor.C.forward(); // go forward Thread.sleep (5000); Motor.C.reverse(); // turn around Thread.sleep (1000); }

Sensor class There are several types of sensors available for the RCX, including: –Touch –Light –Rotation Because each operates differently, there is more complexity built into this class than in most of the rest of the lejos API

Sensor Type and Mode A predefined set of constants is used to describe: –the kind of sensor attached to a particular connector (type) and –the kind of readings that are recorded by the sensor (mode) These values as used as arguments to the Sensor class’s setTypeAndMode method, which is used to initially set up a sensor

Type and Mode examples Sensor.S1.setTypeAndMode(1, 0x20); // 1 represents touch sensor; // 0x20 is boolean mode Sensor.S1.setTypeAndMode(3, 0x80); // 3 represents light sensor; // 0x80 is percent mode

Example import josx.platform.rcx.*; public class GoForward implements SensorConstants { public static void main(String[] args) { Sensor.S2.setTypeAndMode(SENSOR_TYPE_TOUCH, SENSOR_MODE_BOOL); // sets up touch sensor using constants from SensorConstants interface Motor.A.forward(); Motor.C.forward(); while( Sensor.S2.readValue() != 1 ) ; // loop ends (& robot stops) when bumper is touched }

Methods of Sensor class public void activate () –activates sensor –should be called after setTypeAndMode Methods that return Sensor readings: –public final boolean readBooleanValue() –public final int readValue() –public final int readRawValue() Sensor readings can also be handled by a SensorListener as described on the next slide

Sensor Listeners Sensor Listener interface specifies a single method: public void stateChanged(Sensor s, int old, int new) s: event source old: previous value new: new value As with the GUI components we have looked at before, the steps are –define a class that implements the interface, and with the code for the method –add the listener to the appropriate Sensor

Serial class Methods The Serial class is used for RCX to IR and RCX to RCX communication Methods work on the most basic level of network communication, passing around packets in the form of arrays of bytes A separate communications API provides support for higher-level operations

One more example import josx.platform.rcx.*; public class ListenerExample implements SensorConstants, SensorListener { public static void main(String[] args) { ListenerExample exam = new ListenerExample(); Sensor.S1.setTypeAndMode(SENSOR_TYPE_TOUCH, SENSOR_MODE_BOOL); Sensor.S3.setTypeAndMode(SENSOR_TYPE_TOUCH, SENSOR_MODE_BOOL); Sensor.S1.addSensorListener(exam); Sensor.S3.addSensorListener(exam); exam.patrol(); }

Example continues public void patrol () { try { Motor.A.forward(); while (true) { Motor.C.forward(); Thread.sleep(3000); Motor.C.backward(); Thread.sleep(1000); } } catch (Exception e) { }

End of example public void stateChanged(Sensor source, int oldValue, int newValue) { if (newValue == 1) if (source.getId() == 0) // left bumper Sound.playTone(220, 50); else if (source.getId() == 2) // right bumper Sound.playTone(660, 50); } } // end class ListenerExample