LeJOS Loading Sensor Data Time

This is where you talk about the EV3 software itself, installation issues, and programming talk.

Moderators: roger, gloomyandy, skoehler

Brick'obot
New User
Posts: 2
Joined: Wed Jul 09, 2014 10:32 am

LeJOS Loading Sensor Data Time

Postby Brick'obot » Wed Jul 09, 2014 10:41 am

Hi all,

I have installed LeJOS for EV3 on my mac as suggested on the sourceforge website. I used Eclipse Luna and LeJOS beta 0.8.1 to create the image for the SD Card
So, I started developing for the EV3. I wanted to make an "advanced" Hello World program that loads the data of the 3 sensors included with the EV3 home edition kit. The good news is that the code works, however, the time it takes to run the code, makes me worry that I did something wrong. This is the code that I have used:

Code: Select all

import lejos.hardware.Button;
import lejos.hardware.Sound;
import lejos.hardware.lcd.LCD;
import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.hardware.port.MotorPort;
import lejos.hardware.port.SensorPort;
import lejos.hardware.sensor.EV3ColorSensor;
import lejos.hardware.sensor.EV3IRSensor;
import lejos.hardware.sensor.EV3TouchSensor;
import lejos.robotics.RegulatedMotor;
import lejos.utility.Delay;


public class HelloMain {

   private static RegulatedMotor m;
   private static EV3TouchSensor t;
   private static EV3IRSensor i;
   private static EV3ColorSensor c;

   private static float[] sample;
   
   public static void main(String args[]){
      
      Sound.beep();
      LCD.drawString("Hello EV3", 0, 2);
      
      m = new EV3LargeRegulatedMotor(MotorPort.A);
      t = new EV3TouchSensor(SensorPort.S1);
      i = new EV3IRSensor(SensorPort.S4);
      c = new EV3ColorSensor(SensorPort.S3);
      m.setSpeed(400);
      sample = new float[10];
      
      while(!Button.ESCAPE.isDown()){
         t.fetchSample(sample, 0);
         float touchValue = sample[0];
         i.fetchSample(sample, 1);
         float irValue = sample[1];
         c.fetchSample(sample, 2);
         float colorValue = sample[2];

         
         LCD.drawString("Touch: " + touchValue + "\t " + t.sampleSize(), 0, 3);
         LCD.drawString ("IR: " + irValue + "\t " + i.sampleSize() , 0, 4) ;
         LCD.drawString("Color: " + colorValue + "\t " + c.sampleSize(), 0, 5);

         
         if(sample[0] != 0){
            m.forward();
         }
         else{
            m.stop();
         }
         Delay.msDelay(100);
      }
   }
}



I made an image and a video to show you how long it takes to go through the code

Image

VIDEO that shows the process of me loading the EV3 Project over Bluetooth to the EV3 and getting all the data displayed on the brick.
https://www.youtube.com/watch?v=ZIpPulJqhaQ

Does anyone have an idea of what I'm doing wrong? Or is this just how it is supposed to work?

Regards,
Brick'obot

User avatar
gloomyandy
leJOS Team Member
Posts: 5363
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: LeJOS Loading Sensor Data Time

Postby gloomyandy » Wed Jul 09, 2014 4:22 pm

Looks a little slow but not that unexpected. Remember that when you run a program on the EV3 a lot happens as the program starts up...
* The Java VM is loaded and initialized
* The standard class library loads a lot of classes
* The leJOS class library loads a lot more classes
* The leJOS library has to establish connections with the various sensors, reset them, initialize them etc.
* The VM may be using the JIT compiler to convert some of the class library byte code into native code
Most of the above are one off things that tend to happen as your program starts up. In general although being a bit of a pain the time taken is not that important. What is more important is the thing you have not measured, the time it takes to obtain sensor readings and display them and then do it all again. You might want to measure that part of your program. If that is taking a long time then you have a problem.

You may be able to speed things up a little by...
* Using a faster SD card
* Using the Java 8 compact profile (note Java 8 will be supported in the next release of leJOS, it is supported now in the git master version)

Andy

Brick'obot
New User
Posts: 2
Joined: Wed Jul 09, 2014 10:32 am

Re: LeJOS Loading Sensor Data Time

Postby Brick'obot » Sun Jul 13, 2014 11:45 am

Thanks!

Loading time is a lot better when I first make objects of all sensors and motors and not doing this when a part of the program is already running.

Code: Select all

import lejos.hardware.Button;
import lejos.hardware.Sound;
import lejos.hardware.lcd.LCD;
import lejos.hardware.port.SensorPort;
import lejos.hardware.sensor.EV3IRSensor;
import lejos.hardware.sensor.EV3TouchSensor;
import lejos.utility.Delay;


public class TestMain {

   private static EV3TouchSensor t;
   private static EV3IRSensor i;
   private static float[] sample;
   
   public static void main(String[] args) {
      
      LCD.drawString("Starting program", 0, 4);
      
      t = new EV3TouchSensor(SensorPort.S1);
      i = new EV3IRSensor(SensorPort.S4);
      sample = new float[10];
      
      Sound.beep();
      LCD.clear();
      LCD.drawString("TestClass", 0, 0);
      
      while(!Button.ESCAPE.isDown()){
         String modeTouch = t.getName();
         t.fetchSample(sample, 0);
         float touchValue = sample[0];
         
         String IRMode = i.getName();
         i.fetchSample(sample, 1);
         float IRValue = sample[1];
         
         LCD.drawString("" + modeTouch + ":" + touchValue, 0, 3);
         LCD.drawString("" + IRMode + ":" + IRValue, 0 , 4);
         Delay.msDelay(100);
      }
   }
}


Brick'obot

koal
New User
Posts: 5
Joined: Mon Jun 13, 2016 12:55 pm

Re: LeJOS Loading Sensor Data Time

Postby koal » Mon Jun 13, 2016 3:16 pm

I have found a way to speed up the user program initialization.

As it was written earlier the big part of the initialization process is the Java VM loading and initialization. I looked through the code of the EV3 Menu, and found the method execInThisJVM. It is used to run jar-files which are located in Tools directory.

So, I did a simple experiment:
1) logged in to the brick by using SSH
2) copied my jar-file from the directory /home/lejos/programs to the directory /home/root/lejos/tools
3) the file with the same name appeared in the Tools menu on the brick and can be run.

I am not sure if such solution has any limitations. So, probably, someone could comment this.

--
Alexander Kolotov

User avatar
gloomyandy
leJOS Team Member
Posts: 5363
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: LeJOS Loading Sensor Data Time

Postby gloomyandy » Mon Jun 13, 2016 3:56 pm

Yes it has lots of limitations. If your program crashes then the menu will probably no longer work. You probably an not abort the program. You will not be able to use the remote debug facilities. Any resources your program uses will not be automatically freed when it exits. Stdin, stdout.stderr will not be automatically redirected to file and it will not be possible to switch the various output layers to view such output (as it is when running a program normally). There are probably more but that is all I can come up with so far.

If you are really bothered about the VM startup time you may be better of writing your own code to load and execute a jar file and then execute that program once and use it to "run" your other "programs". That way you avoid the vm startup but you avoid many of the issues mentioned above. if things get bad you can exit your starter program and things will get cleaned up.
leJOS news https://lejosnews.wordpress.com/


Return to “EV3 Software”

Who is online

Users browsing this forum: No registered users and 1 guest