Hi, I am having problems with my autonomous code and need some help debugging it. I have many questions, so I’ll list them by numbers and you can pick the ones you want to help with. Thanks for stopping by.
In my autonomous, my potentiometer motors don’t behave correctly, even though it’s the exact same code in drivers. I use “motor = pot value - 400” to set its location in drivers and it works perfectly, but in autonomous it does not keep refreshing the set motor to this value, and I don’t know how to make it do that. When it executes the code it sets the motor to travel in the set direction, but it passes the location. Is there a way to run a loop to refresh this value, while the main sequence of tasks is executed?
same problem, but with shaft encoders. I have two shaft encoders on each side and want them to track distance and rotation, but they don’t refresh like they should. I need two variables to constantly change as the autonomous goes on, the difference between the two sensors for rotation and the average of the two sensors for distance to run in the background of the sequence?
I have more questions that I can’t remember currently, so after the first two are completed I’ll just ask for more help. My competition is Friday this week, so I need a lot of help. Thanks again for helping.
Both (1) and (2) can be fixed by using loops to refresh the motor values. In usercontrol you will have your code inside a
while (true) {
loop.
As you noticed, the downside with a loop is that you can’t run through a sequence (because your looping quickly). You need a separate task (multiple tasks run concurrently) to manage the constantly refreshing variables. See here for more info on tasks.
/* Global variables */
int encoderDifference;
int encoderAverage;
/* Tasks */
task monitorTask() {
while (true) {
motor[xMotor] = SensorValue[xPot] - 400;
// this is be positive or negative difference. You can use abs() to get an absolute difference.
encoderDifference = SensorValue[xEncoder1] - SensorValue[xEncoder2];
encoderAverage = (SensorValue[xEncoder1] + SensorValue[xEncoder2]) / 2;
delay(20); // only loop 50 times a second
}
}
Why two? One is plenty.
You probably store their value in a variable somewhere, the SensorValues update automatically.
Wouldn’t it be easier to calculate the difference and averages when you need them? It’s not a long or overly complicated calculation.
Also you can calculate rotation off one shaft encoder. We know that there’s 360 ‘ticks’ per full revolution of the shaft, which is 1 tick per degree of rotation. The rotation of the shaft (in degrees) can be calculated by
SensorValue[xEncoder] % 360
. The percent is a modulus operator and computes the remainder that results from performing integer division.