Ravens Robotics FRC 2015
MecanumDrive.cpp
Go to the documentation of this file.
1 
10 #include <MecanumDrive.h>
11 
13 {
14  m_Drive = NULL;
15  m_DriveStick = NULL;
16  m_ValueX = m_ValueY = m_ValueRotate = NULL;
17  m_Gyro = NULL;
18  m_GyroButton = NULL;
19  m_GyroButtonIsPressed = false;
20  m_gyroEnabled = false;
21  m_gyroAngle = kNoGyro;
22  m_SmoothAccel = false;
23 }
24 
26  // Delete allocated member objects
27  delete m_Drive; m_Drive = NULL;
28  delete m_DriveStick; m_DriveStick = NULL;
29  delete m_ValueX; m_ValueX = NULL;
30  delete m_ValueY; m_ValueY = NULL;
31  delete m_ValueRotate; m_ValueRotate = NULL;
32  delete m_Gyro; m_Gyro = NULL;
33  delete m_GyroButton; m_GyroButton = NULL;
34 }
35 
36 /*
37  * Called once, to perform initialization that isn't appropriate for constructor.
38  */
40  // Initialize Drive w/ motor safety
41  if (!m_Drive) m_Drive = new RobotDrive (kFrontLeftCh, kBackLeftCh, kFrontRightCh, kBackRightCh);
42  m_Drive->SetExpiration(kSafetyTimeout);
43  m_Drive->SetSafetyEnabled(true);
44 
45  // Initialize Controller
46  if (!m_DriveStick) m_DriveStick = new Joystick(kDriveStickCh);
47 
48  // Initialize SmoothAxis es. SmoothAxes? Whatever.
49  if (!m_ValueX) m_ValueX = new SmoothAxis();
50  if (!m_ValueY) m_ValueY = new SmoothAxis();
52 
53  // Initialize Gyroscope
54  if (!m_Gyro) m_Gyro = new Gyro(kGyroCh);
55 
56  // Initialize Gyro Button
57  if (!m_GyroButton) m_GyroButton = new JoystickButton(m_DriveStick,kGyroButton);
58  m_GyroButtonIsPressed = false;
59 
60  SetSmoothAccel(m_SmoothAccel); // Delegate initialization of SmoothAxiseseses
61 }
62 
63 /*
64  * Component-specific sensory input collector.
65  * Read joystick positions and button states.
66  * TODO: support zero-calibration of controller.
67  */
69  m_ValueX->SetTarget( m_DriveStick->GetX(GenericHID::kRightHand) );
70  m_ValueY->SetTarget( m_DriveStick->GetY(GenericHID::kRightHand) );
71  m_ValueRotate->SetTarget( m_DriveStick->GetX(GenericHID::kLeftHand) );
72 
73  /*
74  * Read GyroButton. Simple logic to debounce the button (register
75  * a change of state just once) by acting the off-to-on transition
76  * only.
77  *
78  * It would have been nice to use the WhenPressed() method, but
79  * that would require us to implement a Command for this operation,
80  * which is not worth the effort.
81  */
82  if (m_GyroButton->Get()) {
83  // Button is pressed - Check if this is the off-to-on transition.
84  if (!m_GyroButtonIsPressed) {
85  // This is it!
86  m_GyroButtonIsPressed = true;
87  // Toggle enablement
89  }
90  }
91  else {
92  // Button isn't pressed.
93  m_GyroButtonIsPressed = false;
94  }
95 
96  // Read gyroscope, if enabled.
97  m_gyroAngle = isGyroEnabled() ? m_Gyro->GetAngle() : kNoGyro;
98 }
99 
100 /*
101  * Component-specific worker function.
102  */
104  m_Drive->MecanumDrive_Cartesian(m_ValueY->GetValue(),
106 }
107 
108 /*
109  * Set Gyroscope enablement.
110  */
111 void MecanumDrive::setGyroEnabled(bool gyroEnabled) {
112  // Documentation for the built-in gyro states that the gyro object should be instantiated
113  // from the constructor of the Robot class, to allow for calibration while stationary.
114  // However, this conflicts with other instructions stating that the hardware may not be
115  // ready for operation at that time, and recommending that objects be instantiated in the
116  // RobotInit function. So, to be safe, we instantiate in RobotInit, but ensure that we
117  // calibrate when enabling use of the gyro. There you go.
118  m_Gyro->Reset(); // calibrate a new 0 heading
119  m_gyroEnabled = gyroEnabled;
120 }
121 
122 /*
123  * Set Smooth Acceleration feature.
124  */
125 void MecanumDrive::SetSmoothAccel(bool enable) {
126  m_SmoothAccel = enable;
127  m_ValueX->setEnabled(enable);
128  m_ValueY->setEnabled(enable);
129  m_ValueRotate->setEnabled(enable);
130  // Todo: report state on display.
131 }
virtual ~MecanumDrive()
virtual void SetSmoothAccel(bool enable)
Joystick * m_DriveStick
joystick controller
Definition: MecanumDrive.h:116
bool isGyroEnabled() const
Definition: MecanumDrive.h:100
void setGyroEnabled(bool gyroEnabled)
SmoothAxis * m_ValueX
left/right speed [-1.0..1.0]
Definition: MecanumDrive.h:118
void setEnabled(bool enabled)
Definition: SmoothAxis.cpp:67
SmoothAxis * m_ValueRotate
Rate of rotation [-1.0..1.0].
Definition: MecanumDrive.h:118
bool m_SmoothAccel
State of smooth acceleration capability.
Definition: MecanumDrive.h:126
float m_gyroAngle
Gyroscope angle measurement.
Definition: MecanumDrive.h:125
Header file for MecanumDrive component.
Gyro * m_Gyro
Gyroscope instance to measure robot's heading.
Definition: MecanumDrive.h:121
SmoothAxis * m_ValueY
fwd/bkwd speed, inverted [-1.0..1.0]
Definition: MecanumDrive.h:118
virtual void ComponentInit()
JoystickButton * m_GyroButton
Button to dis/enable gyro.
Definition: MecanumDrive.h:122
virtual void Sense()
bool m_gyroEnabled
Gyroscope enablement.
Definition: MecanumDrive.h:124
void SetTarget(float newTarget)
Definition: SmoothAxis.cpp:30
float GetValue()
Definition: SmoothAxis.cpp:47
bool m_GyroButtonIsPressed
Button state.
Definition: MecanumDrive.h:123
virtual void DoWork()
RobotDrive * m_Drive
drive instance
Definition: MecanumDrive.h:117