Sensor Confusion - Sensor Modes, Constants, and Functions
Sensor Confusion - Sensor Modes, Constants, and Functions
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
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.
Re: Sensor Confusion -
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
- 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)
| 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)
-
- Posts: 224
- Joined: 30 Oct 2010, 04:10
- Location: 127.0.0.1
- Contact:
Re: Sensor Confusion -
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
Does this answer your problem?
-Tim
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
}
}
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()nxt-ai wrote:Code: Select all
SetSensorType(S1,SENSOR_TYPE_LIGHT_ACTIVE); SetSensorMode(S1,SENSOR_MODE_RAW); ResetSensor(S1);
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/
Minboards IRC Channel #mindboards on Freenode
My blog: http://timpattinson.wordpress.com/
Re: Sensor Confusion -
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
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/
http://bricxcc.sourceforge.net/
Re: Re: Sensor Confusion -
Thanks all.
I still have another question which is better:
or
Also what other constants will work with:
to use the light (ACTIVE).
I still have another question which is better:
Code: Select all
SENSOR_1
Code: Select all
SensorRaw(IN_3)
Code: Select all
SetSensor()
-
- Posts: 224
- Joined: 30 Oct 2010, 04:10
- Location: 127.0.0.1
- Contact:
Re: Sensor Confusion -
These are the constants that work with the SetSensor() function. For using a NXT light sensor (active) you can use SENSOR_NXTLIGHT
eg
eg
Code: Select all
task main()
{
SetSensor(S1, SENSOR_NXTLIGHT);
while(1)
{
NumOut(0,LCD_LINE1,SENSOR_1);
Wait(50);
}
}
-TimNXC Guide wrote:Use the combined sensor type and mode constants to configure both the sensor mode and type in a single function call. More...--------------------------------------------------------------------------------
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/
Minboards IRC Channel #mindboards on Freenode
My blog: http://timpattinson.wordpress.com/
Re: Sensor Confusion -
Cool, so what is:
doing?
Please explain.
Code: Select all
#define _SENSOR_CFG(_type, _mode) (((_type)<<8)+(_mode))
Please explain.
Re: Sensor Confusion - Sensor Modes, Constants, and Functions
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.
For SetSensorType(,) you can use SENSOR_TYPE_NONE.
but what about for SetSensor()? Or are you supposed to use SetSensorType() after the sensor setup.
Re: Sensor Confusion - Sensor Modes, Constants, and Functions
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
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/
http://bricxcc.sourceforge.net/
Re: Sensor Confusion - Sensor Modes, Constants, and Functions
Sorry for my late expression of gratitude soooo,
Thank You.
Thank You.
Who is online
Users browsing this forum: Semrush [Bot] and 0 guests