Sensor Confusion - Sensor Modes, Constants, and Functions
Posted: 14 Jan 2011, 07:55
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;
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
Re: Sensor Confusion -
Posted: 14 Jan 2011, 09:09
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
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()
Does this answer your problem?
-Tim
Re: Sensor Confusion -
Posted: 16 Jan 2011, 05:19
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
Re: Re: Sensor Confusion -
Posted: 17 Jan 2011, 03:52
by nxt-ai
Thanks all.
I still have another question which is better:
Re: Sensor Confusion - Sensor Modes, Constants, and Functions
Posted: 17 Jan 2011, 06:17
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.
Re: Sensor Confusion - Sensor Modes, Constants, and Functions
Posted: 18 Jan 2011, 01:45
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
Re: Sensor Confusion - Sensor Modes, Constants, and Functions
Posted: 21 Jan 2011, 00:54
by nxt-ai
Sorry for my late expression of gratitude soooo,
Thank You.