Our robot has 2 motors with IMEs on our drive. We want to use them to move a certain distance while using PID control to make sure that they move at the same speed. I am thinking about using while loops for this, but I wouldn’t know how to make sure they both travel the same distance before they stop, in just one while loop.
(Please note: I haven’t tried the attached code, but I’m assuming by the way loops work that these statements wouldn’t run at the same time. Also, the encoder values are just random, it’s the concept that matters to my question.)
Also, the front 2 drive motors aren’t using PID control because they don’t have IMEs. Am I going the right way about changing their values to keep them in line with the PID on the back motors?
We had a very similar problem about whether or not we should use tasks or not.
In this link, we were almost forced to use tasks until somebody pointed out something very obvious.
We changed all the while loops to if statements and put them in one big while loop, as follows:
We tested it, and it worked for us! Since the if statements are in a while loop, it’s constantly testing them as if they were normal while loops, AND it runs through both of them!
In addition, In your code, if you were trying to say "Once nMotorEncoder[BLeft]/[BRight] is equal to 1234, then STOP the motors " , just do this:
The code won’t work because it will wait for the first loop to end before even starting the second one.
And yeah I think changing the values is fine.
It might work for some situations, but there is a difference. If you use tasks, once motor[BRight] is greater than or equal to 1234, the loop controlling the right side will stop, so nothing will happen if motor[BRight] becomes less than 1234. However, for the big while loop you are suggesting, the “smaller loops” will never stop.
If and when you find that multitasking (tasks) could be of assistance, have a read of this article. Multitasking is a rabbit hole of complex issues and crashed dreams when done incorrectly however, so be careful.