NXC: what is exactly SensorNormalized doing?

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

NXC: what is exactly SensorNormalized doing?

Post 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.
h-g-t
Posts: 552
Joined: 07 Jan 2011, 08:59
Location: Albania

Re: NXC: what is exactly SensorNormalized doing?

Post 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.
A sophistical rhetorician, inebriated with the exuberance of his own verbosity, and gifted with an egotistical imagination that can at all times command an interminable and inconsistent series of arguments to malign an opponent and to glorify himself.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: what is exactly SensorNormalized doing?

Post by HaWe »

your link refers to RobotC but my question was about NXC
thx nevertheless
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: NXC: what is exactly SensorNormalized doing?

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

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


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: what is exactly SensorNormalized doing?

Post 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)
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC: what is exactly SensorNormalized doing?

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

I'm all for gun control... that's why I use both hands when shooting ;)
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: what is exactly SensorNormalized doing?

Post 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 ?
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: NXC: what is exactly SensorNormalized doing?

Post 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%.
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: NXC: what is exactly SensorNormalized doing?

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