Autonomous

During autonomous mode, the Timmyton will roam around randomly until it encounters an object, then charge, stopping and turning around at the last second. This mode will be similar to the one we made for the Omnilander, but we will introduce some new concepts. Navigate over to the switch tab with the center brick button highlighted to program this final operating mode. Start filling in the switch case with the usual initialization blocks, plus a one second wait directly before  Mode Loop:

Within  Mode Loop, add a switch (port 4, Infrared Sensor | Compare | Proximity, threshold value < | 35). This uses the IR sensor in the Timmyton's nose to check for nearby obstacles.

Then, program the false case. This executes when there are no obstacles nearby. The Timmyton will drive around randomly. First, the brick LED turns green. Then, to get a wandering effect when the Timmyton drives, we will add two random number blocks (from the math section). Set the minimum value to 1 and the maximum value to 100 for both of them. Add a move tank block after the random number blocks (On, On for Seconds, duration of 1 second). Then, plug the output of each random number block into one of the power inputs on the move tank block. This code will generate a random power value for each drive wheel and drive at these power levels for 1 second. Assuming that no obstacle comes into view, two new power values will be randomly generated, and the process repeats, giving the Timmyton's driving a wandering quality. When this code is finished, it looks like this:

Now, we will program the true case, which executes when an object is present. The brick LED will turn red and the Timmyton will aggressively charge the obstacle with its chomping jaws and furious tail; program a medium motor block (port A, On, 100 percent power) and a move tank block (port B + C, On, both motors at 100 percent power). After that, place an infrared sensor switch (port 4, Infrared Sensor | Compare | Proximity, threshold value | 8). This switch checks to see if the Timmyton has moved closer to the object (if not, then it is no longer on track to collide with it):

The true case of this switch executes when the Timmyton is very close to the object. The robot will stop its forward progress; drag out a medium motor block and a move tank block, setting both to the Off state. Two move tank blocks will follow, which will get the Timmyton to reverse, turn around, and continue in a new direction. The first block should be set to On for Seconds with both drive motors set to -75 percent power for a duration of 1 second. The second block should be set to On for Degrees with the left drive motor set to 75 percent power and the right motor set to -75 percent power for a duration of 1 second. After this, the Timmyton will continue in a new direction:

The false case of the switch executes if the Timmyton loses sight of the object while charging it. Inside the false case, place yet another IR sensor switch block (port 4, Infrared Sensor | Compare | Proximity, threshold value | 37). In the true case of this switch, insert a move tank block and a medium motor block, both set to Off:

And for the false case? Just leave it blank, which tells the Timmyton to continue charging:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.223.123