# Getting the distance right

We now have a robot that is capable of rotating to the correct angle for the shot, but we still need to add code that will cause it do move to the correct distance.  When we do this, we are going to assume that the robot is roughly aligned with the tower, but needs to be moved and rotated into the proper position.

In our AutoShootCommand class, we need to add a new state Drive which we will use to drive the robot to the correct distance from the tower.  When this state ends, it will transition to the TurnLeft or TurnRight states, and from their to eventually making the shot.

Currently in the initialize() class we call the StartTurn function to start the robot turning toward the target.  Instead, we now want to set the state to our new Drive state as follows:

We must now add a new case in our execute() function to handle this new state:

The new Driving function will handle the processing for this new state and is defined as follows:

Let’s take a look at this function in detail.  The first thing we do is compute the top position of our target (topPos).  Note that we are not using the top of the bounding box for this purpose, but rather the average of the upper left and upper right corners.  We then compare this to the current position of the horizontal target line.  Now we could stop when the topPos is less than the horizontal target line (data.m_targetHorzPos) but as we discovered with the turning, this will cause our robot to overshoot the target by a small amount.  Thus we add k_driveLead to the target line and stop when that is reached.  The robot will then coast a little farther.  The constant k_driveLead is defined as follows:

If we have not yet reached the position that we need, then we need to set the speed of the motors.  We want to adjust the speed based on how far we currently are from the target.  The following line computes this speed by taking the difference between our topPos and the horizontal target line.  This number is multiplied by the scale factor k_speedScale to give us a power value:

The constant k_speedScale is defined as follows:

After we compute this power we make sure that the value is between some minimum (k_minSpeed) and some maximum (k_maxSpeed) speed to make sure we don’t go either too fast or too slow.  These constants are defined as:

Finally, as long as we are driving, we might as well try and adjust the angle at the same time.  This is what the following two lines try and do:

The first line computes the difference between our target center (centerPos) and the vertical target line.  We then multiply this value by the scale factor k_turnScale to obtain a number which we add to the left motor and subtract from the right motor which will make the robot turn slightly in the correct direction.  The constant k_turnScale  is defined as:

Your AutoShootCommand.java file should now look like:

When you now deploy and run your program, you will find that all you need to do is position the robot so it is roughly aligned with the tower and it will drive and shoot automatically when you press button 3 on the joystick:

Next: Navigation – Calibarte Distance

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.