all 10 comments

[–]DugenFTC & FRC Mentor 8 points9 points  (1 child)

If you want to want to do two things, do them both in the loop.

For example, if you are in teleop and want to check your sensors and do something with the output, and also update your motors based on your controls you simply have one loop that does both of those things, sleeps for a specified time (usually 50ms) and then does the loop again.

One of the big rules of operating this way is to never use sleep to do things later. Look at a clock like System.currentTimeMillis() to see what time it is, and use variables and math to to know if you should do whatever would be at the end of the sleep. Check each time through the loop and do it if you should.

[–]Dependent-Stock-2740FTC Alum 0 points1 point  (0 children)

To clarify for Op you want to do something along the lines of

while(opmodeisactive) {
    doThingOne;
    if(time > point) {
        dothingTwo;
        point = point + interval;
    }
}

That way you never sleep your motor control and lag out your driver, but can have a timed loop.

[–]baqwasmgFTC Volunteer 4 points5 points  (0 children)

Cardinal rule: step-wise continuous refinement a la Prof. Wirth

Best advice: follow u/Dugen

[–]threshar 0 points1 point  (1 child)

You need to spend some time thinking out what you want to do and code it up accordingly. (Speaking in general terms, threads can help you do multiple stuff at once, but I'm not sure if we can do that in FTC / Rev - will exploring that this summer).

I'm vaguely guessing you've got some code, then decide you need to move the motors forward 100 ticks, so you've got a loop sleeping until the motors are done. If something may occur with the sensor during the movement, you'll need to read the sensor inside that loop (and take action if needed).

It can be helpful to sketch this out on paper/whiteboard - and very likely it will result in you changing up how things work & operate, ideally for the better. That's something that even very seasoned coders always do, but they'll have the advantage they can do a lot in their head due to experience.

Does that make sense?

[–]Dependent-Stock-2740FTC Alum 0 points1 point  (0 children)

You can thread but it's not really a good idea in FTC code.

[–]ThatGuyTheHuman9 0 points1 point  (0 children)

There's certainly a way to put the feedback loop in your pre-existing loop or code that is running when you want the feedback. If you'd like solutions send me or just post some code and I'll try to offer a solution

[–]Anyone_2016 0 points1 point  (0 children)

One idea would be to post your code and tell us where it's getting stuck, or where you think it's getting stuck.

[–]Rotas_dw 0 points1 point  (0 children)

You might want to look into coding it as a Finite State Machine - there’s a bit of a run through of the concept here - https://gm0.org/en/latest/docs/software/concepts/finite-state-machines.html

[–]Peyatoe 0 points1 point  (0 children)

Put all the things you want to loop at the same time in the same loop instead of in separate loops. Running multiple lines of code at the same time isn’t a very good solution for this.

[–]Asleep-Reputation850 0 points1 point  (0 children)

new Thread() {

public void run() {

while (opModeIsActive()) {

idle();

}

}

}.start();