LinkJVM – Java on the KIPR Link



LinkJVM – Java on the KIPR Link

0 0


Introduction-to-LinkJVM

LinkJVM introduction slides

On Github LinkJVM / Introduction-to-LinkJVM

LinkJVM

Java on the KIPR Link

Markus Klein / kleiinnn

What is LinkJVM

  • Java Runtime Enironment(JamVM)
  • Java compiler and other tools(javac, jar, ...)
  • Library for controlling the robot with Java

You can execute any JVM language(Scala, JavaScript(Rhino), ...) with LinkJVM.

Why Java?

  • Most students get started with programming using Java
  • Automatic garbage collection
  • Excellent set of basic data types
  • Huge standard library
  • Easy to use multithreading
  • Better maintanability because of object orientation

Part 1

Installation and Setup

Download LinkJVM

You can download a zip file containing all needed resources from the LinkJVM-Fileserver.

Download: files.mklein.co.at/LinkJVM/LinkJVM-Deploy.zip

Installing via LinkJVM-Installer

Put the LinkJVM folder to the root of your flash drive Open the Link´s file manager Browse to media/sda1/LinkJVM Compile linkjvm_installer.c Run linkjvm_installer

C Wrapper

To execute a Java program on the Link via BotUI, you need a C wrapper which starts the JVM.

						#include <stdlib.h>
						#define JAR_LOCATION /path/to/jar
						#define JAR_NAME myJar.jar
 
						int main() {
						    return system("export BOOTCLASSPATH=\"/usr/share/jamvm/classes.zip:\
						        /usr/share/classpath/glibj.zip:/usr/share/classpath/tools.zip:\
						        /usr/lib/linkjvmjava.jar\"; export CLASSPATH=\"/usr/share/jamvm/classes.zip:\
						        /usr/share/classpath/glibj.zip:/usr/share/classpath/tools.zip:\
						        /usr/lib/linkjvmjava.jar:.\"; export LD_LIBRARY_PATH=\"\
						        /usr/lib/classpath:/usr/lib\"; java -jar CLASS_LOCATION/JAR_NAME");
						}
					

Also a Github Gist: gist.github.com/kleiinnn/8649678

LinkJVM-Uploader

LinkJVM automatically uploads a excutable jar file and generates the C wrapper so it can be directly started via BotUI.

Just run LinkJVM-Uploader.jar and select the file you want to upload.

Workflow

You can develop your program with an IDE / editor of your your choice. The LinkJVM-Uploader automatically uploads an jar file to the link and adds the C wrapper for execution via BotUI. After uploading your jar using LinkJVM-Uploader you can directly start it via BotUI.

We also plan to develop an eclipse plugin for automatic wrapping, compilation and uploading.

Part 2

The Robot Library

Robot Library Documentation

JavaDoc API: http://linkjvm.github.io/docs

Activity 1

Stop at the wall

The demobot should drive straight until the lever sensor placed at the front of the demobot bumps.

Using Sensors

Basically, there a two kinds of sensors:

  • Analog sensors(analog sensors, acceleration sensors, ...): Analog sensors implement the IAnalogSensor interface
  • Digital sensors(digital sensors, buttons, ...): Digital sensors implement the IDigitalSensor interface

Analog Sensors

						import linkjvm.sensors.analog.AnalogSensor;
						import linkjvm.sensors.analog.acceleration.AccelerationSensor;
						(...)
						//Create a new analog sensor
						AnalogSensor analog = new AnalogSensor(PORT);
						//Get the current sensor value
						analog.getValue();
						//Create a new x-axis acceleraion sensor
						AccelerationSensor accel = new AccelerationSensor(AccelerationSensor.Axis.X);
						//Get the acceleration sensor's value
						accel.getValue();
					

Digital Sensors

						import linkjvm.sensors.buttons.*;
						import linkjvm.sensors.digital.DigitalSensor;
						(...)
						//Create a new digital sensor
						DigitalSensor digital = new DigitalSensor(PORT);
						//Get the current value
						digital.getValue();
						//Craete a new button
						XButton button = new XButton();
						//Get if the button is pressed
						button.getValue();	
						button.isPressed();
						//Set the button's text
						button.setText("Text!");
					

