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

Epliogue Loggers #34

Open
oh-yes-0-fps opened this issue Dec 14, 2024 · 4 comments
Open

Epliogue Loggers #34

oh-yes-0-fps opened this issue Dec 14, 2024 · 4 comments

Comments

@oh-yes-0-fps
Copy link

I was looking to maybe yoink some stuff and just wanted to callout some things incase u care, feel free to close this if u dont ofc.

CANBus status fetching is a blocking call and will usually take multiple MS, i doubt you really want epilogue calling this.

for your ctre hardware stuff i would recommend doing anonymous implementations for the consumers instead of lambdas to not refetch all the status signals and remake array each time.

new Consumer<EpilogueBackend> {
        BaseStatusSignal absolutePosition = canCoder.getAbsolutePosition(false);
        BaseStatusSignal magnetHealth = canCoder.getMagnetHealth(false);
        BaseStatusSignal position = canCoder.getPosition(false);
        BaseStatusSignal velocity = canCoder.getVelocity(false);

        BaseStatusSignal[] signals = { absolutePosition, magnetHealth, position, velocity };

        public accept(EpilogueBackend eb) {
            BaseStatusSignal.refreshAll(signals);
            //... just log ur stuff now
        }
}
@oh-yes-0-fps
Copy link
Author

oh-yes-0-fps commented Dec 14, 2024

also did you make this https://github.com/Greater-Rochester-Robotics/GRRBase/blob/main/src/main/java/org/team340/lib/util/Profiler.java off my https://github.com/wpilibsuite/allwpilib/blob/6ea95967906d237e8667e9695eb49914ea5ff0b4/wpilibj/src/main/java/edu/wpi/first/wpilibj/Tracer.java pr cuz ive been using that tracer in my code for over a year now. just kinda funny if we made the same thing independantly. Same thing for Tunable, Alliance, PhoenixUtil etc. Like the template overall.

@bryceroethel
Copy link
Member

bryceroethel commented Dec 14, 2024

CANBus status fetching is a blocking call and will usually take multiple MS, i doubt you really want epilogue calling this.

I was waiting to test on a real robot to see the performance implications, we only got our CANivores last week, good to know

for your ctre hardware stuff i would recommend doing anonymous implementations for the consumers instead of lambdas to not refetch all the status signals and remake array each time.

Pretty sure the behavior you're talking about is essentially the current implementation, unless there's some dumb Java wizardry happening that I'm not aware of. The Function<K, V> in computeIfAbsent() should only be invoked on the first call to log a device, afterwards the array/signals are saved and reused

also did you make this https://github.com/Greater-Rochester-Robotics/GRRBase/blob/main/src/main/java/org/team340/lib/util/Profiler.java off my https://github.com/wpilibsuite/allwpilib/blob/6ea95967906d237e8667e9695eb49914ea5ff0b4/wpilibj/src/main/java/edu/wpi/first/wpilibj/Tracer.java pr

I was unaware of your PR when I made that, I think I pulled the implementation from a stray file on my laptop from last season, leftover from when we were trying to debug some loop overruns, it's hard to track though since I've just been force pushing main for the past 4 months 🤪

Same thing for Tunable, Alliance, PhoenixUti

Tunable was drafted last season with some students, as a replacement for setters in initSendable() in subsystems, overall provided a cleaner interface. Usage with an older version of the API can be found in a backport of GRRBase to WPILib 2024 in our Crescendo repo, every constant and PID controller is set up to be tunable. Alliance came up last season because we were tired of writing DriverStation.getAlliance.orElse(Alliance.Blue).equals(Alliance.Blue) all the time, PhoenixUtil and ReduxUtil came out of the remains of the REV config wrappers we ran in 2024 to solve configuration issues

@oh-yes-0-fps
Copy link
Author

great minds think alike fr fr.

On the note of the lambda, everything inside the brackets is executed every time the code is ran. You store the function itself but that has no state other than some captures. Due to the fact that ctre caches status signals its not a huge deal, just a few hash lookups to fetch them again but whatever.

@bryceroethel
Copy link
Member

bryceroethel commented Dec 16, 2024

Still not following what you mean, the outer lambda only runs once per device when it is first logged. The cached consumer will not not re-evaluate the variables it captures.

cache
    .computeIfAbsent(canCoder, key -> {
        // This is in the outer lambda, and is ran when the specified key of the
        // cache is absent, which is only the case when a device is first logged.

        var absolutePosition = canCoder.getAbsolutePosition(false);
        var magnetHealth = canCoder.getMagnetHealth(false);
        var position = canCoder.getPosition(false);
        var velocity = canCoder.getVelocity(false);

        BaseStatusSignal[] signals = { absolutePosition, magnetHealth, position, velocity };

        // See console output below
        System.out.println("Logging CANcoder " + canCoder.getDeviceID());

        // This is the consumer saved in the cache, i.e. the code that will run periodically.
        // Variables such as "absolutePosition" and "signals" are captured by this lambda,
        // and will not be recomputed in future logging cycles.
        return b -> {
            BaseStatusSignal.refreshAll(signals);
            b.log("absolutePosition", absolutePosition.getValueAsDouble());
            b.log("magnetHealth", magnetHealth.getValue().name());
            b.log("position", position.getValueAsDouble());
            b.log("velocity", velocity.getValueAsDouble());
        };
    })
    .accept(backend);

produces:

// ...
********** Robot program startup complete **********
Logging CANcoder 10
Logging CANcoder 11
Logging CANcoder 12
Logging CANcoder 13
// No other console output from the code above

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

No branches or pull requests

2 participants