Fine tuning the alignment

So far, we have the robot aligning itself fairly closely to the target, but not quite close enough.  After we have finished our first turn, we need to wait a short time to let the robot stop moving and then check the target again.  If it is still not close enough, we need to try and nudge the robot slightly to move it closer. One of the problems with trying to rotate the robot into position is that it takes a fair amount of power to overcome the static friction, and once that has happened, the power level is too much for the remaining dynamic friction and the robot turns too far before we can stop it.

What we are going to do is initiate a turn at a fairly low speed, and then as soon as we see the robot move at all (according to the wheel encoders), we are going to cut the power so that it only turns by a very small amount.

To accomplish this we will need three new states, NudgeLeft, NudgeRight, and Pause as follows:

Now when the first turn is complete, we need to pause for a short time to allow the robot to stop and then enter one of the Nudge states.  To start the nudge state we will create the following function

First we check to see if the target is within the acceptable shooting angle, and if it is, set the state to Done.  If the angle is too great, then we determine which direction we need to turn and start turning at the define k_nudgeSpeed which is defined as follows:

In order to determine when the robot has moved, we need to set m_nudgePos to the current position.  We do this by calling the following function:

Here we are returning the difference between the left and right encoders.  Remember that we are turning the robot, so the encoders will be changing it opposite directions.

Next we need to change the TurningLeft and TurningRight functions so that instead of setting the state to Done they call the StartNudge function:

The Pause() function is used to enter the Pause state and is defined as follows:

This function resets the pause timer and sets the state.  The pause timer needs to be declared as follows:

Now we need to add cases to the execute() function to handle the three new states:

Once again, rather than handling these states with inline code, we call functions which are defined as follows:

For the two Nudge states we need to check to see if the robot has moved.  We compute absolute value of the difference between the starting nudge position and the current nudge position, and if it is greater than k_nudgeAmount, then the robot has moved.  At this point, we need to once again pause and then check to see if we are close enough to the target angle, or whether we need to Nudge the robot once again.  The constant k_nudgeAmount is declared as follows:

When in the Pause state, the Pausing function will check the timer to see if the elapsed time is greater than k_pauseTime, and if it is, start the nudgging process.  The constant k_pauseTime is declared as:

Note that the program will keep switching back and forth between the Nudge states and the Pause state until the robot is at the proper shooting angle.

Your function should now look like

Now deploy and run the program and test it out.  Pressing button 3 on the joystick should now cause the robot to rotate to the correct angle for a shot.


Here we can see that we are nicely aligned for the shot.

Next: Making the shot

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.