GETCOLORIDMODE YELLOW AND RED IS ZERO

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

Moderators: roger, gloomyandy, skoehler

hflores
New User
Posts: 14
Joined: Mon Feb 27, 2017 8:50 pm

GETCOLORIDMODE YELLOW AND RED IS ZERO

Postby hflores » Thu Jul 06, 2017 5:01 am

This code return yellow = 0 and red = 0
Help me, please

Code: Select all

       //Read color with sensor
   private float[] ObtenerMuestra()
   {
           // Initializes the array for holding samples
              float[] sample = new float[sampleSize];

           // Gets the sample an returns it
           sampleProvider.fetchSample(sample, 0);
           return sample;
       }   


Code: Select all

public int LuzValor() {
      int ColorReconocido = 0;
      
           sampleProvider = sensorPiso.getColorIDMode();
           sampleSize = sampleProvider.sampleSize();

           float[] sample = ObtenerMuestra();
           ColorReconocido = (int)sample[0];      
          return ColorReconocido;
      }   

//Check color

Code: Select all

   public int VerificarColor(int luzValor)
      {
      int valor;
      switch (luzValor) {
         case Color.RED:   
            valor = luzValor;
         break;
         case Color.YELLOW:   
            valor = luzValor;
         break;
         case Color.BLUE:   
            valor = luzValor;
         break;
         case Color.GREEN:   
            valor = luzValor;
         break;   
         case Color.WHITE:   
            valor = luzValor;
         break;            
         default:
            valor = -1;
            break;
      }
      return valor;
      }

//Call first the function READColor, to read twice the color

Code: Select all

   void READColor(int pos)
   {
      boolean flag = true;
      int colorPrimero = -1, colorSegundo = -1;
      Stopwatch sw = new Stopwatch();
      while(flag)
      {
         sw.reset();
         do
         {
            motores.forward(20, 20);
            Thread.sleep(dT);
            colorPrimero = VerificarColor(sensorLinea.LuzValor());            
         }while(colorPrimero == -1 && sw.elapsed() < 3000);
         Thread.sleep(1000);
         motores.Detener();
         
         if(colorPrimero != -1 )
         {
            sw.reset();
            do
            {
               motores.forward(40, 40);
               Thread.sleep(dT);
               colorSegundo = VerificarColor(sensorLinea.LuzValor());               
            }while(colorSegundo == -1 && sw.elapsed() < 4000);                         
         }
         Thread.sleep(1000);
         if (colorPrimero == colorSegundo && colorPrimero != -1) {
            mIzquierdo.setSpeed(0);
            mDerecho.setSpeed(0);
            arreglo[n] = colorPrimero;
            flag = false;
            LCD.drawString("LeerColorBaseDeTurbina: ", 0, 3);   
            LCD.drawString("COLOR--BASE: " + colorPrimero, 0, 4);   
         } else {
            motores.Recorrer(7.00, 40, 40); // retroceder un poco y volver a intentar
            colorPrimero = -1;
            colorSegundo = -1;
         }
         Thread.sleep(dT);
      }
   }

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

Re: GETCOLORIDMODE YELLOW AND RED IS ZERO

Postby gloomyandy » Thu Jul 06, 2017 8:58 am

I'm sorry I don't understand what your problem is. So how are you testing your code, what happens, what are you expecting to happen?
leJOS news https://lejosnews.wordpress.com/

hflores
New User
Posts: 14
Joined: Mon Feb 27, 2017 8:50 pm

Re: GETCOLORIDMODE YELLOW AND RED IS ZERO

Postby hflores » Thu Jul 06, 2017 12:54 pm

When the robot moves it must detect a yellow object and a red object but both detects value equal to zero.
I show the code to read the color and how the robot is moving.

Please help.

Thank you.-

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

Re: GETCOLORIDMODE YELLOW AND RED IS ZERO

Postby gloomyandy » Thu Jul 06, 2017 2:24 pm

