Engineer Programming - Advanced Topics In this section you will learn about more advanced tools which will help you to make your programs more powerful, versatile, and useful. LEGO Engineer provides programming structures such as conditionals and loops, which enable you to direct and control the flow of your program. Mathematical tools such as random variables and arithmetic operations enable you to process the data generated by your sensors: generating averages, comparing values, or even evaluating complex mathematical functions in real-time. Engineer also provides a system whereby you can write subroutines and save them as Templates, which you can import as icons and string into programs. Finally, you can customize the Front Panel to create a graphical computer interface to your LEGO creations. The Front Panel is similar to the Project Page in the Control Lab software. Programming Structures One of the most valuable features of the Engineer environment is the user's ability to direct the flow of a program. Engineer provides tools that allow you to build several kinds of loops, sequences, and conditional (“if-then-else") statements. These structures are useful when you need the program to check conditions, repeat a task several times, or perform more than one task at the same time. Structures Menu The tools used to create these structures are found in the Structures command sub-menu. Contained in this menu are the following structures: the Sequence Structure, the Case Structure, the “For” Loop, the “While” Loop, and the Formula Node. The Sequence Structure Sequence Structures perform a number of tasks in a set sequence. This makes the programming area look much neater. Each frame in a Sequence Structure is a separate task. After selecting Sequence Structure from the palette, you must draw a box that will represent the structure on your diagram window. When the sequence structure first appears, it only has one frame. To add frames, right-click (Command-click) on the border of the box and select "Add Frame After," "Add Frame Before," or "Duplicate Frame." Once you have more than one frame, you can place commands inside each of the frames. To select a frame, click on the left and right arrows on the upper border. To pass the string from one frame to the next, you must click on the border and select "Add Sequence Local." The string must connect to the sequence local terminal, which becomes the input for the next frame in the sequence. The Case Structure There are times when you want to execute one set of commands if one condition is met (a touch sensor being pushed in, for example), and another set of commands if a different condition is met (a touch sensor being released, for example). LEGO Engineer's way of expressing such "if" statements is through a construct called a Case Structure. A Case Structure is represented by a box with a comparison value (True, False, or Numeric) at the top. When the input to the Case Structure matches that value, the set of commands inside that box is executed. You can cycle through the possible cases by clicking on the arrowheads to the left and right of the comparison value. Note: Case Structures can have many sub-diagrams, but you can only see one case at a time (unlike it appears in the above diagram), sort of like a stacked deck of cards. Clicking on the decrement (left) or increment (right) button at the top of the structure displays the previous or next sub-diagram, respectively. Case Structure example: The above program turns on the motor in one direction if the touch sensor is pushed in, and the other direction if the touch sensor is released. The “While” Loop A while loop keeps repeating a set of commands as long as a certain condition is true. In Engineer, a while loop is represented by a box with a circular arrow icon inside of it. To create a while loop, pick the While Loop icon from the Structures sub-menu. Draw a box in the diagram window and place a sequence of commands inside it, connecting the first and last commands to the rest of the program through the walls of the box (look at the diagram below to see what this looks like). You also need to include a test condition that will stop the loop when it becomes false. Keep in mind that the test condition, like all commands, must be connected to the rest of the program in order to be executed! Finally, place a string connecting the output of the test condition to the circular arrow icon. The test condition must produce a "True" or "False" (Boolean) value in order for the loop to work. When the program is run, a sequence of instructions placed inside of the loop is executed and repeated as long as the green string connected to the circular arrow is true. Example: Looping until a condition is met This program turns on a motor for two seconds in one direction, and then for two seconds in the other direction, and then repeats. The program stops when the touch sensor is pressed in and held. When the touch sensor is not pressed, its output is false, and when it is pressed in, its output becomes true. Since a while loop runs while the test condition is true and stops when it is false, we want our test condition to be the logical opposite of the touch sensor output (true when it is not pressed, and false when it is pressed). In this example, this is achieved by using a logical 'not' operator from the Boolean sub-menu. “I” is your counter variable. When the program begins, the counter variable is set to 0. After the first iteration of the loop “i” is one. After the second iteration, “i” is two. The third iteration executes, and “i” becomes three. The number “i” tells you how many iterations you have done, and can be used in a program much like the Numeric Constant. The “For” Loop The other kind of loop in Engineer is the "For" loop. Instead of terminating the loop when a condition becomes false, the “For” loop terminates when “i” becomes equal to “N” (the number of times you want to iterate through the loop). String the number of iterations that you want to do into the “N.” Example: Looping a specified number of times This example runs motor "C" in one direction until touch sensor "1" is pressed. The motor then reverses direction until the touch sensor is pressed again. This sequence is repeated two more times and then (after the sixth sensor press) the motor stops and the program ends. When you string the last command inside of the loop to the first command outside of the loop, you will notice a broken string coming out of the loop. To fix this problem, Right-click (Command-click) on the black square and select "Disable Indexing" and the string will become complete. The Formula Node A Formula Node is a resizable box in which you can enter algebraic formulas directly into the program. To create a Formula Node, pick the Formula Node tool from the Structures sub-menu, and draw a box in the diagram window. Next, right-click on the border of the box and pick 'Add Input' or 'Add Output' to create input and output nodes, giving them names as you create them. Type one or more formulae inside the box, using the input and output node names as variables. A semicolon “;” is needed at the end of each formula. Finally, string the input and output nodes to the outputs and inputs of the command icons to which they correspond. Example: Using a mathematical formula The following program sets the motor power level (on a cooling fan, for example) to be a function of the temperature sensor's output value. The Formula Node in this example contains a single formula relating temperature to motor speed. Mathematical Operations Also included in LEGO Engineer are three menus with mathematical VI’s. These menus are the Numeric, the Comparison, and the Boolean menus. The two most important items on these menus are the Numeric Constant and The Boolean Constant. These are used frequently in the “For” and “While” loops. To change the value of the Numeric Constant or to change the Boolean constant from TRUE to FALSE, click with the Hand tool found in the Tools Menu. Just as with the formula node, you can use mathematical operations to set the motor power level to be a function of the temperature sensor's output value. Arrays and Clusters The individual icons in the Array and Cluster sub-menus are useful for manipulating sets of data, such as the light sensor data necessary to operate the LEGO scanner. See "Scanner" in the Examples sub-menu for an example of arrays and clusters being used. Templates One convenient feature of LEGO Engineer is the ability to create 'Templates', subroutines that can be represented by their own icons that you create and included in other programs. Creating a Template To create your own template, select a template icon from the Templates sub- menu. Place it in the Diagram Window and double-click on it. A Panel window for that Template will open. Go to that window and pick Show Diagram from the Windows menu, which will open a Diagram Window for that Template. You will notice that in addition to the Green and Red lights, there are Begin and End terminals in the diagram. While you are writing and testing your subroutine, use the Green and Red lights like you normally would. When you are satisfied with the performance of your subroutine, delete the traffic light icons and string your commands instead to the Begin and End terminals. You need to do this so that your subroutine can be used as a Template within a program. Finally, you can edit the icon for that Template by double clicking on the picture of the icon in the upper right corner of the Diagram Window. Save your template to disk if you will want to access it later, in other programs. Importing a Template Importing a stored Template from disk is easily done. Click on the 'Select a VI...' icon in the Command Menu. Choose a Template to load from disk, and then place it on the Diagram Window. You will then be able to string it into your program like an ordinary command. The Front Panel The Front Panel is the place where the person running the program gives real- time input and receives real-time data from the Serial Interface Box. Controls Menu Just like the Functions Menu has all of the icons for use in the Main Window of Engineer, the Controls Menu has all of the icons for use in the Front Panel Window. The Controls Menu is composed of six sub-menus: (from left to right, top to bottom) the Controls sub-menu, the Indicators sub-menu, the Boolean sub-menu, the Graph sub-menu, the Numeric sub-menu, and the Decorations sub-menu. Except for the items in the Decorations menu, all of the items in the all of the sub- menus of the Controls Menu are accompanied by separate and individual icons in the Diagram Window. Some of these items are double precision numbers [DBL], some of these items are integer numbers [INT], some of these items are arrays, some of these items are bundles, and there are still other items that are represented by different icons in the Diagram Window. The following picture shows all of the Controls sub-menus opened at once on the Front Panel Window. The main Controls Menu is in the center of the top row. Front Panel Example: The following program employs the Front Panel and an “Outputs” control (found in the “Controls” sub-menu). It controls the speed and power of the devices connected to the “Outputs” section of the Serial Interface Box. When the program begins, and the “On/Off” button is on, the while loop begins which allows you to control the outputs until the “On/Off” button is turned off. The devices connected to the Serial Interface Box will still run until the touch sensor, connected to a port which can be specified on the Front Panel when the program begins, is activated. Notice that the pink box in the Diagram Window is labeled “Outputs” and is connected to an “Outputs” icon. This is similar to the “Outputs” label in the Front Panel. There are also labels on both the Diagram Window and the Front Panel for “On/Off” and “Sensor Channel.” The label “Touch Sensor” on the Front Panel was written in using the Text Tool in the Tools Window. Front Panel Example: The following program runs a motor as a function of the temperature. The temperature is displayed on the front panel in thermometer and graphical form. Both thermometer and graph are found in the Indicators sub-menu. Two Serial Interface Boxes Two serial interface boxes may be controlled at the same time if you have two free serial ports. String the port control, found in the Controls sub-menu in the Controls palette on the front panel, into the Begin command of each program. Engineer Programming Exercises Exercises Exercise 1 Create an Engineer program that looks like the following. This program turns the motor connected to Serial Interface Box port C on for 10 seconds. Click on the run button to run the program on the Serial Interface Box. Exercise 2 Create a program that waits for the touch sensor to be pushed in before turning on motors A & C. Have the motors on for 6 seconds, rotating to the left. After the 6 seconds, reverse their direction, and have them run until the touch sensor is pushed in again. Don’t forget to include the start and stop traffic lights. Click on the run button to run the program on the Serial Interface Box. If you have any broken strings in your program they must be removed before the program can run. ? Delete broken strings by clicking on them with the select tool and using the delete key or by pressing 'Ctrl-B'. Exercise 3 Create a program that waits until the touch sensor on port 3 is pushed in to turn on lamp A at power level three until the touch sensor is released. After the touch sensor is released lamp A should turn off and motors B and C should turn on at a power level of 1 for 7 seconds. After the 7 seconds the motors should stop and the program should end. Exercise 4 Create a program that that increases the power level setting to motors A & C in one-second intervals. Run it on the Serial Interface Box and test it. Exercise 5 Create a program that continually checks to see if the light level is below 50. If it is below 50, lamp A is turned on at full power. If it’s not below 50 lamp A is off. Exercise 6 Create a program that flashes lamp B and turns on a sound device every time the touch sensor is pushed in. If the touch sensor is not pushed in, nothing should happen. Solutions Exercise 2: Exercise 3: Exercise 4: Exercise 5: Exercise 6: 42 32 Rev.1 Getting Started: A Teacher’s Guide to LEGO Engineer