Combining Commands

Now that we have commands to drive and turn let’s see how we can combine them to create more complex commands. Let’s use this class to create a set of commands that will drive the robot around a certain path.  The first step is to create a new command called DriveCourseCommand. This time instead of copying the ExampleCommand we are going to make this new class inherit from CommandGroup instead of Command. Instead of copying this command, right click on the commands folder and choose New/Class. Then enter DriveCourseCommand as the class name:


Now copy the following code to form the basis of this new class:

Note that for this type of command, we inherit from CommandGroup. Also we do not have the normal command functions (e.g. initialize, execute, etc). This is because this command will be composed of a set of composite commands.

For example, if we want to drive forward 20 inches and then turn right by 90 degrees, we would add the following lines to the constructor:

Notice how I placed a PauseCommand between the DriveForDistanceCommand and TurnCommand. Doing so will increase the accuracy of the turns. Transitioning directly from driving to turning can cause errors like wheel slippage which will throw of our turn calculations. I am also using variables turn90 and speed to store the turn angle and speed. I am doing this since we will want to add multiple of these commands to get the robot to drive our course, and I want to be able to adjust the turn angle and speed for all of the commands in one place.

We can then modify our file so that we run this new command when we press our test button 2.

Now deploy and run your program and verify that it drives forward 20 inches, turns right and then stops.

Now let’s change your program so that the robot will drive in a rectangular pattern 20 inches by 10 inches. In principle this should bring your robot back to the starting position. After you have completed and tested your code, compare it to my solution below.







You will probably find that it was very hard to set the speed and turn parameters correctly to bring the robot back to the exact starting position. In fact, you probably saw that running the same program twice in a row actually produced a slightly different outcome. This is because there are a number of things that go on which introduce slight errors (such as wheel slippage), and these errors accumulate over time and are different every time. You might find that if you slow the robot down, you can get more reproducible results, but you can never reach the point where it is always spot on.

The important thing to take away from this is that this kind of ‘dead reckoning’ navigation is fraught with problems. The only way to overcome these problems is to use sensors (e.g. gyro, distance sensor, camera, etc.) on the robot that give you a way to determine it’s absolute position and orientation by observing and using the environment around it.

Next: Using Sensors

Pierre Pierre, who replica handbag 185cm tall, gucci replica handbags the "Diamond King" in his hermes replica handbags . He has been selected as one of the handbag replica most beautiful 50 people in the "People" magazine. It is also known as replica handbags most elegant and quiet in the world. Unforgettable prince.