Add missing Java examples (#841)

This commit is contained in:
Abiel Fernandez
2019-08-05 11:34:02 +08:00
committed by Peter Johnson
parent 6411bd79c6
commit ba9b517427
9 changed files with 396 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2018-2019 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.canpdp;
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.
*
* <p>If you change your main robot class, change the parameter type.
*/
public static void main(String... args) {
RobotBase.startRobot(Robot::new);
}
}

View File

@@ -0,0 +1,43 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2019 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.canpdp;
import edu.wpi.first.wpilibj.PowerDistributionPanel;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
/**
* This is a sample program showing how to retrieve information from the Power Distribution Panel
* via CAN. The information will be displayed under variables through the SmartDashboard.
*/
public class Robot extends TimedRobot {
private static final int kPDPId = 0;
private final PowerDistributionPanel m_pdp = new PowerDistributionPanel(kPDPId);
@Override
public void robotPeriodic() {
/*
* Get the current going through channel 7, in Amperes. The PDP returns the
* current in increments of 0.125A. At low currents
* the current readings tend to be less accurate.
*/
SmartDashboard.putNumber("Current Channel 7", m_pdp.getCurrent(7));
/*
* Get the voltage going into the PDP, in Volts.
* The PDP returns the voltage in increments of 0.05 Volts.
*/
SmartDashboard.putNumber("Voltage", m_pdp.getVoltage());
/*
* Retrieves the temperature of the PDP, in degrees Celsius.
*/
SmartDashboard.putNumber("Temprature", m_pdp.getTemperature());
}
}

View File

@@ -0,0 +1,29 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2018-2019 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.encoder;
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.
*
* <p>If you change your main robot class, change the parameter type.
*/
public static void main(String... args) {
RobotBase.startRobot(Robot::new);
}
}

View File

@@ -0,0 +1,74 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2019 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.encoder;
import edu.wpi.first.wpilibj.CounterBase;
import edu.wpi.first.wpilibj.Encoder;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
/**
* Sample program displaying the value of a quadrature encoder on the SmartDashboard. Quadrature
* Encoders are digital sensors which can detect the amount the encoder has rotated since starting
* as well as the direction in which the encoder shaft is rotating. However, encoders can not tell
* you the absolute position of the encoder shaft (ie, it considers where it starts to be the zero
* position, no matter where it starts), and so can only tell you how much the encoder has rotated
* since starting. Depending on the precision of an encoder, it will have fewer or greater ticks per
* revolution; the number of ticks per revolution will affect the conversion between ticks and
* distance, as specified by DistancePerPulse. One of the most common uses of encoders is in the
* drivetrain, so that the distance that the robot drives can be precisely controlled during the
* autonomous mode.
*/
public class Robot extends TimedRobot {
/**
* The Encoder object is constructed with 4 parameters, the last two being optional. The first two
* parameters (1, 2 in this case) refer to the ports on the roboRIO which the encoder uses.
* Because a quadrature encoder has two signal wires, the signal from two DIO ports on the roboRIO
* are used. The third (optional) parameter is a boolean which defaults to false. If you set this
* parameter to true, the direction of the encoder will be reversed, in case it makes more sense
* mechanically. The final (optional) parameter specifies encoding rate (k1X, k2X, or k4X) and
* defaults to k4X. Faster (k4X) encoding gives greater positional precision but more noise in the
* rate.
*/
private final Encoder m_encoder =
new Encoder(1, 2, false, CounterBase.EncodingType.k4X);
@Override
public void robotInit() {
/*
* Defines the number of samples to average when determining the rate.
* On a quadrature encoder, values range from 1-255;
* larger values result in smoother but potentially
* less accurate rates than lower values.
*/
m_encoder.setSamplesToAverage(5);
/*
* Defines how far the mechanism attached to the encoder moves per pulse. In
* this case, we assume that a 360 count encoder is directly
* attached to a 3 inch diameter (1.5inch radius) wheel,
* and that we want to measure distance in inches.
*/
m_encoder.setDistancePerPulse(1.0 / 360.0 * 2.0 * 3.1415 * 1.5);
/*
* Defines the lowest rate at which the encoder will
* not be considered stopped, for the purposes of
* the GetStopped() method. Units are in distance / second,
* where distance refers to the units of distance
* that you are using, in this case inches.
*/
m_encoder.setMinRate(1.0);
}
@Override
public void teleopPeriodic() {
SmartDashboard.putNumber("Encoder Distance", m_encoder.getDistance());
SmartDashboard.putNumber("Encoder Rate", m_encoder.getRate());
}
}

View File

