[FTC] Carrying over Variables from Autonomous to Teleop

CD,

My FTC team is wanting to do a few cool things with transferring variables used and changed in autonomous to teleop mode to re-use. The first one we are trying to use is, “Alliance,” where we want to change what the default color of the lights are when not giving any other information to show what alliance we are (mostly for fun and the nice aesthetics). Moving forward we would want to transfer things like heading, field position, and various other values.

I’ve been told by others that a “Singleton” class is the way to do this, but the team and I cannot quite figure out how this works. I’ve also seen others suggest writing to a file (CSV or something) and then reading that file in the start() or init() method. Both of these things are well above my knowledge, and I couldn’t find anything online that resolved this, so I thought CD would be a great place to start.

Our repository can be found here. It is quite simple as I am the programming mentor and generally not too familiar with java.

Thank you!

I have no experience with FTC, but you can read about the Singleton Pattern here. Basically you have a static getInstance() method that always returns the same instance from a static final member variable.

1 Like

Thank you, @bovlb!

To be a bit more specific, our issue is not with creating the singleton, but changing the internal singleton instance in the waitforstart() method.

Here is what we would like to do:

  • Initialize the auto mode
  • Use a button on the controller to set the alliance we are on (B for red or X for blue for example)
  • Run autonomous
  • Automatically pull the alliance variable into the teleop code

We figured out how to get the instance, but we can’t seem to set it properly in the startup code.

I think you need some advice from an FTC expert here. Having said that: Does it not work to call AllianceSingleton.getInstance().SetAlliance() and then AllianceSingleton.getInstance().GetAlliance() ?

1 Like

We had no idea there was another method level below the getInstance()! This is exactly what we were looking for!

Thank you!

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.