No support for the LEGO Colour sensor??

This is where you talk about the NXJ hardware related topics such as the brick, sensors, LEGO pieces, etc.

Moderators: 99jonathan, roger, imaqine

No support for the LEGO Colour sensor??

Postby Tickstart » Wed Jul 13, 2011 4:46 pm

What's the deal, how does it work? There were no useful (to me..) examples in the supplied code, and I can't resolve the API. I'm not a Java beginner, but I feel really stupified by this.

For example, if I want to try it out, and see if the sensor can recognize the colour of my computer, HOW DO I DO?

System.out.println(cs.getColor()); yields nothing, just some stupid "object@blabla" if I remember correctly.
System.out.println(cs.getColorID()); yields "7" regardless of whatever the **** I point it at.
I JUST WANT IT TO SAY "BLACK" :(((((((

And how do I test something, like;
if (cs.getColor() == Color.BLACK) { ...

or;

if (cs.getColorID() == ColorSensor.Color.BLACK (// whats the difference?) ) { ...

THAK MAKES SENSE RIGHT??

Please help me, I'll be really nice to you, I'm not mad I'm just frustrated :(

// Anders
Last edited by Tickstart on Wed Jul 13, 2011 5:37 pm, edited 1 time in total.
Tickstart
New User
 
Posts: 13
Joined: Tue Jul 12, 2011 4:39 pm

Re: No support for the LEGO Colour sensor??

Postby gloomyandy » Wed Jul 13, 2011 5:31 pm

Well you could start by not calling the work of several people "junk", do you really expect help if you start out like that?

Then you could tell us if you have actually tried running the "junk" test program and what results it produces. Do you see any RGB values? Does it display any color values when you point the sensor at standard Lego parts?

Next you could let us know what version of leJOS you are trying to do this with and if you are running your code on the NXT or on A PC. If you are using 0.8.5 then you may be hitting a problem (that is fixed in 0.9.0) that stops some color sensors from working. That is why running the "junk" test program is a good idea as the output will not show any readings if there is a problem...

Finally you could describe how you are testing the color sensor. Are you pointing it at a Lego item? The sensor is really only optimized to handle the standard Lego colors, so it may not recognize the color of your computer. Also note that it only works for reflected light from the LED built into the sensor, it will probably not work if you point it at say a green area of your computer screen.

cs.getColor() returns a ColorSensor.Color object so you can't test against this directly. The color object contains the RGB color values and an id indicating the color that has been detected. To obtain the color id you need to call cs.getColor().getColor(), or you can just call cs.getColorID() if you have no need for the RGB values. So to test against a particular color you should be able to use:
if (cs.getColorID() == ColorSensor.Color.BLACK)

Color.BLACK and ColorSensor.Color.BLACK are the same values. ColorSensor.Color is an extended version of the Color class which adds some extra methods to provide access to information unique to that sensor (in this case the background light levels). Which as an experienced Java programmer you could have found out by reading the source which is here:
http://lejos.svn.sourceforge.net/viewvc ... iew=markup
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3634
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: No support for the LEGO Colour sensor??

Postby Tickstart » Wed Jul 13, 2011 5:34 pm

I'm awfully sorry ;ccccccc I will read your text shortly, just wanted to apologize :(
Tickstart
New User
 
Posts: 13
Joined: Tue Jul 12, 2011 4:39 pm

Re: No support for the LEGO Colour sensor??

Postby gloomyandy » Wed Jul 13, 2011 6:04 pm

Hi,
welcome to the forum, we really do try to help. Take a look at my questions and let us know as much as you can about what it is you are doing and we will try and get things running for you. The Lego sensor is really pretty complex it is in effect 3 devices in one. The sensor class tries to provide access to all of the functions so it is a little complex...

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

Re: No support for the LEGO Colour sensor??

Postby Tickstart » Wed Jul 13, 2011 7:30 pm

Stuff are working out quite nicely now, thank you!!!! You'll hear from me again though I'm afraid :P
Tickstart
New User
 
Posts: 13
Joined: Tue Jul 12, 2011 4:39 pm

Re: No support for the LEGO Colour sensor??

Postby gloomyandy » Wed Jul 13, 2011 7:41 pm

Hi,
Glad you have it working. It really is worth getting the ant build side of things sorted out so you can run our sample code. If you are using Eclipse you should be able to run the ant files by right clicking on the build.xml file (in the individual sample folders - son in this case in the ColorSensorTest folder) and then choosing run As->ant build (there are probably other ways as well but this works fine for me).

Also you may have discovered by now that the sensor does not really work for light grey. The only colors detected by the sensor are:
Color.BLACK, Color.BLUE, Color.GREEN, Color.YELLOW, Color.RED, Color.WHITE
The HiTechnics color sensor will detect a wider range of colors but not the Lego version (at least not using the standard detection algorithm - you may be able to do better using the raw RGB and background values)....

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


Return to NXJ Hardware

Who is online

Users browsing this forum: No registered users and 0 guests

more stuff