| NIST Interschool Tactical Robotics Organization | ||||||||||||||||||||||||||||||||
| < -------------------------------------------------------------------------------------------------------------------> | ||||||||||||||||||||||||||||||||
| Page Navigation | ||||||||||||||||||||||||||||||||
| TRS NQC Guide 2 | ||||||||||||||||||||||||||||||||
| Homepage | ||||||||||||||||||||||||||||||||
| Guide 2: Higher Commands Guide 2: Higher Commands An introduction to Sensors LEGO Mindstorms allows you robots to interact with the environment by using sensors. The RCX has three ports for sensors, which include touch sensors and light sensors. A touch sensor will return the value 1 if it is pressed, and 0 if it is not pressed. A light sensor will give a percentage value between 0 and 100, where 100 is very bright, and 0 is no light. More Bricxcc commands BricxCC is a program that allows you to write and send NQC programs. It also does the function of checking your code, so your robot can run it correctly. NQC code has a special set of rules (lexicon). Last time, we learned about the following functions. Can you remember what they do? - Tasks() - OnFwd() - Wait() - OnRev() - Off() Today we learn more complicated functions 1.) SetSensor(): task main() { SetSensor(SENSOR_1, SENSOR_LIGHT); //___________ //your code } If you will be using sensors on your robot, you need to set them first. This means, inside your main task code, you need to have the SetSensor() function. In order to set the sensor connected to sensor port 1 as a touch sensor, you type into the brackets: �SENSOR_1, SENSOR_TOUCH�. Now the robot will know that there is a touch sensor attached to sensor port 1. If you want to set more than one port with sensors, you just repeat the above instructions for setting the other sensor port 2. Note that a touch sensor, once set, returns 1 for pressed and 0 for not pressed. A light sensor returns values in percentage light. 2.) while(): task main() { SetSensor(SENSOR_1, SENSOR_LIGHT); //____________ while(SENSOR_1 == 0) // _______________________ { OnFwd(OUT_A + OUT_C); //____________________ } Off(OUT_A + OUT_C); } The While() function works with something called a CONDITIONAL. A conditional is a statement that can either be true or false, but not both. In this case, the condition is placed inside the brackets. Since the statement SENSOR_1 == 0 can only be either true or false (If touch sensor on port 1 is pressed, then it�s false. If it�s released, then it�s true). The While() function will LOOP whatever is inside its set of brackets if the condition is TRUE, only until the conditional is no longer true. Note that once you move out of the While loop, the program never goes back to it. If our robot had a touch sensor attached to an array on the front of the robot that starts released, then it will start by moving forwards. It will keep moving forwards until the touch sensor touches something and is no longer pressed. Here�s a tip. If you want something to looped forever, you can trick the program by putting the word �true� inside the brackets where the conditional will be. The program will be tricked into thinking that the conditional is true, and thus will loop the program forever. 3.) if(): task main() { SetSensor(SENSOR_1, SENSOR_TOUCH); //___________ SetSensor(SENSOR_3, SENSOR_TOUCH); //___________ OnFwd(OUT_A + OUT_C); while(true) // ______________________________ { if(SENSOR_1 == 1); // _____________________ { OnRev(OUT_C); // ______________________ Wait(100); // ______________________ OnFwd(OUT_A + OUT_C); // _______________ } if(SENSOR_3 == 1); // ________________________ { OnRev(OUT_A); // ______________________ Wait(100); // ______________________ OnFwd(OUT_A + OUT_C); // ___________ } } } The if() command tells the program to CHECK THE CONDITIONAL, once and only once. If the conditional is found to be true at that point in time, then the program will execute what is inside its set of curly brackets, and the move onto the next line. If the conditional is found to be false, it will skip over the code and move on. If you want the program to check repeatedly, you should use a while loop, as the example above shows. Can you try to predict what this program will do? Assume that you have a tracked robot, dual motor drive, with two sensors. Motor A and sensor array 1 is on the left, motor C and sensor array 3 is on the right. What will happen if we run this program? Building Sensors: When working with sensors in LEGO, you cannot just stick the touch sensor itself on the front of the robot and expect it to work. You need to build a sensor array (a kind of bumper) around the sensor that will allow pressure from touching an obstacle to be transmitted to the sensor. Be creative. Remember that simple is usually best. You can also use rubber bands or string to make your sensor return to the original position. A tip: it is always more effective to build a sensor system where the default position is pressed. When you touch an obstacle, the movement is translated to the sensor being released. This makes it more accurate, and prevents false positives. Briefing for Challenge: �MARS LANDING� Now that you know how you use sensors, you will have to design your robot to be able to respond to its environment. First you�ll have to build your robot first. Think about the following things: - Speed � It has to be fast enough to cross the course in time, but slow enough that it reacts properly to the obstacles - Gear ratio/power � It has to be strong enough to move cross carpeting, but weak enough that it doesn�t mow down obstacles, because that would be a penalty - Traction � It has to have enough traction to move, but not too much that it wastes power on turns - Ground clearance � It has to be high enough off the ground that it doesn�t catch on the carpeting, but it still doesn�t topple over - Turning circle � It has to have a tight enough circle that it can maneuver in a small space, but not so tight that it is difficult for it to turn - Bumper design � It has to react quickly to touching an obstacle, but it needs to be accurate so it isn�t accidentally activated - Robot design � It should be simple. Don�t put on parts for the sake of putting on parts. KISS: Keep It Simple, Stupid. Remember that you only have 3 weeks to finish your robot before we start programming. Good luck, roboteers. |
||||||||||||||||||||||||||||||||
| Proposed Events | ||||||||||||||||||||||||||||||||
| NQC Guides | ||||||||||||||||||||||||||||||||
| Sign the NITRO WebBoard | ||||||||||||||||||||||||||||||||
| Last Updated: August 19th 2004 | ||||||||||||||||||||||||||||||||
| Contact the Organizer | ||||||||||||||||||||||||||||||||
| Name: | ||||||||||||||||||||||||||||||||
| Frank (Tha-An) Lin | ||||||||||||||||||||||||||||||||
| Email: | ||||||||||||||||||||||||||||||||
| [email protected] | ||||||||||||||||||||||||||||||||
| You are Roboteer number: | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
| < -------------------------------------------------------------------------------------------------------------------> | ||||||||||||||||||||||||||||||||