Where to Write Global Variables

Hello,

I know how to create global variables, but I am not sure where is the best place to write to them / define them so they’re available in auto, teleop, etc. We are using global variables to set motor outputs, but they seem to always reset to zero each time I open the code, even if I set them up as a control.

If I write to a global variable in teleop, I don’t think it will be available during autonomous (right?). If I write to the global variable in one of the autonomous routines, I’d have to go to that routine to change the value. Where is the best place to write global variable values so that they can be read from autonomous? Outside of the case structures in the autonomous vi? begin vi? outside of a loop in periodic tasks?

The idea is to be able to go to one place, change some values, and have all instances of that particular motor output be updated. Instead of having to go to every instance of where the motor output is set.

Using the simple framework if you want something to be available in both auto and teleop most team will put it in Periodic Tasks. Meaning every X milliseconds they will write the motor values or whatever from the global and then in both Auto and Teleop they will write to the global.

So you write to the global in both teleop and auto but you only read from and set the motor output in Periodic Tasks. There are many flaws (not critical flaws this will work for your team it just may not be the most efficient) with this way of doing things and this is why the Command and Control framework was set up. I highly recommend you check it out after the season when you have more time to go through things.

You need to change your way of thinking in the last statement just slightly to get what you want. Instead I would say “In any place we can update the motor speed and it will only be set in one place”. Don’t centralize your writes because as you said you need to potentially write in multiple places, centralize your reads.

Let us know if you have more questions!

Thanks for the quick reply!

I think maybe I was unclear, but I believe you answered what I was getting at. I was looking for a centralized location to write global variables to be read in multiple locations, namely autonomous and teleop. The periodic tasks 100ms loop seems like a great place.

Regarding the Command and Control framework, the more I read about it, the more it seems like that’s where we’re heading. I’ll definitely spend some time in the off-season looking into it.

Thanks!

I understood that is what you were asking about but I was trying to caution against that. Why do you need to write to a motor in multiple places rather than just 1 (and have multiple places feed in setpoints)?

I think that’s what we’re doing for the most part. For example, the “intake speed” global variable is written in periodic tasks, say 0.8. Also in periodic tasks, the “intake motors output” global variable is read by the motor output vi for the intake motors. In teleop, when a button is pressed, the “intake speed” global is written (fed) to the “intake motors output” global. “Intake motors output” global is now 0.8 and the motor outputs change.

Have you tried right-clicking the global variable, choosing data operations, and then selecting “make current value default”?

If that works for you, then you wouldn’t need to write to them at all.

If it doesn’t, then write to them in the Begin vi. Certainly don’t do that in periodic tasks. You want to write the value once. There’s no benefit to continue writing it every x ms

The advice to look at command and control is solid. In that, you’d set the intake values inside the intake controller. Then, whether teleop or auton call the code, they’re calling the same exact command so you’d only ever make updates in one location. I’m a big fan of the motor encapsulation it brings.