Introducing PurpleLib: A Custom Library for Robot Code

Hello y’all!

LASA Purple Haze (Team 418) is proud to introduce PurpleLib, a library we have been working on that provides custom wrappers and utilities to speed up the development of advanced software functionality (such as hardware logging and swerve).

Features:

  • Wrappers for an assortment of hardware objects which automatically log key inputs using Mechanical Advantage’s AdvantageKit
    • Includes generics such as analog inputs, limit switches, servos, compressors, and solenoids
    • Includes vendor-specific hardware from REV Robotics, CTR Electronics (using the Version 6 API), and Kauai Labs
  • Extensive swerve support—including MAXSwerve module support along with simulation, rotation PID, traction control, etc.
  • Quality of life features—including configurable input maps, LED strip support, JSON read/write, as well as battery scanning based off Mechanical Advantage’s great work on this topic

For a more complete list of the library’s features, please reference the README.

Installation / Getting Started:

After following the instructions outlined in the README, please check out some of the basic examples at GitHub - lasarobotics/PurpleLibExamples which show the use of LASA Robotics’ PurpleLib library in different situations.

Existing Projects / Status:

PurpleLib is currently being used in our offseason Swerve project (available at GitHub - lasarobotics/PurpleSwerve), making use of hardware primarily from REV Robotics. Moreover, the library will be used to develop our robot code for the 2024 season (which is available at GitHub - lasarobotics/PH2024: 2024 Purple Haze FRC). We are also working on a custom pathfinding solution, known as PurplePath.

The project is still in early development and not all features have been tested on physical hardware as of yet, such as those related to CTRE hardware. Feel free to open a new issue for any bugs you encounter or open a pull request for additional hardware/features. Let’s improve PurpleLib together!

Once again, we would like to thank 6328 Mechanical Advantage for AdvantageKit and AdvantageScope.

As mentioned, there are also various examples located at GitHub - lasarobotics/PurpleLibExamples.

Documentation:

Our API is documented with automatically generated Javadocs available here.

13 Likes

We prioritized what we thought to be common hardware that we use on the team, but we will slowly be adding support for more hardware through the season and in the future.

If there’s a particular hardware component that you feel is important feel free to request it here or in a GitHub issue as mentioned previously.

4 Likes

Versions 2024.1.9, 2024.2.0, and 2024.2.1 Released! :tada:

PurpleLib has been improved a lot over the past weeks, and we have released three new versions.

Features / Major Changes:

  • Updated vendor libraries
  • Traction control improvements
  • Additional Advantage Kit logging
  • Swerve odometer tracking the distance each module has traveled for wheel replacements
  • VictorSPX and TalonSRX hardware support!

As always, feel free to open a new issue for any bugs you encounter or open a pull request for additional hardware/features.

2 Likes

Heyo, I know this is an old post but I had to ask

I was thinking of utilizing advantage kit at the motor level too and was wondering how it worked out for you guys.

More specifically, I was wondering what you guys do if you need to run odometry at a higher rate than the roborio (and consequently on a separate thread).

Also, are you guys planning to keep on maintaining this library?

We do indeed plan on maintaining this library into the future, we just forget to update this post lol.

We found it pretty awesome to build in AdvantageKit support at the motor object level, as it essentially eliminated extra work in the robot project to log these values. We only have to do extra stuff to log higher level subsystem states.

We are working on high frequency odometry now.
I tried to think of the best way to do this that would apply to both the drivetrain use case and other use cases, and I came up with running an “input thread” for every motor controller, which can be run at a configurable frequency. The logging still occurs at 50Hz on the main thread, as calls to AdvantageKit “processInputs” is not thread safe. We’re working on seeing how high we can fast we can run the drivetrain pose estimation, as it appeared that 200Hz was too fast for the roboRIO to handle given this architecture. I hope to settle on 100Hz.

Also, we would very much appreciate any pull requests for more hardware support.

1 Like

I am thinking of just creating a single shared thread across all devices which runs at the speed of the odometry, and the rest will be at 50hz

While there are some nieche use cases for faster signals for things other than odometry, personally I never dealt with such thing, nor a mechanism which required more than 50 hertz to run correctly

When I’ll have some code written I’ll make sure to share (with performance!) and perhaps create a PR

