190 likes | 299 Views
Conditions and Reactions. Dr. Paige H. Meeker Chapter 6. R E M E M B E R ? ? ?. Reminder - Python’s Senses. To get a quick, overall look at the behavior of the senses of the robot’s sensors, use the Myro function “senses()”
E N D
Conditions and Reactions Dr. Paige H. Meeker Chapter 6
R E M E M B E R ? ? ?
Reminder - Python’s Senses • To get a quick, overall look at the behavior of the senses of the robot’s sensors, use the Myro function “senses()” • You will see the results of all the sensor calls in real time (except the camera)
Moving the Robot • Using the sensors and the motors, we can make the robot move. We can also give some conditions of movement using the if statement. Let’s explore “if” some more.
This, That, or the Other if <condition>: <this> Depending on what condition is true, <this>is carried out. If not, <this> never happens
This, That, or the Other if <condition>: <this> else: <that> If condition is true, <this> is carried out. If not, then <other> will be carried out.
This, That, or the Other if <condition>: <this> elif <condition>: <that> else: <other> Depending on what condition is true, <this>, or <that> is carried out. If no conditions are true, then <other> will be carried out.
This, That, or the Other if <condition>: <this> elif <condition>: <that> elif <condition>: <something else> else: <other> Depending on what condition is true, <this>, <that>, or <something else>. If no conditions are true, then <other> will be carried out.
Simple Reactions… • Simple, reactive, behaviors follow a pattern to create in a robot: while timeRemaining(<seconds>): <sense> <decide and then act> OR while <light sensors are within a given range>: <sense> <decide and then act>
Simple Reactions… • Using the light sensors, the robot can detect varying light conditions in its environment. How would you write a program to detect bright light and orient the robot toward it? • Pseudocode: • for some amount of time… • If the left light is brighter than the right light, turn left • Otherwise, turn right
Head Toward the Light… Using the three light sensors the robot can detect varying light conditions in its environment. Let us write a robot program that makes it detect and orient towards bright light.
Pseudocode – Describes Behavior, doesn’t Work (More for Planning Purposes) do for a given amount of time if left light is brighter than right light turn left else turn right Sort of like your outline for a paper.
Still Pseudocode, but Closer to Actual Code while timeRemaining(30): if left light is brighter that right light: turnLeft(1.0) else: turnRight(1.0) Sort of like your rough draft for a paper.
Where would this go? • getLight('left') < getLight('right') • How would you write a complete program that implements the above behavior and test it on your robot?
Code to turn toward light (with a thresh hold added) thresh = 50 while timeRemaining(30): # Get sensor values for left and right light sensors L = getLight('left') R = getLight('right') # decide how to act based on sensors values if (L - R) > thresh: # left is seeing less light than right so turn right turnRight(1.0) elif (R - L) > thresh: # right is seeing less light than left, so turn left turnLeft(1.0) else: # the difference is less than the threshold, stay put stop()
Things to Try… • Type in the “light following” code on p. 147 of your text. • Type in and try the “obstacle” code on p. 149 of your text. http://cs.brynmawr.edu/~dkumar/Myro/Text/June09/PDF/Chapter6.pdf • NOTE: Sometimes the textbook code does not work as you may think it should – how can you alter the code to make it do what you want?
Reactive Behaviors to Implement • Refrigerator Detective • Burglar Alarm • Wall Detector • Hallway Cruiser
Reactive Behaviors to Implement • Refrigerator Detective • Ever wonder if the refrigerator light is always on? • Design a refrigerator detective robot that sits inside the fridge and tells you if the light is on or off. • Burglar Alarm • Watch your door. When the door opens, sound an alarm. • Wall Detector • Write a program that sends the robot straight until it senses a wall in front of it. Have it stop when it senses the wall. • Hallway Cruiser • Imagine the robot is in a walled corridor going around a 2ft by 2ft square box. Write a program that will enable the robot to go around the box.
Assignment Navigating Mazes Due Thursday, November 4th HINT: There’s code to help you in chapter 6. “Plus it” to get your robot to work the best!