Problems with pc API Color & Sonic sensor [SOLVED]

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

Moderators: 99jonathan, roger, imaqine

Problems with pc API Color & Sonic sensor [SOLVED]

Postby Killerzwelch » Mon May 24, 2010 10:16 am

Hi,

I'm currently facing some issues with the PC API when using Color and / or Ultrasonic Sensor.

Attached the very simple source code and the sysouts. Have I forgotten something or is there a bug?

Code: Select all
import java.io.InputStream;
import java.io.OutputStream;

import lejos.nxt.ColorLightSensor;
import lejos.nxt.SensorPort;
import lejos.nxt.UltrasonicSensor;
import lejos.pc.comm.NXTComm;
import lejos.pc.comm.NXTCommFactory;
import lejos.pc.comm.NXTInfo;



public class HelloWorld {

   /**
    * @param args
    */
   public static void main(String [] args) throws Exception {
      try{
      NXTComm nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.USB);
      NXTInfo[] nxtInfo = nxtComm.search("NXT",NXTCommFactory.USB);
      System.out.println(nxtInfo.length);
      if(nxtInfo.length>0)
      {
         
         NXTInfo nxt = nxtInfo[0];
         //nxtComm.open(nxt);
         System.out.println("COnnected");
         int[] b;
         
         ColorLightSensor a = new ColorLightSensor(SensorPort.S2, ColorLightSensor.TYPE_COLORFULL);
         UltrasonicSensor c = new UltrasonicSensor(SensorPort.S4);
         System.out.println(c.getDistance());
         
         for(int i=0;i<100;i++)
         {
            System.out.println("Check" + i);
            System.out.println("Blue: " + a.getBlueComponent());
            System.out.println("Green: " + a.getGreenComponent());
            System.out.println("Red: " + a.getRedComponent());
            System.out.println("Floodlight: " + a.getFloodlight());
            System.out.println("Normalized Light: " + a.getNormalizedLightValue());
            b = a.getColor();
            for (int j : b) {
               System.out.println(j);
            }
            System.out.println("Color: " + a.getColor());
            System.out.println("RAW " + SensorPort.S1.readRawValue());
            
            Thread.sleep(1000);
            
         }
         
         
                  
      }
      else
      {
         System.out.println("No NXT Found");
      }
      }
   catch (Exception e) {
      // TODO: handle exception
   }
   }

}




Sysout:

