Using RCX light sensor with NXT

Discussion specific to the intelligent brick, sensors, motors, and more.
dsjove
Posts: 56
Joined: 22 Jan 2011, 23:22

Using RCX light sensor with NXT

Post by dsjove »

Hello,

I am using my old RCX light sensor with the NXT and NXC. Every couple times the light sensor does not function well. The sensor value does not move from its inital value much for the entire program run. Run the program again, and the values are as expected.

What should I do?

Dave
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Using RCX light sensor with NXT

Post by mattallen37 »

Perhaps your initialization code is not right. Could you post that part of your program? Also, how are you attempting to access the value? I always use SENSOR_n.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
dsjove
Posts: 56
Joined: 22 Jan 2011, 23:22

Re: Using RCX light sensor with NXT

Post by dsjove »

The code is quite simple. Is there a calibration I should be doing?

void Switch_Construct(int s, unsigned int port, int trigger, int filter)
{
switches.port = port;
SetSensor(switches.port, SENSOR_LIGHT);
}

inline int SensorValue2(int port)
{
return SensorValue(port);
}

void Switch_Run(int s)
{
int initValue = SensorValue2(switches.port);
int lastHitTick = 0;
while (true)
{
int currentValue = SensorValue2(switches.port);
TextOut(0, LCD_LINE3, NumToStr(currentValue));
}
}
timpattinson
Posts: 224
Joined: 30 Oct 2010, 04:10
Location: 127.0.0.1
Contact:

Re: Using RCX light sensor with NXT

Post by timpattinson »

dsjove wrote:The code is quite simple. Is there a calibration I should be doing?

Code: Select all

void Switch_Construct(int s, unsigned int port, int trigger, int filter)
{
switches[s].port = port;
SetSensor(switches[s].port, SENSOR_LIGHT);
}

Code: Select all

[/quote]


[quote="djsove"]
inline int SensorValue2(int port)
{
return SensorValue(port);
}
This function is not needed, instead you could use SENSOR_2 (or SENSOR_1 or SENSOR_3 etc. etc.) This is #defined as Sensor(port specified), which is the same thing
djsove wrote:

Code: Select all

