Am I able to have an unset number of parameters in a function in C++? I am wanting my PID functions for next year to be able to call other functions based on how close/far it is away from its target.
I know in the motor code you can either use one variable like this: (yes, I know this is Python, but I’m not fluent in C++ yet)
motor_1.spin(FORWARD)
Or you can use multiple, like this:
motor_1.spin(FORWARD, 100, PERCENT)
Basically, I want my PID loop to be something like this (but in C++ whenever I actually code it):
def drivePID(distance, angle, callDist1, function1, callDist2, function2…):
Blah PID calculations
Blah Driving
Blah Other stuff
if dist < callDist1: function1
if dist < callDist2: function2
…
Is this possible in either Python or C++? Also, anyone had questions, feel free to ask them.
Hi,
I’m not completely sure what you want here, but if you want functions like the motor.spin with different inputs you either need multiple functions, or you can set default values. You do this by doing type var = value
I believe both python and C/C++ have this ability.
If you want to learn C++, I highly suggest the C++ Series by the cherno on youtube.
For a PID, you may want to look into a class to implement the underlying logic. This reduces the lines of code you need to type and can clean up code really nicely.
If you want some code to reference, there are templates on github that you can look at the source code for (Lemlib, Jar, Ez, and Voss to name a few). My code is also avialable on github, if you search my team number, you’ll find it.
Hopefully that helps,
Andrew
2131H Programming Dep.
Ps. You can find me on discord for programming problems, you’ll get faster responces there than the vex forum for this kind of thing.
(I knew I wasn’t clear enough!)
That’s not exactly what I had in mind. When driving in autonomous (using a PID loop) I want to be able to do different functions based on how far I have traveled. For example, when the robot is 314mm from its destination, I want it to activate the mogo clamp (or whatever). I want to be able to have multiple of these (three should do) and specify which actions to do in the function parameters. However, I don’t want all of my PID loops to be this long if I want to only use one of the functoins:
PID(dist, ang, dist1, funct1, dist2, funct2, dist3, funct3)
Is there a way for me to have it so I can call the PID like before and like this:
PID(dist, ang, dist1, funct1)
?
@jpearman, I know the motor functions work like this. Can I do this in my code as well?
Yes, C++ allows functions that have parameters with default values and also overloaded functions (same name, different parameters)
How would I do this in my code? I noticed the @overload
tags in the code eariler. Is this what I use.
Happy cake day @jpearman!