Drug Delivery - Background

From Drexel University NanoEnlightment

Jump to: navigation, search

For the next few weeks you will be working to build and program robots using the LEGO Mindstorms NXT development kit. The physical components of the robots are made up of familiar LEGO parts (mostly beams and pins). The electronic components of the robots are cased within a white plastic "brick" and consist of a number of Atmel micro-controllers and support hardware. The software components of the brick (which you will write) are designed using a programming language know as NXT-G, which is based on a piece of software from National Instruments called LabVIEW.

Contents

Installing the NXT Software

  • Download the software from here (see login instructions on the module main page)
  • Unzip the archive
  • Run setup.exe
  • Follow the installer instructions
  • Apple Faithful: For the moment it looks like our install image lacks the Mac version of the software. We will hopefully be able to fix this soon. In the meantime bring your computer to the lab when you have class and we will have install CDs available for you to use.

Downloading Programs to the NXT

  • With the NXT software running and a program file open:
  1. Connect the NXT to the computer using the USB cable supplied in the kit.
  2. In the lower right hand corner of the NXT software screen there is a control box.
  • Press the center button of this box to compile, download, and run your program.
  • Press the button in the lower left hand corner of this block to compile and download your code, but will not run it. (Useful if you want to disconnect the cable in between.)

Image:Control-box.png

Running Programs on the NXT

Once you have downloaded the program onto the bot using your computer and the USB cable the NXT intelligent brick will have a copy of the program stored in its memory and can now be accessed directly from there. This means that connection from the computer to the NXT intelligent brick using the USB cable is no longer required to run the program.

One you have transferred the program to the NXT intelligent brick you can run it form the brick’s menu as follows:

  1. Switch on the NXT intelligent brick using the Orange button.
  2. Navigate to the My Files menu using the left/right Grey buttons and enter it using the Orange button.
  3. Navigate to the NXT Files menu using the left/right Grey buttons and enter it using the Orange button.
  4. Locate your downloaded program in this menu. The filename you assigned to the program should be listed in this menu.
  5. Enter it using the Orange button and you will see options to RUN, DELETE or SEND.
  6. Use the Orange button again to RUN the program.
  7. Use the dark Grey button below it to stop execution of the program.

Programming the NXT

Generally, once you've learned one programming language, learning additional languages becomes far easier. The same basic building blocks tend to make up each language and learning the new one tends to be just figuring out how to make analogies to the old. We are assuming that at this point you are all familiar with the Maple programming environment from your experiences in CS121 and will attempt, where possible, to make comparisons to its functionality.


The biggest change to get used to when moving to NXT-G is the fact that it is an entirely graphical programming environment. You will not see long blocks of obscure text as you would with Maple or another programming language. While there is a small adjustment period, graphical languages tend to have a shallow learning curve. The best way to get over this is to spend as much time as you can find simply tinkering with the software and hardware (when available).

Loops (for/while)

The concept of a loop is consistent over all programming languages, including NXT-G and Maple code. To refresh your memory, the code block below has been extracted from the CS121 Lab files. The block iterates over the variable 'i' and prints a message with its current value during each iteration.

Maple:

for i from 1 to 5 do
    print("Hello for the ", i, "th time.");
end do;
"Hello for the ", 1, "th time."
"Hello for the ", 2, "th time."
"Hello for the ", 3, "th time."
"Hello for the ", 4, "th time."
"Hello for the ", 5, "th time."


Loops in NXT-G follow the same concepts, but take on a slightly different format. The index is controlled through the loop's control panel. When set in count mode the loop will execute until the index has reached the specified value. The index begins at zero and increments at the end of each iteration. NXT-G can be set to operate based on simple counts as well as sensor trigger events, time intervals, or boolean logic inputs. We encourage you to play with these different options to see what use you can put them to.

Image:Loop.png


Conditionals (if/then)

Conditionals are used in any type of decision making process within a program. They are another element that can be found in any programming language. You may recall the following from CS121: Maple:

for i from 1 to 10 do
  if type(i, odd) then
    printf("%d. I will not use Facebook in class.\n", i);
  else # i is even
    printf("%d. I shall not use Facebook in class.\n", i);
  end if;
end do;
1. I will not use Facebook in class.
2. I shall not use Facebook in class.
3. I will not use Facebook in class.
4. I shall not use Facebook in class.
5. I will not use Facebook in class.
6. I shall not use Facebook in class.
7. I will not use Facebook in class.
8. I shall not use Facebook in class.
9. I will not use Facebook in class.
10. I shall not use Facebook in class.

