WPILib 示例项目

警告

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

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

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

基本范例

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

  • •Arcade Drive (Java<https://github.com/wpilibsuite/allwpilib/tree/main/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/arcadedrive>`__, `C++ <https://github.com/wpilibsuite/allwpilib/tree/main/wpilibcExamples/src/main/cpp/examples/ArcadeDrive> __)通过“Differentialdrive”类使用“arcade”风格的控制演示了实现简单机器人差速驱动的方法

  • •Arcade Drive Xbox Controller(Java, C++):与上一案例实现了相同的功能;他们之间的区别在于此案例使用了XboxController而不是普通的摇杆

  • •Getting Started(Java, C++):这段代码演示了一个简单的自动阶段的驱动例程——半速前进两秒

  • •Mecanum Drive(Java, C++):这一使用了MecanumDrive类的代码实现了一个简单的麦克纳姆轮的驱动

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

  • •Simple Vision (Java, C++):演示如何从USB摄像头转换视频到Driver Station的仪表板中

  • •Relay**Relay** (Java, C++):演示了使用Relay类来通过一组摇杆的按钮控制继电器输出

  • •Solenoids (Java, C++):演示了Solenoid和DoubleSolenoid类的使用,使用操纵杆按钮控制电磁阀输出。

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

  • •TankDrive Xbox Controller(Java, C++):与上例实现了相同的功能;区别在于使用XboxController而不是普通的操纵杆

控制示例

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

  • •DifferentialDriveBot (Java, C++):演示了一个高级的机器人差速驱动方式的实现,包括通过DifferalDriveOdometry类的编码器和陀螺测程,以及通过DifferentialDriveKinematics和PIDController类的PID速度控制组成

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

  • Elevator with trapezoid profiled PID (Java, C++): 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++): Demonstrates field-oriented control of a mecanum robot through the MecanumDrive class in conjunction with a gyro.

  • •MecanumBot (Java, C++):演示了一个高级的麦克纳姆轮的驱动实现,通过MecanumDriveOdometry类中的编码器和陀螺测程,并通过MecanumDriveKinematics和 PIDController类结合PID进行速度控制

  • •PotentiometerPID (Java, C++):演示PIDController类和控制电梯结构位置的电位器

  • •RamseteController (Java, C++):演示RamseteController类的使用来实现自动阶段期间沿着一个特定的轨道行进

  • •SwerveBot (Java, C++):演示了一个高级的转弯驱动实现,包括通过SwerveDriveOdometry类的编码器和陀螺测程,以及通过SwerveDriveKinematics与PIDController类结合PID进行速度控制

  • •UltrasonicPID (Java, C++):演示了使用PIDController类结合超声波传感器使机器行进到距离一个对象特定值处

传感器实例

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

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

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

  • •Duty Cycle Encoder (Java, C++):演示了DutyCycleEncoder类的使用——从PWM类型的绝对式编码器读取值数值

  • •DutyCycleInput (Java, C++):演示如何使用DutyCycleInput类读取PWM输入的频率和占空比

  • •Encoder (Java, C++):演示了如何使用Encoder类从一个正交编码器处读取数值

  • •Gyro (Java, C++):演示使用AnalogGyro类测量机器人的方向以及稳定行驶过程

  • •Intermediate Vision (Java, C++):演示OpenCV和USB摄像头的使用,在捕获的视频上覆盖一个矩形,并将其输出到仪表板

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

  • •Ultrasonic (Java, C++):演示如何使用Ultrasonic类使用超声波传感器结合MedianFilter类进行降噪以读取数据

基于指令的示例

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

  • •ArmBot (Java, C++):演示了使用ProfiledPIDSubsystem控制机器人手臂

  • •ArmBotOffboard (Java, C++):演示了TrapezoidProfileSubsystem结合“智能电机控制器”一起控制机器人手臂

  • •DriveDistanceOffboard (Java, C++):演示使用TrapezoidProfileCommand结合“智能电机控制器”一起使机器人向前移动特定距离

  • •FrisbeeBot (Java, C++): 2013年FRC®赛事Ultimate Ascent中的典型的简单飞盘射击机器人的完整代码。演示通过PIDSubystem类实现简单的PID控制

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

  • •Gyro Drive Commands (Java, C++):演示使用PIDCommand和ProfiledPIDCommand与陀螺仪结合,使机器人始终朝向一个特定的方向并在驾驶时稳定朝向

  • Inlined Hatchbot (Java, C++): 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++): 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++): 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++):演示通过SelectCommand类的使用根据运行时计算的条件运行所选命令之一

  • •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++): 演示了飞轮的状态空间控制

  • StateSpaceFlywheelSysId (Java, C++): 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++):通过一个简单的电梯结构演示物理模拟的使用

  • •ArmSimulation (Java, C++): 通过单节机械臂演示了一个简单物理模拟的使用

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

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

其他例子

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

  • •Addressable LED (Java, C++): 演示了通过对AddressableLED类的使用控制RGB LED灯用于装饰机器人或向操纵手反馈信息

  • 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++): 演示了触觉的“Rumble”向支持的HID(如XboxControllers)的反馈功能的实现

  • •Shuffleboard(Java, C++): 在机器人代码中通过Shuffleboard类的fluent builder API演示在“Shuffleboard”仪表板上配置标签/小部件布局

  • •RomiReference(Java, C++): 基于指令的Romi机器人运行示例。(见41)

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