WPILib 示例项目

警告

虽然我们要尽力保持WPILib示例的功能,但并不意味着可以直接使用示例代码。如果要使代码在用户的机器人上工作,至少要更改特定于机器人的常量。为了演示的目的,许多需要实际测试得出的常数的值都被“伪造”了。我们强烈鼓励用户编写自己的代码(从零开始或从现有模板开始),而不是复制示例代码。

WPILib示例项目演示了许多库功能和使用模式。项目范围从单一功能的简单演示到完整的具有竞争能力的机器人程序。输入Ctrl + Shift + P键,然后选择`WPILib: Create a new project` 并选择示例,即可在VS Code中使用所有这些示例。

../../../_images/create-new-project.png

基本范例

这些示例演示了基本的机器人功能。对于刚开始熟悉机器人编程但机器人功能受到严重限制的入门团队,它们很有用。

  • Arcade Drive (Java, C++, Python): Demonstrates a simple differential drive implementation using “arcade”-style controls through the DifferentialDrive class.

  • Arcade Drive Xbox Controller (Java, C++, Python): Demonstrates the same functionality seen in the previous example, except using an XboxController instead of an ordinary joystick.

  • Getting Started (Java, C++, Python): Demonstrates a simple autonomous routine that drives forwards for two seconds at half speed.

  • Mecanum Drive (Java, C++, Python): Demonstrates a simple mecanum drive implementation using the MecanumDrive class.

  • Motor Controller (Java, C++, Python): Demonstrates how to control the output of a motor with a joystick with an encoder to read motor position.

  • Simple Vision (Java, C++, Python): Demonstrates how to stream video from a USB camera to the dashboard.

  • Relay (Java, C++, Python): Demonstrates the use of the Relay class to control a relay output with a set of joystick buttons.

  • Solenoids (Java, C++, Python): Demonstrates the use of the Solenoid and DoubleSolenoid classes to control solenoid outputs with a set of joystick buttons.

  • TankDrive (Java, C++, Python): Demonstrates a simple differential drive implementation using “tank”-style controls through the DifferentialDrive class.

  • Tank Drive Xbox Controller (Java, C++, Python): Demonstrates the same functionality seen in the previous example, except using an XboxController instead of an ordinary joystick.

控制示例

这些示例演示了通用机器人控件的WPILib实现。可能存在传感器,但不是这些示例的重点概念。

  • DifferentialDriveBot (Java, C++, Python): Demonstrates an advanced differential drive implementation, including encoder-and-gyro odometry through the DifferentialDriveOdometry class, and composition with PID velocity control through the DifferentialDriveKinematics and PIDController classes.

  • Elevator with profiled PID controller (Java, C++, Python): Demonstrates the use of the ProfiledPIDController class to control the position of an elevator mechanism.

  • Elevator with trapezoid profiled PID (Java, C++, Python): Demonstrates the use of the TrapezoidProfile class in conjunction with a “smart motor controller” to control the position of an elevator mechanism.

  • Gyro Mecanum (Java, C++, Python): Demonstrates field-oriented control of a mecanum robot through the MecanumDrive class in conjunction with a gyro.

  • MecanumBot (Java, C++, Python): Demonstrates an advanced mecanum drive implementation, including encoder-and-gyro odometry through the MecanumDriveOdometry class, and composition with PID velocity control through the MecanumDriveKinematics and PIDController classes.

  • PotentiometerPID (Java, C++, Python): Demonstrates the use of the PIDController class and a potentiometer to control the position of an elevator mechanism.

  • RamseteController (Java, C++, Python): Demonstrates the use of the RamseteController class to follow a trajectory during the autonomous period.

  • SwerveBot (Java, C++, Python): Demonstrates an advanced swerve drive implementation, including encoder-and-gyro odometry through the SwerveDriveOdometry class, and composition with PID position and velocity control through the SwerveDriveKinematics and PIDController classes.

  • UltrasonicPID (Java, C++, Python): Demonstrates the use of the PIDController class in conjunction with an ultrasonic sensor to drive to a set distance from an object.

传感器实例

这些示例演示了使用WPILib进行传感器读取和数据处理。可能存在机制控制,但不是这些示例强调的概念。

  • •Axis Camera Sample (Java, C++):演示了OpenCV和Axis网络摄像机的使用,在捕获的视频上覆盖一个矩形,并将其输出到仪表盘

  • Power Distribution CAN Monitoring (Java, C++, Python): Demonstrates obtaining sensor information from a Power Distribution module over CAN using the PowerDistribution class.

  • Duty Cycle Encoder (Java, C++, Python): Demonstrates the use of the DutyCycleEncoder class to read values from a PWM-type absolute encoder.

  • DutyCycleInput (Java, C++, Python): Demonstrates the use of the DutyCycleInput class to read the frequency and fractional duty cycle of a PWM input.

  • Encoder (Java, C++, Python): Demonstrates the use of the Encoder class to read values from a quadrature encoder.

  • Gyro (Java, C++, Python): Demonstrates the use of the AnalogGyro class to measure robot heading and stabilize driving.

  • Intermediate Vision (Java, C++, Python): Demonstrates the use of OpenCV and a USB camera to overlay a rectangle on a captured video feed and stream it to the dashboard.

  • AprilTagsVision (Java, C++): Demonstrates on-roboRIO detection of AprilTags using an attached USB camera.

  • Ultrasonic (Java, C++, Python): Demonstrates the use of the Ultrasonic class to read data from an ultrasonic sensor in conjunction with the MedianFilter class to reduce signal noise.

  • SysIdRoutine (Java, C++, Python): Demonstrates the use of the SysIdRoutine API to gather characterization data for a differential drivetrain.

