ColorSensor pcAPI

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

Moderators: roger, 99jonathan, imaqine

ColorSensor pcAPI

Postby glasseyes42 » Thu Oct 20, 2011 11:45 am

Hi new to the forums etc etc so please forgive any of the usual.

Been learning robotics in university for just over 2 years, our course focuses entirely on lejos so I know "some", but there is so much more to know.

The basic question : "running the program via the pcapi over USB, code that worked on the brick itself locally now no longer works. It is very simply designed to output the ColorSensor RGB values from the brick to System.out on the machine"

When I run this (I will post my code, but its basically the SensorTest sample but for ColorSensor), any of the getRed(), getBlue(), getGreen() returns 0. But if I were to getColorID() it will return those values. Unfortunately I require the RGB values for my project so receiving the stock 6 colours isn't enough.

Short of progressing to working out I2C buffer lengths and checking to see if these are being entirely sent back via LCP I don't know where to go from here...

Thank you very much for help provided.

Info : NXT brick 2.0, OS X Lion, 0.9.0 rev. 4498

SOURCE:
Code: Select all
import lejos.nxt.*;
import lejos.nxt.addon.EOPD;
import lejos.nxt.remote.NXTCommand;
import lejos.pc.comm.*;

public class SensorTest{
   public static void main(String[] args) {
      
      EOPD distance = new EOPD(SensorPort.S3,true);
      ColorSensor colour = new ColorSensor(SensorPort.S2);
      ColorSensor.Color temp;
      
      while(true) {   
         temp = colour.getColor();
         
         
         int test = colour.getColorID();
         
         
         String RGB = "R" + Integer.toString(temp.getRed()) + " G" + Integer.toString(temp.getGreen()) + " B" + Integer.toString(temp.getBlue());
         System.out.println("distance = " + distance.processedValue());
         System.out.println(RGB);
         System.out.println(test);
         try {
            Thread.sleep(1000);
         } catch (InterruptedException ie) {}
      }
   }
}
glasseyes42
New User
 
Posts: 3
Joined: Thu Oct 20, 2011 11:33 am

Re: ColorSensor pcAPI

Postby gloomyandy » Thu Oct 20, 2011 12:49 pm

Hi,
Not all features work from the remote API because of limitations of LCP. In this case the readValues method of a remote sensor port is not implemented:
http://lejos.svn.sourceforge.net/viewvc ... iew=markup
see the code at the end of the above link...

Since you are studying robotics, a really good thing to get into the habit of doing is reading source code as much as possible. So you may find how I worked out what was going on of interest.... In this case I had a look at the ColorSensor code, From this I could see that it used the readValues call of SensorPort, A bit of searching found me the PC implementation of SensorPort (this implements a sensor port over LCP), and the comments tell the story. Strangely it turns out I actually added this code and comments, but had completely forgotten about them!
http://lejos.svn.sourceforge.net/viewvc ... 56&r2=3312

oh and reading the code would also have shown you that the ColorSensor is not an i2c device (it is actually a really odd device, and a good exercise would be to understand how it works and how the interface to it is implemented), so no amount of messing around with the i2c buffer would help!

All the best

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

Re: ColorSensor pcAPI

Postby glasseyes42 » Thu Oct 20, 2011 1:28 pm

Hi,

thank you so much for the quick reply.

Had been looking through the source code - clearly the wrong places :P

I've seen the

Code: Select all
public int readRawValues(int[] values)
       {
           return -1;
       }


and recognise this request from the ColorSensor driver code for the 4 values.

So bluntly put there is no way to get RGB via the remote API over LCP

...in its current state. I've no concept of the LCP protocol right now, happy to learn and plan on reading more on how it is working to see if I can get something to play nice.
I'm wondering if a quicker alternative will be to run a program on the NXT which communicates over bluetooth to a program separate to Lejos - and the program on the brick to act as a puppet. But i've no idea how to do that one so I guess I'll go read up on the communication tutorial
glasseyes42
New User
 
Posts: 3
Joined: Thu Oct 20, 2011 11:33 am

Re: ColorSensor pcAPI

Postby gloomyandy » Thu Oct 20, 2011 2:26 pm

To be honest I would always recommend that you run your own code on the NXT and if you need it communicate with a PC based program to do the things you can't do on the NXT. This gives you much greater flexibility to handle things like real time issues, and also allows you to at least consider handling things like signal loss and other issues that you can't deal with when using LCP. LCP is the DUPLO bricks of Lego robotics...

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

Re: ColorSensor pcAPI

Postby glasseyes42 » Thu Oct 20, 2011 2:35 pm

haha, cheers - loving the duplo comment...

its a correct metaphor though to the lego bricks, the more complex you go, the greater chance of failure
glasseyes42
New User
 
Posts: 3
Joined: Thu Oct 20, 2011 11:33 am


Return to NXJ Hardware

Who is online

Users browsing this forum: No registered users and 0 guests

cron
more stuff