High level sensor control
Using sensors with the RWTH - Mindstorms NXT Toolbox is very simple. That is why this chapter is relatively short. You can look up sensor-specific parameters in the according function help:
Functions for the standard NXT sensors:
More advanced functions and 3rd party external sensor support:
The functions mentioned above provide an easy and simple way to access all sensors. The ultrasonic sensor uses a digital interface and has to be handled in a different way internally, but using these high level functions, you won't see a difference. The only thing worth mentioning: GetUltrasonic is about 1.5 up to 2 times slower than the other Get* functions (because internally 3 or sometimes 4 packets are needed instead of just 2). Same goes for most external 3rd party sensors: Except the HiTechnic Gyro sensor, they're all digital.
The Open* functions use NXT_SetInputMode, while Get* calls NXT_GetInputValues. CloseSensor is necessary to turn off a specific sensor, e.g. turn off the light sensor's red LED in active mode. This will save power.
You can use raw port numbers starting with 0 just as seen in the official Mindstorms documentation, or you can use the named constants SENSOR_1 to SENSOR_4 for better readability. So let's open a few sensors:
OpenSwitch(SENSOR_1); OpenSound(SENSOR_2, 'DB'); % have to specify mode, DB or DBA OpenLight(SENSOR_3, 'ACTIVE'); % have to specify mode, ACTIVE or INACTIVE OpenUltrasonic(SENSOR_4);
Sensors are ready to be used now, very easy:
if GetSwitch(SENSOR_1) % always specify port number! % the touch sensor is "pressed" now, do something end%if if GetSound(SENSOR_2) < 100 % remember, values range from 0 to 1023 % quite silent right now end%if if GetLight(SENSOR_3) > 1000 % VERY bright sunshine :-) end%if if GetUltrasonic(SENSOR_4) < 30 % unit is cm % we seem close to a wall end%if
Clean up when you are done, just as with handles. This saves battery power of your NXT!
% don't forget :-) CloseSensor(SENSOR_1); CloseSensor(SENSOR_2); CloseSensor(SENSOR_3); CloseSensor(SENSOR_4);
Note that it is possible to use NXT_SetInputMode with a custom mode you like instead of Open*, and the Get-functions will still work (this is not true for the ultrasonic sensor, but it has totally different modes anyway). Internally they just return the .NormalizedADVal value. This makes it possible to automatically count claps and still use the simple functions. Example:
% your old code looks like this OpenSound(SENSOR_2, 'DB'); % do something while(something) % note how you continously have to poll the sensor if GetSound(SENSOR_2) > 700 ClapCount = ClapCount + 1; end%if end%while
We now replace OpenSound by this:
NXT_SetInputMode(SENSOR_2, 'SOUND_DB', 'PERIODCOUNTERMODE', 'dontreply'); NXT_ResetInputScaledValue(SENSOR_2); % this is needed if you want to start with 0 if GetSound(SENSOR_2) > 500 % GetSound still works! % hey, this is a loud atmosphere... end%if % we could do whatever we wanted here: pause(10) % take a little 10s nap data = NXT_GetInputValues(SENSOR_2); ClapCount = data.ScaledVal; CloseSensor(SENSOR_2);
That was easy. The NXT did the counting for us. For more details about sensor modes see the LEGO documentation, but in a few words: A "transition" (TRANSITIONCNTMODE) is whenever the value (.NormalizedADVal) changes between the 45% threshold (45% of 1023 is 460). As "period" (PERIODCOUNTERMODE) counts when the value goes down from somewhere above 45%, and then changes back up. The "count" happens during the raising part. To see what exactly is happening try the GUI_WatchAnalogSensor tool.
The main point I wanted to make: As you can see, GetSound / GetLight etc. can peacefully coexist with the NXT_ functions.
Proceed to next chapter.