diff --git a/wpilibcExamples/example_projects.bzl b/wpilibcExamples/example_projects.bzl index 79307ef6b9..545b093c4a 100644 --- a/wpilibcExamples/example_projects.bzl +++ b/wpilibcExamples/example_projects.bzl @@ -58,6 +58,7 @@ SNIPPET_FOLDERS = [ "AnalogPotentiometer", "AprilTagsVision", "CANPDP", + "DifferentialDrive", "DigitalCommunication", "DigitalInput", "DutyCycleEncoder", diff --git a/wpilibcExamples/src/main/cpp/snippets/DifferentialDrive/cpp/Robot.cpp b/wpilibcExamples/src/main/cpp/snippets/DifferentialDrive/cpp/Robot.cpp new file mode 100644 index 0000000000..b03bb90b14 --- /dev/null +++ b/wpilibcExamples/src/main/cpp/snippets/DifferentialDrive/cpp/Robot.cpp @@ -0,0 +1,55 @@ +// Copyright (c) FIRST and other WPILib contributors. +// Open Source Software; you can modify and/or share it under the terms of +// the WPILib BSD license file in the root directory of this project. + +#include "wpi/drive/DifferentialDrive.hpp" +#include "wpi/driverstation/Gamepad.hpp" +#include "wpi/framework/TimedRobot.hpp" +#include "wpi/hardware/motor/PWMSparkMax.hpp" + +/** + * DifferentialDrive snippets for wpilib-docs. Runs the motors with tank + * drive, arcade drive, and curvature drive. + * https://docs.wpilib.org/en/stable/docs/software/hardware-apis/motors/wpi-drive-classes.html + */ +class Robot : public wpi::TimedRobot { + wpi::PWMSparkMax leftMotor{0}; + wpi::PWMSparkMax rightMotor{1}; + wpi::DifferentialDrive robotDrive{ + [&](double output) { leftMotor.SetThrottle(output); }, + [&](double output) { rightMotor.SetThrottle(output); }}; + wpi::Gamepad driverController{0}; + + public: + Robot() { + // We need to invert one side of the drivetrain so that positive voltages + // result in both sides moving forward. Depending on how your robot's + // gearbox is constructed, you might have to invert the left side instead. + rightMotor.SetInverted(true); + } + + void TeleopPeriodic() override { + // Drive with split arcade style + // That means that the Y axis of the left stick moves forward + // and backward, and the X of the right stick turns left and right. + robotDrive.ArcadeDrive(-driverController.GetLeftY(), + -driverController.GetRightX()); + // Tank drive with a given left and right rates + robotDrive.TankDrive(-driverController.GetLeftY(), + -driverController.GetRightY()); + // Arcade drive with a given forward and turn rate + robotDrive.ArcadeDrive(-driverController.GetLeftY(), + -driverController.GetLeftX()); + // Curvature drive with a given forward and turn rate, as well as a + // quick-turn button + robotDrive.CurvatureDrive(-driverController.GetLeftY(), + -driverController.GetLeftX(), + driverController.GetFaceUpButton()); + } +}; + +#ifndef RUNNING_WPILIB_TESTS +int main() { + return wpi::StartRobot(); +} +#endif diff --git a/wpilibcExamples/src/main/cpp/snippets/snippets.json b/wpilibcExamples/src/main/cpp/snippets/snippets.json index 9d90648b18..1caba12b3b 100644 --- a/wpilibcExamples/src/main/cpp/snippets/snippets.json +++ b/wpilibcExamples/src/main/cpp/snippets/snippets.json @@ -303,5 +303,16 @@ ], "foldername": "Solenoid", "gradlebase": "cpp" + }, + { + "name": "Differential Drive", + "description": "Show differential drive control with tank, arcade, and curvature drive in teleop", + "tags": [ + "Differential Drive", + "Gamepad" + ], + "foldername": "DifferentialDrive", + "gradlebase": "java", + "robotclass": "Robot" } ] diff --git a/wpilibjExamples/example_projects.bzl b/wpilibjExamples/example_projects.bzl index bf6d01c9f8..68915de68e 100644 --- a/wpilibjExamples/example_projects.bzl +++ b/wpilibjExamples/example_projects.bzl @@ -59,6 +59,7 @@ SNIPPET_FOLDERS = [ "analogpotentiometer", "apriltagsvision", "canpdp", + "differentialdrive", "digitalcommunication", "digitalinput", "dutycycleencoder", diff --git a/wpilibjExamples/src/main/java/org/wpilib/snippets/differentialdrive/Robot.java b/wpilibjExamples/src/main/java/org/wpilib/snippets/differentialdrive/Robot.java new file mode 100644 index 0000000000..030cc44989 --- /dev/null +++ b/wpilibjExamples/src/main/java/org/wpilib/snippets/differentialdrive/Robot.java @@ -0,0 +1,44 @@ +// Copyright (c) FIRST and other WPILib contributors. +// Open Source Software; you can modify and/or share it under the terms of +// the WPILib BSD license file in the root directory of this project. + +package org.wpilib.snippets.differentialdrive; + +import org.wpilib.drive.DifferentialDrive; +import org.wpilib.driverstation.Gamepad; +import org.wpilib.framework.TimedRobot; +import org.wpilib.hardware.motor.PWMSparkMax; + +/** + * DifferentialDrive snippets for wpilib-docs. Runs the motors with tank drive, arcade drive, and + * curvature drive. + * https://docs.wpilib.org/en/stable/docs/software/hardware-apis/motors/wpi-drive-classes.html + */ +public class Robot extends TimedRobot { + private final PWMSparkMax leftMotor = new PWMSparkMax(0); + private final PWMSparkMax rightMotor = new PWMSparkMax(1); + private final DifferentialDrive robotDrive = + new DifferentialDrive(leftMotor::setThrottle, rightMotor::setThrottle); + private final Gamepad driverController = new Gamepad(0); + + /** Called once at the beginning of the robot program. */ + public Robot() { + // We need to invert one side of the drivetrain so that positive voltages + // result in both sides moving forward. Depending on how your robot's + // gearbox is constructed, you might have to invert the left side instead. + rightMotor.setInverted(true); + } + + @Override + public void teleopPeriodic() { + // Tank drive with a given left and right rates + robotDrive.tankDrive(-driverController.getLeftY(), -driverController.getRightY()); + // Arcade drive with a given forward and turn rate + robotDrive.arcadeDrive(-driverController.getLeftY(), -driverController.getLeftX()); + // Curvature drive with a given forward and turn rate, as well as a button for turning in-place. + robotDrive.curvatureDrive( + -driverController.getLeftY(), + -driverController.getLeftX(), + driverController.getFaceUpButton()); + } +} diff --git a/wpilibjExamples/src/main/java/org/wpilib/snippets/snippets.json b/wpilibjExamples/src/main/java/org/wpilib/snippets/snippets.json index 475b399d96..8a3edbf56a 100644 --- a/wpilibjExamples/src/main/java/org/wpilib/snippets/snippets.json +++ b/wpilibjExamples/src/main/java/org/wpilib/snippets/snippets.json @@ -332,5 +332,16 @@ "foldername": "solenoid", "gradlebase": "java", "robotclass": "Robot" + }, + { + "name": "Differential Drive", + "description": "Show differential drive control with tank, arcade, and curvature drive in teleop", + "tags": [ + "Differential Drive", + "Gamepad" + ], + "foldername": "differentialdrive", + "gradlebase": "java", + "robotclass": "Robot" } ]