Vex Global Function and Varibles

I am trying to create global variables for my code. I don’t know what to put in. These are my variables.
Pros%20Code

What exactly is your question? It is not clear from what you posted.

I want to know the steps in creating a file for global variables.

I’m not sure those are variables

Oh, sorry for misunderstanding, then how do I create a function outside of autonomous.cpp as an example, and then call on it in my autonomous.cpp. This is my function as an example↓ ↓ ↓ ↓
Function

You need to make a header file for it. You put soley the function header of the function you want to declare globally in that header file and #include that header file in the main.h file (towards the bottom there is a section for global header files).

Edit: make sure you #include the main.h file in all of your files. The function header would be void FuncMove(int Distance, int Speed);.

2 Likes

Set it up out of other functions

You need to put everything you want to be global into an .hpp file. Use the extern keyword for variables and objects (things like pros::Motor). Remember only to declare the motors and variables within the .hpp file as otherwise it gets messy. Do the definition within a separate .cpp file.

Here is an example of my code (ignore how one uses #pragma once and the others use #ifndef as they are basically the same thing, its just an inconstancy with my writing). I have all my .hpp files I wrote in a ‘custom’ folder in the pre-existing directory ‘include’ for organizational sake. It can be anywhere as long as you can reference it. The location of the .cpp file doesn’t really matter, but I have it in a ‘sys_control’ folder with my auto, initialize, and teleop files for organizational sake.

My “motors_setup.hpp” file.

#pragma once
#include "main.h"

//declaring these ports exist throughout the whole program
extern pros::Controller master;
extern pros::Motor leftFront_m;
extern pros::Motor rightFront_m;
extern pros::Motor leftBack_m;
extern pros::Motor rightBack_m;

Then my “motors_setup.cpp”

#include "main.h"
//defining what each port is
pros::Controller master(pros::E_CONTROLLER_MASTER);
//port, core color (check chart), reversed?, enc units
pros::Motor leftFront_m(2, pros::E_MOTOR_GEARSET_06, false, pros::E_MOTOR_ENCODER_ROTATIONS);
pros::Motor rightFront_m(1, pros::E_MOTOR_GEARSET_06, true, pros::E_MOTOR_ENCODER_ROTATIONS);
pros::Motor leftBack_m(3, pros::E_MOTOR_GEARSET_06, false, pros::E_MOTOR_ENCODER_ROTATIONS);
pros::Motor rightBack_m(4, pros::E_MOTOR_GEARSET_06, true, pros::E_MOTOR_ENCODER_ROTATIONS);

And then a snippet from inside of “main.h”. #ifdef __cplusplus should already exist, you just need to add the path to your .hpp files.

#ifdef __cplusplus
/**
 * You can add C++-only headers here
 */
 #include "custom/drive_funcs.hpp"
 #include "custom/motors_setup.hpp"
 #include "custom/tracking.hpp"
#endif
3 Likes

For functions, its pretty similar. You just don’t need to use the extern keyword for functions (still keep the definition out, you only want a declaration). An example .hpp and .cpp file like the ones above for an autonomous movement function.

The .hpp file

//can be replaced with #pragma once, is an inconsistency but doesn't really matter. If you use ifndef make sure the name is different for each file.
#ifndef DRIVE_FUNCS
#define DRIVE_FUNCS

//moveAbsDir returns nothing and takes three parameters
void moveAbsDir(double leftX, double leftY, double rightX);
//moveAbsUser returns nothing and takes no parameters
void moveAbsUser();
//moveAbsTo returns a boolean when finished and takes 3 variables
bool moveAbsTo(double x, double y, double z);
#endif

The .cpp file for moveAbsTo (moveAbsUser and moveAbsDir are in a separate .cpp files from moveAbsTo).

#include "main.h"

bool moveAbsTo(double x  = NAN, double y = NAN, double z = NAN){
  //blah blah blah math math math call other function blah.
  //insert your code for function and movement here or something
  return true; 
}

And then in the above example included the “drive_funcs.hpp” into “main.h”. Same principle as with including “motors_setup.hpp” into “main.h”. After that you can use that function anywhere in the program where “main.h” is included.

3 Likes

Thank you for your help.