@@ -35,6 +35,55 @@
"gradlebase": "java",
"mainclass": "Main"
},
{
"name": "PDP CAN Monitoring",
"description": "Demonstrate using CAN to monitor the voltage, current, and temperature in the Power Distribution Panel.",
"tags": [
"Complete List",
"CAN",
"Sensors"
],
"foldername": "canpdp",
"gradlebase": "java",
"mainclass": "Main"
},
{
"name": "Solenoids",
"description": "Demonstrate controlling a single and double solenoid from Joystick buttons.",
"tags": [
"Actuators",
"Joystick",
"Pneumatics",
"Complete List"
],
"foldername": "solenoid",
"gradlebase": "java",
"mainclass": "Main"
},
{
"name": "Encoder",
"description": "Demonstrate displaying the value of a quadrature encoder on the SmartDashboard.",
"tags": [
"Complete List",
"Digital",
"Sensors"
],
"foldername": "encoder",
"gradlebase": "java",
"mainclass": "Main"
},
{
"name": "Relay",
"description": "Demonstrate controlling a Relay from Joystick buttons.",
"tags": [
"Actuators",
"Joystick",
"Complete List"
],
"foldername": "relay",
"gradlebase": "java",
"mainclass": "Main"
},
{
"name": "Ultrasonic",
"description": "Demonstrate maintaining a set distance using an ultrasonic sensor.",

View File

@@ -0,0 +1,29 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2018-2019 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.relay;
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.
*
* <p>If you change your main robot class, change the parameter type.
*/
public static void main(String... args) {
RobotBase.startRobot(Robot::new);
}
}

View File

@@ -0,0 +1,54 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2019 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.relay;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Relay;
import edu.wpi.first.wpilibj.TimedRobot;
/**
* This is a sample program which uses joystick buttons to control a relay. A Relay (generally a
* spike) has two outputs, each of which can be at either 0V or 12V and so can be used for actions
* such as turning a motor off, full forwards, or full reverse, and is generally used on the
* compressor. This program uses two buttons on a joystick and each button corresponds to one
* output; pressing the button sets the output to 12V and releasing sets it to 0V.
*/
public class Robot extends TimedRobot {
private final Joystick m_stick = new Joystick(0);
private final Relay m_relay = new Relay(0);
private static final int kRelayForwardButton = 1;
private static final int kRelayReverseButton = 2;
@Override
public void teleopPeriodic() {
/*
* Retrieve the button values. GetRawButton will
* return true if the button is pressed and false if not.
*/
boolean forward = m_stick.getRawButton(kRelayForwardButton);
boolean reverse = m_stick.getRawButton(kRelayReverseButton);
/*
* Depending on the button values, we want to use one of
* kOn, kOff, kForward, or kReverse. kOn sets both outputs to 12V,
* kOff sets both to 0V, kForward sets forward to 12V
* and reverse to 0V, and kReverse sets reverse to 12V and forward to 0V.
*/
if (forward && reverse) {
m_relay.set(Relay.Value.kOn);
} else if (forward) {
m_relay.set(Relay.Value.kForward);
} else if (reverse) {
m_relay.set(Relay.Value.kReverse);
} else {
m_relay.set(Relay.Value.kOff);
}
}
}

View File

@@ -0,0 +1,29 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2018-2019 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.solenoid;
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.
*
* <p>If you change your main robot class, change the parameter type.
*/
public static void main(String... args) {
RobotBase.startRobot(Robot::new);
}
}

View File

@@ -0,0 +1,60 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2019 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.solenoid;
import edu.wpi.first.wpilibj.DoubleSolenoid;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Solenoid;
import edu.wpi.first.wpilibj.TimedRobot;
/**
* This is a sample program showing the use of the solenoid classes during operator control. Three
* buttons from a joystick will be used to control two solenoids: One button to control the position
* of a single solenoid and the other two buttons to control a double solenoid. Single solenoids can
* either be on or off, such that the air diverted through them goes through either one channel or
* the other. Double solenoids have three states: Off, Forward, and Reverse. Forward and Reverse
* divert the air through the two channels and correspond to the on and off of a single solenoid,
* but a double solenoid can also be "off", where the solenoid will remain in its default power off
* state. Additionally, double solenoids take up two channels on your PCM whereas single solenoids
* only take a single channel.
*/
public class Robot extends TimedRobot {
private final Joystick m_stick = new Joystick(0);
// Solenoid corresponds to a single solenoid.
private final Solenoid m_solenoid = new Solenoid(0);
// DoubleSolenoid corresponds to a double solenoid.
private final DoubleSolenoid m_doubleSolenoid = new DoubleSolenoid(1, 2);
private static final int kSolenoidButton = 1;
private static final int kDoubleSolenoidForward = 2;
private static final int kDoubleSolenoidReverse = 3;
@Override
public void teleopPeriodic() {
/*
* The output of GetRawButton is true/false depending on whether
* the button is pressed; Set takes a boolean for whether
* to use the default (false) channel or the other (true).
*/
m_solenoid.set(m_stick.getRawButton(kSolenoidButton));
/*
* In order to set the double solenoid, if just one button
* is pressed, set the solenoid to correspond to that button.
* If both are pressed, set the solenoid will be set to Forwards.
*/
if (m_stick.getRawButton(kDoubleSolenoidForward)) {
m_doubleSolenoid.set(DoubleSolenoid.Value.kForward);
} else if (m_stick.getRawButton(kDoubleSolenoidReverse)) {
m_doubleSolenoid.set(DoubleSolenoid.Value.kReverse);
}
}
}