NXC Line Follower

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
devil11
Posts: 5
Joined: 01 Nov 2010, 00:01

NXC Line Follower

Post by devil11 »

Hi.
I'm trying to use the Color Sensor like a Light Sensor with nxc. I tried the line follower wrote by Benedettelli on his tutorial Programming LEGO NXT Robots using NXC but it doesn't work. The robot (classic Quick Start of NXT 2.0 kit) go always straight and the light of the Light Sensor is always off.
This is the code I used:

Code: Select all

#define LUMINOSITA_SOGLIA 35

task main()
{
	SetSensorLight(IN_2); // Definisce il sensore 2 come sensore della luce
	
	OnFwd(OUT_AC, 75);
	while(true)
	{
		if (Sensor(IN_3) > LUMINOSITA_SOGLIA)
		{
			OnRev(OUT_C, 75);
			until(Sensor(IN_3) <= LUMINOSITA_SOGLIA);
			OnFwd(OUT_AC, 75);
		}
	}
}
I read also Hansen's book LEGO MINDSTORMS NXT Power Programming - Robotics in C but the Light Sensor set the sensor in a difficult way, that i don't understand.
What is the problem? How I can set the Color Sensor working like the old Light Sensor?
Thank you for replies.
Rocco.

Ps: Excuse me for the english! =S
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: NXC Line Follower

Post by muntoo »

S2 != S3

--

In case you didn't get that:

To make sure you don't encounter this problem be sure to declare a single constant. It makes your code easier to read, too:

Code: Select all

//#define PORT_LIGHT S3 //IN_3 is the NBC version
//OR
const byte PORT_LIGHT = S3; //IN_3 is the NBC version
Last edited by muntoo on 01 Nov 2010, 03:02, edited 1 time in total.
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC Line Follower

Post by afanofosc »

The color sensor cannot be used like the light sensor without some code changes. It isn't a light sensor so you can't simply configure the port to which it is connected for use with a light sensor. If you do that then the firmware treats the color sensor like it is the NXT 1.0/1.1 light sensor and that won't work. You have to tell the firmware the correct sensor type if you want it to communicate with the sensor correctly. To use a color sensor as a light sensor you need to use SetSensorColorRed(port). And as muntoo pointed out, make sure you use the right port number throughout your program. Once you have correctly configured the port to work with the color sensor then you can use Sensor(port) or SensorValue(port) or SENSOR_n (where n is 1..4) to read the light sensor value from the color sensor.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
devil11
Posts: 5
Joined: 01 Nov 2010, 00:01

Re: NXC Line Follower

Post by devil11 »

muntoo wrote:S2 != S3

--

In case you didn't get that:

To make sure you don't encounter this problem be sure to declare a single constant. It makes your code easier to read, too:

Code: Select all

//#define PORT_LIGHT S3 //IN_3 is the NBC version
//OR
const byte PORT_LIGHT = S3; //IN_3 is the NBC version
:shock:
Right!
This was a novice fault. :?
devil11
Posts: 5
Joined: 01 Nov 2010, 00:01

Re: NXC Line Follower

Post by devil11 »

afanofosc wrote:The color sensor cannot be used like the light sensor without some code changes. It isn't a light sensor so you can't simply configure the port to which it is connected for use with a light sensor. If you do that then the firmware treats the color sensor like it is the NXT 1.0/1.1 light sensor and that won't work. You have to tell the firmware the correct sensor type if you want it to communicate with the sensor correctly. To use a color sensor as a light sensor you need to use SetSensorColorRed(port). And as muntoo pointed out, make sure you use the right port number throughout your program. Once you have correctly configured the port to work with the color sensor then you can use Sensor(port) or SensorValue(port) or SENSOR_n (where n is 1..4) to read the light sensor value from the color sensor.

John Hansen
Ok.It's clear. But why we need SetSensorColorRed() with the new Color Sensor to work like the old Light Sensor?

Another question. In your book, for the line follower, you first calibrate the sensor. It this really necessary? Or we can make a simple line follower without calibration (like on Benedettelli's tutorial)?
devil11
Posts: 5
Joined: 01 Nov 2010, 00:01

Re: NXC Line Follower

Post by devil11 »

Now it works!

Code: Select all

#define LUMINOSITA_SOGLIA 35

task main()
{
	SetSensorColorRed(IN_3); // Definisce il sensore 3 come sensore della luce
	
	OnFwd(OUT_AC, 75);
	while(true)
	{
		if (Sensor(IN_3) > LUMINOSITA_SOGLIA)
		{
			OnRev(OUT_C, 75);
			until(Sensor(IN_3) <= LUMINOSITA_SOGLIA);
			OnFwd(OUT_AC, 75);
		}
	}
}
Thank you very much!

Maybe I also understand why the ColorRed. Red is the light emitted by the sensor to measure the light reflected (there isn't a withe light like on the old sensor). Right? So it works also with ColorBlue and ColorGreen?

Still thank you! :D
m-goldberg
Posts: 73
Joined: 29 Sep 2010, 12:05

Re: NXC Line Follower

Post by m-goldberg »

Two points.

1. The older light sensor has a red LED, not a white one. Red LEDs are the cheapest kind.

2. Calibration definitely helps a line follower if it's going to used in more than one ambient light situation. In low light the threshold values will quite a bit lower than in bright light.
Regards, Morton
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC Line Follower

Post by afanofosc »

As Morton mentions, the reason for calibrating a line follower is so that it works well in the current ambient lighting situation. If you will only run it in exactly the same lighting conditions as you test it in then you don't need calibration but usually that is not how things work in real life. The values you get back from the light sensor will vary in different lighting and differing line following surfaces.

Using the green or blue LED in the color sensor will return different values than what you get using the red LED and probably will not allow you to distinguish between black and white as well as the red LED does. You should experiment with the color sensor to see what values you get from it over white and black colors with the 3 different LEDs so that you are familiar with its capabilities.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
devil11
Posts: 5
Joined: 01 Nov 2010, 00:01

Re: NXC Line Follower

Post by devil11 »

afanofosc wrote:As Morton mentions, the reason for calibrating a line follower is so that it works well in the current ambient lighting situation. If you will only run it in exactly the same lighting conditions as you test it in then you don't need calibration but usually that is not how things work in real life. The values you get back from the light sensor will vary in different lighting and differing line following surfaces.

Using the green or blue LED in the color sensor will return different values than what you get using the red LED and probably will not allow you to distinguish between black and white as well as the red LED does. You should experiment with the color sensor to see what values you get from it over white and black colors with the 3 different LEDs so that you are familiar with its capabilities.

John Hansen
Yes, I understand the reasons of calibration but I have to proceed step by step! And calibration is a high step, but I will try calibration sooner or later. :D

I will also try if the line follower work well with the blue LED or the green LED and I will post my result.

Still thank you for replies! :D
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC Line Follower

Post by mattallen37 »

m-goldberg wrote:...The older light sensor has a red LED, not a white one. Red LEDs are the cheapest kind...
I think that the main reason for being red, is that most sensors are tuned for sensitivity in the red/infrared spectrum.
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: No registered users and 0 guests