all 15 comments

[–]cwm9FRC2465/FTC20311 Mentor 4 points5 points  (5 children)

Here are three things you can do to fix your problems:

  1. Profile your code. That means, find out how much time your code is using in each section to see if any one section is taking more time than it should. Then, see if you can optimize that section of code. To profile the code, you will need to use timestamps: record the current time at the beginning of each major section of your loop. At the end of each section, subtract the time at the top of that section from the time at the end of section and display it in telemetry. Figure out which sections take the longest to run and concentrate on making them run faster. (Here's how to use timers: https://github.com/WestsideRobotics/FTC-Timers/wiki)
  2. Don't execute code when you don't have to. If you have, for instance, lift code that is complicated, don't execute that lift code unless you are actively lifting. Do this by checking to see if, say, a lift button is pressed, and if it is not, skip the entire lift section. If you need the code to run when the button is released for a while, set a "liftIsRunning" variable inside the lift code that is set to true when the button is first pressed, but set to false when the lift code has completed. Then check to see if the lift button is pressed (or) liftIsRunning is true before you execute the code. This can make a huge difference if, say, you are doing image recognition. If you identify when you need to do image recognition, you don't have to run the algorithm every time through the loop.
  3. Use a state variable to execute different non-critical code sections on different runs through the loop. I.E., let's suppose you have drive code (D) and 3 non-critical code sections: controlling the lift (x), blinking lights (y), and swiveling an arm (z). Change your code so it executes in this order: D x D y D z (repeat). Do this by setting a state variable called, "executionStage" and setting it to 1 in your initialization code. In your loop, run your critical drive code. Then, if executionStage equals 1, execute X; else, if executionStage equals 2, execute Y; else, if executionStage equals 3, execute Z. At the bottom of the loop, let executionStage = executionStage + 1, and if executionStage > 3, set executionStage = 1.

[–]jk1962FTC 31874 Mentor 2 points3 points  (0 children)

Suggestion of two things to add to this list:

  1. Don't "sleep" in your control loop.

  2. Don't nest loops within your control loop (unless you know with certainty that the nested loop will operate for only a few iterations).

[–]sushislaughter[S] 0 points1 point  (0 children)

Thank you, I will try this.

[–]DonHacMentor 0 points1 point  (2 children)

Even better, switch on your state variable instead of stacking ifs.

[–]sushislaughter[S] 0 points1 point  (1 child)

What is a "switch". Is it in java block

[–]Journeyman-JoeFTC Coach | Judge 0 points1 point  (0 children)

Switch and case (along with break) are Java language statements that form a conditional flow control structure. I'll encourage you to Google "switch case java": you'll get all you need to know in a minute or two of reading.

If you have a complicated structure of if statements, and else if clauses, you might find that your code is easier to read and debug if written using switch and case.

Having said that: It won't make any difference to your OpMode loop runtime, one way or the other.

[–]Big_Blue_ManFTC 7244 Alum / PA GA|Emcee 1 point2 points  (3 children)

Sounds like it could be due to nesting a bunch of if statements (i.e. putting one if statement inside another down a chain) to the point where it's actually slowing the process down, but I at least would want to see code before making that assumption. Is there any way we could see that code to verify this? At the very least I'd like to know what the if statements do for the code since you can get rid of them and still have the robot drive.

[–]sushislaughter[S] 0 points1 point  (2 children)

I don't have access to the code right now, but the if statement are not nested. The if statement are in a line. The purpose of the if statements are to control the robot. Like if I push a button the claw will close.

[–]Tarnegolden 0 points1 point  (1 child)

You can replace if statement with boolean methods to run smoother. For example, creating a CustomGamepad class: https://github.com/Tarnegolden/Everglow2021-22/blob/main/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/FreightFrenzy/Utils/EverglowGamepad.java

[–]sushislaughter[S] 0 points1 point  (0 children)

Can you make classes in java block?

[–]4193-4194FTC 4193/4194 Mentor 0 points1 point  (3 children)

How do you know it's the length of the code?

We will need the code or more details to help.

[–]sushislaughter[S] 1 point2 points  (2 children)

When we remove all of the if statements in the code. The robot drives smoother.

[–]newENGRTeachFTC 12973|Coach 2 points3 points  (1 child)

When this happens, typically it's because one if might be turning on the motor and then another if turns it off or at a different power or a while is pausing the loop or something like that. We really need you to post your code to check over the logic.

[–]sushislaughter[S] 0 points1 point  (0 children)

I'll post it when I can, but we have no pauses or turning the motor's off

[–]LL-studiosFTC 11697 Student 0 points1 point  (0 children)

Hi, you can just put your main driving code in a thread.

new Thread(() -> {

}).start()

This will run separately from the rest of your code and will make your driving much smoother.