Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables, logic and sensors Day 3 Computer Programming through Robotics CPST 410 Summer 2009.

Similar presentations


Presentation on theme: "Variables, logic and sensors Day 3 Computer Programming through Robotics CPST 410 Summer 2009."— Presentation transcript:

1 Variables, logic and sensors Day 3 Computer Programming through Robotics CPST 410 Summer 2009

2 7/1/09Harry Howard, CPST 410, Tulane University2 Course organization  Course home page 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.

3 7/1/09Harry Howard, CPST 410, Tulane University3 PhotosPhotos

4 7/1/09Harry Howard, CPST 410, Tulane University4 Mindstorms NXT 2.0  You can preorder it now, and it will ship by 1 Aug 2009.  $ 279.99  http://shop.lego.com/B yTheme/Product.aspx? p=8547&cn=17 http://shop.lego.com/B yTheme/Product.aspx? p=8547&cn=17

5 Wired! Kelly §7

6 7/1/09Harry Howard, CPST 410, Tulane University6 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 7/1/09Harry Howard, CPST 410, Tulane University7 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.

8 7/1/09Harry Howard, CPST 410, Tulane University8 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.

9 7/1/09Harry Howard, CPST 410, Tulane University9 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), and  drop it in front of the MOVE block.

10 7/1/09Harry Howard, CPST 410, Tulane University10 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.

11 7/1/09Harry Howard, CPST 410, Tulane University11 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.

12 7/1/09Harry Howard, CPST 410, Tulane University12 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.

13 7/1/09Harry Howard, CPST 410, Tulane University13 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).

14 7/1/09Harry Howard, CPST 410, Tulane University14 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.

15 7/1/09Harry Howard, CPST 410, Tulane University15 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.

16 7/1/09Harry Howard, CPST 410, Tulane University16 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?

17 7/1/09Harry Howard, CPST 410, Tulane University17 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)

18 7/1/09Harry Howard, CPST 410, Tulane University18 Play time  Take a few minutes to play with the program.

19 NXC

20 7/1/09Harry Howard, CPST 410, Tulane University20 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).

21 7/1/09Harry Howard, CPST 410, Tulane University21 Now go to the File menu and start a new program.

22 7/1/09Harry Howard, CPST 410, Tulane University22 Reserved words identifiers and keywords, p. 135 __RETURN__CharLongsub __RETVAL__constMutexswitch __STRRETVAL__continueprioritytask __TMPBYTE__defaultrepeattrue __TMPWORD__doreturntypedef __TMPLONG__elsesafecallunsigned AbsfalseShortuntil AsmforSignvoid BoolgotoStartwhile breakifStop Byteinlinestring Caseintstruct

23 7/1/09Harry Howard, CPST 410, Tulane University23 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.

24 Variables in NXC Hansen p. 135ff

25 7/1/09Harry Howard, CPST 410, Tulane University25 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.

26 7/1/09Harry Howard, CPST 410, Tulane University26 Simple numerical variables Keyword for typeDescription short, int16 bit; first bit indicates sign unsigned int16 bit; no bit for sign; rare long32 bit; first bit indicates sign unsigned long32 bit; no bit for sign; rare

27 7/1/09Harry Howard, CPST 410, Tulane University27 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;

28 7/1/09Harry Howard, CPST 410, Tulane University28 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.

29 True or false? Feedback Kelly §8-9

30 7/1/09Harry Howard, CPST 410, Tulane University30 Robot does something (motor cortex) Environment changes (sensory cortex) feedforwardfeedback The place of feedback

31 7/1/09Harry Howard, CPST 410, Tulane University31 Robot does something (motor cortex) Environment changes (sensory cortex) feedforward feedback The complete system Robot decides (prefrontal cortex) ?

32 7/1/09Harry Howard, CPST 410, Tulane University32 All the sensors are in the Complete palette

