Sensor Confusion - Sensor Modes, Constants, and Functions

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
nxt-ai
Posts: 36
Joined: 10 Jan 2011, 05:02

Sensor Confusion - Sensor Modes, Constants, and Functions

Post by nxt-ai »

I'm still learning NXC and I've been trying to make sense of sensor functions but to no avail. After looking through program demos and extensive Googleingoo I found at least one set of functions that seems to do the exact sane thing. So which is better:

SetSensorType(S1,SENSOR_TYPE_LIGHT_ACTIVE);
SetSensorMode(S1,SENSOR_MODE_RAW);
ResetSensor(S1);
...
int SVL = SENSOR_1;

SetSensorType(IN_3, IN_TYPE_LIGHT_ACTIVE);
SetSensorMode(IN_3, IN_MODE_RAW);
...
SV = SensorRaw(IN_3);

Which is better, or at least what the difference?

Thank You
Last edited by nxt-ai on 17 Jan 2011, 06:14, edited 1 time in total.
mightor
Site Admin
Posts: 1079
Joined: 25 Sep 2010, 15:02
Location: Rotterdam, Netherlands
Contact:

Re: Sensor Confusion -

Post by mightor »

The IN_* ones are for NBC and S1...4 are for NXC. I believe the S[1-4] have preference when programming NXC if I remember what John Hansen told me correctly. They both work, though.

- Xander
| My Blog: I'd Rather Be Building Robots (http://botbench.com)
| RobotC 3rd Party Driver Suite: (http://rdpartyrobotcdr.sourceforge.net)
| Some people, when confronted with a problem, think, "I know, I'll use threads,"
| and then two they hav erpoblesms. (@nedbat)
timpattinson
Posts: 224
Joined: 30 Oct 2010, 04:10
Location: 127.0.0.1
Contact:

Re: Sensor Confusion -

Post by timpattinson »

I think that you can do all that in one call of SetSensor()
This program will hopefully display the value of the light sensor on the screen, as a value out of 100

Code: Select all

task main()
{
    SetSensor(IN_1 , SENSOR_NXTLIGHT); // SENSOR_1 or IN_1 or S1 are exactly the same thing and are interchangeable.
    //The complete list of sensor 
    //names is in the NXC Guide  just search for Combined sensor type and mode constants
 
    while(1) // or while(true) (also doesn't make any difference)
    {
        ClearLine(LCD_LINE1);
        NumOut(0,LCD_LINE1, SENSOR_1); // in this case SENSOR_1 = Sensor(S1)
        Wait(50); // waits, because otherwise the screen flickers 
    } 
}  

nxt-ai wrote:

Code: Select all

SetSensorType(S1,SENSOR_TYPE_LIGHT_ACTIVE);
SetSensorMode(S1,SENSOR_MODE_RAW);
ResetSensor(S1);
I don't think the ResetSensor() is needed, unless a different sensor was plugged into that port within the same execution of the program, and even then before the SetSensor() and the SetSensorMode()
Does this answer your problem?
-Tim
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/
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Sensor Confusion -

Post by afanofosc »

ResetSensor is needed to make your program code wait until the firmware has finished configuring your sensor. IN_* is intended for NBC while S* is intended for NXC. All NBC constants can be used in NXC, though, so a lot of people do use IN_* when I recommend that they use S*. Same for the type and mode constants.

The SENSOR_TYPE_* and SENSOR_MODE_* and S* port names (and SENSOR_n macros) are all modeled after similar or identical constant names in NQC for the RCX brick.

SensorRaw is not at all the same as SENSOR_n except that if you configure a sensor in Raw mode then the Scaled value (which is what you read with SENSOR_n) happens to be the same value as the Raw value which you can read with SensorRaw. A sensor configured in a non-Raw mode can still have its raw value read using SensorRaw but you can also read the processed or scaled value via Sensor(port) or the SENSOR_n macros.

SetSensor calls SetSensorType, SetSensorMode, and ResetSensor. But there are not combined type and mode constants for all the possible combinations of types and modes so if you want a raw light sensor, for example, you will need to use the lower level function calls.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
nxt-ai
Posts: 36
Joined: 10 Jan 2011, 05:02

Re: Re: Sensor Confusion -

Post by nxt-ai »

Thanks all.
I still have another question which is better:

Code: Select all

SENSOR_1
or

Code: Select all

SensorRaw(IN_3)
Also what other constants will work with:

Code: Select all

SetSensor()
to use the light (ACTIVE).
timpattinson
Posts: 224
Joined: 30 Oct 2010, 04:10
Location: 127.0.0.1
Contact:

Re: Sensor Confusion -

Post by timpattinson »

These are the constants that work with the SetSensor() function. For using a NXT light sensor (active) you can use SENSOR_NXTLIGHT
eg

Code: Select all

task main()
{
    SetSensor(S1, SENSOR_NXTLIGHT);
    while(1)
    {
        NumOut(0,LCD_LINE1,SENSOR_1);
        Wait(50);
    }
}

NXC Guide wrote:Use the combined sensor type and mode constants to configure both the sensor mode and type in a single function call. More...
Defines
#define _SENSOR_CFG(_type, _mode) (((_type)<<8)+(_mode))
#define SENSOR_TOUCH _SENSOR_CFG(SENSOR_TYPE_TOUCH, SENSOR_MODE_BOOL)
#define SENSOR_LIGHT _SENSOR_CFG(SENSOR_TYPE_LIGHT, SENSOR_MODE_PERCENT)
#define SENSOR_ROTATION _SENSOR_CFG(SENSOR_TYPE_ROTATION, SENSOR_MODE_ROTATION)
#define SENSOR_CELSIUS _SENSOR_CFG(SENSOR_TYPE_TEMPERATURE, SENSOR_MODE_CELSIUS)
#define SENSOR_FAHRENHEIT _SENSOR_CFG(SENSOR_TYPE_TEMPERATURE, SENSOR_MODE_FAHRENHEIT)
#define SENSOR_PULSE _SENSOR_CFG(SENSOR_TYPE_TOUCH, SENSOR_MODE_PULSE)
#define SENSOR_EDGE _SENSOR_CFG(SENSOR_TYPE_TOUCH, SENSOR_MODE_EDGE)
#define SENSOR_NXTLIGHT _SENSOR_CFG(SENSOR_TYPE_LIGHT_ACTIVE, SENSOR_MODE_PERCENT)
#define SENSOR_SOUND _SENSOR_CFG(SENSOR_TYPE_SOUND_DB, SENSOR_MODE_PERCENT)
#define SENSOR_LOWSPEED_9V _SENSOR_CFG(SENSOR_TYPE_LOWSPEED_9V, SENSOR_MODE_RAW)
#define SENSOR_LOWSPEED _SENSOR_CFG(SENSOR_TYPE_LOWSPEED, SENSOR_MODE_RAW)
#define SENSOR_COLORFULL _SENSOR_CFG(SENSOR_TYPE_COLORFULL, SENSOR_MODE_RAW)
#define SENSOR_COLORRED _SENSOR_CFG(SENSOR_TYPE_COLORRED, SENSOR_MODE_PERCENT)
#define SENSOR_COLORGREEN _SENSOR_CFG(SENSOR_TYPE_COLORGREEN, SENSOR_MODE_PERCENT)
#define SENSOR_COLORBLUE _SENSOR_CFG(SENSOR_TYPE_COLORBLUE, SENSOR_MODE_PERCENT)
#define SENSOR_COLORNONE _SENSOR_CFG(SENSOR_TYPE_COLORNONE, SENSOR_MODE_PERCENT)
--------------------------------------------------------------------------------
-Tim
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/
nxt-ai
Posts: 36
Joined: 10 Jan 2011, 05:02

Re: Sensor Confusion -

Post by nxt-ai »

Cool, so what is:

Code: Select all

#define _SENSOR_CFG(_type, _mode) (((_type)<<8)+(_mode)) 
doing?
Please explain. :)
nxt-ai
Posts: 36
Joined: 10 Jan 2011, 05:02

