mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
[examples] Add C++ TankDrive example (#3493)
This is a basic C++ example that demonstrates a simple differential drive implementation using “tank”-style controls through the DifferentialDrive class and an ordinary joystick.
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
// 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 <frc/Joystick.h>
|
||||
#include <frc/TimedRobot.h>
|
||||
#include <frc/drive/DifferentialDrive.h>
|
||||
#include <frc/motorcontrol/PWMSparkMax.h>
|
||||
|
||||
/**
|
||||
* This is a demo program showing the use of the DifferentialDrive class.
|
||||
* Runs the motors with tank steering.
|
||||
*/
|
||||
class Robot : public frc::TimedRobot {
|
||||
frc::PWMSparkMax m_leftMotor{0};
|
||||
frc::PWMSparkMax m_rightMotor{1};
|
||||
frc::DifferentialDrive m_robotDrive{m_leftMotor, m_rightMotor};
|
||||
frc::Joystick m_leftStick{0};
|
||||
frc::Joystick m_rightStick{1};
|
||||
|
||||
public:
|
||||
void RobotInit() override {
|
||||
// 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.
|
||||
m_rightMotor.SetInverted(true);
|
||||
}
|
||||
|
||||
void TeleopPeriodic() override {
|
||||
// Drive with tank style
|
||||
m_robotDrive.TankDrive(m_leftStick.GetY(), m_rightStick.GetY());
|
||||
}
|
||||
};
|
||||
|
||||
#ifndef RUNNING_FRC_TESTS
|
||||
int main() {
|
||||
return frc::StartRobot<Robot>();
|
||||
}
|
||||
#endif
|
||||
@@ -97,6 +97,19 @@
|
||||
"gradlebase": "cpp",
|
||||
"commandversion": 2
|
||||
},
|
||||
{
|
||||
"name": "Tank Drive",
|
||||
"description": "An example program which demonstrates the use of Tank Drive with the DifferentialDrive class",
|
||||
"tags": [
|
||||
"Getting Started with C++",
|
||||
"Robot and Motor",
|
||||
"Joystick",
|
||||
"Complete List"
|
||||
],
|
||||
"foldername": "TankDrive",
|
||||
"gradlebase": "cpp",
|
||||
"commandversion": 2
|
||||
},
|
||||
{
|
||||
"name": "Mecanum Drive",
|
||||
"description": "An example program which demonstrates the use of Mecanum Drive with the MecanumDrive class",
|
||||
|
||||
Reference in New Issue
Block a user