Page 1 of 1
RCX rotation sensor with NXC
Posted: 09 Apr 2011, 05:19
by mattallen37
I am trying to use an RCX rotation sensor with the NXT (NXC). It all functions as it should, but I am using it in a clock application, so a signed 16 bit variable doesn't last very long (in this case, about 11 minutes). Is there any way to get a 32 bit value (signed or unsigned long) from the FW? According to my calculations, a signed long would allow for over a year of constant use (500+ days).
I think this may be a FW issue, but I am not sure.
Re: RCX rotation sensor with NXC
Posted: 09 Apr 2011, 06:28
by muntoo
If there isn't, an alternative solution would be to check if it's abs(rotationcount)>32000; if so, increase `unsigned long base_rotationcount` by 32768*sign(rotationcount). Then, you detect the arithmetic overflow (when it happens... unless every time it overflows, it's reset to 0 or some other value... I don't know what that would be), and get the adjusted rotation counts. It's crude, but unless your motor's moving at 30k degrees/ms, it should work. (Unless there is no arithmetic overflow, or reset to 0/etc... then it won't work.)
The exact implementation for this will take a bit of research.
Re: RCX rotation sensor with NXC
Posted: 09 Apr 2011, 06:37
by mattallen37
Not sure exactly what you mean, but I found a solution. Although I would like 32 bit support for the sensor, this works fine in this situation.
Code: Select all
if(SENSOR_1>=32760){ClearSensor(S1);}
I have a way to deal with the "overflow". I also know it could be == 32767, but there is a limiting factor elsewhere in the program.
Re: RCX rotation sensor with NXC
Posted: 09 Apr 2011, 21:21
by muntoo
I'd recommend lowering that down to 16384 (or even eliminating it, and relying on 10 seconds only):
Code: Select all
// #define ELIMINATE
long base_value[4] = {0, 0, 0, 0};
task forever()
{
while(1)
{
#ifndef ELIMINATE
if(abs(SENSOR_1) >= 16384)
{
#endif
base_value[S1] += SENSOR_1;
ClearSensor(S1);
#ifndef ELIMINATE
}
#endif
Wait(10000); // You can change this value, if you want; although, 10 second polling won't really affect your program speed, even by 1/100000 of a second, on average
// The wait value is based on the time it can potentially take to rotate the motor by 16383 degrees in the worst case scenario... And 10 seconds for moving the motor *that* would require 1638 degrees/second
}
}
long ReadSensor(const byte port)
{
return(Sensor(port) + base_value[port]);
}
This way, there's no chance of error (unless the NXT "freezes" for a few seconds, but the motors keep working, and this is terribly unlikely).
Re: RCX rotation sensor with NXC
Posted: 09 Apr 2011, 21:26
by afanofosc
The firmware does not support more than 2 bytes for sensor values.
John Hansen
Re: RCX rotation sensor with NXC
Posted: 10 Apr 2011, 08:51
by mattallen37
muntoo wrote:I'd recommend lowering that down to 16384 (or even eliminating it, and relying on 10 seconds only):
This way, there's no chance of error (unless the NXT "freezes" for a few seconds, but the motors keep working, and this is terribly unlikely).
I may lower it a little, but for now it works great. Thanks for the code idea, but I think I found a good solution for my needs.
Re: RCX rotation sensor with NXC
Posted: 10 Apr 2011, 09:04
by mattallen37
afanofosc wrote:The firmware does not support more than 2 bytes for sensor values.
John Hansen
Okay, that's what I figured. Thanks for confirming.