We’re starting to experiment with MagicBot and ran into a challenge with how it handles hierarchical Components that I’m curious if anyone else has.
The situation is related to the order in which Components are initialized vs executed.
When it comes to initialization, I’d like to start with the lowest level components (1) like something talking directly to a drive motor. Then the higher level components can be initialized with a reference to the lower level component and read things back out like where an absolute encoder might be starting.
But then at execution time, the higher level components should go first (2) so they can provide input to the lower level components.
Right now I have things setup so the higher level components go first in all cases (which is right for execution), but then my init code has very awkward handling for the lower level component not being ready when the higher level component is being setup.
1 Like
Why would the higher level component care about the precise position of the encoder? Maybe a more concrete small example would be helpful to illustrate here?
1 Like
The scenario was the higher level Drive component during setup was creating a swerve estimator:
self.estimator = SwerveDrive4PoseEstimator(
self.kinematics,
self.imu.getRotation2d(),
self.get_module_positions(),
initial_pose,
stateStdDevs=(0.05, 0.05, 0.01),
visionMeasurementStdDevs=(0.4, 0.4, 0.03),
)
self.get_module_positions(),
in this case asks the lower level swerve module components for their current positions and since those components haven’t been setup yet I had to hack in some guard code to handle that they didn’t know yet.
Full code example available here: PythonTest/components/swerve/drive.py at 24d7fdf983d3953bd9a1f8f67ddf0c5f78dcf02d · Team488/PythonTest · GitHub
Ah, that makes sense. Yeah, that isn’t going to work with the way initialization is setup as-is.
The thing you want seems like it would be pretty reasonable and straightforward to do. In _create_components, we could create a tree of dependent components and do a stable toposort on it, and use that to order initialization.
Created an issue on github to track this: magicbot: toposort components to determine initialization order · Issue #211 · robotpy/robotpy-wpilib-utilities · GitHub
1 Like