diff --git a/wpilibcExamples/CMakeLists.txt b/wpilibcExamples/CMakeLists.txt index f79cd15d68..36212ee9ac 100644 --- a/wpilibcExamples/CMakeLists.txt +++ b/wpilibcExamples/CMakeLists.txt @@ -9,6 +9,7 @@ subdir_list(SNIPPETS ${CMAKE_SOURCE_DIR}/wpilibcExamples/src/main/cpp/snippets) add_custom_target(wpilibcExamples) add_custom_target(wpilibcExamples_test) +add_custom_target(wpilibcExamples_snippets) foreach(example ${EXAMPLES}) file( @@ -72,8 +73,8 @@ foreach(snippet ${SNIPPETS}) src/main/cpp/snippets/${snippet}/c/*.c ) add_executable(snippet${snippet} ${sources}) - wpilib_target_warnings(${snippet}) + wpilib_target_warnings(snippet${snippet}) target_include_directories(snippet${snippet} PUBLIC src/main/cpp/snippets/${snippet}/include) target_link_libraries(snippet${snippet} wpilibc wpilibNewCommands romiVendordep xrpVendordep) - add_dependencies(wpilibcExamples snippet${snippet}) + add_dependencies(wpilibcExamples_snippets snippet${snippet}) endforeach() diff --git a/wpilibcExamples/src/main/cpp/snippets/AnalogEncoder/cpp/Robot.cpp b/wpilibcExamples/src/main/cpp/snippets/AnalogEncoder/cpp/Robot.cpp new file mode 100644 index 0000000000..171d19933e --- /dev/null +++ b/wpilibcExamples/src/main/cpp/snippets/AnalogEncoder/cpp/Robot.cpp @@ -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. + +#include + +#include +#include + +/** + * AnalogEncoder snippets for frc-docs. + * https://docs.wpilib.org/en/stable/docs/software/hardware-apis/sensors/encoders-software.html + */ +class Robot : public frc::TimedRobot { + public: + Robot() {} + + void TeleopPeriodic() override { + // Gets the rotation + m_encoder.Get(); + } + + private: + // Initializes an analog encoder on Analog Input pin 0 + frc::AnalogEncoder m_encoder{0}; + + // Initializes an analog encoder on DIO pins 0 to return a value of 4 for + // a full rotation, with the encoder reporting 0 half way through rotation (2 + // out of 4) + frc::AnalogEncoder m_encoderFR{0, 4.0, 2.0}; +}; + +#ifndef RUNNING_FRC_TESTS +int main() { + return frc::StartRobot(); +} +#endif diff --git a/wpilibcExamples/src/main/cpp/snippets/EncoderDrive/cpp/Robot.cpp b/wpilibcExamples/src/main/cpp/snippets/EncoderDrive/cpp/Robot.cpp new file mode 100644 index 0000000000..8d888e46a8 --- /dev/null +++ b/wpilibcExamples/src/main/cpp/snippets/EncoderDrive/cpp/Robot.cpp @@ -0,0 +1,56 @@ +// 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 + +/** + * Encoder drive to distance snippets for frc-docs. + * https://docs.wpilib.org/en/stable/docs/software/hardware-apis/sensors/encoders-software.html + */ +class Robot : public frc::TimedRobot { + public: + Robot() { + // Configures the encoder's distance-per-pulse + // The robot moves forward 1 foot per encoder rotation + // There are 256 pulses per encoder rotation + m_encoder.SetDistancePerPulse(1.0 / 256.0); + // Invert the right side of the drivetrain. You might have to invert the + // other side + rightLeader.SetInverted(true); + // Configure the followers to follow the leaders + leftLeader.AddFollower(leftFollower); + rightLeader.AddFollower(rightFollower); + } + void AutonomousPeriodic() override { + // Drives forward at half speed until the robot has moved 5 feet, then + // stops: + if (m_encoder.GetDistance() < 5) { + drive.TankDrive(0.5, 0.5); + } else { + drive.TankDrive(0, 0); + } + } + + private: + // Creates an encoder on DIO ports 0 and 1. + frc::Encoder m_encoder{0, 1}; + // Initialize motor controllers and drive + frc::Spark leftLeader{0}; + frc::Spark leftFollower{1}; + frc::Spark rightLeader{2}; + frc::Spark rightFollower{3}; + frc::DifferentialDrive drive{[&](double output) { leftLeader.Set(output); }, + [&](double output) { rightLeader.Set(output); }}; +}; + +#ifndef RUNNING_FRC_TESTS +int main() { + return frc::StartRobot(); +} +#endif diff --git a/wpilibcExamples/src/main/cpp/snippets/EncoderHoming/cpp/Robot.cpp b/wpilibcExamples/src/main/cpp/snippets/EncoderHoming/cpp/Robot.cpp new file mode 100644 index 0000000000..23eaf5a9a1 --- /dev/null +++ b/wpilibcExamples/src/main/cpp/snippets/EncoderHoming/cpp/Robot.cpp @@ -0,0 +1,40 @@ +// 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 + +/** + * Encoder mechanism homing snippets for frc-docs. + * https://docs.wpilib.org/en/stable/docs/software/hardware-apis/sensors/encoders-software.html + */ +class Robot : public frc::TimedRobot { + public: + void AutonomousPeriodic() override { + // Runs the motor backwards at half speed until the limit switch is pressed + // then turn off the motor and reset the encoder + if (!m_limit.Get()) { + m_spark.Set(-0.5); + } else { + m_spark.Set(0); + m_encoder.Reset(); + } + } + + private: + frc::Encoder m_encoder{0, 1}; + frc::Spark m_spark{0}; + // Limit switch on DIO 2 + frc::DigitalInput m_limit{2}; +}; + +#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 5edd312bb9..ccf912c5db 100644 --- a/wpilibcExamples/src/main/cpp/snippets/snippets.json +++ b/wpilibcExamples/src/main/cpp/snippets/snippets.json @@ -19,5 +19,38 @@ ], "foldername": "DutyCycleEncoder", "gradlebase": "cpp" + }, + { + "name": "AnalogEncoder", + "description": "Snippets of AnalogEncoder class usage for frc-docs.", + "tags": [ + "Hardware", + "Encoder", + "Analog" + ], + "foldername": "AnalogEncoder", + "gradlebase": "cpp" + }, + { + "name": "EncoderDrive", + "description": "Snippets of driving to a distance for frc-docs.", + "tags": [ + "Hardware", + "Encoder", + "Differential Drive" + ], + "foldername": "EncoderDrive", + "gradlebase": "cpp" + }, + { + "name": "EncoderHoming", + "description": "Snippets of homing a mechanism for frc-docs.", + "tags": [ + "Hardware", + "Encoder", + "Digital Input" + ], + "foldername": "EncoderHoming", + "gradlebase": "cpp" } ] diff --git a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analogencoder/Main.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analogencoder/Main.java new file mode 100644 index 0000000000..0e074925d0 --- /dev/null +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analogencoder/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.analogencoder; + +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/analogencoder/Robot.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analogencoder/Robot.java new file mode 100644 index 0000000000..aa09daf933 --- /dev/null +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analogencoder/Robot.java @@ -0,0 +1,33 @@ +// 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.analogencoder; + +import edu.wpi.first.wpilibj.AnalogEncoder; +import edu.wpi.first.wpilibj.TimedRobot; + +/** + * AnalogEncoder snippets for frc-docs. + * https://docs.wpilib.org/en/stable/docs/software/hardware-apis/sensors/encoders-software.html + */ +public class Robot extends TimedRobot { + // Initializes an analog encoder on Analog Input pin 0 + AnalogEncoder m_encoder = new AnalogEncoder(0); + + // Initializes an analog encoder on DIO pins 0 to return a value of 4 for + // a full rotation, with the encoder reporting 0 half way through rotation (2 + // out of 4) + AnalogEncoder m_encoderFR = new AnalogEncoder(0, 4.0, 2.0); + + /** Called once at the beginning of the robot program. */ + public Robot() {} + + @Override + public void teleopPeriodic() { + // Gets the rotation + m_encoder.get(); + + m_encoderFR.get(); + } +} diff --git a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/encoderdrive/Main.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/encoderdrive/Main.java new file mode 100644 index 0000000000..e351e54948 --- /dev/null +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/encoderdrive/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.encoderdrive; + +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/encoderdrive/Robot.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/encoderdrive/Robot.java new file mode 100644 index 0000000000..de41a19102 --- /dev/null +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/encoderdrive/Robot.java @@ -0,0 +1,48 @@ +// 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.encoderdrive; + +import edu.wpi.first.wpilibj.Encoder; +import edu.wpi.first.wpilibj.TimedRobot; +import edu.wpi.first.wpilibj.drive.DifferentialDrive; +import edu.wpi.first.wpilibj.motorcontrol.Spark; + +/** + * Encoder drive to distance snippets for frc-docs. + * https://docs.wpilib.org/en/stable/docs/software/hardware-apis/sensors/encoders-software.html + */ +public class Robot extends TimedRobot { + // Creates an encoder on DIO ports 0 and 1 + Encoder m_encoder = new Encoder(0, 1); + // Initialize motor controllers and drive + Spark m_leftLeader = new Spark(0); + Spark m_leftFollower = new Spark(1); + Spark m_rightLeader = new Spark(2); + Spark m_rightFollower = new Spark(3); + DifferentialDrive m_drive = new DifferentialDrive(m_leftLeader::set, m_rightLeader::set); + + /** Called once at the beginning of the robot program. */ + public Robot() { + // Configures the encoder's distance-per-pulse + // The robot moves forward 1 foot per encoder rotation + // There are 256 pulses per encoder rotation + m_encoder.setDistancePerPulse(1.0 / 256.0); + // Invert the right side of the drivetrain. You might have to invert the other side + m_rightLeader.setInverted(true); + // Configure the followers to follow the leaders + m_leftLeader.addFollower(m_leftFollower); + m_rightLeader.addFollower(m_rightFollower); + } + + /** Drives forward at half speed until the robot has moved 5 feet, then stops. */ + @Override + public void autonomousPeriodic() { + if (m_encoder.getDistance() < 5.0) { + m_drive.tankDrive(0.5, 0.5); + } else { + m_drive.tankDrive(0.0, 0.0); + } + } +} diff --git a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/encoderhoming/Main.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/encoderhoming/Main.java new file mode 100644 index 0000000000..f014009221 --- /dev/null +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/encoderhoming/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.encoderhoming; + +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/encoderhoming/Robot.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/encoderhoming/Robot.java new file mode 100644 index 0000000000..34e60d1b5b --- /dev/null +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/encoderhoming/Robot.java @@ -0,0 +1,35 @@ +// 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.encoderhoming; + +import edu.wpi.first.wpilibj.DigitalInput; +import edu.wpi.first.wpilibj.Encoder; +import edu.wpi.first.wpilibj.TimedRobot; +import edu.wpi.first.wpilibj.motorcontrol.Spark; + +/** + * Encoder mechanism homing snippets for frc-docs. + * https://docs.wpilib.org/en/stable/docs/software/hardware-apis/sensors/encoders-software.html + */ +public class Robot extends TimedRobot { + Encoder m_encoder = new Encoder(0, 1); + Spark m_spark = new Spark(0); + // Limit switch on DIO 2 + DigitalInput m_limit = new DigitalInput(2); + + /** + * Runs the motor backwards at half speed until the limit switch is pressed then turn off the + * motor and reset the encoder. + */ + @Override + public void autonomousPeriodic() { + if (!m_limit.get()) { + m_spark.set(-0.5); + } else { + m_spark.set(0.0); + m_encoder.reset(); + } + } +} 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 abc1bd45e1..0f44d35eb3 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 @@ -21,5 +21,41 @@ "foldername": "dutycycleencoder", "gradlebase": "java", "mainclass": "Main" + }, + { + "name": "AnalogEncoder", + "description": "Snippets of AnalogEncoder class usage for frc-docs.", + "tags": [ + "Hardware", + "Encoder", + "Analog" + ], + "foldername": "analogencoder", + "gradlebase": "java", + "mainclass": "Main" + }, + { + "name": "EncoderDrive", + "description": "Snippets of driving to a distance for frc-docs.", + "tags": [ + "Hardware", + "Encoder", + "Differential Drive" + ], + "foldername": "encoderdrive", + "gradlebase": "java", + "mainclass": "Main" + }, + { + "name": "EncoderHoming", + "description": "Snippets of homing a mechanism for frc-docs.", + "tags": [ + "Hardware", + "Encoder", + "Digital Input" + ], + "foldername": "encoderhoming", + "gradlebase": "java", + "mainclass": "Main" } ]