Well a red object should return zero as that is the value that leJOS uses for red. So it just sounds like your sensor is nor differentiating between red and yellow very well. So you need to try and work out why that might be. Have you tested your sensor just on a red and a yellow object? Make sure you use an actual LEGO yellow brick as this will be the colour that the sensor has been optimised for. Does your sensor recognise those objects correctly? If so how does the yellow of the brick compare to whatever it is that you are trying to recognise with your robot? You may also need to experiment with the placement of your sensor (how far away from the object) and the lighting conditions (does building a shade around the sensor help).

I doubt if there is anything wrong with your code. However I notice that you constantly call getColorIdMode before every fetchSample, there is no need to do this, just get it once and store the handle for use later.
leJOS news https://lejosnews.wordpress.com/

hflores
New User
Posts: 14
Joined: Mon Feb 27, 2017 8:50 pm

Re: GETCOLORIDMODE YELLOW AND RED IS ZERO

Postby hflores » Thu Jul 06, 2017 3:12 pm

Thank you

Already probe, when the robot is stopped also reads the same values, use another sensor and have the same problem. He sometimes reads value three. That's why I call constantly to getColorIdMode, so that at some point I read three.
The sensor is closest to the object and reads three.

How do you optimize this? How to improve my code so that it reads as soon as possible even using the sensorPiso.setFloodlight (Color.NONE) statement. I have the problem.

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

Re: GETCOLORIDMODE YELLOW AND RED IS ZERO

Postby gloomyandy » Thu Jul 06, 2017 3:46 pm

I'm sorry but I don't understand your comments.

It makes no sense to call getColorIdMode more than once, you are just wasting cpu cycles. Look at the source of the sensor driver to see how it works.
https://sourceforge.net/p/lejos/ev3/cod ... ensor.java

No changes to your code will make the sensor any better at differentiating between red and yellow, you may be able to make changes to your robot to do that (like shading the sensor from external light).

You may be able to do a better job of detecting the different colours by using the sensor in RGB mode and comparing the values returned but this is not a trivial thing to do.

What exactly is it that you are testing the colour of? Is it some sort of track or competition board (post a picture of it if you can). It may just be that the yellow colour being used is not the same as the LEGO yellow.

Oh and please stop sending a private message to me for each of your posts. Just post your questions on the forum.
leJOS news https://lejosnews.wordpress.com/

hflores
New User
Posts: 14
Joined: Mon Feb 27, 2017 8:50 pm

Re: GETCOLORIDMODE YELLOW AND RED IS ZERO

Postby hflores » Thu Jul 06, 2017 5:00 pm


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

Re: GETCOLORIDMODE YELLOW AND RED IS ZERO

Postby gloomyandy » Thu Jul 06, 2017 8:00 pm

That is a very narrow area that you will be viewing with the sensor, I'm not surprised it is struggling. If you can perhaps you should double up the walls to give it a bigger target to view.
leJOS news https://lejosnews.wordpress.com/

hflores
New User
Posts: 14
Joined: Mon Feb 27, 2017 8:50 pm

Re: GETCOLORIDMODE YELLOW AND RED IS ZERO

Postby hflores » Thu Jul 06, 2017 8:30 pm

Thanks, I have to change the robot design

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

Re: GETCOLORIDMODE YELLOW AND RED IS ZERO

Postby gloomyandy » Thu Jul 06, 2017 8:37 pm

Also does it have to be red and yellow? You may find some other colour works better than yellow!
leJOS news https://lejosnews.wordpress.com/

hflores
New User
Posts: 14
Joined: Mon Feb 27, 2017 8:50 pm

Re: GETCOLORIDMODE YELLOW AND RED IS ZERO

Postby hflores » Thu Jul 06, 2017 8:51 pm

Rules of competition, this can be red, yellow, green, black, white, blue


Return to “EV3 Software”

Who is online

Users browsing this forum: No registered users and 2 guests