Need code veteran to double check or help

I am by far the most advanced programmer in my robotics club, but I still feel like there has to be a much better way to do some of the things I have accomplished on my robot.
Code is written in C++ with VEXcode Pro v5

My code is the (lone) file found at GitHub - c0w3rd/97038A: Robotics code for my highschool team -- Freshman Four, 97038A

That’s just the project control file, you need to commit all the source code as well.

3 Likes

Sorry! Have committed the rest of the code if you’re willing to take a look now

1 Like

I’m no veteran, but I have done quite a bit of coding for Vex. I’ll write things as I notice them.

  1. Use multiple files to separate your code into smaller sections. For example, you might separate your code into drive functions, shooter functions, autons, usercontrol, and main.

  2. You don’t really have any control methods for driving or turning. Consider using a PID controller or just a P loop to increase the accuracy of your autos.

  3. I think half the code I’m seeing is just to check whether the drivetrain is overheated. Take all that out and fix your drive physically so it doesn’t overheat.

If you’re feeling adventurous, maybe try out the JAR-Template:

It’s for VexCode and requires you to use a gyro. That’s about it.

3 Likes

Some of your code can be simplified while still doing the same thing.

The code

bool burn = false;


while (true) {
    if (Controller1.ButtonA.pressing()) {
      frontLeft.setVelocity(-10, percent);
      frontRight.setVelocity(-10, percent);
      backLeft.setVelocity(80, percent);
      backRight.setVelocity(80, percent);
      burn = true;
    } else {
      burn = false;
    }

    if(burn == false) {
      frontRight.setVelocity((Controller1.Axis3.position(percent) - Controller1.Axis1.position(percent) * 2), percent);
      backRight.setVelocity((Controller1.Axis3.position(percent) - Controller1.Axis1.position(percent) * 2), percent);
      frontLeft.setVelocity((Controller1.Axis3.position(percent) + Controller1.Axis1.position(percent) * 2), percent);
      backLeft.setVelocity((Controller1.Axis3.position(percent) + Controller1.Axis1.position(percent) * 2), percent);
    }
}

can be replaced with just

while (true) {
    if (Controller1.ButtonA.pressing()) {
      frontLeft.setVelocity(-10, percent);
      frontRight.setVelocity(-10, percent);
      backLeft.setVelocity(80, percent);
      backRight.setVelocity(80, percent);
    } else {
      frontRight.setVelocity((Controller1.Axis3.position(percent) - Controller1.Axis1.position(percent) * 2), percent);
      backRight.setVelocity((Controller1.Axis3.position(percent) - Controller1.Axis1.position(percent) * 2), percent);
      frontLeft.setVelocity((Controller1.Axis3.position(percent) + Controller1.Axis1.position(percent) * 2), percent);
      backLeft.setVelocity((Controller1.Axis3.position(percent) + Controller1.Axis1.position(percent) * 2), percent);
    }
}

They both do the same thing, but the first is unnecessary as burn will always be false whenever Controller1.ButtonA is not pressed.

Also, doing frontLeftFuse == true in your if statements is not necessary, as the boolean variable will pass its value to the if statement.

if (frontLeftuse) { //This does the same thing
 //code
}
1 Like

Okay, I have to pick on this. It is technically true, but as someone who has reviewed a LOT of code at this point, doing this can actually lead to harder-to-read code. Why? Because when you start shortcutting with true statements, you’ll get into the habit of doing the shortcut for false statements.

I can tell you completely from experience, when your code has developed some weird bug and you’re in a time crunch, it’s VERY easy to miss the intent of the code if you’re reviewing as an outside source (or even reviewing code you write couple weeks/months/years ago).

Be intentional about what your code is saying if the language allows it. It will save your sanity some day, I promise.

2 Likes

Meant to include in my last post:

Also, do it because the compiler won’t care, it assembles to machine instructions in the same way; improved legibility code makes everyone’s life easier.

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.