rosflight / rosflight_ros_pkgs

ROS packages for the ROSflight autopilot
http://rosflight.org/
BSD 3-Clause "New" or "Revised" License
86 stars 56 forks source link

Mavrosflight timer abstraction #138

Closed jbwillis closed 3 years ago

jbwillis commented 3 years ago

The two most relevant files to look at to get an idea of what I am doing are timer_interface.h and ros_timer.h . Essentially I have a base class AbstractTimer which simply provides an interface to start and stop functions. Then, there is another base class TimerInterface with a createTimer function which returns an AbstractTimer. It uses CRTP to pass down the createTimer to its inherited ROSTimerInterface class. Since the ROSTimerInterface class is created in rosflight_io, it has to be able to handle the creation of multiple timers, so it calls new and keeps track of the timers in a vector.

I am really not convinced this is a correct or best way to approach this, but it's my best shot at it. I may be missing something fundamental that would make this all come together easily. If you can provide some feedback, I'd really appreciate it.

jbwillis commented 3 years ago

Thanks for the feedback. std::chrono sounds like a great choice.

My understanding was CRTP is necessary when I need to pass a template argument into a class that is acting as the base class for an interface. This comes up here because I need to be able to pass a pointer to the class containing the callback function into the TimerInterface class. Since this requires a template, it is no longer eligible to be virtual. Is there a different/cleaner way to do this?

I was trying to avoid using CRTP, which is why TimeInterface does not use it.