Though this whole thing might take me a while so don’t expect anything soon

Take a look at what we’ve got so far. You can see in the “high-frequency-pose-estimation” branch.

While I generally agree that mechanisms haven’t needed high frequency, I like the flexibility of being able to do so, especially with the new robot controller on the horizon.

1 Like

We are exploring your library for use on a chassis using MK4I-L3 (6.75 ratio), Neo drive and steer motors, and a Rev ThroughBore encoder. We have downloaded your example code, which favors MAXSwerveModules. Unfortunately the JavaDocs isn’t working, so we are really struggling to find the right changes to make for the module switch. Thanks for making another alternative available for teams to try! - Jason

Hey sorry about that. JitPack has been acting weird for the Javadocs.
Try this link.
And you’ll wanna use Gradle versions from the develop branch, as we’re making changes and undergoing more development, now that it’s the offseason.

Team 5406 Celt-X has been working on support for the Mk4i and has gotten pretty far along. I largely support their structure for the Mk4i module, with a few changes that I’d recommend.

Potentially using a different encoder, like a Redux Canandcoder, or a REV through bore encoder adapter to use on the Mk4, so that you can do PID directly based on the absolute encoder.

Also, designing a “calibration tool” similar to the MAXSwerve module, for the Mk4, which makes “locking” the modules much easier, and simplifies some logic.

Thanks for the reply - that link for the docs works!

Last season we used CanCoders for steer and in the offseason we are testing Rev through bore encoders to help cut down on CAN traffic and just simplify. So this test is with the Rev through bore encoders.

We used YAGSL with last season and were very happy! As I am sure you follow, most of the problems teams have brining up robots is aligning and properly setting inversions. I am a fan of the simplicity of the gears facing left and using a straight edge to align wheels front to back.

Do you have intention of merging 5406’s work in, any timeline? - Jason

5406 hasn’t put in a merge request, so I don’t have a timeline. If there’s enough need, we could fast-track it, and replicate the work ourselves, with the mentioned changes.

What I like about using a calibration tool, is that you can pre-calibrate modules before putting it on the robot. And like I said, it simplifies logic in the code to have each module calibrated the same way, you just have a different offset in the code depending on where the module is located (left front, right front etc). And it makes calibrating much more foolproof. Slip the tool over the module, then zero the encoder with the REV Hardware Client, or CTRE Phoenix Tuner.

Paging @nuclearnerd about Celt-X’s plans to submit a merge request to PurpleLib for SDS Mk4 swerve modules.

I don’t get involved much in the software side of things. I think a PurpleLib pull request is still on the horizon, but I can’t say (or influence) when. I know our software team is doing some intensive rewrites this summer, especially in the run-up to the two summer off-seasons we plan to attend, so their focus is elsewhere.

In the meantime, our 2024 code base is open. That’s not as nice as an ordered contribution to your library, but while we wait you’re welcome to review it to see if there’s anything there of interest.

Apologies for the delays, PurpleLib 2025 is on the horizon with some new exciting features.

Took a while due to the REVLib 2025 changes, and other vendor hardware.

We hope to release it soon (hopefully before Christmas).

2 Likes

PurpleLib 2025

(Almost) A Year of Development Later…

Hey y’all,

It’s been a little while since our last full update, but we’ve been hard at work behind the scenes! Over the past year, our team has been actively developing and using PurpleLib, and we’ve rolled out several key features that make it even more flexible and useful for a wider range of teams. Complete documentation is—as always—available on the project’s GitHub repository. In the meantime, here’s a quick summary of the major updates.

Brand New Hardware Support

We’ve added support for several new devices, such as the Canandgyro and ThriftyNova (…pending the release of ThriftyLib 2025). We’ve also greatly expanded our support for CTRE devices, including the TalonFX motor controller (which wraps the new Phoenix 6 API).

One of our most important changes, however, is the addition of support for several new swerve modules—including those made by Swerve Drive Specialities (SDS) and West Coast Products (WCP)—for both REV and CTRE configurations. As of right now, the full list of swerve modules supported is:

  • REV MAXSwerve (REV Spark only)
  • SDS MK4/MK4i/MK4c/MK4n
  • WCP Swerve X/Xi/X2
  • TTB Swerve

