Sensors and Feedback
In this training module, we will explore the various sensors commonly used in FRC robots. We will also cover how to read data from these sensors and use that data to provide feedback for robot control in our software.
Clean up noisy sensor readings with WPILib's filters and measure the lag each one adds. Open the Signal Filter Lab in a new tab โ
Presentationโ
The exercises below are the Sensors and Feedback lesson in Scriptum. Open the lesson, start the simulated robot and follow its README. Each step is checked with the Verify button.
New Java in this lesson: if / else statements, comparison operators, and static final constants.
Each side of the drivetrain has a Falcon 500 driving the wheels through an 10.71:1 gearbox: the motor turns 10.71 times for every 1 turn of the wheel. The encoder sits inside the motor, so it counts motor turns. In the lesson you set GEAR_RATIO to 10.71 so the drivetrain can divide that out and report the wheel's angle.
Exercisesโ
Sensorsโ
- Convert the encoder readings (
getLeftPositionRad(),getRightPositionRad()) from wheel radians to meters- meters = wheel radians * wheel radius (0.0762 m)
- Check it with the cross-out (railroad track) method: write each conversion as a fraction worth 1, cross out the units that cancel, and the unit left over is the unit of your answer
- Why radians: from rad/s, m/s takes two fractions (the gear ratio and the wheel radius). From a motor's RPM it takes four: รท 60 for seconds, ร 2ฯ for radians, then the gear ratio and the wheel radius. Same 4.75 m/s, two more places to slip
- Show chassis speeds in AdvantageScope
- Linear speed in m/s (forward / backward)
- Angular speed in rad/s (rotation)
- Show distance traveled by driving
- Use encoders to measure distance
linear_speedandangular_speedare local variables, made fresh each loop.distance_meters_is a field (a class member), so it keeps its value between loops andautonomousPeriodic()can read it. The trailing_is the team's naming for fields: never putdoublein front of it inside a method, or you make a new local that hides the field
Loggingโ
- Write the logging lines yourself: at the end of
robotPeriodic(), add oneMwLog.logline for each valueRobot/LinearSpeedwithlinear_speed(m/s)Robot/AngularSpeedwithangular_speed(rad/s)Robot/Distancewithdistance_meters_(m)- Keys are case sensitive, so spell them exactly
- Why it matters: the robot runs 50 loops a second and a match happens once, so the log is the only record of what the code measured. Log every new value before you trust it
Conditionsโ
- Add a deadband so the robot ignores small stick drift
- Declare a
DEADBANDconstant instead of a magic number: a bare0.1in the code says nothing about what it's for, and every copy has to be changed by hand - Use an
ifstatement on each stick to set small values to zero
- Declare a
Feedbackโ
- Implement a simple Bang Bang controller to drive 2 meters in autonomous
- Use encoders to measure distance
- Use an
if/else: a constant speed until the robot reaches the target distance, then stop - Where does the robot actually stop? Why?
- Run Auto a second time: resetting the encoders in
autonomousInit()makes it start counting from zero again
Related Documentationโ
Softwareโ
- Variables and Data Types: Declaring variables and constants.
- Programming Standards: How the team names locals, fields (trailing
_), constants, methods and classes. - AdvantageScope: Viewing the values you log with
MwLog.log. - Conditions: Explanation of conditional statements in programming.
- Control Types: Overview of different control types used in robotics.
- VSCode: Our primary Integrated Development Environment (IDE) for programming.
- Elastic: Guide on using the Elastic dashboard for monitoring robot data.