Controlling Motors and Servos

						import linkjvm.motors.Motor;
						import linkjvm.motors.Servo;
						(...)
						//Create a new servo
						Servo servo = new Servo();
						//Enable the servo
						servo.enable();
						//Set the servo's position
						servo.setPosition(1000);
						//Disable the servo
						servo.disable();
						//Create a new motor
						Motor motor = new Motor();
						//Turn the motor at a specified percentage
						motor.turn(60);
						//Stop the motor
						motor.freeze();
					

Activity 1 - Solution

						public static void main(String[] args){
						    Motor leftMotor = new Motor(0);
						    Motor rightMotor = new Motor(1);
						    DigitalSensor bumpSensor = new DigitalSensor(0);
						    leftMotor.turn(100);
						    rightMotor.turn(100);
						    while(!bumpSensor.getValue()){
						        try {
						            Thread.sleep(100);
						        } catch (InterruptedException e) {}
						    }
						    leftMotor.freeze();
						    rightMotor.freeze();		
						}	
					

Activity 2

Exploring the room with the create

The create should drive straight until it bump. Then it should turn and start again exploring the room.

Controlling the Create

An instance of the create class is used to control the create. The method names are pretty equal to libkovan.

						import linkjvm.create.Create;
						(...)
						Create create = new Create();
						//Establishes a connection to the create
						create.connect();
						//Move the create forward
						create.driveDirect(leftMotorSpeed, rightMotorSpeed);	
						//Returns the right cliff value
						create.getRightCliff();
						//Returns the left bumper value
						create.getRightBump();
						//Closes the connection to the create
						create.disconnect();
					

Activity 2 - Solution

					public static void main(String[] args) {
					    Create create = new Create();
					    create.connect();
					    SideButton sb = new SideButton();
					    BButton bb = new BButton();
					    bb.setText("Start");
					    while(!bb.getValue());
					    while(!sb.getValue()) {
					        if(create.getLeftBump())
					            create.spinClockwise(250);
					        else if(create.getRightBump())
					            create.spinCounterClockwise(250);
					        else
					            create.driveStraigth(400);
					        try {
					            Thread.sleep(50);
					        } catch (InterruptedException e) {}
					    }
					    create.stop();
					    create.disconnect();
					}
					

Activity 3

Finding the biggest object

The robot should find the biggest object and move to it.

Vision

The LinkJVM's vision system is a little bit different to libkovan's:

You can find the vision classes in the linkjvm.vision package.

Activity 3 - Solution

					public static void main(String[] args) throws InterruptedException {
					    Create create = new Create();
					    create.connect();
					    camera = new ImageProcessor(new CameraConfig(Resolution.MED_RES), 0);
					    camera.openCamera();
					    SideButton sb = new SideButton();
					    while(!sb.getValue()) {
					        camera.update();
					        if(camera.getObjectCount() > 0) {
					            Rectangle rec = camera.getBoundingBox(0);
					            if(rec.getWidth() > 80){
					                if(rec.getCenter().getX() < 290){
					                    create.driveDirect(100, 0);
					                }
					                else if(rec.getCenter().getX() > 350){
					                    create.driveDirect(100, 0);
					                }
					                Thread.sleep(500);
					            }
					        }
					        create.stop();
					    }
					    create.disconnect();
					    camera.close();
					}
					

LinkJVM Debugger

The LinkJVM-Debugger allows you to send debug messages to your computer running a Debugger-Client.

Just run DebuggerClient.jar.

Usage

						import linkjvm.debugger.Debugger;
						(...)
						Debugger debugger = new Debugger("PC IP");
						debugger.write("Some text!");
						debugger.writeln("Some line!");
						debugger.close();
					

Resources

{The End}

Made with <3

Contact: m@mklein.co.at

© 2014, Markus Klein © 2014, LinkJVM