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 Main Arduino Board so you should use the following constructor:
robotCore.PWMMotor.PWMMotor( int pwmPin, int dirPin )
You will, of course, need the pin numbers:
private static final int k_PWMPin = 6; private static final int k_DirPin = 7;
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:
robotCore.DigitalInput.DigitalInput(int pin)
The pin numbers to use are:
private static final int k_LeftLimit = 11; private static final int k_rightLimit = 12;
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:
checkLimit() { power = stop motor if (Turning left) { if (Left limit is not pressed) { power = current power } } else if (Turning right) { if (Right limit is not activated) { power = current power } } Set motor to power }
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.