Matbotix sensor is giving incorrect values, even after code manipulations

We were testing the MatBotix sensor from the 2019 KoP and using the syntax on MatBotix’s website to pull info from the sensor. We had used the code on their webpage to convert the data to inches, but we weren’t getting correct values when we printed to the driver’s console. We tried removing the scaling factor (0.125 per the website), but the values we were getting without the scaling factor was the same as the scaled data. Just to have a look, we multiplied the values by ten in the code, but we still got the same values as the previous two tries being printed to the driver’s console.

1 Like

Hi @Kaira!

Could you give us a few more details about how you’re using the sensor? What controller is it plugged into (Arduino, RoboRIO, etc)? How is it connected (analog input, PWM, etc)? This pre-release article may answer some of your questions.

It would be good to share the code as well. Part of the description reads as though it could be a S/W bug…

1 Like

Yeah, I’m thinking bug. If you changed the calculation but the answer is the same, that’s a big red flag.

1 Like

An example of the scaled data versus what you actually expected to see would be very helpful.

We are using PWM cable to connect the sensor to the roboRIO. It is plugged into Analog IN slot 0.

We are using code from previous FRC robot code and commented out what we didn’t need. We added in code for the sensor. We wanted to test to make sure the sensor would do what we asked, and adjust the rest of the code accordingly.

This is the code we added in to accommodate for the sensor:

import edu.wpi.first.wpilibj.AnalogInput; //get analog inputs from sensor

public class Robot extends TimedRobot { //Starts the robot skeleton code, already exists and does the basics: setup section and loop section
// factor to convert sensor values to a distance in inches
private static final double kValueToInches = 0.125;
private final AnalogInput m_ultrasonic = new AnalogInput(kUltrasonicPort);

public void teleopPeriodic() { //TeleOp. Loops this code to check things
System.out.println(“IN TELEOP!!”);
//m_myRobot.arcadeDrive(xbox.getY(Hand.kLeft), xbox.getX(Hand.kLeft)); //Establishing basic rules for how the drive train will move
m_myRobot.arcadeDrive(joyDrive.getY(GenericHID.Hand), joyDrive.getX(GenericHID.Hand));

// sensor returns a value from 0-4095 that is scaled to inches
double currentDistance = m_ultrasonic.getValue() * kValueToInches;

Check the article linked by BenjaminA to be sure things are hooked up properly.

I don’t have a way to test anything right now, but in that same article, they use getVoltage() instead of getValue(), which returns an integer – getVoltage() returns a double. That’s worth a shot.

If that doesn’t work, maybe let us know what’s being printed…

1 Like

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