I am using a TimedRobot base with subsystems running in the TeleopPeriodic() function. Today, I have tried and tried to implement our NavX micro, but whenever I try and get any point of data from it, it overruns the loop of 0.02 seconds that the TimedRobot uses. This is my code for the NavX:
`public class Gyro extends TimedRobot {
AHRS ahrs;
public static XboxController controller = new XboxController(0);
public void init() {
ahrs = new AHRS(I2C.Port.kMXP);
}
public void main() {
if (isOperatorControl() && isEnabled()) {
Timer.delay(0.020);
SmartDashboard.putNumber("Compass", ahrs.getCompassHeading());
SmartDashboard.putNumber("Heading", ahrs.getFusedHeading());
SmartDashboard.putNumber("Speed", ahrs.getVelocityX());
SmartDashboard.putNumber("Accerlation", ahrs.getWorldLinearAccelX());
}
}
}`
I have tried using both an if statement and a while loop, adding up to 2 seconds of delay, and simply just returning the values instead of displaying them to the dashboard. Anything I do, it still takes longer than 20MS to perform and therefore does not let me control my robot. The rest of the subsystems can be found here. Is the way I implemented the different subsystems just fundamentally incorrect and causing too much delay to start with?
Ok, so… this is my first year programming in java and for robotics in general, so I am extremely sorry if none of this code makes sense…
I probably don’t know how to use the extends thing properly, I thought I needed it to make the isOperatorControl and isEnabled Booleans work.
The init and main functions are how I call the specific parts of the code to run in the main robot.java file. I have my subsystems split into different files, and this is the way I get them to run:
Is that not how I’m supposed to do multi-file? Without those call signs, I couldn’t get my subsystems to run unless they were all in the same file…
I appreciate your help, and eagerly await your reply!
Why does gyro.init() get invoked each time teleop is entered? The AHRS class should only be instantiated once; if teleop is started more than once (like when practicing) is the AHRS class being instantiated multiple times?
You might also consider posting the error messages you see; in addition to your source code, this helps others better understand what is actually going on.
The error messages I am getting is a spam of the warning that the loop of 20MS is overrun and critical errors from my motor controller safeties that they aren’t being updated enough. I moved the AHRS to RobotInit() and restarted the robot and the problem still persists.
This still happens even without the Timer.delay. Although I am confused as to why this code is implemented in the base example that Kauai provides if it shouldn’t be there. Isn’t that only delaying the NavX outputs and nothing else? The drivetrain shouldn’t be affected by a delay in code that doesn’t modify it, correct?
Assuming you’re looking at the Java example there, that uses SampleRobot, not IterativeRobot or TimedRobot. Do not try to apply patterns required for SampleRobot in Iterative/TimedRobot.