90 likes | 258 Views
Java for Robots. How to program an NXT robot with a Java Brain Bert G. Wachsmuth Seton Hall University. The Setup. Differential drive robot with ultrasound distance sensor mounted in front Java VM uploaded to NXT Connection NXT <-> PC via USB or Bluetooth
E N D
Java for Robots How to program an NXT robot with a Java Brain Bert G. Wachsmuth Seton Hall University
The Setup • Differential drive robot with ultrasound distance sensor mounted in front • Java VM uploaded to NXT • Connection NXT <-> PC via USB or Bluetooth • Eclipse setup for Java programming • Eclipse configured for LeJOS
Java Program • A Java program is called a “class” • Java is case-sensitive • Statements are grouped with { … } or end in a semicolon ; • Every Java program for the NXT has the same blueprint: import lejos.nxt.*; public class Test { public static void main(String args[]) { } }
Java Basics Java programs contain the basic frame work and • “state” variables to store global info and data (aka fields) • functions to define particular subtasks (aka methods) • Comments to explain code in plain English • Single-line comments start with // • Multi-line comments are enclosed in /* … */ Program execution starts at the “main” method and proceeds linearly unless redirected.
import lejos.nxt.*; public class Test { // global variables go here: static int heading = 0.0; // function to define subtask: static void turn(int degrees) { // subtask code goes here } public static void main(String args[]) { // this is where the program starts // code to call the ‘turn’ function goes here } }
Defining Variables • All variables must have a name, a type, and a value compatible with the type. • All variables must be declared before use • All variables have a range where they are valid: • State variables are valid everywhere, e.g.: • static int stateVariable = -9; • Local variables are valid in the block they are defined in • Variables can have types int, float (= 5.7f), double (= 5.7), boolean (= true or false), char (= ‘b’), or String (= “text”) • There is a special type called “void” and a special value named “null”
Defining Functions • Functions have header and a body • Header defines name, input variables (and types), and return type (often void) • Body defines what the function actually does public static void turn(int degrees) { // code here } • Functions must be called with values matching the input variables as defined in the function header
NXT Programs To write Java code for the NXT, your first statement must be: import lejos.nxt.*; Then you can use special commands relevant to your NXT. All possible commands are documented in the LeJOS API, which must be available as you program. See: Robotics Pages -> Lectures -> link: LeJOS API or C:\JavaNXT\lejos_nxj\docs\api\index.html • Click on “lejos.nxt” in top-left frame, then pick class in bottom left, to read details in main info frame.
Sample Programs • Make some noise • Show some text • Move forward for 1 second • Rotate for 1 second • Move forward for one 360-degree wheel rotation • Move forward for x degrees wheel rotation • Move forward for x cm • Rotate for x degrees