Turret Subsystem

Our final subsystem is used to control the motor that drives the Turret. The turret is designed so that it can rotate through an angle of 180 degrees with hard stops on each end of that rotation. There are also limit switches on the ends that will allow us to turn the motor off if an attempt is made to move the turret beyond the hard stops.

The turret motor is connected to the Auxiliary Arduino Board so you should use the following constructor:

With pin numbers and i2c address:

We will also need a way to read the limit switches and we will use the DigitalInput class for this. You will need to create two instances of this class, one for the left limit switch and one for the right. You should use the following constructor:

Using pin:

Note, the way the robot is wired, the get() function of the DigitalInput class will return true if the limit switch is activated.

Now you will need to stop the motor when the turntable hits the limit switches. You could do this in the various commands that we create which use the Turret subsystem, but this would require us to duplicate our code in multiple places. It would also not be very secure since if we forgot to do the check in a command, the motor would not turn off when the turret hit the hard stop.

What we really want is for the Turret subsystem to automatically handle the limit switches so that we don’t have to do this everywhere we use it. To accomplish this you should create a function which checks the limit switches. In that function, you should turn the motors off if the motor is turning the turret in the direction of a given limit switch and that limit switch is active. Here is some ‘pseudo’ code which shows you how such a function might be constructed:

Then for our setPower() function, we should set the ‘current power’ to the power that is passed in and then call our checkLimit() function. We should also call the checkLimit() function from the periodic() function to ensure that once we start the turret rotating it will automatically stop once the limit switch is active.

Once you have completed this subsystem, create a Turntable command which will rotate the turntable at a constant power either left or right. Then create two instances of this command and tie them to two buttons, one that turns the turntable left and the other that turns it right while the buttons are held.

Next: Vision Tracking