I have incurred an issue when running my PROS program. I am new to pros so bear with me. Basically, I have a function in the opcontrol.cpp file for the drivetrain which assigns the velocity of the motors to a joystick value. In another file, which I named hardware_def.cpp, I have all the motor and controller definitions. I included both files. When I downloaded the program however, nothing happened. I don’t get any errors and it even detects when the competition switch is in driver control or autonomous.
I got this triple checked by some fellow programmers and they couldn’t even find the issue. I also coded a simple p-loop for autonomous function just to check if the controller was working or not, and that didn’t work either.
All the ports are correct, the motors just don’t move. Can someone double check my PROS src files on GitHub.com. I will provide the link here.
double leftStick = Controller1.get_analog(ANALOG_LEFT_Y);
double rightStick = Controller1.get_analog(ANALOG_RIGHT_Y);
the issue might be that it is supposed to be…
double leftStick = Controller1.get_analog(pros::E_CONTROLLER_ANALOG_LEFT_Y);
double rightStick = Controller1.get_analog(pros::E_CONTROLLER_ANALOG_RIGHT_Y);
That might by the issue, but I got that part of the API from the pros website, as seen in this screenshot:
I circled it. Nonetheless, I will try it and see if that makes a difference.
Also, in the autonomous.cpp file, I am getting in error where I call the “pLoop” method (towards the bottom of the file). It’s saying that it is an “undeclared identifier” even though I defined that method in the “PID_P-loop_IME.cpp” file and included it in the autonomous file. The parameters also align with the method so I’m not sure why I’m getting that error.
Your git doesn’t seem to have your header files (.h or .hpp). PROS for V5 is based off of C++. So you need to use header files to combine two .cpp files, and not by directly including the two .cpp files. If you want I can try and upload an example program in a bit.
extern pros::Controller Controller1;
extern pros::Motor lfront;
extern pros::Motor lback;
extern pros::Motor rfront;
extern pros::Motor rback;
extern pros::ADIEncoder lEncoder;
extern pros::ADIEncoder rEncoder;
extern pros::ADIGyro gyroDrive;
//true/false is for direction of motor (true = reversed)
pros::Motor lfront(11, pros::E_MOTOR_GEARSET_18, false, pros::E_MOTOR_ENCODER_DEGREES);
pros::Motor lback(12, pros::E_MOTOR_GEARSET_18, false, pros::E_MOTOR_ENCODER_DEGREES);
pros::Motor rfront(20, pros::E_MOTOR_GEARSET_18, true, pros::E_MOTOR_ENCODER_DEGREES);
pros::Motor rback (19, pros::E_MOTOR_GEARSET_18, true, pros::E_MOTOR_ENCODER_DEGREES);
//encoder ports A-H numbered 1-8
//false meaning value not reversed
pros::ADIEncoder lEncoder (3, 4, false);
pros::ADIEncoder rEncoder (5, 6, false);
Include hardware_def.hpp when you need to use the variables.
Just curious, would you say header files in these cases are comparable to interfaces and/or abstract classes in java? I’m more familiar with java and I’m just trying to figure out when to implement a .hpp along with my .cpp. For example, would I, for my method library, create a .hpp to go with it wit public variables and abstract method headers? Will my method library not work otherwise?
Most of these responses do get to the core of the problem - in C++, you should never include source (cpp) files into each other.
The way the compiler works is (unlike RobotC) it compiles each source file individually and separately. Then, it merges all the compiled sections into one executable. If you include cpp files into each other, the final executable will contain duplicates of the functions and variables, because it compiled it multiple times in different locations.
For each file to be compiled separately without the rest of the code, you need to give it placeholders for variables in other files, via header files.
It is a good idea to read through http://learncpp.com/.
and S.4.1a - S.4.3a
Not exactly, but maybe depending on how you want to look at it. Think of header files as telling your source file “hey, these functions and variables exist and you can call/use these” and source files as “this is where those functions/variables actually exist.” Ish… It’s easy to make it more complicated and I like to favor a more fundamental understanding over technical precision.
As Theo and others have mentioned, source files are compiled separately and then linked together to create one executable. If you repeat the same variable/function names, the process of linking these files together fails - does the
lEncoder refer to the variable in
opcontrol.cpp or the one in
autonomous.cpp? When you create a header file, you can tell the compiler for that source file that some variables or functions exist in the whole program, but won’t be created here.
So, you create a source file
hardware_def.cpp that initializes your different Motors, ADIEncoders, etc. You create a
hardware_def.hpp file that describes the existence of those variables in
hardware_def.cpp. And then in
opcontrol.cpp, etc. you can include that header file so that the compiler knows that those variables exist. @Codec did that for you.
Can you upload an example, it would be easier to understand if I saw it. I understand the concept though.