LeJOS Loading Sensor Data Time

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

Moderators: roger, gloomyandy, skoehler

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
Brick'obot
New User
 
Posts: 2
Joined: Wed Jul 09, 2014 10:32 am

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
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4042
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

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
Brick'obot
New User
 
Posts: 2
Joined: Wed Jul 09, 2014 10:32 am


Return to EV3 Software

Who is online

Users browsing this forum: Majestic-12 [Bot] and 1 guest

more stuff