Bricxcc, NXC, Color Sensor, NXT 2.0

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
roboteer
Posts: 12
Joined: 12 Aug 2011, 04:00

Bricxcc, NXC, Color Sensor, NXT 2.0

Post by roboteer »

Hi,

I am trying to get the code from Mr Hansen's book for the line follower to work with the new color sensor in NXT 2.0. Has anyone been able to make the new robot follow the black line using the new color sensor instead of the old light sensor? Is there any NXC sample code to do this. My robot follows the line a little then it just does other things.

Thanks for the help,
:roll:
mcsummation
Posts: 220
Joined: 23 Jan 2012, 17:07
Location: Round Rock, TX

Re: Bricxcc, NXC, Color Sensor, NXT 2.0

Post by mcsummation »

Please post a code snippet of your usage of the color sensor. Although it took me some time to determine how to use it properly in NXC, it is actually quite simple.
roboteer
Posts: 12
Joined: 12 Aug 2011, 04:00

Re: Bricxcc, NXC, Color Sensor, NXT 2.0

Post by roboteer »

Here is a code snippet of where I make the motors do something depending on the color value.
switch (SV){
case INPUT_BLACKCOLOR: // 1
OnFwd(OUT_A, FAST_SPEED);
OnFwd(OUT_C, SLOW_SPEED);
break;

case INPUT_BLUECOLOR: // 2
OnFwd(OUT_A, SLOW_SPEED);
OnFwd(OUT_C, SLOW_SPEED);
break;

case INPUT_GREENCOLOR: // 3
OnFwd(OUT_A, SLOW_SPEED);
OnFwd(OUT_C, SLOW_SPEED);
break;

case INPUT_YELLOWCOLOR: // 4
OnFwd(OUT_A, SLOW_SPEED);
OnFwd(OUT_C, SLOW_SPEED);
break;

case INPUT_REDCOLOR: // 5
OnFwd(OUT_A, SLOW_SPEED);
OnFwd(OUT_C, SLOW_SPEED);
break;

case INPUT_WHITECOLOR: // 6
OnFwd(OUT_A, SLOW_SPEED);
OnFwd(OUT_C, FAST_SPEED);
break;

default : write = "unknown: ";
OnFwd(OUT_A, SLOW_SPEED);
OnFwd(OUT_C, SLOW_SPEED);
}
mcsummation
Posts: 220
Joined: 23 Jan 2012, 17:07
Location: Round Rock, TX

Re: Bricxcc, NXC, Color Sensor, NXT 2.0

Post by mcsummation »

a) You really ought to use the "code" control when you are posting here.
b) What is the definition of "sv"? That is, how did you set up the sensor?
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Bricxcc, NXC, Color Sensor, NXT 2.0

Post by afanofosc »

To use the color sensor like the NXT 1.0 light sensor you configure it using SetSensorColorRed(port) and then read its value using SENSOR_n where "n" is 1..4 depending on which port you have the sensor attached to. In this case you would get a value from 0 to 100 which you can use after calibrating what value is returned directly over white and what value is returned directly over black. You could also use the raw value in this mode by calling SensorRaw(port). Using the color sensor for line following like you might have previously used the NXT 1.0 light sensor works best when you configure the sensor in this manner. If you configure the color sensor as a full color sensor using SetSensorColorFull(port) then you would get a color number back by reading its value using SENSOR_n (as before) but I think you will have a much harder time trying to use the sensor in this mode as a line following sensor. The device is slower in full color mode and when you deviate off of the black line you would get a shade of grey rather than true black or true white which could cause you all sorts of trouble in your code.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
roboteer
Posts: 12
Joined: 12 Aug 2011, 04:00

Re: Bricxcc, NXC, Color Sensor, NXT 2.0

Post by roboteer »

Thank you so much Mr. Hansen. This is very helpful. I will try this and will let you know if I have further questions. Your book has been very helpful, by the way. I also found your Bricxcc tool very helpful.

Cheers
ggrinton
Posts: 16
Joined: 24 Feb 2012, 12:18

Re: Bricxcc, NXC, Color Sensor, NXT 2.0

Post by ggrinton »

I am interested in this thread since, as a very new user, I have been having similar problems (challenges!).

I found that if I use SetSensorColorFull(S3); then I can read the light value with code like
  • int light, RawColor[];
    ReadSensorColorRaw(S3, RawColor);
    light = RawColor[3];
but if I try to use light = SensorRaw(S3); I don't get any value returned.

On the other hand, if I use SetSensorColorRed(S3); then to get a meaningful light value, I need to use light = SensorRaw(S3); and I don't get any meaningful values from ReadSensorColorRaw.

Are there some examples of code for using this sensor anywhere around please?
roboteer
Posts: 12
Joined: 12 Aug 2011, 04:00

Re: Bricxcc, NXC, Color Sensor, NXT 2.0

Post by roboteer »

I am still working through the old line-follower code. It is very tricky. I did find this sample code. I believe that it is NXC but have not tried it yet. I also do not know if it applies to the default color sensor. Try it at your own risk, of course. :o

http://www.techbricks.nl/downloads/line ... 20NXT2.nxc
roboteer
Posts: 12
Joined: 12 Aug 2011, 04:00

Re: Bricxcc, NXC, Color Sensor, NXT 2.0

Post by roboteer »

Does anyone know if it matters which way the color sensor is facing? Does it matter if it is put at the front of the robot versus the rear of the robot?
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Bricxcc, NXC, Color Sensor, NXT 2.0

Post by mattallen37 »

SensorRaw returns the RAW ADC value of the sensor port (of pin 1). AFAIK, the color sensor doesn't even have connection to that pin. With a color sensor, the value should always be 1023.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
Post Reply

Who is online

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