Drive Inches With Class

In the last section we showed you how to calibrate your encoders so you could specify distances in inches instead of the arbitrary ticks that the encoders return.  We did computing the inches to ticks ratio and creating a function that does the conversion.  For this program, doing it that way is probably find.  However, the power of C++ gives us another way to approach the problem which we explore in this chapter.

We have, provided for us, a class RobotEncoder which we can use to read an arbitrary tick count which is proportional to the distance the robot moves.  However, what we would really like is an encoder class which returns the distance directly in inches.

As it turns out, C++ will let us create a new class which extends the functionality of the built in RobotEncoder class.  C++ (as well as many other high level languages) has a feature called inheritance whereby you can create a new class that is based on an existing class.  This new class will have all of the functionality of the existing base class, but will also allow us to add new functionality.

To begin, load the SimpleRobotDriveInches example program as our starting point:

Once again, this is the program which we ended up with in the last chapter.

We now want to define our new class, which we will call MyEncoder which will be based on the built in RobotEncoder class.  The first step is to add the framework for the class declaration.  Enter the following code just before the declaration of your MyRobot class:

This is how we create a new class which is based on an existing class.  Our new class is named MyEncoder and it is based on the built in class RobotEncoder.  This new class will have all of the functionality of the RobotEncoder class but will allow us to add additional features.  Note the public declaration right before RobotEncoder.  This is required, if we want users of our class to have access to the methods of the RobotEncoder class (which we do in this case).

We can use our new MyEncoder class by replacing the instances of RobotEncoder in our program.  In the variable declaration section we want to change the following lines:

to:

This declares our encoder pointers to be of our new class. We also will need to change the initialization of those pointers. In our Setup function we need to change the two lines:

to:

When we try and compile this program, however, we find the following error:

What is wrong?  The problem is that we need to pass the port number into the constructor, but our class has not defined any constructors that take any arguments.  So what we need to do is to create a constructor that will allow us to specify the port number for the encoder.

To do this, we add the following code to our MyEncoder class:

Now as we recall, constructors look very much like functions, except they are always named the same as the class and never have a return value specified.  Notice also that we added the public declaration before we defined our constructor.  We need to do this if we wish our constructor to be accessible to users of our class.

Now our constructor take a parameter which will be the port number for the encoder that we will pass in when we create an instance.  Note, however, that we currently not doing anything with this number.  This cannot be right!

If we remember, the constructor for the RobotEncoder class, upon which our class is based, requires us to provide one integer argument which specifies the port.  So what we need to do is to somehow make our constructor pass the encoderNo that it is receiving to the constructor for the base class RobotEncoder.  We do this by changing our constructor as follows:

This tells the compiler that, before executing our constructor, that it should first call the constructor for the RobotEncoder class and pass it the value encoderNo.

At this point we should be able to compile our program, and if we run it, we will find that it works just as it did before.  This is because this new class, MyEncoder, now has exactly the same functionality as the original base class, RobotEncoder.

Now we can start to modify our class to add additional functionality.  What we want to add is a function that will return the distance in inches.  We will declare this function as follows:

This code should be added right after the MyEncoder constructor that we just created.

The body of our function is very simple.  First we call the GetPosition function of the RobotEncoder base class, which returns the distance in ticks.  We then multiply this number by the scale factor we computed in the previous section (i.e. 0.008) and return the result.  Note that we do not need any qualifiers to access the GetPosition function of RobotEncoder. This is because our class inherits from that class and thus has access to all (non private) functions and variables.  Also note that when we declared our GetDistanceInches function we had it return a type float instead of integer, because we wish to be able to measure the distance in fractions of an inch rather than just whole numbers.

Now the only thing left is to use our new function to measure the distance.  We do this by changing the following line in our Loop function:

to:

We can also delete the GetDistanceInches function that we created in our MyRobot class, since we will no longer be needing it.

If we now compile and run our program, we once again should find that the robot drives straight for 20 inches.

Before we move on to the next chapter we need to clean up a few things.  If we look at our code, we once again see that we have an arbitrary number (i.e. 0.008) in the middle of our code.  Like before, we want to move this to a variable and initialize it to our 0.008 value.  In addition, to make this class more versatile, we would like a way to change the scale value to accommodate robots with different wheel sizes.  To this effect we will add a second function SetScale which will allow us to do this.  Taking these changes into account, our MyEncoder class should now look like:

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.