Conditionals in NXT-G are implemented using the switch statement set to accept a 'Logic' 'Value' as an input. The same concept demonstrated in the maple code is illustrated in the following NXT-G program. There are a few minor changes as there is no 'is-odd' type command in NXT-G, but the concept remains the same. The program will iterate over a loop index, checking to see if the value of that index is even or odd. The outcome of this comparison is used to display, alternately a :-) or :-( .
Image:Conditional.png
Code available here.

(Not so) Basic Math

This is one aspect where typed programming languages have a sure advantage over graphical ones. To do simple (or not so simple) math in Maple you simply type the statement you want evaluated (e.g. (2 + 2) * 5) and press enter, as in the following example:

Maple:

> (2+2)*5;
     20 

This same operation, performed in NXT-G, is slightly more difficult. The 'Math' block must be inserted into the program for each operator (+, -, *, /) in the expression. The operator is chosen from a drop down list and the values of the operands can be typed directly into the block's control panel or can be wired in from other parts of the program. Please note the absence of the exponentiation operator.
Image:Math.png


Debugging

When sitting at a computer terminal it is relatively easy to simply dump data out to the screen in the form of text in order to debug oddities in computer programs. When dealing with robots, designers do not always have the luxury of a full color 17" display, and must often resort to creative ways of retrieving information on the state of a program (LEDs are a personal favorite). The NXT system sits somewhere between these two extremes, providing us with a minimal text display for information output. To 'see' whats going on within the NXT we can convert numeric data to text and print it to the screen. The simple program below does this with the value of a timer. Image:Debug.png

Variables

Using simple variables in text based languages like Maple is very straightforward. Something as simple as:
Maple:

> x := 10;
    10
> x + 5;
    15

will allow you to assign and use a variable. Graphical languages like NXT-G and LabVIEW have managed to make things both easier and harder at the same time. For simple tasks, variables are replaced with the concept of 'wires' which can be used to carry information from one point in a program to another. For more complex tasks, variables must be specially created through a menu found on the main toolbar at the top of the interface under "Edit->Define Variables".
Image:Variable.png
From this menu you can create variables of three different types: Logic, Number, or Text. These variables can only be accessed by using the special "Variable" block which passes whatever value is currently stored in the variable onto a wire for access by other blocks. The "Variable" block can only be used for either a read or write operation at one time.

Wires vs. Variables

To demonstrate the differences we present a simple program which will continuously read the value off of a light sensor, add 3 to its value, and print that to the screen. In the first version we use only wires, in the second we pass the value into a variable as an intermediate.
Image:Wire-vs-var.png

Functions

  1. Build a program which performs the task you wish the function to perform. The inputs and outputs of the function are represented by variables. The names of these variables will be used as the names of the inputs and outputs of the function later.
  2. Highlight the parts of the program you wish to have included within the function (do not include input/output variables).
  3. From the menu bar along the top of the screen select: Edit -> Make a New MyBlock
  4. You will be presented with a dialog which shows an overview of your function. Check to ensure that all desired blocks are included. Give the function a name using the box in the upper right hand corner. Select 'Next' and pick out an icon for the function.
  5. Press 'Finish'.
  6. You will see that parts of your program have been replaced with the new MyBlock. You can use this block here or in any other programs.

Linearity of Code

Programming for robots requires a slight shift in thinking from programming with completely abstract code. The functions you execute (motor and sensor commands) will all have effects in reality. While all programming is time dependent, waiting for the robot to execute a physical command will introduce delays in processing that create delays you may not have noticed in a strictly abstract environment. You must take care to assure that your program will not stall at positions you do not intend.

Example:
Image:Time.png
You want to program your NXT to respond to simple sensor feedback. You want the unit to drive forward until a touch sensor is triggered, at which point you want it to stop and perform some other action (reverse direction maybe). One (not so effective) way to do this is by setting a sensor triggered loop to repeat a timed motor command. Timed motor commands are one type of command which will pause processing until they are completed. The loop may terminate when the sensor is pressed, but the motor command must still finish, and will hold the rest of the program until its time interval (in this case 10 seconds) has finished.
An alternate way to perform the same task, but with an instantaneous response time, is to make use of 'Unlimited' motor commands. These commands will simply turn the motor on and continue processing your program. The motor will continue to run until an explicit stop command is issued. By inserting a sensor triggered loop between the start and stop commands (which will effectively hold processing until the sensor is triggered) we can create a motor control with instantaneous response to the sensor input. (Note: Placing the unlimited motor command within the loop will have the same effect.)


Bottom Line: Be mindful of effects that will pause your code, and prevent it from progressing to the next command.

Errors

Out of Memory

Occasionally you will run out of memory on the NXT

From the NXT Software help pages:
"Note: On rare occasions you can get an out-of-memory error message even when the graphical memory overview indicates there is free space available. This is caused by memory “fragmentation.” Fix this problem by using the Delete All User Files button but remember to back up any of your essential files before proceeding."

Example Programs

*Note - All images on this page proudly created using the professional version of MS-Paint

Personal tools