How to make P Loop (V5 C++)

As you can probably tell, I’m new to function coding. In which function would I define error, and how?

Also, I don’t see how angle is double-defined, and the time it is, it seems global to me.

How would I make a way to exit the loop?

What is the “not a structure or union” error and how do I fix it?

Finally, I think a large part of my issue is not knowing how to identify certain things. For example
void Drive(int direction, int num, int units)
What text would I put in place of direction, num, and units to say that I want to be able to put in the drive direction, number of units, and what unit I want?

It’s all so confusing to me, I’m sorry.

motor_group   leftDrive( lb, lm, lf );
motor_group   rightDrive( rb, rm, rf );

void turn(float angle, float kP){
     float error = angle-Inertia.rotation(degrees);
     while(error>1 || error<-1){
          error = angle-Inertia.rotation(degrees);
          leftDrive.spin(forward, error*kP);
          rightDrive.spin(reverse, error*kP);
     }
}

turn(90, 1.5);

Here is code that (probably) compiles, exits when error is sufficiently small, and turns to an angle of 90 degrees.

Go on learncpp.com for a little while

2 Likes

One more question.
I get the error
no matching member function to call for 'spin'
I know this is because there are one too many parameters, however there are one too few parameters for it to be spinFor, for which I would need to add a unit at the end. What unit would I use to make it work?

I can’t test right now, but I’m assuming it’s “volt/volts” and it was just omitted. I’ll be back with results unless told otherwise.

This error is telling you that it doesn’t know why you’re trying to use spin, so there’s something wrong with what’s immediately in front of the period. I’m guessing you didn’t copy the motor groups, so the code is trying to spin a motor/motor group that isn’t defined.

I think you’re completely underestimating the value of understanding what functions do. Once you have a solid understanding of functions and how they work, understanding algorithms like this will become much easier.

As an example, last year, I was trying to learn odometry. At first, it made absolutely no sense to me. But I read documents, watched videos, and wrote code until it made sense. It took a long time, but once I understood what I was doing, it became so much easier to implement it and expand on it.

The same thing happened when I was trying to learn pure pursuit, except it was completely on another level. It took me months to even understand the algorithm, and several times I felt like I had reached a dead end. Sometimes it felt like none of the resources I was looking at explained things well, or at all. However, no one is going to guide you through the entire process. There were many things that I had to figure out by myself. After a few months, I was finally able to create an extremely basic implementation of it that I plan to use for competition, and understanding what I was doing was by far the most important thing.

This process shouldn’t be too difficult for learning functions, but only as long as you do the work yourself in trying to understand them, not expecting someone to answer all your questions. I think you should try to understand them before using them for VEX. There are countless resources out there to learn them. For me personally, videos usually work better than written text when I’m trying to learn a new concept, but do whatever works best for you to first actually understand the concept of functions before trying to use them for VEX. This will involve a lot of watching videos/reading websites and definitely a lot of writing basic code yourself. Many times, it might feel like you’ve reached a dead end, but you haven’t. Keep trying, and you’ll get there eventually.

7 Likes