RCX rotation sensor with NXC

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

RCX rotation sensor with NXC

Post 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.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: RCX rotation sensor with NXC

Post 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.
Image

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


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: RCX rotation sensor with NXC

Post 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.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: RCX rotation sensor with NXC

Post 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).
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: RCX rotation sensor with NXC

Post by afanofosc »

The firmware does not support more than 2 bytes for sensor values.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: RCX rotation sensor with NXC

Post 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.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: RCX rotation sensor with NXC

Post 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.
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 1 guest