QuarterbackTurret.h#
Description#
This file defines the following enums:
Enum
States
TurretMode
manual, automatic, combine
TurretUnits
degrees, counts
AssemblyAngle
straight, angled, unknownAngle
CradleState
forward, back
TargetReceiver
receiver_1, receiver_2
CombinePosition
combineLeft, combineStraight, combineRight
FlywheelSpeed
slow_inwards, stopped, slow_outwards, lvl1_outwards, lvl2_outwards, lvl3_outwards, maximum
This file also defines the following constants:
Turret Speed Constants:
Constant
Value
QB_TURRET_NUM_SPEEDS
7
flywheelSpeeds
{-0.1, 0, 0.1, 0.215, 0.31, 0.3875, 1.0}
Debounce and Delay Constants:
Constant
Value
QB_BASE_DEBOUNCE_DELAY
50L
QB_ASSEMBLY_TILT_DELAY
200L
QB_CRADLE_TRAVEL_DELAY
750L
QB_CIRCLE_HOLD_DELAY
750L
QB_TRIANGLE_HOLD_DELAY
200L
QB_CROSS_HOLD_DELAY
200L
QB_TURRET_INTERPOLATION_DELAY
5L
QB_TURRET_THRESHOLD
35
QB_TURRET_STICK_SCALE_FACTOR
0.15
Speed Constants:
Constant
Value
QB_MIN_PWM_VALUE
0.1
QB_HOME_PCT
0.125
QB_HANDOFF
0.3
QB_HOME_MAG
0.1
QB_ASM_SPEED
0.3
Turret Angle Calculation Constants:
Constant
Value
QB_COUNTS_PER_ENCODER_REV
1250
QB_COUNTS_PER_TURRET_REV
10556
QB_COUNTS_PER_TURRET_DEGREE
29.321
QB_TURRET_SLOP_COUNTS
540
Turret Homing Constants:
Constant
Value
QB_TURRET_STOP_LOOP_DELAY_MS
10
QB_TURRET_STOP_THRESHOLD_MS
500
QB_TURRET_HOME_STOP_FACTOR
0
QB_TURRET_MANUAL_CONTROL_FACTOR
4
Turret PID Controller Constants:
Constant
Value
QB_TURRET_PID_THRESHOLD
3
QB_TURRET_PID_MIN_DELTA_T
5
QB_TURRET_PID_MAX_DELTA_T
25
QB_TURRET_PID_BAD_DELTA_T
250
QB_NORTH_OFFSET
0
QB_AUTO_ENABLED
false
UART Communication Pins:
Constant
Value
Notes
RX2
16
Reciever Pin
TX2
17
Transmitter Pin
In addition, this file creates the QuarterbackTurret class, featuring relevant variable declarations and function prototypes. More information about these functions can be found in the documentation for QuarterbackTurret.cpp.
Included Headers#
Robot/Robot.hRobot/MotorControl.hps5Controller.hUtilities/Debouncer.hAdafruit_LIS3MDL.hHardwareSerial.hADXL335.h
Enums#
Enum |
Definitions |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
Combine Specific#
Enum |
Definitions |
|---|---|
|
|
Defined Variables#
Turret Setup#
QB_TURRET_NUM_SPEEDS7
FlywheelSpeedsAccepts
QB_TURRET_SPEEDSas the array length-0.1, 0, 0.1, 0.3, 0.5, 0.7, 1.0
Debounce & Miscellaneous Delay Constants#
Name |
Value |
isLong? |
|---|---|---|
|
50 |
✓ |
|
750 |
✓ |
|
750 |
✓ |
|
200 |
✓ |
|
200 |
✓ |
|
5 |
✓ |
|
35 |
X |
|
0.25 |
X |
Speed Constants#
Name |
Value |
|---|---|
|
0.08 |
|
0.125 |
|
0.3 |
|
0.1 |
Turret Angle Calculation Constants#
QB_COUNTS_PER_ENCODER_REVNumber of ticks per encoder revolution
1000
QB_COUNTS_PER_TURRET_REVThe ratio is 27:1 for the falcon to turret, and the ratio is 5:1 for the falcon to the encoder (for a 12t driving sprocket on 60t gear)
Encoder spins 5.4 times (or 27/5) for every turret revolution equates for 5400 ticks per rev
5400
QB_COUNTS_PER_TURRET_DEGREE15 ticks per degree (5400 / 360)
15
QB_TURRET_SLOP_COUNTSDue to the high levels of backlash between the input and output, there are 540 ticks used in the encoder to add a delay before the turret actually starts to move. This prevents problems when switching directions.
540
Turret Homing Constants#
QB_TURRET_STOP_LOOP_DELAY_MS*ASSUMED to be a delay (measured in milliseconds) between iterations of the turret’s move loop
10
QB_TURRET_STOP_THRESHOLD_MSMUST be a multiple of
QB_TURRET_STOP_LOOP_DELAY_MS500
QB_TURRET_HOME_STOP_FACTORA correction constant for homing,
*ASSUMED to be multiplied into the stop counts in the final homing algorithm
0.5
QB_TURRET_MANUAL_CONTROL_FACTORHigher values equate to less sensitivity during manual control (Basically dividing the clock of the loop)
4
Turret PID Controller Constants#
QB_TURRET_PID_THRESHOLDThe acceptable error in the position control (in degrees) that will zero the error constants / build up
3
QB_TURRET_PID_MIN_DELTA_T*ASSUMED to be a failsafe, if the PID error values are not updated fast enough, such as if the controller hangs for half a second, the PWM values will be massive compared to what they should be
5
QB_TURRET_PID_MAX_DELTA_TThe maximum time between updates
QB_TURRET_PID_BAD_DELTA_T*ASSUMED to be the maximum time before the turret needs to be zeroed out
250
QB_NORTH_OFFSETDeals with the problems of zeroing the turret but then holding a set angle afterwards
NOTE: May be obsolete now
0
QB_AUTO_ENABLEDEnables or disables Auto Mode. Used for testing
False
UART Communication Pins#
Pin |
Name |
Value |
|---|---|---|
|
Reciever Pin |
16 |
|
Transmitter Pin |
17 |
Classes#
QuarterbackTurret#
Private#
As is the case for most of these variables, they are initiated in a .h file, and instanciated in the corresponding .cpp file.
MotorControl Instances
Taken from the MotorControl file
cradleActuatorturretMotorassemblyMotorflywheelLeftMotorflywheelRightMotor
Pin Declartion
Name |
Type |
Value |
Modifyable? |
|---|---|---|---|
|
Unsigned 8-bit int |
defined in |
X |
|
Unsigned 8-bit integer |
defined in |
X |
|
Unsigned 8-bit integer |
defined in |
✓ |
Joystick Inputs
Name |
Type |
Value |
Modifyable? |
|---|---|---|---|
|
float |
defined in |
✓ |
|
float |
defined in |
✓ |
Note
Note: Write the purpose found in .h when doccumenting .cpp for both of the above*
Autonomous Targeting
Name |
Enum |
Value |
Modifyable? |
|---|---|---|---|
|
|
✓ |
|
|
|
|
✓ |
|
|
✓ |
Note
Note: These derive from the enums earlier in this doc*
Setup & Status variables
Name |
Type |
Value |
Modifyable? |
|---|---|---|---|
|
bool |
True or False |
✓ |
|
bool |
True or False |
✓ |
|
bool |
True or False |
✓ |
Note
Note: The definitions for these items will be included in the .cpp file for convenience sake*
Assembly Movement
The definitions for these items will be included in the .cpp file for convenience
Again, these are enums, not variables
Note
Continue to create a table for next time here.
~ PW
Name |
Enum |
Value |
Modifyable? |
|---|---|---|---|
|
|
currentAssemblyAngleEnum:
AssemblyAngleValue:
Straight,AngleModifyable: YES
targetAssemblyAngleEnum:
AssemblyAngleValue:
Straight,AngleModifyable: YES
assemblyMovingType: boolean
Value: True or False
Modifyable: YES
assemblyTriggerToggledType: boolean
Value: True or False
Modifyable: YES
assemblyStartTimeType: unsigned 8-bit integer
Value: Defined in
QuarterbackTurret.cppModifyable: YES
Ball Cradle State Variables
Again, some of these are enums, the ones that have enums are denoted
currentCradleStateEnum:
CradleStateValue:
Forward,BackModifyable: YES
targetCradleStateEnum:
CradleStateValue:
Forward,BackModifyable: YES
cradleMovingType: boolean
Value: True or False
Modifyable: YES
cradleStartTimeType: unsigned 32-bit integer
Value: defined in QuarterbackTurret.cpp
Modifyable: YES
Flywheel State Variables
currentFlywheelStageEnum:
FlywheelSpeedValue: Defined in QuarterbackTurret.cpp
Modifyable: YES
targetFlywheelStageEnum:
FlywheelSpeedValue: Defined in QuarterbackTurret.cpp
Modifyable: YES
currentFlywheelSpeedType:
floatValue: Defined in QuarterbackTurret.cpp
Modifyble: YES
flywheelManualOverrideType: boolean
Value: True or False
Modifyable: YES
Turret State Variables
currentTurretSpeedType: float
Value: Defined in QuarterbackTurret.cpp
Modifyable: YES
targetTurretSpeedType: float
utmsCtrType: Signed 8-bit Integer
Value: 0
Modifyable: YES
UTMS_CTR_MAXType:
defineValue: 15
Modifyable: NO
Private Encoder State Variables
targetTurretEncorderCountType: Signed 8-bit Integer
Value: Defined in QuarterbackTurret.cpp
Modifyable: YES
errorEncoderCountType: Signed 8-bit Integer
Value: Defined in QuarterbackTurret.cpp
Modifyable: YES
slopErrorType: Signed 8-bit Integer
Value: Defined in QuarterbackTurret.cpp
Modifyable: YES
stopErrorType: Signed 8-bit Integer
Value: Defined in QuarterbackTurret.cpp
Modifyable: YES
manualHeadingIncrementCountType: Unsighed 8-bit Integer
Value: Defined in QuarterbackTurret.cpp
Modifyable: YES
turretMovingType:
trueorfalseValue: Boolean
Modifyable: YES
Robot Relative Headings
curretRelativeHeadingType: Signed 16-bit Integer
Value: Defined in QuarterbackTurret.cpp
Modifyable: YES
targetRelativeHeadingType: Signed 16-bit Integer
Value: Defined in QuarterbackTurret.cpp
Modifyable: YES
curretnRelativeTurretCountType: Signed 32-bit Integer
Value: Defined in QuarterbackTurret.cpp
Modifyable: YES
Absolute (World Relative) Headings
currentAbsoluteHeadingType: Signed 16-bit Integer
Value: Defined in QuarterbackTurret.cpp
Modifyable: YES
targetAbsoluteHeadingType: Signed 16-bit Integer
Value: Defined in QuarterbackTurret.cpp
Modifyable: YES
turretLaserStateType: Unsigned 8-bit Integer
Value: Defined in QuarterbackTurret.cpp
Modifyable: YES
Control Input Debouncers
Note: All of these are instances of the
DebouncerclassdbSharedbOptionsdbSquaredbDpadUpdbDpadDowndbDpadUpdbDpadDowndbDpadLeftdbDpadRight
Macro Button Debouncer
dbCircledbTriangledbCrossdbTurretInterpolator
Magnometer#
Setting up the Magnometer
lis3mdlDescription: This is the instanciation of the magnometer referenced in the code. It allows for one to access information from the
Adafruit_LIS3MDL.hclass.Parent Class:
Adafruit_LIS4MDL.h
useMagnetometerDescription: A variable that allows for one to enable/disable the magnetometer at will.
Type:
boolean
holdTurretStillEnabledDescription: A variable that allows one to use the magnetomer for only the handoff and not the holding code
Type:
boolean
Calibrating the Magnetometer
mag_yVal,mag_xValDescription: The current x and y values read by the magnetometer (after correcting for errors)
Type:
intDefault Value: 0
mag_xMax,mag_xMinDescription: The maximum and minimum values for
xrecorded when calibrating the magnometer & stores in their respective variablesType:
intDefault Value: -1000000 & 1000000
mag_yMax,mag_yMinDescription: The maximum and minimum values for
yare recorded when calibrating the magnometer & stores in their respective variablesType:
intDefault Value: -1000000 & 1000000
mag_xHalf,mag_yHalfDescription: Half of the the total range of expected the respective
xandyvalues. Used to shift the their respective values back to the origin (0,0)Type:
intDefault Value: 0
mag_xSign,mag_ySignDescription: Used to determine whether to add the respective
halfvalue or subtract it when shifting the values back to the origin. If the value istrue, the it subtracts, if not, it adds them.Type:
booleanDefault Value:
false
northHeadingDegreesDescription: An offset variable. Its purpose is to re-align the magnetometer due to errors in calculating the proper direction (caused by interference from the motors & other mechanical devices nearby). Right now it is only partially working
Type:
integerDefault Value: 0
headingRadDescription: The current calculated heading in radians using the
xandyvalues after calibrationType: Float
Default Value: N/A
headingDegDescription: The current calculated heading in degrees. Converts from
headingRadType: Float
Default Value: N/A
PID Variables
Used to correct offsets calculated by the code that need to account for reality
PID_ERROR_AVG_ARRAY_LENGTHType: DEFINE
Description: The length of the array used for averaging the error
Value: 5
prevErrorValsType: Integer Array
Description: An array of previously measured error values; used to average the collected error values and offset the random spikes in the magnetometer
Length:
PID_ERROR_AVG_ARRAY_LENGTHValues: {0, 0, 0, 0, 0}
prevErrorIndexType: Integer
Description: Used to update the index accessed from the
prevErrorVals, allows each index to be accessed over timeDefault Value: 0
previousTimeType: Long
Description: Used to calculate errors and deltaT (in respect to the PID function)
ePreviousType: Float
Description: The percent error from the magnetometer builds over time - this is used to record the previous and current error values
Default Value: 0
eIntegralType: Float
Description: The current error integral calculated and added to
ePreviousin the PID loopDefault Value: 0
kpType: Float
Description: The proportional gain used in PID calculations
Default Value: 0.005
kiType: Float
Description: The integral gain used in PID calculations
Default Value: 0.0012
kdType: Float
Description: The derivative gain used in PID calculations
Default Value: 0.0
turretPIDSpeedType: Float
Description: The calculated PWM used in the PID loop
Default Value: 0
minMagSpeedType: Float
Description: Sets a minimum bound on the PWM signal to prevent errors in the PID loop. PWM signals below this range usually fail to make the motor turn and throw off the PID calculations.
Defualt Value: 0.075
Magnetometer Functions#
magnetometerSetup()
As defined in
QuarterbackTurret.cpp:First checks to see if the LIS3MDL has been identified or not.
If not, it sets the sensitivity, operation mode, rate of data transmission, and the error range of the magnetic field detection to a series of pre-defined values. Finally, it enables certain features of the LIS3MDL. Runs once on startup.
Type:
voidcalibMagnetometer()
As defined in
QuarterbackTurret.cpp:First rotates the turret 360 degrees and calibrates how to calculate movement given input from the LIS3MDL. Used to make sure that the data is processed properly and that the outputs are to the correct angles.
Type:
voidcalculateHeadingMag()
As defined in
QuarterbackTurret.cppRun inside of the
calibMagnetometer()function. Uses the values calculated in the beginning ofcalibMangetometer()to determine the current angle of the turret, corrects and verifies that the ranges ofxandyare legal. It then calculates the offset and integrates it into the measurement from the magnetometer.
Type:
void