Re: Sensor Confusion - Sensor Modes, Constants, and Functions

Post by nxt-ai »

Also how does once turn off the sensor with SetSensor(,) ?
For SetSensorType(,) you can use SENSOR_TYPE_NONE.
but what about for SetSensor()? Or are you supposed to use SetSensorType() after the sensor setup.
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Sensor Confusion - Sensor Modes, Constants, and Functions

Post by afanofosc »

SetSensor and the NXC API constants that work with it do not include every possible combination of type and mode. It's best to use SetSensorType followed by SetSensorMode (and then ResetSensor) if you need to use a type and mode combination that is not already defined in the NXC API. To turn off the light you can either turn off the sensor port with a call to SetSensorType with SENSOR_TYPE_NONE but then you'll get no valid readings at all from that port until you configure it again as some non-NONE sensor type. If you still want to use the sensor on that port but without the light then you would use SENSOR_TYPE_LIGHT_INACTIVE. Are you really using an NXT light sensor or are you using an NXT color sensor or an RCX light sensor? Each one will use a different type to control its light. If you are using the color sensor then you turn off the light with SetSensorType with SENSOR_TYPE_COLORNONE.

SENSOR_1..SENSOR_4 are equivalent to SensorValue(S1)..SensorValue(S4) (as well as SensorScaled() and Sensor()). SensorRaw always returns the raw sensor value regardless of what mode you have configured it in. If it is in RAW mode then SENSOR_1 and SensorValue(S1) will return the same value as SensorRaw(S1). Look at the generated NBC code with a simple program if you want to see which one produces the most optimal code.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
nxt-ai
Posts: 36
Joined: 10 Jan 2011, 05:02

Re: Sensor Confusion - Sensor Modes, Constants, and Functions

Post by nxt-ai »

Sorry for my late expression of gratitude soooo,
Thank You.
:)
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 0 guests