Monday, August 18, 2014

Sensors in Pro-Bot: Relay Driving by Pro-Bots using Sensors

Pro-Bot has three kinds of sensors built into it: touch, light and sound sensors. Before we start writing programs for Pro-Bot that use the sensors, let’s see what purpose these sensors serve and what “sensing” means.

Sensing 


When someone taps your shoulder, how do you know you were touched? When the light bulb goes on in a dark room, how do you know the room suddenly got bright? When you put a candy in your mouth, how do you know that it is sweet? Because your skin sensed the touch, or your eyes sensed the light, or your tongue sensed the taste… Once you sense something, you typically react to it, don’t you? When you sense the touch, you may turn around to see who tapped your shoulder; when you sense the light coming on in the dark room, you may squint your eyes and try to figure out what is in that room; when you sense the sweetness on your tongue, you may feel happy and say “yummy”…

Your skin, eyes, tongue, etc., have “sensors” that sense some “stimulus” like touch, light, taste, etc., and enable you to respond to it. You may have also noticed that you have different sensors for different functions. Sensors are made to detect very specific stimuli. For example: your skin doesn’t see, you have eyes to do that; your eyes don’t taste the sweetness of the candy, you have taste buds on your tongue to do that.

Now what if a robot could behave similarly (it may not behave in exactly the same ways as you do)? A robot can be fitted with sensors and programmed to respond in a certain way when the sensor senses a stimulus.

Pro-Bot has 3 kinds of sensors - one senses light, one senses contact (or touch) on its front and rear bumpers and the other senses sound. Pro-Bot’s sensors must be turned on, if you want them to detect stimuli and respond to them (they are switched off by default). Think of it as needing your eyes to be open to see the light. The sensors detect only the specific stimuli that they are designed for. For example, the touch sensor or the light sensor on Pro-Bot will not detect or respond to sounds. They will respond only to touch and light stimuli respectively. However, short sharp sounds (like a loud clap or a short yell) may be detected by the sound sensor and you can program the robot to respond to it.

Now, how does Pro-Bot react when these sensors sense something? Consider the touch sensors on Pro-Bot’s front and rear bumpers. You can program Pro-Bot to do something when those sensors sense a contact (such as bumping into something, or getting bumped by something). Similarly, the light sensor on Pro-Bot can be programmed to do something when it detects a change in lighting.

Pro-Bot has 5 specialized Procedures that correspond to inputs from its sensors. These are 
33 FRONT
34 REAR
35 DARK
36 LIGHT
37 SOUND

You can access and modify the above Procedures via the Menu button on the control pad. The instructions in each of these Procedures will be executed when the the corresponding sensor detects a stimulus. If the Procedure corresponding to the sensor is empty (if you decide not to react to a stimulus), Pro-Bot does not respond to changes in the sensor condition. 
So, what happens after Pro-Bot has responded to a stimulus? Before you answer that question, consider this scenario: Imagine that you are sitting in your chair and reading a book. Your friend comes over, taps you on the shoulder and asks you something. Your skin’s touch sensor senses the tap, and your ears (another of your sensors), sense the spoken words. Maybe your friend was asking you to join her in a game. Let us say you respond saying “Later”. What do you do next? You would continue reading that interesting book, right? 
Let’s analyze what just happened. You were doing something… then you got “interrupted” by your friend… you “handled” that interruption… and then you got back to doing your reading… A computer or a robot can react the same way. When its sensor detects a stimulus, Pro-Bot can react to it by running a specific program, and after it is done, Pro-Bot continues with what it was doing before the interrupt happened. For example, if Pro-Bot was driving and midway, it entered a dark tunnel, it would detect the change in light and may turn on the headlights (if you programmed it to respond that way) and after that, it would continue driving along. After it is done with the response to the stimulus, Pro-Bot resumes the steps in the main program. 

What you have learned above is a fundamental behavior in Computer Science and Robotics: handling interrupts

Let us test our understanding now with an assignment that uses sensors.



Relay Driving by Pro-Bots using Touch Sensors:


Computer Science Concepts involved:   Procedures, Sensors to detect and react to stimuli, a quick peek into Interrupt handling

