Page 1 of 1
NXC: what is exactly SensorNormalized doing?
Posted: 16 Jun 2011, 16:11
by HaWe
hi,
what is
Code: Select all
unsigned int SensorNormalized ( const byte & port )
exactly doing? There's unfortunately no info in the NXC docs.
Re: NXC: what is exactly SensorNormalized doing?
Posted: 16 Jun 2011, 17:15
by h-g-t
Never used it but according to this site (
http://www.robotc.net/support/nxt/Minds ... ensors.htm) it is returning a percentage value (1 -100) as opposed to the raw value which can be anywhere between 0 and 1023.
Re: NXC: what is exactly SensorNormalized doing?
Posted: 16 Jun 2011, 17:17
by HaWe
your link refers to RobotC but my question was about NXC
thx nevertheless
Re: NXC: what is exactly SensorNormalized doing?
Posted: 16 Jun 2011, 18:44
by muntoo
Just for reference for "future people" that may stumble across this page:
For a touch sensor, this is 0 (released) or 1 (true), I believe. For the light sensor, it's 0% to a 100%. For the US sensor, it's simply in centimeters. The sound sensor is in dB.
Re: NXC: what is exactly SensorNormalized doing?
Posted: 16 Jun 2011, 19:01
by HaWe
can you show as the related definitions of what the fw is doing here? (I unfortunately can't read fw or byte code)
Re: NXC: what is exactly SensorNormalized doing?
Posted: 17 Jun 2011, 14:43
by mattallen37
When you set up a port to be used for a specific sensor, it not only does what it needs to electrically, but it also changes the way it reads it. For analog sensors, it read them in RAW (0-1023), and then it scales them to the range specified by the sensor type and/or mode.
If you specify the sensor as a touch sensor, any RAW value below ~450 (IIRC) is returned as a 1, and anything over that is a 0. If it is a light sensor, it scales it to 0-100%, but I'm not sure if it is linear or over the full RAW range.
Even when the type and mode is specified, you can still read the RAW value.
Re: NXC: what is exactly SensorNormalized doing?
Posted: 17 Jun 2011, 15:38
by HaWe
thank you!
the Sensor definitions of the Lego fw are really confusing me: too many keywords which do anything obscured and odd or weird and unpredictable.
now is there a way to use SetSensor or SetSensorType or SetSensorMode to do the same as SensorNormalized ?
Re: NXC: what is exactly SensorNormalized doing?
Posted: 18 Jun 2011, 01:09
by muntoo
mattallen37 wrote:If you specify the sensor as a touch sensor, any RAW value below ~450 (IIRC) is returned as a 1, and anything over that is a 0. If it is a light sensor, it scales it to 0-100%, but I'm not sure if it is linear or over the full RAW range.
For the light sensor, it's something like (IIRC):
Code: Select all
processed = 100% - ((raw - 600) / (1024 - 700));
#define WRAP(a,b,c) min(max(a,b),c)
SensorNormalized() = WRAP(100 - ((SensorRaw() - 600) * 100 / (1024 - 700)), 0, 100);
Anything around
r = ~600
is
p = 100%
, and around
r = ~700
is
p = 0%
.
Re: NXC: what is exactly SensorNormalized doing?
Posted: 18 Jun 2011, 16:28
by mattallen37
doc-helmut wrote:thank you!
the Sensor definitions of the Lego fw are really confusing me: too many keywords which do anything obscured and odd or weird and unpredictable.
now is there a way to use SetSensor or SetSensorType or SetSensorMode to do the same as SensorNormalized ?
I know what you mean, and I am still somewhat confused by all the options as well. instead of using all these special things, I use RAW for almost everything (except digital sensors or the RCX rotation sensor).
AFAIK SensorNormalized is the same as using "SENSOR_1", or "Sensor(S1)". Setting the type and/or mode has a direct effect on it.
For analog sensors, I recommend using SensorRaw(). I haven't tried this yet, but you should try using an inline operation (I don't remember what it's called), like this: "(SensorRaw(S1)<700?1:0)" to read the boolean state of the touch sensor. You should be able to use very simple math to work for any of the analog sensors from the RAW values.