Build a Self Driving Car With the GoPiGo3

5. Drive to a Destination

In this step, we will program our self driving car to drive around the inside box on the example course, non-stop, forever. This doesn't seem very useful, but what we're going to do is simplify some code with a function. This will make it easier to navigate later on.

Make a Function

Let's take the code from the previous step and make a function. A function is a set of programming blocks that we are going to call over and over. How does it help? If we're going to do something multiple times, it will save us time: we don't have to write the same thing over and over, we just call the set of commands a name, and then tell the robot to do that over and over again.

You can find Function blocks in Advanced Bloxter.

In this example, we will call the function "GoToTurn" and we will use the blocks we had in the previous step that took us to the turn. Place the "Follow the line until" and the "line follower reads all black" in the function block. Then tell the robot to drive forward 7 cm. We won't put the turn block into this: sometimes we may want to turn left, and sometimes we may want to turn right. We might also want to drive straight through an intersection. So what we do after we get to the intersection should go outside our function.

Tie It All Together

The code above will drive the GoPiGo around the town square we have in our picture. Make a function and give it a try. Though a self driving car that drives in squares (or circles) isn't very useful, we are getting to something that is!