Color Sensor and Data collision and Bluetooth/USB link

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
floppi
Posts: 48
Joined: 25 Jul 2012, 22:55

Color Sensor and Data collision and Bluetooth/USB link

Post by floppi »

Hello,

I Don't know where to post my topic, I've many questions !

I want to read rgb values from the color sensor ( nxt2 version ).
( the aim : creating a color scanner with the nxt )

1) I use direct command to communicate with my NXT with Delphi on my PC on bluetooth.
I do not find how to read RGB values with direct commands, I can only obtain the number of the color ( from 1 to 6 ).
Is there Direct Commands which are not explained on the lego doc ?

2) my workaround : I decided to use the same trick as Motorcontrol with motors
I wrote a simple NXC program wich use ReadSensorColorRaw and store a formatted string of the RGB values in a bluetooth mailbox.
Reading mailbox with comport emulation on bluetooh has some bug, sometimes no data is received or unexpected data, is Bluetooth reliable ?

3) So my nxc program write a string in a mailbox every 50ms, my pc read the mailbox on a loop.
Are the mailbox protected while the NXT is writing data, is there a risk I read some corrupted values with my pc ?

Thanks in advance for your help.

Stéphane.
Last edited by floppi on 29 Jul 2012, 17:46, edited 1 time in total.
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: color sensor and data collision ?

Post by mattallen37 »

I don't write custom computer applications, but I haven't had much success with NXT <BT> PC. It always seems extremely buggy. NXT <BT> NXT doesn't seem so bad though.

For computer interfacing, I suggest you use USB.

No matter what communication method you use, you should implement at least a simple error check. An 8-bit checksum is extremely easy to implement (at least on the NXT side), and it should work fairly well (if it matches, then I think the odds are 255 to 1 that the data is correct).

Probably the best way to avoid errors is to make the PC request the info, and then have the NXT take a sensor reading and respond to the request (instead of constantly putting the latest data into the mailbox, possibly while the PC is trying to read it).
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
floppi
Posts: 48
Joined: 25 Jul 2012, 22:55

Re: color sensor and data collision ?

Post by floppi »

thanks for your response.

Communicating with bluetooth is easy because it's a serial port emulation.
I don't find information on how to communicate on USB.

I follow your suggest on making the pc requesting the info !
i will add checksum later.

Here is what i do :

unsigned int rawValues[4];
string value,fill,message,zero,box1;
int i;

task main(){
zero="000";
fill="";
box1="";
SetSensorColorFull(S1);
while(true){
while (strlen(box1)==0)
{
ReceiveMessage(1, true, box1); // waiting for data on mailbox
Wait(50);
}
if (box1=="Color") // color data requested
{
ReadSensorColorRaw(S1, rawValues);
message = "";
for (i = 0;i<4;i++) // create a formated string 4*3 characters with values
{
value = NumToStr(rawValues);
fill = RightStr(zero,3-strlen(value));
message += fill+value;
}
SendMessage(0, message); // send formatted string to mailbox
Wait(50);
box1="";
}
}
}
floppi
Posts: 48
Joined: 25 Jul 2012, 22:55

Re: color sensor and data collision ?

Post by floppi »

hello,

Can someone help me with USB and NXT ?

I have found libusb, klibusb, parser for delphi, but it is too complicated for me.
Now, the only way I have found is to write the main program with NXC, data will be stored on a file on nxt brick, and then I read the file with the pc.

I've got another problem, rgb raw values are not enough accurate or I do not know how to use them !
floppi
Posts: 48
Joined: 25 Jul 2012, 22:55

Re: color sensor and data collision ?

Post by floppi »

ok, 2 days of hard work trying to communicate with the brick on USB.

I try to explore Bricx source, I try Libusb ( a generic driver for usb device ), found a parser for Delphi (pascal), hours to try to make this work.
No success !!

Finally, the simple way, Lego NXT drivers is based on winusb.sys. ( standard usb driver )
I've found an example on how to communicate using MS supplied standard USB driver in delphi language.

Here is the link for those it might interest :

https://sourceforge.net/projects/winusb-delphi/
Warning : NXT Endpoint is on $82, by default this example is on $81, you must change this value so that you can read data from NXT.

Very simple to use !

At least only one problem remains for me : rgb raw values which are not accurate.
spillerrec
Posts: 358
Joined: 01 Oct 2010, 06:37
Location: Denmark
Contact:

Re: Color Sensor and Data collision and Bluetooth/USB link

Post by spillerrec »

When you say they are not accurate, what do you mean? The color sensor from Lego is only supposed to differentiate between 6 colors, you shouldn't expect it to me more accurate than needed to reliable detect those.
Of course you also need to calibrate the sensor, i.e. map the raw values to the actual values. You can also try using the scaled values instead of the raw values.
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
floppi
Posts: 48
Joined: 25 Jul 2012, 22:55

Re: Color Sensor and Data collision and Bluetooth/USB link

Post by floppi »

I know that this color sensor is designed for differentiate 6 colors, but i though that I can differientate more colors if i had the RGB values.
I only try to read ONE rgb values in RAw mode ( value : 0 to 1024 ), so i make a simple thing, I pick the R value ( then G and B) and do this : R/1024*255 to obtain value from 0 to 255 like standard RGB values and transform this in color. it's not good.

Now that I Communicate On USB which is very fast, I can read several times for the same color I want to identify and see the average rgb value.
And I will do myself the match between read value/real color.

Some testing is needed.
floppi
Posts: 48
Joined: 25 Jul 2012, 22:55

Re: Color Sensor and Data collision and Bluetooth/USB link

Post by floppi »

ok, I've got a big problem :

I work on usb with direct command on windows 7 x64.

I can set sensor type on port, read value by command getoutputstate, etc....
I can write message to mailboxon nxt.
So usb work in the both way !

But each time i try to read a mailbox, no response is send by the nxt , and usb communication seem's to be out of order.
I use the same command with bluetooth and everything is ok, nxt sends the message !

I send this message : 0x00 0x13 0x00 0x00 0x01

Can someone help me ? Or have a clue ?
spillerrec
Posts: 358
Joined: 01 Oct 2010, 06:37
Location: Denmark
Contact:

Re: Color Sensor and Data collision and Bluetooth/USB link

Post by spillerrec »

The mailbox messages must be strings that are NULL terminated, if I remember correctly. That means, only one NULL and it must be at the very end. I think the Lego docs said something about this in relation to either BT or USB, but I don't quite remember what. So I have a suspicion that those extra NULLs might mess up the protocol.
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
floppi
Posts: 48
Joined: 25 Jul 2012, 22:55

Re: Color Sensor and Data collision and Bluetooth/USB link

Post by floppi »

thanks for trying to help me.

My messages were correctly formatted with the final 0.

I've retried all at the beginning with the klibusb library ( more control on USB ).

Finally, the same problem, and then I found where it was.

I sized all my array with 64 bytes, but for USB the max is 59, and that was the cause of the bug !!!!!
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 0 guests