Page 1 of 2

Bricxcc, NXC, Color Sensor, NXT 2.0

Posted: 07 Mar 2012, 19:28
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:

Re: Bricxcc, NXC, Color Sensor, NXT 2.0

Posted: 07 Mar 2012, 19:43
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.

Re: Bricxcc, NXC, Color Sensor, NXT 2.0

Posted: 07 Mar 2012, 21:30
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);
}

Re: Bricxcc, NXC, Color Sensor, NXT 2.0

Posted: 07 Mar 2012, 23:57
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?

Re: Bricxcc, NXC, Color Sensor, NXT 2.0

Posted: 08 Mar 2012, 01:19
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

Re: Bricxcc, NXC, Color Sensor, NXT 2.0

Posted: 08 Mar 2012, 02:14
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

Re: Bricxcc, NXC, Color Sensor, NXT 2.0

Posted: 09 Mar 2012, 11:53
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?

Re: Bricxcc, NXC, Color Sensor, NXT 2.0

Posted: 09 Mar 2012, 17:07
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

Re: Bricxcc, NXC, Color Sensor, NXT 2.0

Posted: 09 Mar 2012, 17:13
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?

Re: Bricxcc, NXC, Color Sensor, NXT 2.0

Posted: 09 Mar 2012, 19:00
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.