-
Notifications
You must be signed in to change notification settings - Fork 642
[examples] Add compilable code snippets #7909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
wpilibcExamples/src/main/cpp/snippets/Encoder/cpp/Robot.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// 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 <numbers> | ||
|
||
#include <frc/Encoder.h> | ||
#include <frc/TimedRobot.h> | ||
#include <frc/smartdashboard/SmartDashboard.h> | ||
#include <wpi/deprecated.h> | ||
|
||
/** | ||
* Encoder snippets for frc-docs. | ||
* https://docs.wpilib.org/en/stable/docs/software/hardware-apis/sensors/encoders-software.html | ||
*/ | ||
WPI_IGNORE_DEPRECATED | ||
class Robot : public frc::TimedRobot { | ||
public: | ||
Robot() { | ||
// Configures the encoder to return a distance of 4 for every 256 pulses | ||
// Also changes the units of getRate | ||
m_encoder.SetDistancePerPulse(4.0 / 256.0); | ||
// Configures the encoder to consider itself stopped after .1 seconds | ||
m_encoder.SetMaxPeriod(0.1_s); | ||
// Configures the encoder to consider itself stopped when its rate is below | ||
// 10 | ||
m_encoder.SetMinRate(10); | ||
// Reverses the direction of the encoder | ||
m_encoder.SetReverseDirection(true); | ||
// Configures an encoder to average its period measurement over 5 samples | ||
// Can be between 1 and 127 samples | ||
m_encoder.SetSamplesToAverage(5); | ||
} | ||
|
||
void TeleopPeriodic() override { | ||
// Gets the distance traveled | ||
m_encoder.GetDistance(); | ||
|
||
// Gets the current rate of the encoder | ||
m_encoder.GetRate(); | ||
|
||
// Gets whether the encoder is stopped | ||
m_encoder.GetStopped(); | ||
|
||
// Gets the last direction in which the encoder moved | ||
m_encoder.GetDirection(); | ||
|
||
// Gets the current period of the encoder | ||
m_encoder.GetPeriod(); | ||
|
||
// Resets the encoder to read a distance of zero | ||
m_encoder.Reset(); | ||
} | ||
|
||
private: | ||
// Initializes an encoder on DIO pins 0 and 1 | ||
// Defaults to 4X decoding and non-inverted | ||
frc::Encoder m_encoder{0, 1}; | ||
|
||
// Initializes an encoder on DIO pins 0 and 1 | ||
// 2X encoding and non-inverted | ||
frc::Encoder m_encoder2x{0, 1, false, frc::Encoder::EncodingType::k2X}; | ||
}; | ||
|
||
#ifndef RUNNING_FRC_TESTS | ||
int main() { | ||
return frc::StartRobot<Robot>(); | ||
} | ||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[ | ||
{ | ||
"name": "Encoder", | ||
"description": "Snippets of Encoder class usage for frc-docs.", | ||
"tags": [ | ||
"Hardware", | ||
"Encoder" | ||
], | ||
"foldername": "Encoder", | ||
"gradlebase": "cpp" | ||
} | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/encoder/Main.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.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); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to show return values here? (I know we'll need to disable unused variable warnings).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't currently show return values in frc-docs encoder page.
I think the question would be whether you declare the variable inline, which shows the type and would be more useful to new programmers, but isn't what you'd want to do in a periodic function in the snippet, or just set a variable.
I'd tend to keep it exactly as it is in the docs to make it faster to get everything converted to snippets and find/fix breakage, and do improvements later.