Skip to content

Commit 271b1f2

Browse files
committed
more examples
1 parent 6ac3cf2 commit 271b1f2

File tree

5 files changed

+157
-1
lines changed

5 files changed

+157
-1
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) FIRST and other WPILib contributors.
2+
// Open Source Software; you can modify and/or share it under the terms of
3+
// the WPILib BSD license file in the root directory of this project.
4+
5+
package frc.robot.examples;
6+
7+
import edu.wpi.first.wpilibj.DigitalInput;
8+
9+
/**
10+
* Examples of using digital inputs.
11+
*/
12+
public final class DigitalInputExamples {
13+
14+
/**
15+
* Digital Inputs detect a true or false value.
16+
* They can be limit switches to determine when a motor has reached the end of "its track".
17+
* They can also be photosensors to detect when an object is inside the robot or intake.
18+
*/
19+
private void DigitalInput_Example() {
20+
// Create DigitalInput using digital input channel on RoboRIO - these are simple sensors that just return true or false
21+
final int channelOnRoboRio = 1;
22+
final DigitalInput sensor = new DigitalInput(channelOnRoboRio);
23+
24+
// Get if the sensor is tripped or pressed
25+
boolean isTripped = sensor.get();
26+
}
27+
28+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) FIRST and other WPILib contributors.
2+
// Open Source Software; you can modify and/or share it under the terms of
3+
// the WPILib BSD license file in the root directory of this project.
4+
5+
package frc.robot.examples;
6+
7+
import com.kauailabs.navx.frc.AHRS;
8+
9+
import edu.wpi.first.wpilibj.SerialPort.Port;
10+
11+
/**
12+
* Examples of using the NavX gyro
13+
* https://pdocs.kauailabs.com/navx-mxp/
14+
*/
15+
public final class GyroExamples {
16+
17+
/**
18+
* https://simple.wikipedia.org/wiki/Pitch,_yaw,_and_roll
19+
*/
20+
private void NavX_GetRobotAngles_Example() {
21+
// Robot has One NavX usually either connected via MXP or USB on the RoboRIO
22+
// final Port navxPort = Port.kUSB;
23+
final Port navxPort = Port.kMXP;
24+
final AHRS navx = new AHRS(navxPort);
25+
26+
// robot's angle in degrees
27+
// -360 degrees is the same as 0 degrees is the same as 360 degreees
28+
// -540 degrees is the same as -180 degrees is the same as 180 degrees is the same as 540 degrees
29+
// and so on...
30+
double robotAngle = navx.getAngle();
31+
32+
// Yaw, Roll, Pitch always return (-180, 180]
33+
// that is, as low as -179.99 degrees and as high as 180.0 degrees
34+
double yaw = navx.getYaw();
35+
double roll = navx.getRoll();
36+
double pitch = navx.getPitch();
37+
38+
// robot heading rate of change in degrees/second
39+
double robotAngleRateOfChange = navx.getRate();
40+
}
41+
42+
}

src/main/java/frc/robot/examples/MotorControllerExamples.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public final class MotorControllerExamples {
2222
* https://www.andymark.com/products/spark-bushed-dc-motor-controller
2323
*/
2424
private void PWM_Controller_Example() {
25-
// Create the PWM Motor Controller
25+
// Create the PWM Motor Controller using connected channel on RoboRIO
2626
// in this case we are using a Spark from RevRobotics
2727
// The Spark motor object normally consturcted inside the Subsystem the motor belongs to
2828
final int pwmChannel = 1;
@@ -83,7 +83,14 @@ private void CAN_SparkMax_Controller_Example() {
8383

8484
// PIDF Controller for Velocity and Postion control modes
8585
SparkPIDController pidControllerForVelocityOrPositonControl = canMotorController.getPIDController();
86+
8687
// These modes requires PIDF values
88+
89+
// https://docs.wpilib.org/en/stable/docs/software/advanced-controls/introduction/introduction-to-pid.html
90+
// https://docs.wpilib.org/en/stable/docs/software/advanced-controls/introduction/pid-video.html#pid-introduction-video-by-wpi
91+
// The math looks harder than it really is - understanding at a conceptual level will help you understand the rigor more
92+
93+
// set PIDF values - these values are somewhat empirical and change with every unique system/robot
8794
pidControllerForVelocityOrPositonControl.setP(1);
8895
pidControllerForVelocityOrPositonControl.setI(0.01);
8996
pidControllerForVelocityOrPositonControl.setD(0.5);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) FIRST and other WPILib contributors.
2+
// Open Source Software; you can modify and/or share it under the terms of
3+
// the WPILib BSD license file in the root directory of this project.
4+
5+
package frc.robot.examples;
6+
7+
import edu.wpi.first.wpilibj.DoubleSolenoid;
8+
import edu.wpi.first.wpilibj.PneumaticsModuleType;
9+
import edu.wpi.first.wpilibj.Solenoid;
10+
import edu.wpi.first.wpilibj.DoubleSolenoid.Value;
11+
12+
/**
13+
* Pneumatics examples.
14+
*/
15+
public final class PneumaticsExamples {
16+
17+
/*
18+
* Solenoids can be used to controller pneumatic cylinders are set to true or false
19+
*/
20+
private void Solenoid_Example() {
21+
final int digialChannelOnRoboRio = 2;
22+
final Solenoid piston = new Solenoid(PneumaticsModuleType.REVPH, digialChannelOnRoboRio);
23+
24+
// Extend
25+
piston.set(true);
26+
27+
// Retract
28+
piston.set(false);
29+
}
30+
31+
/*
32+
* Sometimes solenoids are dual action and have 3 potential values
33+
* Forward, Reverse, Off
34+
*/
35+
private void DoubleSolenoid_Example() {
36+
final int forwardChannelOnRoboRio = 2;
37+
final int reverseChannelOnRoboRio = 2;
38+
final DoubleSolenoid piston = new DoubleSolenoid(PneumaticsModuleType.REVPH, forwardChannelOnRoboRio, reverseChannelOnRoboRio);
39+
40+
// Extend
41+
piston.set(Value.kForward);
42+
43+
// Retract
44+
piston.set(Value.kReverse);
45+
46+
// Shortly after setting Forward or Reverse, turn off to prevent unnecessary current flowing through the solenoid.
47+
// Usually this can be done by setting the desired value, waiting half a second then turning off the solenoid
48+
piston.set(Value.kOff);
49+
}
50+
51+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) FIRST and other WPILib contributors.
2+
// Open Source Software; you can modify and/or share it under the terms of
3+
// the WPILib BSD license file in the root directory of this project.
4+
5+
package frc.robot.examples;
6+
7+
import edu.wpi.first.wpilibj.Ultrasonic;
8+
9+
/**
10+
* Ultrasonic sensor examples.
11+
*/
12+
public final class UltrasonicExamples {
13+
14+
/*
15+
* Example Ultrasonic Sensor: https://kb.vex.com/hc/en-us/articles/360038608771-Using-the-V5-3-Wire-UltraSonic-Range-Finder
16+
*/
17+
private void Ultrasonic_Distance_Example() {
18+
// Create sensor using an input and output channel on the RoboRIO
19+
final int pingChannel = 1;
20+
final int echoChannel = 2;
21+
final Ultrasonic ultrasonic = new Ultrasonic(pingChannel, echoChannel);
22+
23+
// Supports range in both inches and mm
24+
double distanceToObjectInInches = ultrasonic.getRangeInches();
25+
double distanceToObjectInMillimeters = ultrasonic.getRangeMM();
26+
}
27+
28+
}

0 commit comments

Comments
 (0)