[ Pobierz całość w formacie PDF ]
.66LoopsA loop is a series of commands that you want to be executed repeatedly.NQC offers three flavors of loop:repeat (expression value) [statements]This command simply repeats the given statements value times.while (boolean condition) [statements]This loop repeats the supplied statements until condition is no longer true.do [statements] while (boolean condition)This loop is similar to while but the statements are executed before the condition is tested.The statements will alwaysbe executed at least once, which is not true for a while loop.Let's look at an example.The following code plays a sound every half second while a light sensor attached to input 3 sees dark:while (SENSOR_3 LIGHT2) {toggle();Wait(TIMEOUT " 2);}}}}sub toggle() {if (state == LEFT) {OnRev(OUT_A);OnFwd(OUT_C);state = RIGHT;}else {OnFwd(OUT_A);OnRev(OUT_C);state = LEFT;}}The main task performs three important initializations which I haven't mentioned yet.First, main initializes the value of thestate variable.It just uses LEFT79arbitrarily.Next, main configures input 2 for a light sensor.Finally, it starts the lightWatcher task.Using Two Light SensorsIn this section, I'll present an NQC program that works with the two light sensor version of Trusty.As you may recall,programming this robot in RCX Code was cumbersome.The programming is a lot cleaner in NQC.It's pretty straightforward to translate Table 3-1 into source code.The basic strategyis to use a state variable to represent the four states of the robot, represented by the four lines of Table 3-1.Then one taskexamines the sensors and updates the state variable.Another task examines the state variable and sets the motors appropriately.The four possible states are represented by constant values.A fifth value, INDETERMINATE, is used when one or both of thelight sensor values is not in the dark or light range:#define BOTH_ON 3#define LEFT_ON 1#define RIGHT_ON 2#define BOTH_OFF 0#define INDETERMINATE 255The main task simply tests the value of the state variable and sets the motors accordingly.No action is taken forBOTH_OFF and INDETERMINATE:while (true) {if (state == BOTH_ON)OnFwd(OUT_A + OUT_C);else if (state == LEFT_ON) {Off(OUT_A);OnFwd(OUT_C);}else if (state == RIGHT_ON) {Off(OUT_C);OnFwd(OUT_A);}}A separate task, watcher, examines the light sensor values and sets the state variable.Here is the entire source code forthe two sensor version of Trusty:int state;// "ON" refers to whether the light// sensor is on the line.If it is,// the light sensor is seeing black.#define BOTH_ON 3#define LEFT_ON 180#define RIGHT_ON 2#define BOTH_OFF 0#define INDETERMINATE 255// Thresholds for light and dark.#define DARK2 35#define LIGHT2 40#define DARK3 40#define LIGHT3 45#define POWER 4task main() {initialize();while (true) {if (state == BOTH_ON)OnFwd(OUT_A + OUT_C);else if (state == LEFT_ON) {Off(OUT_A);OnFwd(OUT_C);}else if (state == RIGHT_ON) {Off(OUT_C)OnFwd(OUT_A);}}}sub initialize() {SetSensor (SENSOR_2, SENSOR_LIGHT);SetSensor (SENSOR_3, SENSOR_LIGHT);SetPower(OUT_A + OUT_C, POWER);OnFwd(OUT_A + OUT_C);start watcher;}task watcher() {while (true) {if (SENSOR_2 LIGHT2) {if (SENSOR_3 LIGHT3) state = BOTH_OFF;else state = INDETERMINATE;}else state = INDETERMINATE;}}81Online ResourcesNQC Not Quite Chttp://www.enteract.com/~dbaum/lego/nqc/This is the official site for NQC.You can download the current release, read the documentation, or browse a FAQ.Dave Baumhas packed a lot of useful information into this site, including such gems as how to create a cable to connect your Macintosh tothe IR tower.This site also includes the definitive NQC documentation.Lego Robots: RCX Command Centerhttp://www.cs.uu.nl/people/markov/lego/rcxcc/RCX Command Center (RcxCC), developed by Mark Overmars, is built on top of NQC.It's a Windows application thatprovides a friendly graphic interface to the features of NQC.It includes a syntax-colored program editor, real-time control ofthe RCX, utilities for making your RCX play music, and useful help files.I highly recommend this application.Lego Robot Pages [NQC Tutorial]http://www.cs.uu.nl/people/markov/lego/Mark Overmars, creator of RcxCC (the previous entry), has written a detailed introduction to NQC.It's available off his mainweb page as PDF, Word97, PostScript, or RTF.This document is a gentle and thorough introduction to NQC.Kevin Saddi's NQC Reference Pagehttp://home1.gte.net/ksaddi/mindstorms/nqc-reference.htmlThis page provides a distilled view of NQC.It's very handy when you can't remember the input type constants, or you'd like tosee NQC's commands organized by function.Single-line code samples are also included.Hitachi Single-Chip Microcomputer H8/3297 Series&http://semiconductor.hitachi.com/products/pdf/h33th014d2.pdfThis PDF document has all the crufty details on the Hitachi H8 that forms the heart of the RCX.The specific model is theH8/3292, which is covered in the manual.This information is not for casual browsing you probably won't need to look hereunless you start writing your own firmware.(Hitachi's web site is a little flakey.If you're having trouble with this URL, trystarting at the URL http://semiconductor.hitachi.com/h8/ and searching for the H8/3292.)825Minerva, a Robot with an ArmIn this chapter:" Building Instructions" Programming" Directional Transmission" Pulleys" Mechanical Design" Two Sensors, One Input" Where Am I?" Online ResourcesMinerva is a mechanical marvel.Although she has the same wheel layout as Trusty, Minerva's drivetrain is radically different.In addition, she has a simple arm with a grabber, which allows her to pick up and drop small objects
[ Pobierz całość w formacie PDF ]