Skip to content
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

Example Best-Practices of Command-Based usage #6618

Open
wants to merge 38 commits into
base: main
Choose a base branch
from

Conversation

tom131313
Copy link

Add an Example Best-Practices of Command-Based usage.
Issue #6616
Reference: CD thread
Utilize an LED-based system to show various best-practices in coding Command-Based programs.

@tom131313 tom131313 requested review from a team as code owners May 13, 2024 00:11
Comment on lines 23 to 28
periodicTask.register(()->strip.start(), 0.1, 0.01);
}

public void periodic() {
strip.setData(buffer);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to put strip.setData(buffer) in the periodic callback and remove periodic() altogether? strip.start() should be invoked once. Any specific reason to offset by 1ms and run at 100ms?

import edu.wpi.first.wpilibj.AddressableLEDBuffer;
import frc.robot.PeriodicTask;

public class RobotSignalsLEDbufferLEDSubsystem {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why such a verbose name, also I would not include Subsystem suffix since this is no longer a subsystem. How about LEDResource?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I apologize for leaving some trash from the start and then got distracted trying to get output from the example and getting it into a WPILib Pull Request.

My refactored version is available here. Believe it or not it does resemble the original except I have added working output with commentary and didn't pursue the anticipated new AddressableLED functions which we can add when they are readily available. (Xbox controller buttons A, B, and X do something in different robot modes. Now I see that I should document the expected behavior.)

I can't remember - are we including the word subsystem in the Java class/file name or not? I left in subsystem but scrubbed Command and Trigger from names.)

I would appreciate your and @Oblarg comments on what can be done better as a best practice. Thanks.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@illinar I have completed a working example LED resource/subsystem/view based about 99% on your example code.

I did find @SamCarlberg nice addition to the LED process that you recommended. I copied his entire work into my example since I don't see any other way to reference it in my code. All those files would have to be deleted from my example and appropriate imports made to the WPILib when it has those files available.

My changes to your code are trivial to make it a little easier to test. I simulated a couple of events with Xbox controller button presses and changed an onTrue to whileTrue. I added a third view of the LEDs only to display your dynamic Enable/Disable Patterns.

Since the basis of all LED addressable views is the same I have only one class for the LEDview and instantiate it three times for the Top, Main, and EnableDisable views.

I welcome comments from all - you, @Oblarg and anyone to further refine structure.

Thanks for your contribution of a fine example.


*/
@FunctionalInterface
public interface PeriodicTask {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this should be part of WPILIB API surface if this pattern is an officially blessed mechanism of injecting periodic processing support.

Copy link
Member

@calcmogul calcmogul May 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this pattern is an officially blessed mechanism of injecting periodic processing support

It's not. The official mechanism is TimedRobot.addPeriodic().

…ommandbasedbestpracticeled/subsystems/LEDMainSubsystem.java
…ommandbasedbestpracticeled/subsystems/LEDTopSubsystem.java
…ommandbasedbestpracticeled/subsystems/RobotSignalsLEDbufferLEDSubsystem.java
…ommandbasedbestpracticeled/subsystems/VisionSubsystem.java
…ommandbasedbestpracticeled/subsystems/LEDPattern.java
…ommandbasedbestpracticeled/PeriodicTask.java
…ommandbasedbestpracticeled/subsystems/Shooter.java
@PeterJohnson PeterJohnson changed the title #6616 Example Best-Practices of Command-Based usage Example Best-Practices of Command-Based usage May 20, 2024
@tom131313
Copy link
Author

@Oblarg I've completed the "best practice" example usage of command based (I haven't changed anything in 5 minutes now so it must be done!).

It is composed of the work of @illinar , @bovlb , @SamCarlberg plus I added the methods and tests I created for the "DisjointSequence" project. I think it demonstrates the ideas that were listed in the CD thread with some brief but far from complete commentary on why things are as they are.

There are some deficiencies as far as a WPILib submission goes: no C++, no Python, and only once did I dare to type "m_" variable prefix and sure enough my keyboard caught on fire and that was that.

I learned a lot and will use this for my team. I'm happy to share with anyone. If it misses the point as a WPILib example, I can close the PR and delete it as I have my own copy also in my GitHub repositories. I'm happy to do some more work on it if that is useful.

Thanks for your leadership on this subject.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants