[examples] Add an ExpansionHubSample example (#8810)

This commit is contained in:
Thad House
2026-04-25 15:43:24 -07:00
committed by GitHub
parent 6aa1611959
commit a3968faa57
6 changed files with 113 additions and 1 deletions

View File

@@ -66,7 +66,9 @@ def tagList = [
/* --- Misc --- */
/* (try to keep this section minimal) */
"EventLoop", "Mechanism2d", "Preferences", "Skeleton"
"EventLoop", "Mechanism2d", "Preferences", "Skeleton",
"FTC", "ExpansionHub", "OpModes"
]
task checkTemplates(type: Task) {

View File

@@ -11,6 +11,7 @@ EXAMPLE_FOLDERS = [
"elevatorsimulation",
"elevatortrapezoidprofile",
"encoder",
"expansionhubsample",
"gettingstarted",
"gyro",
"hatchbotcmdv3",

View File

@@ -23,6 +23,21 @@
"robotclass": "Robot",
"commandversion": 2
},
{
"name": "Expansion Hub Sample",
"description": "Sample the functionality of the Expansion Hub motors.",
"tags": [
"Hardware",
"ExpansionHub",
"FTC",
"OpModes",
"SmartDashboard"
],
"foldername": "expansionhubsample",
"gradlebase": "java",
"robotclass": "Robot",
"commandversion": 2
},
{
"name": "Elevator with trapezoid profiled PID",
"description": "Reach elevator position setpoints with trapezoid profiles and smart motor controller PID.",

View File

@@ -0,0 +1,37 @@
// 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.examples.expansionhubsample;
import org.wpilib.opmode.PeriodicOpMode;
import org.wpilib.system.Timer;
public class DefaultAutoMode extends PeriodicOpMode {
private final Robot robot;
private final Timer timer = new Timer();
public DefaultAutoMode(Robot robot) {
this.robot = robot;
}
@Override
public void start() {
timer.reset();
timer.start();
}
@Override
public void periodic() {
if (timer.get() < 2.0) {
robot.motor0.setThrottle(0.5);
robot.motor1.setThrottle(0.5);
} else if (timer.get() < 4.0) {
robot.motor0.setThrottle(0.9);
robot.motor1.setThrottle(0.9);
} else {
robot.motor0.setThrottle(0.0);
robot.motor1.setThrottle(0.0);
}
}
}

View File

@@ -0,0 +1,28 @@
// 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.examples.expansionhubsample;
import org.wpilib.driverstation.DefaultUserControls;
import org.wpilib.opmode.PeriodicOpMode;
public class DefaultTeleMode extends PeriodicOpMode {
private final Robot robot;
private final DefaultUserControls userControls;
public DefaultTeleMode(Robot robot, DefaultUserControls userControls) {
this.robot = robot;
this.userControls = userControls;
}
@Override
public void periodic() {
robot.motor0.setThrottle(-userControls.getGamepad(0).getLeftY());
robot.motor1.setThrottle(-userControls.getGamepad(0).getRightY());
robot.motor2.setThrottle(-userControls.getGamepad(0).getLeftX());
robot.motor3.setThrottle(-userControls.getGamepad(0).getRightX());
robot.servo0.setPosition(userControls.getGamepad(0).getLeftTriggerAxis());
robot.servo1.setPosition(userControls.getGamepad(0).getRightTriggerAxis());
}
}

View File

@@ -0,0 +1,29 @@
// 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.examples.expansionhubsample;
import org.wpilib.driverstation.DefaultUserControls;
import org.wpilib.driverstation.UserControlsInstance;
import org.wpilib.framework.OpModeRobot;
import org.wpilib.hardware.expansionhub.ExpansionHubMotor;
import org.wpilib.hardware.expansionhub.ExpansionHubServo;
/**
* This is a demo program showing the use of the Expansion Hub motors and servos. The motors and
* servos are driven using the controllers in the telop opmode, and timed in the auto op mode.
*/
@UserControlsInstance(DefaultUserControls.class)
public class Robot extends OpModeRobot {
public final ExpansionHubMotor motor0 = new ExpansionHubMotor(0, 0);
public final ExpansionHubMotor motor1 = new ExpansionHubMotor(0, 1);
public final ExpansionHubMotor motor2 = new ExpansionHubMotor(0, 2);
public final ExpansionHubMotor motor3 = new ExpansionHubMotor(0, 3);
public final ExpansionHubServo servo0 = new ExpansionHubServo(0, 0);
public final ExpansionHubServo servo1 = new ExpansionHubServo(0, 1);
/** Called once at the beginning of the robot program. */
public Robot() {}
}