Issue with coding ADIS16448 Gyro when enabling in Teleop

Hello,

I am a team mentor helping out with some programming. I have been trying to code motor controllers, encoders and an ADIS16448 on our testbed control system. Currently, I’m just trying to get the Gyro’s Angle value to show up on the SmartDashboard. I can get other values (like encoders and text etc) but when I try use the .getAngle() function in the code, it will deploy but as soon as I try to enable the code, it will crash and flash “no robot code”. i’ve tried other functions of they Gyro with the same error. I even tried doing a System.out.println and the same thing happens only when I try to send the angle value.

Here are the 2 lines of code that will crash the roborio once I hit enable:
SmartDashboard.putNumber(“Angle”, m_imu.getAngle());
System.out.println("Angle: " + m_imu.getAngle());

Below is the code (which was built from some of the sample code provided). It’s rather messy with a LOT of commented out code but it’s just for our testbed to get the Gyro working.

// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

package frc.robot;

import edu.wpi.first.util.sendable.SendableRegistry;

import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.drive.DifferentialDrive;
import edu.wpi.first.wpilibj.motorcontrol.PWMSparkMax;
import edu.wpi.first.wpilibj.motorcontrol.PWMTalonSRX;

import edu.wpi.first.wpilibj.motorcontrol.Spark;
import edu.wpi.first.wpilibj.motorcontrol.VictorSP;
import edu.wpi.first.wpilibj.simulation.ADIS16448_IMUSim;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj.motorcontrol.PWMSparkFlex;

import com.ctre.phoenix.motorcontrol.ControlMode;
//import edu.wpi.first.wpilibj.motorcontrol.
import com.ctre.phoenix.motorcontrol.can.TalonSRX;

import com.revrobotics.CANSparkMax;
import com.revrobotics.RelativeEncoder;
import com.revrobotics.CANSparkLowLevel.MotorType;

import edu.wpi.first.wpilibj.ADIS16448_IMU;

/**

  • This is a demo program showing the use of the DifferentialDrive class. Runs the motors with
  • arcade steering.
    */
    public class Robot extends TimedRobot {

//Spark spark = new Spark(0); // 0 is the RIO PWM port this is connected to

//SparkMax Motor Controller
private final PWMSparkMax m_NeoMini = new PWMSparkMax(10);
// private final PWMSparkMax m_rightMotor = new PWMSparkMax(1);
// private final PWMSparkMax m_rightMotor = new PWMSparkMax(3);
private CANSparkMax m_SparkMaxMotor = new CANSparkMax(10, MotorType.kBrushless);

//Spark Motor Controller
private final Spark m_leftMotor = new Spark(0);
private final Spark m_rightMotor = new Spark(1);

//VEX CRT Talon Speed Controller
// private final
// private final PWMSparkMax m_rightMotor = new PWMSparkMax(1);
//TalonSRX motor_TalonSRX_3 = new TalonSRX(3);
//private final TalonSRX m_rightMotor = new TalonSRX(3);

TalonSRX _talon3 = new TalonSRX(3); // Change ‘0’ to match device ID in Tuner. Use VictorSPX for Victor SPXs
TalonSRX _talon4 = new TalonSRX(4); // Change ‘0’ to match device ID in Tuner. Use VictorSPX for Victor SPXs
//Joystick _joystick = new Joystick(0);

// private final PWMSparkMax m_rightMotor = new PWMSparkMax(1);
private final DifferentialDrive m_robotDrive =
new DifferentialDrive(m_leftMotor::set, m_rightMotor::set);

//JOYSTICK Declaration
private final Joystick m_stick = new Joystick(0); //TEMPORARILY CHANGED TO 2nd Joystick Port 1
private final Joystick m_rightStick = new Joystick(1);

//ENCODER Declaration

private RelativeEncoder m_SparkMaxEncoder;

//GYRO Declaration

private final double kAngleSetpoint = 0.0;
private final double kP = 0.005; // propotional turning constant
//public static final ADIS16448_IMU imu = new ADIS16448_IMU();
//private ADIS16448_IMU m_imu;
private ADIS16448_IMU m_imu;
private ADIS16448_IMUSim m_imuSim;

//ADIS16448_IMU gyro = new ADIS16448_IMU();

public Robot() {
SendableRegistry.addChild(m_robotDrive, m_leftMotor);
SendableRegistry.addChild(m_robotDrive, m_rightMotor);
}

@Override
public void robotInit() {
// We need to invert one side of the drivetrain so that positive voltages
// result in both sides moving forward. Depending on how your robot’s
// gearbox is constructed, you might have to invert the left side instead.
m_rightMotor.setInverted(true);
}

@Override
public void teleopPeriodic() {
// Drive with arcade drive.
// That means that the Y axis drives forward
// and backward, and the X turns left and right.
m_robotDrive.arcadeDrive(-m_stick.getY(), -m_stick.getX());

//double stick = _joystick.getRawAxis(1);
double stick = m_stick.getRawAxis(2);

double stick2 = m_rightStick.getRawAxis(0);

//NEED TO FIX SPEED FOR NeoMINI!!!
// m_NeoMini.set(.5);
_talon3.set(ControlMode.PercentOutput, stick);
_talon4.set(ControlMode.PercentOutput, stick);

//m_NeoMini.set(ControlMode.PercentOutput,stick);

// m_SparkMaxMotor.set(0.5);
m_SparkMaxMotor.set(m_rightStick.getY());

// motor_TalonSRX_3.set(m_stick.getZ());

// motor_TalonSRX_3.set(0.5);
/*
Spark spark = new Spark(0); // 0 is the RIO PWM port this is connected to

spark.set(-0.75); // the % output of the motor, between -1 and 1

VictorSP victor = new VictorSP(1); // 1 is the RIO PWM port this is connected to

victor.set(0.6); // the % output of the motor, between -1 and 1

SmartDashboard.putBoolean(“Bridge Limit”, bridgeTipper.atBridge());
SmartDashboard.putNumber(“Bridge Angle”, bridgeTipper.getPosition());
SmartDashboard.putNumber(“Swerve Angle”, drivetrain.getSwerveAngle());
SmartDashboard.putNumber(“Left Drive Encoder”, drivetrain.getLeftEncoder());
SmartDashboard.putNumber(“Right Drive Encoder”, drivetrain.getRightEncoder());
SmartDashboard.putNumber(“Turret Pot”, turret.getCurrentAngle());
SmartDashboard.putNumber(“Turret Pot Voltage”, turret.getAverageVoltage());

 */

m_SparkMaxEncoder = m_SparkMaxMotor.getEncoder();

SmartDashboard.putNumber("Static Number Ten: ", 10);
SmartDashboard.putNumber("SparkMaxEncoder: ", m_SparkMaxEncoder.getPosition());

// double turningValue = (kAngleSetpoint - m_imu.getAngle()) * kP;
// SmartDashboard.putNumber("Gyro Value: ", 20);

//SmartDashboard.putNumber("Gyro Value: ", m_imu.getAccelX());

SmartDashboard.putNumber(“Angle”, m_imu.getAngle());

//SmartDashboard.putNumber(“Z”, m_imu.getGyroAngleZ());

System.out.println(“HELLO WORLD!”);
//System.out.println("Angle: " + m_imu.);
// System.out.println("Angle: " + 30);
System.out.println("Angle: " + m_imu.getAngle());

}

Here is a snapshot of the drivestation log

Any help would be greatly appreciated!!

This is your problem–it’s never initialized, so you’re getting a NullPointerException. You need to do something similar to the commented-out line a few lines above and initialize it with new ADIS16448_IMU();

Thank you so much for your response and help. We got it working!

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.