Chapter 7. Advanced Programming

In this chapter, we will explore some more advanced algorithms with a focus on what we need for navigation. This will be particularly useful for programming your robot to navigate an obstacle course or play a complex game, such as FIRST Lego League. In this chapter, we will cover how to:

  • Use Loops and Switches to drive in a square
  • Use Loops and Switches to avoid an obstacle
  • Navigate using Motor Rotation sensor feedback
  • Navigate using Gyro Sensor feedback
  • Simplify our program using My Blocks
  • Simplify our program using Arrays

Using any of our four base robots, we will make the robot go forward, make a 90 degree turn, go forward, and repeat this sequence several times. In this chapter, I have included programs using both the Gyro Sensor and a process of navigation called dead reckoning. Dead reckoning, which is short for deduced reckoning, consists of calculating how far the wheels of your robot should turn by using the built-in shaft encoders (Motor Rotation sensors) on the wheels. For the dead reckoning programs, I used the skid-bot from Chapter 3, Drive Train and Chassis.

Loop and the Motor Rotation sensors

Let's start by using dead reckoning to drive in a square. As you can see in the following screenshot, we will first use a Loop block from the Flow Control palette and select the Count mode for the Loop block. We can have the robot repeat this turning sequence several times. Inside this loop, the Move Steering block will tell the robot to move forward for one rotation of the wheels. We will then have a Move Steering block that will turn the robot. Remember that a 500-degree turn of the motor shaft is not equivalent to a 500-degree turn of the entire robot.

This is a 500-degree turn of the motor shaft and the wheels, which I chose based on the design of the skid-bot. Depending on which robot design you chose to use, you will need to vary the value that the rotation sensor uses to trigger the stop of the turning motion to approximate a 90-degree turn. If you use any of the other robots from Chapter 3, Drive Train and Chassis (such as the caster-bot or the tread-bots) you will have to change the values for the rotation of the motor shaft. You could base this value on the diameter of the wheels you are using. Alternatively, you could determine the relationship between the distance and the turning of the wheel by using Port View and a ruler.

Loop and the Motor Rotation sensors

Try running this program. As you can see, it takes patience and some trial and error to get the robot to turn close to 90 degrees. Additionally, the amount that the robot turns can vary with the surface, especially when using treads. If you add several appendages to your robot, changing the center of mass of your robot can also affect how the robot turns.

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

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