Strange color sensor behaviour

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

Moderators: roger, gloomyandy, skoehler

Strange color sensor behaviour

Postby KiwiBird » Thu Jan 02, 2014 4:59 am

I am trying to use the ev3 color sensor, but I am noticing some very strange behaviour. When I run my program normally, the color sensor flashes a red light, and when I get it to scan the color, it can take up to 4 seconds for the light to go blue and the color to be detected. Then the light color goes back to red. Sometimes, it seems to crash and exit the program.

However, If I run my program with LCD Debug, the sensor always has a blue light and is ready to scan a color. it will return the color instantly, and never seems to crash. Here is my code:

Code: Select all
Port p = LocalEV3.get().getPort("S1");
SensorModes sm = new EV3ColorSensor(p);
SensorMode colorSensor = sm.getMode("ColorID");
while (true)
      {
                        int sampleSize = colorSensor.sampleSize();
                        float samples[] = new float[sampleSize];
                        colorSensor.fetchSample(samples, 0);
         float color = samples[0];
         
         screenGraphics.clear();
         screenGraphics.drawString(String.valueOf(color), screenWidth/2, screenHeight/2, GraphicsLCD.BASELINE|GraphicsLCD.HCENTER);
         int but = Button.waitForAnyPress(2);
         if (but == Button.ID_ESCAPE) break;      }


Is there a way to keep the sensor ready in normal mode? Why does it make a difference?

Edit: changed code, but still inconsistent behaviour. Also, I sometimes get a socket exception: too many files open. This is reproducible with colortest sample program.
User avatar
KiwiBird
New User
 
Posts: 9
Joined: Fri Dec 27, 2013 6:12 am

Re: Strange color sensor behaviour

Postby gloomyandy » Thu Jan 02, 2014 10:15 am

No Idea what is going on. Please post details so we can try and reproduce...
1. What version of leJOS are you running.
2. Please post details of the code that fails and how you are building that code (what version of eclipse. Java etc)
3. How are you running the program, what steps are needed to reproduce the issue. How long does it take before the problem is seen.

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

Re: Strange color sensor behaviour

Postby KiwiBird » Thu Jan 02, 2014 10:54 pm

Actualy, I would mainly like to know how the color sensor SHOULD behave. What does the red flashing light mean? Should the light from the color sensor be blue all the time, or only when it is getting a value?
User avatar
KiwiBird
New User
 
Posts: 9
Joined: Fri Dec 27, 2013 6:12 am

Re: Strange color sensor behaviour

Postby gloomyandy » Fri Jan 03, 2014 12:08 am

The red flashing light typically means that the sensor is being reset. When reading color ids the sensor will normally be displaying a white/blue light. Have you tried testing your sensor using the Lego software? Perhaps there is a problem with it? As I said if you post an example of your code and provide the details I've asked for we can try and reproduce the problems your are seeing.
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4119
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Strange color sensor behaviour

Postby KiwiBird » Fri Jan 03, 2014 6:24 am

I THINK I SOLVED MY PROBLEM!

I tried using the normal lego software, and it was all working. There was just one small change i made though, I connected the sensor to the default port 3. I then went back to lejos, ran the colortest program with the sensor connected to port 3 (And selected port 3 in the program) and it worked. This time, there was no flashing red light, just a solid red light which then changed to solid white/blue and stayed that way. I then tried my program with port 3, and it worked just like that. To check, I went back to port one, which made the sensor flash red most of the time and act unpredictably, even crashing. Then back to 3, and it works. I have no idea why, but having the sensor in port 3 makes it work while port 1 doesn't.

If you want to reproduce my problem, try running colortest with the sensor in port 1, using lejos 0.5.0 alpha. I would like to know if this problem happens to anyone else.
User avatar
KiwiBird
New User
 
Posts: 9
Joined: Fri Dec 27, 2013 6:12 am

Re: Strange color sensor behaviour

Postby gloomyandy » Fri Jan 03, 2014 2:53 pm

I will try the color sensor when I get chance, but I'm pretty sure it works fine (I have two and will try both). Have you tried testing your sensor with the standard firmware and the sensor connected to port 1? There are hardware differences between ports 1&2 and ports 3&4 so it is worth checking that your sensor works correctly with the Lego software on port 1.

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

Re: Strange color sensor behaviour

Postby LastSamurai » Fri Jan 03, 2014 5:20 pm

I did just test the Ev3 with Lejos Alpha 0.5 and colortest. Sensor on S1 worked as well as S4. Did you perhaps select the wrong port while testing (on the screen)?
There might be some other problems http://www.lejos.org/forum/viewtopic.php?f=18&t=5327with this sensor though ;)
LastSamurai
New User
 
Posts: 9
Joined: Thu Dec 26, 2013 8:22 pm

Re: Strange color sensor behaviour

Postby RMS » Sun May 25, 2014 6:20 pm

Hi,
I stumbled across this post while looking for some tips on using the EV3 color sensor.
I have also noted that this thread is quite old, but thought a comment could be useful to others browsing the forum as well.
Although I'm a newbie around here and I wouldn't call myself an expert programmer especially in Java, I could imagine that defining variables (sampleSize, but, samples[]) inside an infinite while loop might lead to the described problems.
At least I would not rely on intrinsic mechanisms of the language preventing allocation of new memory in each cycle...

Hope this helps the one or the other.
RMS
New User
 
Posts: 2
Joined: Fri May 02, 2014 11:14 pm


Return to EV3 Software

Who is online

Users browsing this forum: Yahoo [Bot] and 3 guests

more stuff