Encoder Bug (Random Values -- Problem -- Temporary Solution)

We changed our program to use three wire encoders instead of internal motor encoders and suddenly noticed large random values being printed to our brains screen. We print each encoders values to the screen. To figure out what was happening, we slowly commented out our code until there was nothing left, which must have meant the bug was coming from the get encoder position function itself. We minimized our code to just get the motor encoders, tried pre-initializing their positions to 0, and even tried unplugging them, but still got large negative values. We noticed two things: These values were set only after a certain period of time, and that these values were triggered by calling the .position(degrees) method of the encoders. This bug only occurs on the first call of the position method as well, and it was consistent with other brains that we had. Our brains are also up to date.

We did solve the issue, but it uses a hack-like solution. To solve this issue, this code must be run before anything else (at the beginning of the program):

  LeftEncoder.setPosition(0, degrees); 
  RightEncoder.setPosition(0, degrees); 
  CenterEncoder.setPosition(0, degrees); 

This code fixes the bug by first waiting 100 ms for the bug to ready itself (it won’t work if we don’t do this), then it gets the encoders positions which triggers the bug. It then wait’s 100 ms for all the encoders to be set to random values, and finally, sets them back to 0.

This is a hack, but not a problem with our code. We are wondering if anyone has any input on this problem.