Math Concepts Involved:   Linear measurements, Solving real world problems by modeling with mathematics

Grade Levels:   3, 4, 5

Hours Required:   1

Materials Required:   A pre-set path drawn for the Pro-Bot to drive on, preferably marked with blue tape. Optionally, a shoebox with one vertical side cut open to act as a garage for Pro-Bot


Programming Assignment:


Let’s look at a simple task to start off with, involving 2 Pro-Bots. The steps are listed below.
  1. Mark a path on the floor with blue tape that is about 40 cm long. You can also mark a target finish line at the end of the path.
  2. Place one Pro-Bot at the beginning of the path, facing forward and ready to drive along the path. 
  3. Place the second Pro-Bot at approximately the midpoint of the path, 20 cm away from the start point, facing forward and ready to start driving. (Both cars face the same direction.)
  4. Optionally, place a ‘garage’ (made out of an upside down shoe box with one side cut for the car to enter) at the very end of the path.
  5. Make sure that the sensors are set to "On" from the Menu button on Pro-Bot.
  6. Program your Pro-Bots so that the first car starts driving along the path while the second car is waiting. The first car hits the back of the second car, makes a beep sound and stops. The second car now starts driving. It drives all the way into the finish line/ garage. It makes a beep sound and stops. (Optional step: when it gets inside the garage, it switches its headlights on.)


Now, how can we program the two Pro-Bots to do this?
  • For the first Pro-Bot, the task involves driving forward, say 20 cm, to reach the second car and then reacting to a touch on the front bumper when it hits the second Pro-Bot. So, your program for the first Pro-Bot has to be split up into two parts - the Main program that handles the driving forward part and Procedure 33 FRONT that handles the contact to the front sensor. You will have to edit Procedure 33 FRONT to make the beep sound & add in a few Pause instructions to make the car stop.
  • For the second car, driving starts only when it gets hit on the rear bumper. To make the car wait, your Main program shall have a few Pause instructions in a loop. You would also need to modify Procedure 34 REAR to make the car react to the hit on the rear bumper and drive to the finish line/ garage. Optionally, once inside the garage, the light sensor can detect the darkness and respond to it; for this modify the procedure 35 DARK to switch on the lights.

Once the two Pro-Bots have been programmed, press the GO button on both cars at the same time and watch the relay race happen! Here is a sample set of programs, for two Pro-Bots kept at a distance of 20 cm from each other and the finish line at a distance of 20 cm from the second Pro-Bot:



Pro-Bot 1:

  • Main - Fd 20

  • 33 FRONT -  Sound 3
                              Rpt 20 [
                              Ps
                              ]

Pro-Bot 2:

  • Main - Rpt 20 [
                   Ps
                   ]

  • 34 REAR -  Fd 20
                           Sound 3

  • Optionally, 35 DARK - Light On

Note:   This project can be easily extended to include more cars and more complicated paths. Since we do not have a Stop instruction available, we can make the car stop by using the Pause instruction in a loop. 


Extension


If working on the relay race project with 3 or more cars, all the cars other than the first and last ones would need to handle both their front and rear touch sensors. Let’s look at an example with 3 cars, assuming they are kept at a distance of 20 cm each. In this case, the first car would behave as Pro-Bot 1 above and the last car would behave as Pro-Bot 2 above. Here is a sample set of programs for the 3 car relay race:


First  Pro-Bot :

  • Main - Fd 20

  • 33 FRONT -  Sound 3
                              Rpt 20 [
                              Ps
                              ]

Middle Pro-Bot:

  • Main: Rpt 20 [
                  Ps
                  ]

  • 34 REAR -  Fd 20
                           Sound 3
                                
  • 33 FRONT -  Sound 3
                              Rpt 20 [
                              Ps
                              ]

Last Pro-Bot :

  • Main - Rpt 50 [                     // Remember to make the Pro-Bot wait longer 
Ps // here to allow for the other two cars to catch up.
                   ]

  • 34 REAR -  Fd 20
                           Sound 3

  • Optionally, 35 DARK - Light On




No comments:

Post a Comment