Skip to content

Conversation

Murat65536
Copy link

Closes #8191

@Murat65536 Murat65536 requested a review from a team as a code owner September 16, 2025 20:24
@github-actions github-actions bot added the component: wpilibj WPILib Java label Sep 16, 2025
@calcmogul
Copy link
Member

calcmogul commented Sep 17, 2025

fyi, these changes can be ported to C++ as well, despite the issue specifically mentioning Java.

@Murat65536
Copy link
Author

Murat65536 commented Sep 20, 2025

fyi, these changes can be ported to C++ as well, despite the issue specifically mentioning Java.

@calcmogul
I have very little experience with C++ compared to Java and VS Code shows a bunch of errors in any C++ file I try to open. Might be because I'm on Linux.

I see this:

void TimedRobot::AddPeriodic(std::function<void()> callback,
units::second_t period, units::second_t offset) {

What I don't see is where second_t is defined. I also don't see what units is. Something external? I installed Windows. I see it now. Annoying that eveything's broken on Linux but I guess it makes sense. C++ isn't cross-platform until you make it behave that way.

@ThadHouse
Copy link
Member

What I don't see is where second_t is defined. I also don't see what units is. Something external? I installed Windows. I see it now. Annoying that eveything's broken on Linux but I guess it makes sense. C++ isn't cross-platform until you make it behave that way.

Linux definitely does work. However by default in allwpilib, the project intellisense generation doesn't run. You can run ./gradlew generateVsCodeConfig to force that generation.

@Murat65536
Copy link
Author

That makes more sense. There's probably a reason that it doesn't when I open up the project that I don't know. Maybe bad stuff happens if I close the project before the command completes?

@Murat65536
Copy link
Author

@calcmogul
Should I be using templates for this? Sorry if I'm missing something really easy or obvious. I don't have much experience with C++, especially in large projects.

@KangarooKoala
Copy link
Contributor

You can just use the SI units (units::second_t, units::hertz_t, units::meter_t, etc.) for the C++ version, since our C++ units library will automatically convert between compatible unit types. (For example, you could pass 1_ft as an argument to a method expecting units::meter_t)

@Murat65536
Copy link
Author

On the C++ tests, I'm getting this message:
more than one instance of overloaded function "insert frequency overloaded function" matches the argument list
From my understanding, both units::second_t and units::hertz_t are perfectly fine with accepting plain numbers. They don't need to be a specific type like in Java. I don't have experience with all the quirks of C++ but based on this, it doesn't seem possible to overload functions with frequency. Maybe I'm wrong?
The workflows also seem to support this:

wpilibc/src/test/native/cpp/TimerTest.cpp(87): error C2668: 'frc::Timer::HasElapsed': ambiguous call to overloaded function
    SET RUNFILES_MANIFEST_ONLY=1
C:\bazelroot\vbgseepj\execroot\__main__\bazel-out\arm64_windows-fastbuild\bin\wpilibc\_virtual_includes\wpilibc.static\frc/Timer.h(105): note: could be 'bool frc::Timer::HasElapsed(units::frequency::hertz_t) const'
    SET TEMP=C:\Users\RUNNER~1\AppData\Local\Temp
C:\bazelroot\vbgseepj\execroot\__main__\bazel-out\arm64_windows-fastbuild\bin\wpilibc\_virtual_includes\wpilibc.static\frc/Timer.h(103): note: or       'bool frc::Timer::HasElapsed(units::time::second_t) const'
    SET TMP=C:\Users\RUNNER~1\AppData\Local\Temp
wpilibc/src/test/native/cpp/TimerTest.cpp(87): note: while trying to match the argument list '(units::time::millisecond_t)'
    SET VSLANG=1033

Copy link
Contributor

@KangarooKoala KangarooKoala left a comment

Choose a reason for hiding this comment

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

First: Please make the following changes to the C++ overload set to match Java.

  • Add void StartPeriodic(units::hertz_t frequency); to Notifier.h. (Don't forget to copy-paste the doc comment above void StartPeriodic(units::second_t period); and to replace period with frequency.) The implementation can be StartPeriodic(1.0 / frequency);.
  • Add explicit TimedRobot(units::hertz_t frequency = 1 / kDefaultPeriod);. (Again, copy-paste the doc comment from the units::second_t version and replace period with frequency.) For this one, you'll want to use the following syntax in the TimedRobot.cpp file to delegate to the main constructor:
    TimedRobot::TimedRobot(units::hertz_t frequency) : TimedRobot{1 / frequency} {}
  • Remove Timer::HasElapsed(units::hertz_t)- The Java equivalent would be Timer.hasElapsed(Frequency), but that is intentionally not present, since it's not useful.
  • Remove Watchdog::SetTimeout(units::hertz_t)- Similar to the above note for Timer::HasElapsed().
  • Remove BooleanEvent::Debounce(units::hertz_t, frc::Debouncer::DebounceType)- Similar to the above note for Timer::HasElapsed().

Second: The compilation error seems to be a problem with the C++ units library we're currently using. I'll work on a fix.

Copy link
Contributor

@KangarooKoala KangarooKoala left a comment

Choose a reason for hiding this comment

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

Nice work!

Copy link
Contributor

@KangarooKoala KangarooKoala left a comment

Choose a reason for hiding this comment

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

Seems okay to me for what it's worth, though you should probably run wpiformat -f wpilibc/src/main/native/cpp/TimedRobot.cpp to make sure the long constructor line doesn't need to be broken up. (If you haven't installed wpiformat, just run python3 -m pip install wpiformat)

@calcmogul
Copy link
Member

This PR is blocked on #8267.

@calcmogul calcmogul added the state: blocked Something is blocking action. label Oct 2, 2025
calcmogul
calcmogul previously approved these changes Oct 2, 2025
@calcmogul calcmogul removed the state: blocked Something is blocking action. label Oct 3, 2025
Copy link
Contributor

@KangarooKoala KangarooKoala left a comment

Choose a reason for hiding this comment

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

Seems good to me, for what it's worth.

@Murat65536 Murat65536 requested a review from calcmogul October 13, 2025 21:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[java] Use units for more input parameters

6 participants