Add examples for DMA, DutyCycle, DutyCycleEncoder and AddressableLED (#2100)

This commit is contained in:
Thad House
2019-11-18 22:12:17 -08:00
committed by Peter Johnson
parent 5891628112
commit 500c43fb84
14 changed files with 538 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 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.addressableled;
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,57 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 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.addressableled;
import edu.wpi.first.wpilibj.AddressableLED;
import edu.wpi.first.wpilibj.AddressableLEDBuffer;
import edu.wpi.first.wpilibj.TimedRobot;
public class Robot extends TimedRobot {
private AddressableLED m_led;
private AddressableLEDBuffer m_ledBuffer;
private int m_count;
@Override
public void robotInit() {
// PWM port 0
// Must be a PWM header, not MXP or DIO
m_led = new AddressableLED(0);
// Default to a length of 12, start empty output
// Length is expensive to set, so only set it once, then just update data
m_led.setLength(12);
// Reuse buffer
m_ledBuffer = new AddressableLEDBuffer(12);
m_led.setData(m_ledBuffer);
m_led.start();
}
@Override
public void robotPeriodic() {
// Zero out all LEDS
for (int i = 0; i < m_ledBuffer.getLength(); i++) {
m_ledBuffer.setLED(i, 0, 0, 0);
}
// Set 1 single LED to red
m_ledBuffer.setLED(m_count, 50, 0, 0);
// Continue moving LED
m_count++;
if (m_count >= 12) {
m_count = 0;
}
// Buffer must be written to update.
m_led.setData(m_ledBuffer);
}
}

View File

@@ -0,0 +1,29 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 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.dutycycleencoder;
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,49 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 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.dutycycleencoder;
import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj.DutyCycleEncoder;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
@SuppressWarnings({"PMD.SingularField"})
public class Robot extends TimedRobot {
private DigitalInput m_input;
private DutyCycleEncoder m_dutyCycleEncoder;
@Override
public void robotInit() {
m_input = new DigitalInput(0);
m_dutyCycleEncoder = new DutyCycleEncoder(m_input);
// Set to 0.5 units per rotation
m_dutyCycleEncoder.setDistancePerRotation(0.5);
}
@Override
public void robotPeriodic() {
// Connected can be checked, and uses the frequency of the encoder
boolean connected = m_dutyCycleEncoder.isConnected();
// Duty Cycle Frequency in Hz
int frequency = m_dutyCycleEncoder.getFrequency();
// Output of encoder
double output = m_dutyCycleEncoder.get();
// Output scaled by DistancePerPulse
double distance = m_dutyCycleEncoder.getDistance();
SmartDashboard.putBoolean("Connected", connected);
SmartDashboard.putNumber("Frequency", frequency);
SmartDashboard.putNumber("Output", output);
SmartDashboard.putNumber("Distance", distance);
}
}

View File

@@ -0,0 +1,29 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 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.dutycycleinput;
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,39 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 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.dutycycleinput;
import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj.DutyCycle;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
@SuppressWarnings({"PMD.SingularField"})
public class Robot extends TimedRobot {
private DigitalInput m_input;
private DutyCycle m_dutyCycle;
@Override
public void robotInit() {
m_input = new DigitalInput(0);
m_dutyCycle = new DutyCycle(m_input);
}
@Override
public void robotPeriodic() {
// Duty Cycle Frequency in Hz
int frequency = m_dutyCycle.getFrequency();
// Output of duty cycle, ranging from 0 to 1
// 1 is fully on, 0 is fully off
double output = m_dutyCycle.getOutput();
SmartDashboard.putNumber("Frequency", frequency);
SmartDashboard.putNumber("Duty Cycle", output);
}
}

View File

@@ -449,5 +449,38 @@
"gradlebase": "java",
"mainclass": "Main",
"commandversion": 1
},
{
"name": "Duty Cycle Encoder",
"description": "Demonstrates the use of the Duty Cycle Encoder class",
"tags": [
"Getting Started with Java"
],
"foldername": "dutycycleencoder",
"gradlebase": "java",
"mainclass": "Main",
"commandversion": 2
},
{
"name": "Duty Cycle Input",
"description": "Demonstrates the use of the Duty Cycle class",
"tags": [
"Getting Started with Java"
],
"foldername": "dutycycleinput",
"gradlebase": "java",
"mainclass": "Main",
"commandversion": 2
},
{
"name": "Addressable LED",
"description": "Demonstrates the use of the Addressable LED class",
"tags": [
"Getting Started with Java"
],
"foldername": "addressableled",
"gradlebase": "java",
"mainclass": "Main",
"commandversion": 2
}
]