Execute Function on Autonomous Selecetion - MagicBot

Heyo! We are using Motion Profiling with the Talon SRX’s this season during the autonomous period and the basic work concept is that you have to load the trajectory into a top buffer of a Talon before you want to execute the profile - and then while executing, movement from the top to the lower buffer is handled automatically during autonomous. Thing is, you have to load the points before executing such that the Talon won’t run out of points during execution - since from our testing - loading points and running the motion profile at the same time is a bit risky because the roboRIO can sometimes lag a bit an cause a lack of points to execute.

Our thought was to load the trajectory points into the Talons on robot initialization - but the trajectory we load is different for each autonomous mode - so we have to load the set of points required for the current chosen autonomous (We use MagicBot with AutonomousStateMachines for that matter).

So to our question - is there a way in magicbot (or in general that works with magicbot) to execute a function on autonomous select (when the robot is disabled) - that recieves the autonomous mode and can choose to execute something based on that mode (load points in our case)? Or any other suggestion to implement our need?

Thanks in advance,
~ Nick S.

What you could do is add a state at the beginning of your state machine to load in the points and then as soon as they’re loaded, call next_state() to get into your actual motion profiling. It should have a fairly low overhead and it’ll be customizable to each autonomous.

Yeah, I thought of that, didn’t try it yet, I’ll test the amount of time it takes to actually load the points in the first state - I was just worrying that it might take some time from the autonomous period to do that so I’m just asking if there might be a way to do it before actually enabling the autonomous.

That was my worry with it too. If it’s too slow for you, you could also poll the autonomous selection networktable value in disabledPeriodic and if it changes, load in the trajectories. That might be a better way to do it anyway, that way it’s only attempting to do it during disabled periods.

1 Like

Depending on how long it takes to load the points, the following two options might work?

  • There’s an autonomousInit function (but then you’d have to figure out which mode was called)
  • Each autonomous mode has an ‘on_enable’ function which is called when it’s loaded and about to run (this runs before autonomousInit)

Otherwise, polling the NetworkTables value in disabled is going to be your easiest solution. The SendableChooser would be at self._automodes.chooser… though that’s not public API, it’s not likely to change either.

Hmm. Is that last bit actually true? Each component’s on_enable gets called before autonomousInit, but I suspect the auto mode’s on_enable gets called after. (Looks like I forgot to document this when adding it in.)

Thanks! If I use an AutonomousStateMachine I’ll have to call the super().on_enable() function in my on_enable() right?

Correct