Since support for more swerve modules has always been one of our most requested feature additions, we’ve also made it easy to add support for custom modules (or those from other vendors) by creating a respective abstract class in the swerve/child directory that follows the structure seen in the other module declarations.

We know this has been a long time coming, so we appreciate your patience :slight_smile:

The full list of supported hardware devices, including swerve modules, is available in the projects README. More information about using swerve modules in particular can be found here.

Swerve Parent Class

With the addition of new swerve modules, we’ve created a universal Swerve Drive template that can be extended to implement a drive subsystem with the same great features, regardless of hardware configuration or chosen module. This parent class currently supports the following functionality:

  • Robot rotation PID and anti-tip
  • Field centric or robot centric drive
  • Automatic module “locking”
  • Traction control
  • Swerve second order kinematics correction
  • Odometry
  • Easy PhotonVision integration
  • Aim at point (can be used for aiming at targets)

Plus, more features are always on the way!

Additional documentation on the swerve parent class is available here. For an example of how to implement a swerve drive subsystem with PurpleLib, please reference the PurpleSwerve repository.

PhotonVision

This year, our team has also been pouring a lot of effort into our vision systems, which make extensive use of PhotonVision. In terms of PurpleLib, we have created an AprilTag Camera class that wraps much of the PhotonLib boilerplate for pose estimation. It also filters out invalid poses to provide stable, accurate vision localization data to the pose estimator. Moreover, simulation support for AdvantageScope is built in!

Vision Simulation

We’ve also been experimenting with a custom machine learning pipeline on our own fork of PhotonVision, which communicates with a PyTorch script via gRPC. Our goal is to enable us (and other teams) to leverage a custom mini-PC coprocessor for game piece detection, rather than relying on the built-in machine learning pipeline that depends on the RKNN unit exclusive to the Rockchip SoC (used on the Orange Pi).

State Machines

Our team has created several classes that make it easy to implement state machines for any subsystem you desire. This allows improved accessibility over solely command-based infrastructure. For more information, please reference the appropriate documentation here.

High Frequency Odometry

Another exciting feature we’ve implemented is high-frequency pose estimation. This estimation is performed on a thread that updates once every 10 milliseconds (i.e. 100 Hz), while logging still occurs at 50Hz on the main thread, as calls to AdvantageKit “processInputs” are not thread safe.

Health Monitoring

In addition to the device-level logging available in our hardware wrappers through AdvantageKit, we’ve also implemented health monitoring and automatic recovery using the Monitorable abstract class. If a device fails, our wrappers will attempt to reinitialize it up to 3 times by default, before declaring it dead. The status of a device can be read through the isHealthy and isDead methods. This feature is also available at the subsystem level.

Summary

With these recent updates to PurpleLib, we aim to provide the same powerful features across a wider range of hardware configurations. This allows more teams within the FRC community to benefit from our efforts, while also offering existing teams greater flexibility to switch hardware without significantly altering their codebase or sacrificing functionality. Along with an expanding list of devices that serve as drop-in replacements for existing APIs, enabling automatic device-level logging via AdvantageKit, and additional swerve modules, we’ve also implemented a variety of quality-of-life features—with even more on the way! For example, we are currently working on enhancing the realism of robot simulation by integrating physics through maple-sim.

Examples on how to use PurpleLib can be found here, as well as the PurpleSwerve project. Our past season code is also available here. However, these may not be fully updated to use the 2025 version of PurpleLib at the time of this post as our team is off for Winter Break. I’ll make sure to also post a link to the code for our 2025 robot and our OpenAlliance build thread when they become available, so that you can follow our future progress through the season. In the meantime, check out our automatically generated Javadocs here.

5 Likes

@jkreidler @nuclearnerd There’s your official MK4i support :point_up:

1 Like

@thatonemf5503

Awesome! Amazing to see the effort put in for the advancement of the community, thank you!

1 Like

Of course, feel free to ask questions or report any issues. We’ll support users as best we can.

PurpleLib 2025.0.1 has been released :tada:

This uses the stable version 2025 versions of all vendordeps, except PhotonLib.

This also fixes a bug for the TalonFX wrapper where it always assumes the TalonFX is on the roboRIO CAN bus.