The Art of LEGO MINDSTORMS NXT-G Programming ®®terry griffin covers nxt-g 1.0 and 2.0 The Art of le. Nxt Programs For Tribot Fighter. The section below refers to the Lego Digital Designer program. Robot Challenge: NXT Tribot Grabber. The first visual programming environment was called LEGOsheets, since it was created by the University of Colorado in 1994 based on AgentSheets. The original Mindstorms Robotics Invention System kit contained.
NXT Unplugged - Programming Without the Computer
With the NXT 1.26 (don't use that version!) and 1.28 firmware, you can write simple programs directly on the NXT brick. This is really handy for activities where you need the NXT to just do simple tasks - where the main goal is construction, rather than programming. It's under the 'NXT Program' icon. Press . When you select it, the next screen tells you which ports you should use to connect the motors and sensors (if you're using any). Make sure your wires are connected correctly. Now we're ready to program. Press .
The Big Picture - Five Program Blocks
This screen illustrates how you enter a program. You have 5 'blocks' that hold one step of the program. The black triangle indicates that you're going to enter the action for the first block. The five blocks are not all the same - different blocks can do different things:
Block 1 : Action (move forward, backward, turn, or make a sound). Block 2 : Wait-For (time or sensor). Block 3 : Action - same as before. Block 4 : Wait-For - same as before. Block 5 : Stop or Loop.
An example program might be:
1 - Move forward. 2 - Continue moving until the touch sensor is pressed. 3 - Back up turning right for 2 seconds. 4 - (empty) 5 - Loop 'forever'.
This program will make the robot wander around the room until the dog finds it and starts chewing on it, or something like that. We'll now step through the process of building a program.
Fifteen Action Choices
The first and third blocks can contain actions that make the robot move or play a sound. If you scroll left and right using the arrow buttons, you'll see that there are 15 different actions: The two musical notes play a sounds of different pitches. The empty box means 'do nothing'. You probably wouldn't want to 'do nothing' in the first action box, but you might in the second. All of the other choices are movement. The movement choices come in pairs, with and without a number. With the number, the NXT does that movement for either two or five seconds, depending on the number. Without the number, the NXT will do the action until the wait-for condition in the next block is satisfied. The possible movements are, from left to right:
Back up, turning left.
Go forward, turning left.
(do nothing)
Go straight backward.
Go straight forward.
Go forward, turning right.
Back up, turning right.
(play a sound)
Choose your action, then press . The next screen shows two things. First, the black triangle has moved to the second block, indicating that you're going to choose a wait-for condition. Second, you can see a tiny spec of an icon in the first block - which is actually enough to tell you which action was selected. In this case, the action is forward (the arrow) for 5 seconds (the speck at the tip of the arrow).
Nine Wait-For Conditions
The second and fourth blocks can contain wait-for conditions that make therobot continue moving until the condition is satisfied. If you scroll left and right using thearrow buttons, you'll see that there are nine different conditions: The empty box means 'don't wait - just go on to the next block'. The possible conditions are, from left to right:
Wait until the sound sensor hears something.
Wait until the light sensor sees more light.
Wait until the light sensor sees less light.
Wait until the touch sensor is pressed.
Don't wait - immediately cancel the previous movement.
Wait for 2, 5, or 10 seconds.
Wait until the ultrasonic range finder notices an object getting close.
If you're familiar with programming these wait-for conditions in NXT-G, you've probably noticed that there's no way to set thresholds here – how bright, how loud, or what distance. You're right. The NXT makes a guess about the threshold, probably based on the sensor value when the wait-for block starts. When the condition is satisfied (for example, the touch sensor is pressed), the movement (and the only possibility is movement) chosen in the previous block is canceled. 'Don't wait' can be confusing, because the motors will immediately stop. So, if the motor runs for just a fraction of a second and stops, you've probably chosen a 'run until' in an action block followed by a 'don't wait' in the next block. You should only choose 'don't wait' when the previous action was a timed movement or sound. Choose your wait-for condition, then press . The next two blocks (3 and 4) are exactly the same as 1 and 2. So, for example, you could choose 'forward 5 seconds, wait 2 seconds, backward 5, wait 2 seconds'. The last block lets you choose what happens when the first four blocks have finished. You've got two choices: stop and loop. Stop means just that - run the first four blocks and then stop. This will be fine for many situations, but is also useful when you're debugging a program that you plan to have loop. Loop means to keep running the first four blocks, over and over - forever. Or, at least until the batteries run down. Choose whether to stop or loop, then press . Finally - you've finished the program. Just press to run it. While the program is running, the five blocks are shown with the black arrow underneath the active block. This is hand if, for example, your robot runs without stopping - you'd probably see that it was stuck in one of the wait-for blocks, waiting for... something. The other two choices are to go back to the main NXT menu - forgetting about the program or to save the program. Personally, I don't find the save option to be very interesting, because it takes longer to enter the name for the program than to re-create the program.
What if I make a mistake?
The dark-gray 'back' or 'up' button is your friend, at least a little. Each time you press it, you will erase one program block. So, pressing it once will take you to block 5, press again to get to block 4, etc. I hope that you've found this tutorial helpful.
Colophon
I hope you enjoyed the graphics I put in here. They are actual screen shots from my NXT, which I obtained using a handy utility called 'NXT Screen', which is part of NeXT Tools for Mac OS X, an open-source project. Those screen shots actually end up in gray scale. I massaged them into black-and-white, then changed the background color to green, using GraphicConverter.
Reading S'ensors
In Building Robots with LEGO Mindstorms NXT, 2007
The Servo Motor Encoder (Rotation Sensor)
The legacy RCX rotation sensor was always known for its lack of reliability with readings when turning at both low and high speeds. Robot makers had to play with code to provide stability to readings returned from this sensor.
LEGO decided to integrate an encoder (rotation sensor) directly within its new NXT servo motors (Figure 4.10). There are two benefits to this: The encoder functionality was improved, and the NXT received three rotation sensors built right into the motors that don't require additional sensor ports! The interactive servo motor (as it's also referred to) allows you to measure both speed and distance in a variety of formats, including degrees, rotations, and seconds. It acts as both a motor and a rotation sensor, and has a dedicated block for each of these in NXT-G. In RobotC, you would simply set the parameters for driving the motor as you normally would while using other commands to read the encoder values to measure rotation.
Figure 4.11 shows an internal view of the servo motor with the encoder (in blue) located to the left of the larger orange drum (the motor). In reality, the encoder is actually a black wheel that contains 12 holes which allow the optical sensor to read 24 on/off states with each full rotation. This provides the NXT with a great deal of resolution to detect position down to the nearest degree. From the image, you can also see how the NXT motor is internally geared. There is enough torque to drive wheels/tracks directly. Even though the RIS motors are also internally geared, they have limited torque that usually required an additional geartrain—especially in sumo competitions!
Having an encoder built directly into the servo motor allows robot designers to develop more sophisticated drive mechanisms that enable your robot to do things such as drive straight, even over rough terrain. This functionality works out of the box with NXT-G. When you program your robot, the move block pairs two motors together, enabling the NXT to monitor the encoders of both motors while correcting them on the fly to ensure that the robot is tracking straight. The general idea is that the program monitors rotations on both motors. If one falls behind, it adjusts the speed of one motor to compensate for the lag, which keeps the robot driving straight.
You can try this yourself by creating the TriBot from within Robo Center (sample robots in NXT-G). Following the programming guide, you will use a move block to allow for both drive motors to be synchronized. Once built, run the robot and follow along beside it. Press your finger to one of the wheels and then let go. Note how at first you slow down one side of the robot, but then it speeds that side up to bring the robot back to driving in a straight line.
As mentioned earlier, you can use a single servo motor to both move an elevator as well as determine which floor it is on. With the new level of accuracy in these motors, you can determine the position of the elevator by performing some simple tests to find which angle values represent each floor. To do this, create the elevator unit and manually rotate the motor while viewing the encoder rotation values in NXT-G (or the RobotC poll brick window). Jot down the rotation angle for each floor. Then, simply identify in your program these angles as stop points for the elevator unit.
The encoder functionality is very powerful for the future of NXT robots, as it opens the door by enabling your robots to be “location-aware” by performing tasks such as room mapping. The sky is the limit here.
Bricks & Chips …
How the Servo Motor Encoder Works
The NXT motor encoder detects movement similar to the way an older computer mouse (with the ball) works. One of the first things Philippe Hurbain (Philo) did when he got his NXT set was to dismantle the servo motors to have a look under the hood. His site (see Appendix A) provides detail on this. Figure 4.12 shows the motor and encoder components cut away from the rest of the motor. The encoder wheel (the black wheel to the right) is driven directly off the motor. The wheel has a number of holes in it which allow the optical sensor to detect on/off states as it spins. A beam of light is generated from the optical sensor (the gray square box covering the encoder wheel) on one side of the encoder wheel and shines through to the other, which falls upon a photocell. As the motor spins, the encoder rotates and causes light to alternate through a series of on/off states. The sensor picks this up and passes the information to the NXT for processing. Unlike the older rotation sensor, because the motor is directly coupled to the encoder wheel, direction is automatically handled, as the NXT always knows which way it is driving the motor based on the programming done in the software.
Through RobotC, it is possible to detect motor stall conditions by monitoring the motor encoder rotation as your motor turns. Knowing the power and expected output of the motor, you can match this with the speed at which the encoder is actually turning and detect when the motor has stopped turning. This has an added bonus, as you could conceivably create a robot that does not need a touch sensor to detect when it has hit a wall. You can simply monitor the motor rotations and judge it this way—when you hit a wall, you can detect that the motors have either slowed or stopped, and after a period of, say, one or two seconds, force a decision to back up or turn.
NOTE
The NXT supports three types of sensors: passive, active, and digital. The main difference is that passive sensors do not require a current generator to supply power to the sensor, whereas active sensors do. Digital sensors use l2C communication and typically have a microcontroller to handle sampling of the environment.
Passive sensors include the touch sensor (NXT and RCX), light and sound sensors (NXT), and the RCX temperature sensor. Active sensors include the RCX light and rotation sensors. Digital sensors include the NXT ultrasonic sensor and numerous third-party sensors such as color, compass, pressure, gyro, and acceleration sensors.