void Switch_Run(int s)
{
int initValue = SensorValue2(switches[s].port);
int lastHitTick = 0;
while (true)
{
int currentValue = SensorValue2(switches[s].port);
TextOut(0, LCD_LINE3, NumToStr(currentValue));
}
}[
Maybe it would be a good idea to post the rest of your code, to see how these functions are implemented
Also- what are you trying to do
-Tim
-
*EDIT* Are you sure it's not just a bad connection *EDIT*
Commit to Lego Mindstorms StackExchange Q&A http://area51.stackexchange.com/proposals/4105
Minboards IRC Channel #mindboards on Freenode
My blog: http://timpattinson.wordpress.com/
dsjove
Posts: 56
Joined: 22 Jan 2011, 23:22

Re: Using RCX light sensor with NXT

Post by dsjove »

inline int SensorValue2(int port)
{
return SensorValue(port);
}

Is necessary because
int initValue = SensorValue(switches.port);
Cannot handle the expression as an input parameter.
I didn't want to explicitly write the local variable every time.
The array is essentially letting me pass-by-ref a train-switch struct into a generic function.

The code works most of the time. Sometimes though, simply put, the sensor gets fixated on a value and does not budge too much.
If I take my hand and repeatedly force sensor value changes, it will start providing decent values.
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Using RCX light sensor with NXT

Post by afanofosc »

A lot depends on when/where you call Switch_Construct. Can you show us that? Are you wanting a raw value or a scaled (percent) value or what?

To optimize your code I highly recommend that you not repeatedly (especially in a loop) index into an array of structs and then read one field of that struct. Instead, grab the struct once and better yet, grab the port field from that struct once. Then you wouldn't need to use your user-defined SensorValue2 function either.

Code: Select all

void Switch_Construct(int s, unsigned int port, int trigger, int filter)
{
	switches[s].port = port;
	SetSensor(port, SENSOR_LIGHT);
}

void Switch_Run(int s)
{
	byte port = switches[s].port;
	int initValue = Sensor(port);
	int lastHitTick = 0;
	while (true)
	{
		int currentValue = Sensor(port);
		TextOut(0, LCD_LINE3, NumToStr(currentValue));
		Wait(MS_3); // sensor values are only updated once every 3 ms anyway.
	}
}
Also, do not let yourself get confused by the way values are written to the LCD screen using the above approach. A value of 50 followed by a value of 5 would still look like 50 on the LCD since you are not erasing the display (the 5 would overwrite the 5 of the previous 50 value). I would also use NumOut rather than calling NumToStr in TextOut. That's precisely why NumToStr exists.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
dsjove
Posts: 56
Joined: 22 Jan 2011, 23:22

Re: Using RCX light sensor with NXT

Post by dsjove »

I have seen that text out does not clear the line. The text out is for debugging. I am seeing the sensor get hung up on values below 10 and not change for than +-3. If I put my hand in front of the sensor enough times, it eventually starts reading correctly. It is an RCX sensor hooked up to the Nxt.

Code: Select all

struct Switch
{
	int port;
	int trigger;
	int filter;
	bool position;
	bool occupied;
	bool passedTrigger;
};

mutex lock;
Switch switches[2];

inline int SensorValue2(int port)
{
	return SensorValue(port);
}

void Switch_Construct(int s, unsigned int port, int trigger, int filter)
{
	Acquire(lock);
	switches[s].port = port;
	switches[s].trigger = trigger;
	switches[s].filter = filter;
	switches[s].position = 0;
	switches[s].occupied = 0;
	switches[s].passedTrigger = 0;
	SetSensor(switches[s].port, SENSOR_LIGHT);
	Release(lock);
}

inline int SensorValue2(int port)
{
	return SensorValue(port);
}

void Switch_Run(int s)
{
	int initValue = SensorValue2(switches[s].port);
	int lastHitTick = 0;
	while (true)
	{
		int currentTick = CurrentTick();
		int currentValue = SensorValue2(switches[s].port);
		Acquire(lock);
		if (abs(initValue - currentValue) >= switches[s].trigger)
		{
			lastHitTick = CurrentTick();
			switches[s].occupied = true;
		}
		else
		{
			switches[s].occupied = (lastHitTick != 0 && (currentTick - lastHitTick) < switches[s].filter);
		}
		Release(lock);
		TextOut(0, LCD_LINE2, NumToStr(switches[s].occupied));
		TextOut(0, LCD_LINE3, NumToStr(currentValue));
		Wait(MS_3);
	}
}

task Switch_Run0()
{
	Switch_Run(0);
}

task manualSwitch()
{
	int pressedCount = 0;
	bool state = false;
	while (true)
	{
		int newCount = ButtonCount(BTNEXIT, false);
		if (newCount != pressedCount)
		{
			pressedCount = newCount;
			Acquire(lock);
			if (!switches[0].occupied)
			{
				if (state)
				{
					OnFwd(OUT_A, 100);
					Wait(90);
					Coast(OUT_A);
				}
				else
				{
					OnRev(OUT_A, 100);
					Wait(90);
					Coast(OUT_A);
				}
				state = !state;
			}
			Release(lock);
		}
		Wait(MS_3);
	}
}

task switchMain()
{
	SetLongAbort(true);
	Switch_Construct(0, S2, 5, 3000);
	StartTask(Switch_Run0);
	StartTask(manualSwitch);
}
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Using RCX light sensor with NXT

Post by afanofosc »

Is the code you posted the entire program? If it is then you are running an old version of the compiler that does not complain when you are missing a task called main.

There is no reason for the RCX light sensor to behave in the way you describe based on the way the code is written in the firmware so I am inclined to look for problems in your program.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
dsjove
Posts: 56
Joined: 22 Jan 2011, 23:22

Re: Using RCX light sensor with NXT

Post by dsjove »

The main task simply starts several tasks. This is the one that controls the switch. I have the latest compiler and latest firmware. You had previously helped me get it all set up a week or so ago :P And your non e-book just showed up on my doorstep yesterday!

When the program is simply a loop accessing the sensor and nothing more, I see this problem. The RCX sometimes gets hung up on a very small value range.
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Using RCX light sensor with NXT

Post by afanofosc »

I will run a few tests with one of my RCX light sensors to see if I can duplicate the problems you are encountering. Could you try reading the raw value and see if it gets "hung" like what you are seeing for the scaled percentage value? You can use SensorValueRaw(port), SensorRaw(port), or GetInput(port, RawValueField) to read this value regardless of how the sensor is configured.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest