From f774e47c807a1b22aeb3e500cdd1ac8195ffb6ef Mon Sep 17 00:00:00 2001 From: Austin Shalit Date: Mon, 29 Oct 2018 15:37:30 -0400 Subject: [PATCH] Add an example showing how to use a hid rumbler (#1394) --- .../main/cpp/examples/HidRumble/cpp/Robot.cpp | 34 +++++++++++++++++++ .../src/main/cpp/examples/examples.json | 9 +++++ .../wpi/first/wpilibj/examples/examples.json | 10 ++++++ .../wpilibj/examples/hidrumble/Main.java | 29 ++++++++++++++++ .../wpilibj/examples/hidrumble/Robot.java | 33 ++++++++++++++++++ 5 files changed, 115 insertions(+) create mode 100644 wpilibcExamples/src/main/cpp/examples/HidRumble/cpp/Robot.cpp create mode 100644 wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/hidrumble/Main.java create mode 100755 wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/hidrumble/Robot.java diff --git a/wpilibcExamples/src/main/cpp/examples/HidRumble/cpp/Robot.cpp b/wpilibcExamples/src/main/cpp/examples/HidRumble/cpp/Robot.cpp new file mode 100644 index 0000000000..f78bd711b1 --- /dev/null +++ b/wpilibcExamples/src/main/cpp/examples/HidRumble/cpp/Robot.cpp @@ -0,0 +1,34 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2018 FIRST. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#include +#include +#include +#include + +/** + * This is a demo program showing the use of GenericHID's rumble feature. + */ +class Robot : public frc::TimedRobot { + public: + void AutonomousInit() override { + // Turn on rumble at the start of auto + m_hid.SetRumble(frc::GenericHID::RumbleType::kLeftRumble, 1.0); + m_hid.SetRumble(frc::GenericHID::RumbleType::kRightRumble, 1.0); + } + + void DisabledInit() override { + // Stop the rumble when entering disabled + m_hid.SetRumble(frc::GenericHID::RumbleType::kLeftRumble, 0.0); + m_hid.SetRumble(frc::GenericHID::RumbleType::kRightRumble, 0.0); + } + + private: + frc::XboxController m_hid{0}; +}; + +int main() { return frc::StartRobot(); } diff --git a/wpilibcExamples/src/main/cpp/examples/examples.json b/wpilibcExamples/src/main/cpp/examples/examples.json index 44ebb16cb0..8bd1e21fc9 100644 --- a/wpilibcExamples/src/main/cpp/examples/examples.json +++ b/wpilibcExamples/src/main/cpp/examples/examples.json @@ -144,6 +144,15 @@ "foldername": "GyroMecanum", "gradlebase": "cpp" }, + { + "name": "HID Rumble", + "description": "An example program showing how to make human interface devices rumble.", + "tags": [ + "Joystick" + ], + "foldername": "HidRumble", + "gradlebase": "cpp" + }, { "name": "PotentiometerPID", "description": "An example to demonstrate the use of a potentiometer and PID control to reach elevator position setpoints.", diff --git a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/examples.json b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/examples.json index 19af701cd1..39a92e5c91 100644 --- a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/examples.json +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/examples.json @@ -98,6 +98,16 @@ "gradlebase": "java", "mainclass": "Main" }, + { + "name": "HID Rumble", + "description": "An example program showing how to make human interface devices rumble.", + "tags": [ + "Joystick" + ], + "foldername": "hidrumble", + "gradlebase": "java", + "mainclass": "Main" + }, { "name": "Motor Controller", "description": "Demonstrate controlling a single motor with a joystick", diff --git a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/hidrumble/Main.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/hidrumble/Main.java new file mode 100644 index 0000000000..297bbf10df --- /dev/null +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/hidrumble/Main.java @@ -0,0 +1,29 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2018 FIRST. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +package edu.wpi.first.wpilibj.examples.hidrumble; + +import edu.wpi.first.wpilibj.RobotBase; + +/** + * Do NOT add any static variables to this class, or any initialization at all. + * Unless you know what you are doing, do not modify this file except to + * change the parameter class to the startRobot call. + */ +public final class Main { + private Main() { + } + + /** + * Main initialization function. Do not perform any initialization here. + * + *

If you change your main robot class, change the parameter type. + */ + public static void main(String... args) { + RobotBase.startRobot(Robot::new); + } +} diff --git a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/hidrumble/Robot.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/hidrumble/Robot.java new file mode 100755 index 0000000000..c7f6029a46 --- /dev/null +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/hidrumble/Robot.java @@ -0,0 +1,33 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2018 FIRST. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +package edu.wpi.first.wpilibj.examples.hidrumble; + +import edu.wpi.first.wpilibj.GenericHID.RumbleType; +import edu.wpi.first.wpilibj.TimedRobot; +import edu.wpi.first.wpilibj.XboxController; + +/** + * This is a demo program showing the use of GenericHID's rumble feature. + */ +public class Robot extends TimedRobot { + private final XboxController m_hid = new XboxController(0); + + @Override + public void autonomousInit() { + // Turn on rumble at the start of auto + m_hid.setRumble(RumbleType.kLeftRumble, 1.0); + m_hid.setRumble(RumbleType.kRightRumble, 1.0); + } + + @Override + public void disabledInit() { + // Stop the rumble when entering disabled + m_hid.setRumble(RumbleType.kLeftRumble, 0.0); + m_hid.setRumble(RumbleType.kRightRumble, 0.0); + } +}