diff --git a/wpilibcExamples/src/main/cpp/snippets/LimitSwitch/cpp/Robot.cpp b/wpilibcExamples/src/main/cpp/snippets/LimitSwitch/cpp/Robot.cpp new file mode 100644 index 0000000000..7fc54ed6e1 --- /dev/null +++ b/wpilibcExamples/src/main/cpp/snippets/LimitSwitch/cpp/Robot.cpp @@ -0,0 +1,50 @@ +// 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 +#include +#include +#include +#include + +/** + * Limit Switch snippets for frc-docs. + * https://docs.wpilib.org/en/stable/docs/software/hardware-apis/sensors/limit-switch.html + */ +class Robot : public frc::TimedRobot { + public: + void TeleopPeriodic() override { SetMotorSpeed(m_joystick.GetRawAxis(2)); } + void SetMotorSpeed(double speed) { + if (speed > 0) { + if (m_toplimitSwitch.Get()) { + // We are going up and top limit is tripped so stop + m_motor.Set(0); + } else { + // We are going up but top limit is not tripped so go at commanded speed + m_motor.Set(speed); + } + } else { + if (m_bottomlimitSwitch.Get()) { + // We are going down and bottom limit is tripped so stop + m_motor.Set(0); + } else { + // We are going down but bottom limit is not tripped so go at commanded + // speed + m_motor.Set(speed); + } + } + } + + private: + frc::DigitalInput m_toplimitSwitch{0}; + frc::DigitalInput m_bottomlimitSwitch{1}; + frc::PWMVictorSPX m_motor{0}; + frc::Joystick m_joystick{0}; +}; + +#ifndef RUNNING_FRC_TESTS +int main() { + return frc::StartRobot(); +} +#endif diff --git a/wpilibcExamples/src/main/cpp/snippets/snippets.json b/wpilibcExamples/src/main/cpp/snippets/snippets.json index ccf912c5db..27fd0e46a1 100644 --- a/wpilibcExamples/src/main/cpp/snippets/snippets.json +++ b/wpilibcExamples/src/main/cpp/snippets/snippets.json @@ -52,5 +52,15 @@ ], "foldername": "EncoderHoming", "gradlebase": "cpp" + }, + { + "name": "LimitSwitch", + "description": "Snippets of Limit Switch for frc-docs.", + "tags": [ + "Hardware", + "Digital Input" + ], + "foldername": "LimitSwitch", + "gradlebase": "cpp" } ] diff --git a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/limitswitch/Main.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/limitswitch/Main.java new file mode 100644 index 0000000000..19c1da0b45 --- /dev/null +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/limitswitch/Main.java @@ -0,0 +1,25 @@ +// 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 edu.wpi.first.wpilibj.snippets.limitswitch; + +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/snippets/limitswitch/Robot.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/limitswitch/Robot.java new file mode 100644 index 0000000000..73c2b6c398 --- /dev/null +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/limitswitch/Robot.java @@ -0,0 +1,51 @@ +// 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 edu.wpi.first.wpilibj.snippets.limitswitch; + +import edu.wpi.first.wpilibj.DigitalInput; +import edu.wpi.first.wpilibj.Joystick; +import edu.wpi.first.wpilibj.TimedRobot; +import edu.wpi.first.wpilibj.motorcontrol.PWMVictorSPX; + +/** + * Limit Switch snippets for frc-docs. + * https://docs.wpilib.org/en/stable/docs/software/hardware-apis/sensors/limit-switch.html + */ +public class Robot extends TimedRobot { + DigitalInput m_toplimitSwitch = new DigitalInput(0); + DigitalInput m_bottomlimitSwitch = new DigitalInput(1); + PWMVictorSPX m_motor = new PWMVictorSPX(0); + Joystick m_joystick = new Joystick(0); + + @Override + public void teleopPeriodic() { + setMotorSpeed(m_joystick.getRawAxis(2)); + } + + /** + * Sets the motor speed based on joystick input while respecting limit switches. + * + * @param speed the desired speed of the motor, positive for up and negative for down + */ + public void setMotorSpeed(double speed) { + if (speed > 0) { + if (m_toplimitSwitch.get()) { + // We are going up and top limit is tripped so stop + m_motor.set(0); + } else { + // We are going up but top limit is not tripped so go at commanded speed + m_motor.set(speed); + } + } else { + if (m_bottomlimitSwitch.get()) { + // We are going down and bottom limit is tripped so stop + m_motor.set(0); + } else { + // We are going down but bottom limit is not tripped so go at commanded speed + m_motor.set(speed); + } + } + } +} diff --git a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/snippets.json b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/snippets.json index 0f44d35eb3..75d78b3284 100644 --- a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/snippets.json +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/snippets.json @@ -57,5 +57,16 @@ "foldername": "encoderhoming", "gradlebase": "java", "mainclass": "Main" + }, + { + "name": "LimitSwitch", + "description": "Snippets of Limit Switch for frc-docs.", + "tags": [ + "Hardware", + "Digital Input" + ], + "foldername": "limitswitch", + "gradlebase": "java", + "mainclass": "Main" } ]