variables, logic and sensors day 3 computer programming through robotics cpst 410 summer 2009
TRANSCRIPT
7/1/09 Harry Howard, CPST 410, Tulane University 2
Course organization
Course home page (http://robolab.tulane.edu/CPST410/)
Lab (Newcomb 442) will be open for practice with 3-4 Macs, but you can bring your own laptop and all robots.
7/1/09 Harry Howard, CPST 410, Tulane University 4
Mindstorms NXT 2.0
You can preorder it now, and it will ship by 1 Aug 2009.
$ 279.99http://shop.lego.
com/ByTheme/Product.aspx?p=8547&cn=17
7/1/09 Harry Howard, CPST 410, Tulane University 6
Hubs and wires
Kelly's introduction to wiring is not based on real NXT-G blocks, but rather on ones he made up for the sake of perspicuity.
I would rather you work with real blocks, so I will ignore his presentation. You should still read it, however.
We will go back to what we skipped in §3 (because it was wrong).
7/1/09 Harry Howard, CPST 410, Tulane University 7
Data hubs
The settings of many blocks can be changed from other parts of the program through their data hub.
Create a new program "VariableMove".Drop a MOVE block into it.
Change its duration to degrees.Make sure your motors are set correctly.
Put your cursor at the bottom of the MOVE block and try to pull down the indented line.
7/1/09 Harry Howard, CPST 410, Tulane University 8
The MOVE block data hub
Running the cursor over each icon reveals which setting it accepts.
If you have an Internet connection, click on the 'More help' link at the bottom right.
7/1/09 Harry Howard, CPST 410, Tulane University 9
The VARIABLE block
To illustrate the usefulness of the data hub, we will try out a new block that does not come up in detail until §18, the VARIABLE block.
Change to the Complete palette (the three interlocking squares at the bottom left).
Select the big orange plus sign (Data),pull out a suitcase (Variable), anddrop it in front of the MOVE block.
7/1/09 Harry Howard, CPST 410, Tulane University 10
Using the VARIABLE block
The list shows the three types that a variable can have: Logic, Number, and Text.Number and Text mean what they say; Logic we will
take up later.Change the type from Logic to Number.
Note that the action is set to Read and the value (0) is grayed out so that you cannot change it.We want to change it, so set it to Write and change the
value to 180.In programming terms, we have declared a variable of
type number and name 'Number 1' and set its value to 180.
7/1/09 Harry Howard, CPST 410, Tulane University 11
Using the VARIABLE block, cont.
However, we cannot use the variable yet. We have to make it available to the rest of the program by
reading it, but a VARIABLE block can only be set to read or write, not both.
So drop another VARIABLE block after the first. Set its type to Number. Set its action to Read.
Now pull out a wire from its output plug and connect it to the MOVE input plug for Duration. You might not get it right the first time, but persevere.
7/1/09 Harry Howard, CPST 410, Tulane University 12
The program
Now we can use the variable.
Save, download, and run the program.
How far does your Tribot go? 180 degrees, because the
setting on the MOVE data hub for Duration only accepts degrees.
7/1/09 Harry Howard, CPST 410, Tulane University 13
Let's display something
Drop a DISPLAY block after the MOVE block.What should we display?
How about the number of degrees the wheels turn?Pull down the data hub of the DISPLAY block.
Where would we connect the VARIABLE block to display its value?
CRUCIAL LIMITATION: Type cannot change. Most of the input plugs are for numbers, but these
numbers are used in positioning something on the LCD screen (and selecting the diameter of a circle).
7/1/09 Harry Howard, CPST 410, Tulane University 14
Type conversion
The trick is to display the number 180 as the text "180".
But these two objects are of different types.NXT-G only supplies one block for type
conversion, in the Complete palette under the Advanced icon (four squares).The NUMBER TO TEXT block.
7/1/09 Harry Howard, CPST 410, Tulane University 15
The NUMBER TO TEXT block
Drop one between the MOVE and DISPLAY blocks.
Pull a wire out of the Duration output plug of the MOVE block and connect it to the input plug of the NUMBER TO TEXT block.Again, this may take some trial and error.
Pull a wire out of the output plug of the NUMBER TO TEXT block and connect it to the Text input plug of the DISPLAY block.
7/1/09 Harry Howard, CPST 410, Tulane University 16
The program
Remember that the DISPLAY block is very fast, so pull a TIME WAIT block out of the Common palette and drop it after the DISPLAY block. Set it to 10 s.
Save, download, and run the program. What happens?
7/1/09 Harry Howard, CPST 410, Tulane University 17
Wires and plugs
[A] Input plug
[B] Output plug
[C] Number data wire (yellow)
[D] Logic data wire (green)
[E] Text data wire (orange)
[F] Broken data wire (gray)
7/1/09 Harry Howard, CPST 410, Tulane University 18
Play time
Take a few minutes to play with the program.
7/1/09 Harry Howard, CPST 410, Tulane University 20
First look at BricxCC
Open BricxCC and move the Brick Command Center window to the left so that the Templates window is visible (see next slide).
7/1/09 Harry Howard, CPST 410, Tulane University 22
Reserved wordsidentifiers and keywords, p. 135
__RETURN__ Char Long sub
__RETVAL__ const Mutex switch
__STRRETVAL__ continue priority task
__TMPBYTE__ default repeat true
__TMPWORD__ do return typedef
__TMPLONG__ else safecall unsigned
Abs false Short until
Asm for Sign void
Bool goto Start while
break if Stop
Byte inline string
Case int struct
7/1/09 Harry Howard, CPST 410, Tulane University 23
Parsing errors
Usually because of missing or incorrect punctuation: } ; ) The parser expects these things in certain places. If its expectations are not met, it gives an error message. It also gets thrown off track for the upcoming code and
thus generates lots of error messages only because of the first one.
7/1/09 Harry Howard, CPST 410, Tulane University 25
Variables
NXC has about 11 types of variables.Today we will concentrate on the single
numbers.Since a number can take up a lot of storage
space depending on how precisely it is specified, we want to be frugal with them.
7/1/09 Harry Howard, CPST 410, Tulane University 26
Simple numerical variables
Keyword for type Description
short, int 16 bit; first bit indicates sign
unsigned int 16 bit; no bit for sign; rare
long 32 bit; first bit indicates sign
unsigned long 32 bit; no bit for sign; rare
7/1/09 Harry Howard, CPST 410, Tulane University 27
Variable declaration
Variables are declared using the keyword for the type, followed by a list of variable names which are separated by commas and terminated with a semicolon:int x;
long y, z;
An equals sign can be followed by an initial value:int x=1, y=2, z;
7/1/09 Harry Howard, CPST 410, Tulane University 28
Constants
When a program contains a value that does not change during execution, it's a good idea to give it a name to make the program easier to understand.
This is done in NXC with a macro definition using the compiler directive #define, e.g.;#define METER2MM 1000
#define DEG2ROT 360 Compiler directives do not end in a semicolon. During compilation, each macro is replaced by its value.
7/1/09 Harry Howard, CPST 410, Tulane University 30
Robot does something
(motor cortex)
Environment changes
(sensory cortex)
feedforwardfeedback
The place of feedback
7/1/09 Harry Howard, CPST 410, Tulane University 31
Robot does something
(motor cortex)
Environment changes
(sensory cortex)
feedforward
feedback
The complete system
Robot decides
(prefrontal cortex)
?
7/1/09 Harry Howard, CPST 410, Tulane University 33
Sensor overview
NXT Sensor Conditions
Touch Pressed, released, bumped
Sound > n (0 - 100)
Light > n (0 - 100)
Ultrasonic < n (0 - 100)
Rotation n (0 - 360)
7/1/09 Harry Howard, CPST 410, Tulane University 34
Sensors and conditions
Once a sensor senses that its condition has been met, it produces a value of Yes (or True or 1).
Otherwise, it produces a value of No (or False or 0).
Such values of yes or no are known as logical values (though binary is just as accurate, and maybe more).
7/1/09 Harry Howard, CPST 410, Tulane University 35
Check the sensors
Plug a sensor into a sensor port (the ones along the bottom).
Create a program that displays the output of the sensor on the screenHint: See if any of the output plugs on the data
hub help.
7/1/09 Harry Howard, CPST 410, Tulane University 38
Different enviornments
The Light and Sound sensors can respond differently in different environmentsFor example, the maximum in a brightly lit
room will be different from the maximum in a dimly lit room
NXT-G provides a CALIBRATION block to account for this kind of variation
7/1/09 Harry Howard, CPST 410, Tulane University 39
The CALIBRATION block
Found in Complete > Advanced
Sets the minimum (0%) and maximum (100%) values detected by a sound or light sensor.
7/1/09 Harry Howard, CPST 410, Tulane University 40
Usage
Although a single one can set both the min and max, it is safer to check each extreme separately, i.e. use one CALIBRATION block for the min and another for the max.
How do you decide the min and max?Design a program to calibrate the light
sensor for our room.
7/1/09 Harry Howard, CPST 410, Tulane University 42
Sensor options
You may recall that the sensor blocks had lots of options.
These have to be set by special statements in NXC.
There are three things to setthe sensor port: 1, 2, 3, 4the sensor type: light, sound, touch, etc.the sensor mode: how the data is collected
7/1/09 Harry Howard, CPST 410, Tulane University 43
Sensor port names
There are three naming schemesS1, S2, S3, S4IN_1, IN_2, IN_3, IN_4SENSOR_1, SENSOR_2, SENSOR_3,
SENSOR_4
7/1/09 Harry Howard, CPST 410, Tulane University 44
Set sensor type to a port
SetSensorLight(port)SetSensorSound(port)SetSensorTouch(port)
7/1/09 Harry Howard, CPST 410, Tulane University 45
Sensor type constants
Sensor Type Meaning
SENSOR_TYPE_NONE no sensor configured
SENSOR_TYPE_TOUCH NXT or RCX touch sensor
SENSOR_TYPE_LIGHT_ACTIVE NXT light sensor with light
SENSOR_TYPE_LIGHT_INACTIVE NXT light sensor without light
SENSOR_TYPE_SOUND_DB NXT sound sensor with dB scaling
SENSOR_TYPE_SOUND_DBA NXT sound sensor with dBA scaling
7/1/09 Harry Howard, CPST 410, Tulane University 46
Sensor mode constants
Sensor Mode Meaning
SENSOR_MODE_RAW raw value from 0 to 1023
SENSOR_MODE_BOOL boolean value (0 or 1)
SENSOR_MODE_EDGE counts number of boolean
transitions
SENSOR_MODE_PULSE counts number of boolean periods
SENSOR_MODE_PERCENT value from 0 to 100
SENSOR_MODE_ROTATION rotation (16 ticks per revolution)
7/1/09 Harry Howard, CPST 410, Tulane University 47
SetSensor function
It is convenient to set the type and the mode at the same time.
The SetSensor function makes this a little easier by providing a set of standard type/mode combinations (on the next slide) and a single function to call them:SetSensor(port, constant configuration)
ExampleSetSensor(S1, SENSOR_TOUCH);
7/1/09 Harry Howard, CPST 410, Tulane University 48
Sensor type-mode constants
Sensor configuration constant
Type Mode
SENSOR_TOUCH SENSOR_TYPE_TOUCH SENSOR_MODE_BOOL
SENSOR_PULSE SENSOR_TYPE_TOUCH SENSOR_MODE_PULSE
SENSOR_EDGE SENSOR_TYPE_TOUCH SENSOR_MODE_EDGE
SENSOR_LIGHT SENSOR_TYPE_LIGHT SENSOR_MODE_PERCENT
SENSOR_ROTATION SENSOR_TYPE_ROTATION SENSOR_MODE_ROTATION
7/1/09 Harry Howard, CPST 410, Tulane University 49
Reading sensor values
Once these options have been set, the processed sensor reading can be read from its port using the function: Sensor(port)
Example x = Sensor(S1); // read sensor at port 1
The ultrasonic sensor has its own function: SensorUS(port)x = SensorUS(S1); // read ultrasonic sensor at port 1