Difficulties with Color Sensor

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

Moderators: roger, gloomyandy, skoehler

pgagnebin
New User
Posts: 3
Joined: Fri Aug 28, 2015 6:28 am

Difficulties with Color Sensor

Postby pgagnebin » Fri Jan 01, 2016 8:59 am

Hello (and Happy new year)
I'm trying to use te color sensor in a Lejos app.
At first, le sensor correctly run with the Lego Mindstorm software.
I wrote this code to test with Lejos :

Code: Select all

import java.util.ArrayList;

import lejos.hardware.Button;
import lejos.hardware.ev3.LocalEV3;
import lejos.hardware.lcd.LCD;
import lejos.hardware.port.Port;
import lejos.hardware.sensor.EV3ColorSensor;
import lejos.robotics.SampleProvider;

public class TestEV3ColorSensor {

   public static void main(String[] args) {
      TestEV3ColorSensor application = new TestEV3ColorSensor();
      application.run();
   }
   
   // Variables d'instance
   private ArrayList<String> modes;
   private EV3ColorSensor sensor;
   
   // Constructeur
   public TestEV3ColorSensor() {
   }
   
   // Boucle
   public void run() {
      LCD.drawString("EV3Color", 3, 0);

      // get a port instance
      Port port = LocalEV3.get().getPort("S3");

      // Get an instance of the Ultrasonic EV3 sensor
      sensor = new EV3ColorSensor(port);
      
      String msg;
      modes = sensor.getAvailableModes();
      int currentMode = 0;

      SampleProvider provider = sensor.getMode("RGB");
      float[] sample = new float[provider.sampleSize()];
      
      boolean terminer = false;
      setNewMode(currentMode);
      
      Button.waitForAnyPress();
      
      
      do {
         int bouton = Button.readButtons();
         switch(bouton) {
            case Button.ID_ESCAPE :
            {
               terminer = true;
               break;
            }
            case Button.ID_UP :
            {
               if (currentMode ==  0) {
                  currentMode = modes.size()-1;
               } else{
                  currentMode--;
               }
               setNewMode(currentMode);
               while(Button.readButtons() == bouton);
               break;
            }
            case Button.ID_DOWN :
            {
               if (currentMode == modes.size()-1) {
                  currentMode = 0;
               } else {
                  currentMode++;
               }
               setNewMode(currentMode);
               while(Button.readButtons() == bouton);
               break;
            }
            case Button.ID_ENTER :
            {   
               if (sensor.isFloodlightOn()) {
                  sensor.setFloodlight(false);
               } else {
                  sensor.setFloodlight(true);
               }
               while(Button.readButtons() == bouton);
               setNewMode(currentMode);
               break;
            }
         }
         
         if (currentMode == 2) {
            /*
            for (int i=0; i<sample.length; i++){
               sensor.fetchSample(sample, i);
            }
            msg = String.format("%f %f %f", sample[0], sample[1], sample[2]);
            */
            msg= String.format("%d  ", sample.length);
         } else {
            msg = String.format("%d  ", sensor.getColorID());
         }
         LCD.drawString(msg, 0, 6);
         
      } while(terminer == false);

      
   }
   
   // Programme le nouveau mode de fonctionnement
   public  void setNewMode(int mode) {
      String msg;
      for (int i = 0; i < modes.size(); i++) {
         msg = String.format(" %c %s",
               mode == i ? '>' : ' ', modes.get(i));
         LCD.drawString(msg, 0, i+1);
      }
      msg = String.format("lampe : %s ", sensor.isFloodlightOn());
      LCD.drawString(msg, 0, 5);
      sensor.setCurrentMode(modes.get(mode));
      
      /*
      int sampleSize = sensor.sampleSize();           
      msg = String.format("taille %d  ", sampleSize);
      LCD.drawString(msg, 0, 7);
      */

   }

}


Unfortunaly, the colorid value show an incoherent value for red (show 0), yellow (show 3, like green), green (show 1 or 6) and béack (-1, 7 anr 13).
Second problem/question, how read correctly values in RGB mode ?
Best regards,
Pascal

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

