I have been working with Team 8 for the past few days and we’ve noticed that our loop period is significantly overrunning the allotted 20ms. It’s currently on the order of 100-150ms (varies greatly), so all the code is only running at about 7-10Hz, which is painfully slow and visibly manifests itself in control responsiveness.
We spent some time tracing what part of the code is taking up all of this time. What surprised me is that there doesn’t seem to be one singular hold up – everything just runs slowly.
We started by logging the time taken for each function call in teleopPeriodic (first image). It makes sense that reading sensors (update_state) and setting outputs (update_hardware) take the longest, but the timing of all the function calls seems an order of magnitude off.
We then looked at the timing within update_state, trying to pinpoint a single function call that was blocking (second image). What we found was that each part of the method seemed to just be taking a long time. The encoders are the hall effects on the NEOs, the pigeon is connected via a Talon SRX, and the ultrasonic is through the RIO.
Lastly, we tried emptying teleopPeriodic and measuring how accurate the loop period was (third image). Surprisingly, the loop period varied from 15ms to 21ms, finally oscillating between 19ms and 21ms after roughly ~100 cycles. (the number in the image is just System.nanoTime()/1.0e-6)
I took a quick look at the CPU utilization with our normal code, which hovers around 70% and occasionally spikes to 85-90% for very brief periods of time. The CAN bus also sits very consistently around 60-70% utilization. All of our libraries and firmware are up to date as well.
We are thoroughly perplexed as to what could be causing the loop overruns, inconsistent loop period, and whether the observations are connected. I am under the impression other teams run more computationally intensive tasks on their RoboRIO without problem, which further confuses me.
Open to any suggestions of how to further diagnose / explore this, or comments on similar experiences.