Week 7 RCX math The RCX does not follow the traditional rule Please Excuse My Dear Aunt Sally. Example: Parenthesis, Exponents, Multiplication, Division Addition And Subtraction. The following program drove The line follower shooter bot. /* shooter_linefollower_bot.nqc Written by Professor Mohamad Hassoun, February 2001 (Last modified October 04) Single Light Sensor based black line tracking robot (search for line capability) with side IR-based object detection. Missile firing at detected object. IT IS IMPORTANT THAT THE TRACK LIGHT THRESHOLD BE PLACED ON THE BLACK TRACK INITIALLY JUST BEFORE THE PROGRAM IS RUN. THIS ALLOWS THE ROBOT TO SET THE PROPPER TRACK THRESHOLD UNDER ANY ROOM LIGHT BACKGROUND. Program uses one light sensor, one IR sensor, and one touch sensor. It drives 3 motors */ #define EYE_1 #define EYE_2 #define BUMP #define LEFT #define RIGHT #define CENTER SENSOR_1 SENSOR_2 SENSOR_3 OUT_A OUT_C OUT_B #define TURN_SPEED #define INITIAL_TIME 4 3 int direction, time, LINE_THRESHOLD; int x, OBSTACKLE_DETECTION_THRESHOLD; task main() /* The following part sets the light sensors thresholds based on room light condition and tracks (black line) light reflection. It also starts all tasks including line tracking */ { SetSensor(EYE_1, SENSOR_LIGHT); SetSensor(EYE_2, SENSOR_LIGHT); SetSensor(BUMP, SENSOR_TOUCH); RCX how to read the sensor. // IR sensor // light sensor // Touch sensor OBSTACKLE_DETECTION_THRESHOLD = 40; RCX how to detect an obstacle Tells the Tells the // The following part sets the threshold based on room light condition x = EYE_2; LINE_THRESHOLD = x + 5; help control RCX reaction start shooter; start bumper; start follow_line; to start the defined task } Add 5 to Tells the RCX task follow_line() // Follow black tarck { // initialize the variables direction = 1; time = INITIAL_TIME; // start driving OnFwd(LEFT+RIGHT); Moves robot while(true) { // read the sensor value if (EYE_2 < LINE_THRESHOLD) Monitors the sensor and this condition must be met. { // we are over the line; do nothing. } else { // need to find the line again ClearTimer(0); if (direction == 1) { SetPower(RIGHT+LEFT, TURN_SPEED); Rev(LEFT); } else { SetPower(RIGHT+LEFT, TURN_SPEED); Rev(RIGHT); } while(true) loop in Task follow line Second infinite { // have we found the line? if (EYE_2 < LINE_THRESHOLD) { time = INITIAL_TIME; break; interrupts infinite loop; } if (Timer(0) > time) { // try the other direction direction = direction*(-1); time *= 2; doubles time break; To stop inner infinite loop } } SetPower(RIGHT+LEFT, OUT_FULL); Fwd(RIGHT+LEFT); } } } task shooter() /* If IR sensor detects a dark object, the robot shoots it. An active reading scheme using two IR sensors is employed in order to eliminate ambiant light effects. */ { while(true) { if (EYE_1 > OBSTACKLE_DETECTION_THRESHOLD) { Off(RIGHT+LEFT); PlaySound(SOUND_FAST_UP); OnFwd(CENTER); Wait(10); OnRev(CENTER); Wait(13); Off(CENTER); OnFwd(RIGHT+LEFT); } } } /* The following task makes the robot backup briefly and then attempt a 180 degrees turn. This way, the robut ends up tracking the black line in the opposite direction. */ task bumper() { while(true) Infinite loop: helps the program run continuously checking sensors { if(BUMP == 1) // Bumper activated if sensor 3 reads 1 { stop follow_line; stop shooter; These stops are here to help the robot execute task smoothly without any confusion OnRev(RIGHT+LEFT); Wait(50); OnFwd(RIGHT); Wait(175); OnFwd(RIGHT+LEFT); turn @ 180 Degrees start follow_line; start shooter; resume tasks Helps the robot Helps robot } } } We also learned about the different mathematical capabilitiesof the RCX. The RCX is unable to display numbers that extend outside of the domain. This means there are limits and if the calculations do not fall within the specified range the numerical result will be incorrect. //tarek_sorter.nqc Program to sort red and silver ping-pong balls. //Must have a red as first ball in feeder at start. //September, 2004 Prof. M. Hassoun int red_ball, no_ball; task main () { SetSensor (SENSOR_1, SENSOR_LIGHT); SetSensor (SENSOR_2, SENSOR_TOUCH); SetSensor (SENSOR_3, SENSOR_TOUCH); ClearTimer (0); A timer is used to stop the program after 500 seconds= 50000 no_ball = 30; Wait(75); if (SENSOR_2 != 1) { OnRev(OUT_A + OUT_C); until(SENSOR_2 == 1); Off(OUT_A + OUT_C); calibration for the arm( the part that drops the balls) } Wait(75); red_ball = SENSOR_1 - 12; // color calibration 12 to allow a range for the infrared light drop_ball(); while(true) { sort(); if(Timer(0) > 5000) { Off(OUT_A+OUT_B+OUT_C); This is the main infinite loop break; the infinite loop. } } } This is the initial Subtract This breaks from void sort() The sort command enables the machine to activate the sensors and separate the ball { until(SENSOR_1 >= no_ball); The program will stop here if the sensor does not collect a ball reading Wait(100); if(SENSOR_1 >= red_ball) { PlaySound (SOUND_UP); if (SENSOR_2 != 1) { OnFwd(OUT_A + OUT_C); Wait(15); OnRev(OUT_A + OUT_C); until(SENSOR_2 == 1); Off(OUT_A + OUT_C); } Wait(50); drop_ball(); } Wait(75); if(SENSOR_1 < red_ball && SENSOR_1 > no_ball) Sets calibration for silver ball { PlaySound (SOUND_DOWN); if (SENSOR_3 != 1) Sensor #3 is the one that controls the arm. { OnRev(OUT_A + OUT_C); Wait(15); OnFwd(OUT_A + OUT_C); until(SENSOR_3 == 1); Off(OUT_A + OUT_C); } Wait(50); drop_ball(); } Wait(75); } void drop_ball() { OnRev(OUT_B); Wait(60); Float(OUT_B); allows the arm to return to hold the balls in place } This command The above program was used to run this project. This is the Ball Sorter Bot. The problem solving process Designing- Builder must understand the specifications of the objectives and the constraints. Be knowledgeable of the project goal. BuildingAnalyzing & refining