Re: Difficulties with Color Sensor

Postby gloomyandy » Fri Jan 01, 2016 10:20 am

Hi, firstly I have moved your post. The section you originally posted in was for discussions about future development of leJOS not for posting questions about the current version.

Secondly I'm sorry but I don't understand your question. Your program seems to provide many ways of using the color sensor, but it is not obvious what exactly your problem is. Please provide more details of what it is you are trying to do and provide examples of the problems you are seeing. So for instance what mode are you trying to use? What are you then doing with the sensor (what are you pointing it at) and what results are you expecting and what results do you actually get?

You mention that the sensor works fine with the LEGO software, well leJOS does very little with this device other than pass on the results read directly from it, so there really should be little difference between the results from the LEGO software and leJOS. I assume you have read the leJOS documentation for the device:
http://www.lejos.org/ev3/docs/lejos/har ... ensor.html

If there is something you don't understand then you can always look at the source:
https://sourceforge.net/p/lejos/ev3/cod ... ensor.java

Finally note that if you are using ColorIdMode then the actual color IDS returned differ from those used by the LEGO software leJOS returns values defined by the Color class:
http://www.lejos.org/ev3/docs/lejos/robotics/Color.html

You can see the actual values here:
http://www.lejos.org/ev3/docs/constant- ... olor.BLACK
leJOS news https://lejosnews.wordpress.com/

pgagnebin
New User
Posts: 3
Joined: Fri Aug 28, 2015 6:28 am

Re: Difficulties with Color Sensor

Postby pgagnebin » Sun Jan 03, 2016 10:00 am

Hello Andy and Thanks for answer and presicions.
The target was to write a program to illustrate and understans how Lejos Sensor Classes are working.
I wrote a program more simple, is working fine :

Code: Select all

   public static void main(String[] args) {

      // Colors list
      String[] colors = {
            "Rouge", "Vert", "Bleu",
            "Jaune", "Magenta",   "Orange",
            "Blanc", "Noir", "Rose",
            "Gris", "Gris clair", "Gris foncé",
            "Cyan",   "Brun"
            };

      // Say hello
      LCD.drawString("EV3Color", 3, 0);
      
      // Create sensor
      Port port = LocalEV3.get().getPort("S3");
      EV3ColorSensor sensor = new EV3ColorSensor(port);

      // Use ColorID Mode
      SampleProvider provider = sensor.getMode("ColorID");

      // To store data
      float sample[] = new float[provider.sampleSize()];
      String msg = "";
      
      // Loop
      while(Button.readButtons() != Button.ID_ESCAPE)
      {
         // Read & convert
         sensor.fetchSample(sample, 0);
         int nColor = (int)sample[0];
         // Prepare display & display
         msg = String.format("color id %2d", nColor);
         LCD.drawString(msg, 0, 2);
         if (nColor >= 0 && nColor < colors.length)
            msg = String.format("%-12s", colors[nColor]);
         else
            msg = String.format("%-12s", "inconnue");
         LCD.drawString(msg, 0, 4);
         Delay.msDelay(25);
      }
      
      // Release button
      while(Button.readButtons() != 0);
      // Terminate
      sensor.close();
   }

Colors 0, 1, 2, 3, 6 and 7 and correctly detected, not the others.
Please note the comment for "getColorIdMode" : "Measures the color ID of a surface. The sensor can identify 8 unique colors (NONE, BLACK, BLUE, GREEN, YELLOW, RED, WHITE, BROWN)." make some misunderstanding in color numbering, because colors mentioned afre exactly the same and in the same order as Lego colors.
Best regards.
Pascal

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

Re: Difficulties with Color Sensor

Postby gloomyandy » Sun Jan 03, 2016 1:22 pm

Hi,
yes the documentation is wrong/out of date. We changed things some time ago so that all of the color sensors (NXT/EV3/HiTechnics) return the same color ids. Some sensors like the ones from HiTechnics recognize more colours, some like the LEGO ones less.

Andy
leJOS news https://lejosnews.wordpress.com/


Return to “EV3 Software”

Who is online

Users browsing this forum: clplaneguy, jippiee and 1 guest