Skip to content

Commit 87b5013

Browse files
authored
Fix Gyro code examples (#1725)
Invert right side of drive Reduce P in turn to angle to a more reasonable value Fix sign so the turn to angle actually turns
1 parent ecda95c commit 87b5013

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

source/docs/software/hardware-apis/sensors/gyros-software.rst

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ The following example shows how to stabilize heading using a simple P loop close
191191

192192
DifferentialDrive drive = new DifferentialDrive(leftMotors, rightMotors);
193193

194+
@Override
195+
public void robotInit() {
196+
rightMotors.setInverted(true);
197+
}
198+
194199
@Override
195200
public void autonomousPeriodic() {
196201
// Setpoint is implicitly 0, since we don't want the heading to change
@@ -218,6 +223,10 @@ The following example shows how to stabilize heading using a simple P loop close
218223

219224
frc::DifferentialDrive drive{leftMotors, rightMotors};
220225

226+
void Robot::RobotInit() {
227+
rightMotors.SetInverted(true);
228+
}
229+
221230
void Robot::AutonomousPeriodic() {
222231
// Setpoint is implicitly 0, since we don't want the heading to change
223232
double error = -gyro.GetRate();
@@ -257,6 +266,11 @@ The following example shows how to stabilize heading using a simple P loop close
257266

258267
DifferentialDrive drive = new DifferentialDrive(leftMotors, rightMotors);
259268

269+
@Override
270+
public void robotInit() {
271+
rightMotors.setInverted(true);
272+
}
273+
260274
@Override
261275
public void autonomousInit() {
262276
// Set setpoint to current heading at start of auto
@@ -292,6 +306,10 @@ The following example shows how to stabilize heading using a simple P loop close
292306

293307
frc::DifferentialDrive drive{leftMotors, rightMotors};
294308

309+
void Robot::RobotInit() {
310+
rightMotors.SetInverted(true);
311+
}
312+
295313
void Robot::AutonomousInit() {
296314
// Set setpoint to current heading at start of auto
297315
heading = gyro.GetAngle();
@@ -320,7 +338,7 @@ Much like with heading stabilization, this is often accomplished with a PID loop
320338
// Use gyro declaration from above here
321339

322340
// The gain for a simple P loop
323-
double kP = 1;
341+
double kP = 0.05;
324342

325343
// Initialize motor controllers and drive
326344
Spark left1 = new Spark(0);
@@ -334,21 +352,26 @@ Much like with heading stabilization, this is often accomplished with a PID loop
334352

335353
DifferentialDrive drive = new DifferentialDrive(leftMotors, rightMotors);
336354

355+
@Override
356+
public void robotInit() {
357+
rightMotors.setInverted(true);
358+
}
359+
337360
@Override
338361
public void autonomousPeriodic() {
339362
// Find the heading error; setpoint is 90
340363
double error = 90 - gyro.getAngle();
341364

342365
// Turns the robot to face the desired direction
343-
drive.tankDrive(kP * error, kP * error);
366+
drive.tankDrive(kP * error, -kP * error);
344367
}
345368

346369
.. code-tab:: c++
347370

348371
// Use gyro declaration from above here
349372

350373
// The gain for a simple P loop
351-
double kP = 1;
374+
double kP = 0.05;
352375

353376
// Initialize motor controllers and drive
354377
frc::Spark left1{0};
@@ -361,12 +384,16 @@ Much like with heading stabilization, this is often accomplished with a PID loop
361384

362385
frc::DifferentialDrive drive{leftMotors, rightMotors};
363386

387+
void Robot::RobotInit() {
388+
rightMotors.SetInverted(true);
389+
}
390+
364391
void Robot::AutonomousPeriodic() {
365392
// Find the heading error; setpoint is 90
366393
double error = 90 - gyro.GetAngle();
367394

368395
// Turns the robot to face the desired direction
369-
drive.TankDrive(kP * error, kP * error);
396+
drive.TankDrive(kP * error, -kP * error);
370397
}
371398

372399
As before, more-advanced implementations can use more-complicated control loops.

0 commit comments

Comments
 (0)