33 7/1/09Harry Howard, CPST 410, Tulane University33 Sensor overview NXT SensorConditions TouchPressed, released, bumped Sound> n (0 - 100) Light> n (0 - 100) Ultrasonic< n (0 - 100) Rotationn (0 - 360)

34 7/1/09Harry Howard, CPST 410, Tulane University34 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).

35 7/1/09Harry Howard, CPST 410, Tulane University35 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 screen  Hint: See if any of the output plugs on the data hub help.

36 7/1/09Harry Howard, CPST 410, Tulane University36 DisplaySensor.rbt

37 Calibration Kelly §23

38 7/1/09Harry Howard, CPST 410, Tulane University38 Different enviornments  The Light and Sound sensors can respond differently in different environments  For 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

39 7/1/09Harry Howard, CPST 410, Tulane University39 The CALIBRATION block  Found in Complete > Advanced  Sets the minimum (0%) and maximum (100%) values detected by a sound or light sensor.

40 7/1/09Harry Howard, CPST 410, Tulane University40 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.

41 Sensors in NXC

42 7/1/09Harry Howard, CPST 410, Tulane University42 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 set  the sensor port: 1, 2, 3, 4  the sensor type: light, sound, touch, etc.  the sensor mode: how the data is collected

43 7/1/09Harry Howard, CPST 410, Tulane University43 Sensor port names  There are three naming schemes  S1, S2, S3, S4  IN_1, IN_2, IN_3, IN_4  SENSOR_1, SENSOR_2, SENSOR_3, SENSOR_4

44 7/1/09Harry Howard, CPST 410, Tulane University44 Set sensor type to a port  SetSensorLight(port)  SetSensorSound(port)  SetSensorTouch(port)

45 7/1/09Harry Howard, CPST 410, Tulane University45 Sensor type constants Sensor TypeMeaning SENSOR_TYPE_NONEno sensor configured SENSOR_TYPE_TOUCHNXT or RCX touch sensor SENSOR_TYPE_LIGHT_ACTIVENXT light sensor with light SENSOR_TYPE_LIGHT_INACTIVENXT light sensor without light SENSOR_TYPE_SOUND_DBNXT sound sensor with dB scaling SENSOR_TYPE_SOUND_DBANXT sound sensor with dBA scaling

46 7/1/09Harry Howard, CPST 410, Tulane University46 Sensor mode constants Sensor ModeMeaning SENSOR_MODE_RAWraw value from 0 to 1023 SENSOR_MODE_BOOLboolean value (0 or 1) SENSOR_MODE_EDGEcounts number of boolean transitions SENSOR_MODE_PULSEcounts number of boolean periods SENSOR_MODE_PERCENTvalue from 0 to 100 SENSOR_MODE_ROTATIONrotation (16 ticks per revolution)

47 7/1/09Harry Howard, CPST 410, Tulane University47 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)  Example  SetSensor(S1, SENSOR_TOUCH);

48 7/1/09Harry Howard, CPST 410, Tulane University48 Sensor type-mode constants Sensor configuration constant TypeMode SENSOR_TOUCHSENSOR_TYPE_TOUCHSENSOR_MODE_BOOL SENSOR_PULSESENSOR_TYPE_TOUCHSENSOR_MODE_PULSE SENSOR_EDGESENSOR_TYPE_TOUCHSENSOR_MODE_EDGE SENSOR_LIGHTSENSOR_TYPE_LIGHTSENSOR_MODE_PERCENT SENSOR_ROTATIONSENSOR_TYPE_ROTATIONSENSOR_MODE_ROTATION

49 7/1/09Harry Howard, CPST 410, Tulane University49 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

50 7/1/09Harry Howard, CPST 410, Tulane University50 Next time  P1  This will just be a quiz on the blocks  Whatever we did not finish today  Random numbers  'Line Follower' - project done in class


Download ppt "Variables, logic and sensors Day 3 Computer Programming through Robotics CPST 410 Summer 2009."

Similar presentations


Ads by Google