Found NXT: NXT 0016530CEBC4
1
COnnected
Found NXT: NXT 0016530CEBC4
255
Check0
Blue: 16335
Green: 16335
Red: 16335
Floodlight: NONE
Normalized Light: 65535
0
0
0
Color: [I@1389e4
RAW 65535

EDIT:
I just tried the sensor test:
Found NXT: NXT 0016530CEBC4
light = 100
distance = 255
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 8
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7
light = 100
distance = 7

So the Ultrasonic is here working, but the light sensor always returns 100.

What is the correct class? I have the RGB Color Sensor with 3 holes in the front:
- RGB LEDs
- a black one (IR?)
- a white one (maybe light)


Thanks & Kind regards
Dominik
Last edited by Killerzwelch on Tue May 25, 2010 4:56 pm, edited 1 time in total.
Killerzwelch
New User
 
Posts: 11
Joined: Mon May 24, 2010 9:49 am

Postby gloomyandy » Mon May 24, 2010 11:05 am

Have you checked that the color sensor works with a program on the NXT? You may need to update your NXT menu program (which is what talks to the sensor when using it remotely).

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

Postby Killerzwelch » Mon May 24, 2010 6:04 pm

Hi Andy,

thanks for the fast reply.

Code: Select all
import lejos.nxt.*;

public class SensorTest {
  public static void main(String[] args) throws Exception {
    LightSensor light = new LightSensor(SensorPort.S1);

    while (true) {
      LCD.drawInt(light.readValue(), 4, 0, 0);
      LCD.drawInt(light.readNormalizedValue(), 4, 0, 1);
      LCD.drawInt(SensorPort.S1.readRawValue(), 4, 0, 2);
      LCD.drawInt(SensorPort.S1.readValue(), 4, 0, 3);
    }
  }
}


has returned the same value. Do you have an idea what this may be? What should I update? NXT Menu? You mean the lojos firmware?

100
1023
0
100

Thanks
Dominik
Killerzwelch
New User
 
Posts: 11
Joined: Mon May 24, 2010 9:49 am

Postby gloomyandy » Mon May 24, 2010 6:58 pm

Your test program is testing a light sensor not a ColorSensor. Try using the ColorSensorTest sample....

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

Postby Killerzwelch » Mon May 24, 2010 8:26 pm

Hmmm... okay Where to find it? I found in the repository this:

http://lejos.svn.sourceforge.net/viewvc ... ision=3683

but its full of errors.
- No singleton
- Colors instead of Color
- Colors.NONE does not exist
- ColorSensor is used (not the LEGO one)
and many more

What am I doing wrong? Can you please send me the link of the java file?
I also downloaded the new classes.jar that you've provided in the other topics - no success.

Thanks
Dominik
Killerzwelch
New User
 
Posts: 11
Joined: Mon May 24, 2010 9:49 am

Postby gloomyandy » Mon May 24, 2010 9:36 pm

If you are using Windows (you didn't provide any details of your system when asking for help) then you already have it. It is installed by the 0.8.5 installer in the lejosNXJProjects/samples dir in your home directory. The one you are looking at is the updated version that uses the development version of leJOS not the 0.8.5 release. If you are not on Windows then try searching for the filename. You can see the 0.8.5 version here...

http://lejos.svn.sourceforge.net/viewvc ... iew=markup

You may want to take a look at the other samples that are provided as part of the release.

If you find that this program does not work, you will find other threads on this forum describing the problems and providing details of how to obtain and install the updated classes.jar. (Sorry don't have time to dig the threads out for you).

You may also want to consider if using the remote API is the best way to implement your project. It does not really provide the real time control required for many projects. If you need to have a PC component you may want to consider splitting your code into two parts one that runs on the NXT and performs sensor reading and motor control and that communicates with the other part of your code (the none real time part) via Java I/O streams.

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

Postby Killerzwelch » Tue May 25, 2010 8:02 am

Hi Andy.

I'm using Win7 64 Bit and NXT 2.0

Seems that there was something queer. I downloaded the classes.jar again and stored them in my lib folder. Made a rebuild and now I got good values. But still - what must I do to get this remote working? All that I've found in the forum was a fix for the classes.jar, but (correct my if I'm wrong) I need pccomm.jar.

Thanks
Dominik
Killerzwelch
New User
 
Posts: 11
Joined: Mon May 24, 2010 9:49 am

Postby gloomyandy » Tue May 25, 2010 9:00 am

Which classes.jar did you download? What test program are you running? Please try and provide as much detail as possible, as it makes it much easier to help...


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

Postby Killerzwelch » Tue May 25, 2010 9:27 am

I downloaded the
www.gloomy-place.net/lejos/classes.jar

As testprogram I used
ColorSensorTest.java
(provided by the installation of leJOS) I uploaded the program to the NXT and got real values.

What I already tried is to replace the ColorLightSensor.class in the pccomm.jar with the .class file of the classes.jar but nothing has changed. When remotely connected via PC I only got -1 and 0 as return values.

Sorry, the Lejos as such is pretty new for me (I had once used this in school in 2007 or 2008) so I currently don't know what exactly is needed to analyse problems...

Thanks for your patience :)

regards
Dominik
Killerzwelch
New User
 
Posts: 11
Joined: Mon May 24, 2010 9:49 am

Postby gloomyandy » Tue May 25, 2010 10:37 am

Ok, the bug that is fixed is in the low level interface to the ColorSensor, you don't need to make any changes to the PC side classes to fix it (so put things back to the release versions). However the problem is that when using the remote classes, you need a program running on the NXT that supports the Lego Control Protocol (LCP) and in particular implements the LCPResponder class. The normal way to do this is to use the leJOS menu code that runs on the NXT (when you start it), as the responder. The problem you have is that the menu code on your NXT is the one supplied with 0.8.5 and this was built using the old version of classes.jar that has the bug in it. So you have two choices...
1. Rebuild the leJOS menu code and re-flash your NXT with that.
2. Write a small program that runs a responder and run that when you want to talk to your NXT.

Option 1 is probably the easy option. To do this you need to do the following... from a cmd window...
0. Make sure that you have an updated classes.jar file in the leJOS lib directory in your program files dir (you may already have done this).
1. Locate the leJOS menu code directory it is called startup and the program is StartupText.java.
2. Compile the program using nxjc StartupText.java
3. Link the program using nxjlink -v StartupText -o StartupText.bin
4. If the above works you now have an updated menu program. You now need to flash the NXT with this program. The easy way to do this is to copy the StartupText.bin file you have just built into the leJOS bin directory in your program files dir. Make sure you make a copy of the original first. Then turn on your NXT and run nxjflash to update the firmware....

Note that you may have multiple copies of the leJOS classes.jar file on your system make sure that they have all been updated.

Finally as I said before, doing things using the remote interface may not be the best way to program your robot, you may want to consider alternatives....

Good luck

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

Postby Killerzwelch » Tue May 25, 2010 4:55 pm

Thats great! It has worked! :)

Thanks a lot!
Killerzwelch
New User
 
Posts: 11
Joined: Mon May 24, 2010 9:49 am

Re: Problems with pc API Color & Sonic sensor [SOLVED]

Postby Qiana » Sat Dec 10, 2011 8:38 pm

Thank you very much for this information! I have been unable to get the remote sensor to work with the build that I am working on. I followed the suggestions here, and was finally able to get it working properly. I am really excited about the project that I am currently working on. The concept was inspired by a project that I had read about by wolf technical. I hope to have this project completed soon, and resolving this issue was a huge step in the right direction. I really appreciate the help!
Qiana
New User
 
Posts: 1
Joined: Sat Dec 10, 2011 8:35 pm


Return to NXJ Software

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 1 guest

more stuff