基于指令的示例

这些示例演示了 :ref:`Command-Based framework <docs/software/commandbased/what-is-command-based:What Is “Command-Based” Programming?>`的使用。

  • ArmBot (Java, C++, Python): Demonstrates the use of a ProfiledPIDSubsystem to control a robot arm.

  • ArmBotOffboard (Java, C++, Python): Demonstrates the use of a TrapezoidProfileSubsystem in conjunction with a “smart motor controller” to control a robot arm.

  • DriveDistanceOffboard (Java, C++, Python): Demonstrates the use of a TrapezoidProfileCommand in conjunction with a “smart motor controller” to drive forward by a set distance with a trapezoidal motion profile.

  • FrisbeeBot (Java, C++, Python): A complete set of robot code for a simple frisbee-shooting robot typical of the 2013 FRC® game Ultimate Ascent. Demonstrates simple PID control through the PIDSubystem class.

  • •Gears Bot (Java, C++):一套完整的WPI演示机器人代码:GearsBot

  • Gyro Drive Commands (Java, C++, Python): Demonstrates the use of PIDCommand and ProfiledPIDCommand in conjunction with a gyro to turn a robot to face a specified heading and to stabilize heading while driving.

  • Inlined Hatchbot (Java, C++, Python): A complete set of robot code for a simple hatch-delivery bot typical of the 2019 FRC game Destination: Deep Space. Commands are written in an “inline” style, in which explicit subclassing of Command is avoided.

  • Traditional Hatchbot (Java, C++, Python): A complete set of robot code for a simple hatch-delivery bot typical of the 2019 FRC game Destination: Deep Space. Commands are written in a “traditional” style, in which subclasses of Command are written for each robot action.

  • •MecanumControllerCommand (Java, C++): 演示使用TrajectoryGenerator和MecanumControllerCommand类用麦克纳姆轮实现轨迹的生成与追踪

  • RamseteCommand (Java, C++, Python): Demonstrates trajectory generation and following with a differential drive using the TrajectoryGenerator and RamseteCommand classes. A matching step-by-step tutorial can be found here.

  • Select Command Example (Java, C++, Python): Demonstrates the use of the SelectCommand class to run one of a selection of commands depending on a runtime-evaluated condition.

  • •SwerveControllerCommand(Java, C++):使用TrajectoryGenerator和SwerveControllerCommand类演示轨迹生成和转向驱动。

状态空间示例

这些示例演示了 :ref:`State-Space Control <docs/software/advanced-controls/state-space/state-space-intro:Introduction to State-Space Control>`的使用。

  • StateSpaceFlywheel (Java, C++, Python): Demonstrates state-space control of a flywheel.

  • StateSpaceFlywheelSysId (Java, C++, Python): Demonstrates state-space control using SysId’s System Identification for controlling a flywheel.

  • •StateSpaceElevator (Java, C++): 演示电梯的状态空间控制

  • •Statespacarm (Java, C++): 演示机械臂的状态空间控制

  • •Statespacedrivessimulation(Java, C++): 结合RAMSETE路径跟踪控制器和Field2d类演示差速传动系统的状态空间控制

模拟物理实例

这些示例说明了物理模拟的用法。

  • ElevatorSimulation (Java, C++, Python): Demonstrates the use of physics simulation with a simple elevator.

  • ArmSimulation (Java, C++, Python): Demonstrates the use of physics simulation with a simple single-jointed arm.

  • •Statespacedrivessimulation(Java, C++): 结合RAMSETE路径跟踪控制器和Field2d类演示差速传动系统的状态空间控制

  • •StateSpaceDrivesSimulation (Java, C++): 结合RAMSETE路径跟踪控制器和Field2d类演示了差速传动系统的状态空间控制

其他例子

这些示例演示了不属于上述任何类别的其他WPILib功能。

  • Addressable LED (Java, C++, Python): Demonstrates the use of the AddressableLED class to control RGB LEDs for robot decoration and/or driver feedback.

  • DMA (Java, C++): Demonstrates the use of DMA (Direct Memory Access) to read from sensors without using the RoboRIO’s CPU.

  • •HAL(C++): 演示了HAL(硬件抽象层)的使用(不使用WPILib的其余部分)这个例子是针对进阶用户的(仅限C++)

  • HID Rumble (Java, C++, Python): Demonstrates the use of the “rumble” functionality for tactile feedback on supported HIDs (such as XboxControllers).

  • Shuffleboard (Java, C++, Python): Demonstrates configuring tab/widget layouts on the “Shuffleboard” dashboard from robot code through the Shuffleboard class’s fluent builder API.

  • RomiReference (Java, C++, Python): A command based example of how to run the Romi robot.

  • Mechanism2d (Java, C++, Python): A simple example of using Mechanism2d.