We are using the Rev Robotics V3 color sensor and when the code is run it does recognize various values of red, green, and blue. We have calibrated it, but it still will not recognize the specific detected color on the color strip. Does anyone know why this is and how we can fix it?
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
//NEW CODE
package frc.robot;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.I2C.Port;
import edu.wpi.first.wpilibj.I2C;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj.util.Color;
import com.revrobotics.ColorSensorV3;
import com.revrobotics.ColorMatchResult;
import com.revrobotics.ColorMatch;
public class Robot extends TimedRobot {
I2C color = new I2C(Port.kOnboard,0x52);
private final ColorSensorV3 m_colorSensor = new ColorSensorV3(Port.kOnboard);
private final ColorMatch m_colorMatcher = new ColorMatch();
// color.write(0x00,192);
private final Color kBlueTarget = ColorMatch.makeColor(0.121, 0.430, 0.447);
private final Color kGreenTarget = ColorMatch.makeColor(0.165, 0.587, 0.249);
private final Color kRedTarget = ColorMatch.makeColor(0.520, 0.356, 0.125);
private final Color kYellowTarget = ColorMatch.makeColor(0.320, 0.563, 0.114);
@Override
public void robotInit() {
final ColorMatch m_colorMatcher = new ColorMatch();
m_colorMatcher.addColorMatch(kBlueTarget);
m_colorMatcher.addColorMatch(kGreenTarget);
m_colorMatcher.addColorMatch(kRedTarget);
m_colorMatcher.addColorMatch(kYellowTarget);
}
@Override
public void autonomousInit() {
}
/**
* This function is called periodically during autonomous.
*/
@Override
public void autonomousPeriodic() {
}
/**
* This function is called periodically during operator control.
*/
@Override
public void teleopPeriodic() {
Color detectedColor = m_colorSensor.getColor();
/**
* Run the color match algorithm on our detected color
*/
String colorString;
ColorMatchResult match = m_colorMatcher.matchClosestColor(detectedColor);
m_colorMatcher.setConfidenceThreshold(0.01);
if (match.color == kBlueTarget) {
colorString = "Blue";
} else if (match.color == kRedTarget) {
colorString = "Red";
} else if (match.color == kGreenTarget) {
colorString = "Green";
} else if (match.color == kYellowTarget) {
colorString = "Yellow";
} else {
colorString = "Unknown";
}
/**
* Open Smart Dashboard or Shuffleboard to see the color detected by the
* sensor.
*/
SmartDashboard.putNumber("Red", detectedColor.red);
SmartDashboard.putNumber("Green", detectedColor.green);
SmartDashboard.putNumber("Blue", detectedColor.blue);
SmartDashboard.putNumber("Confidence", match.confidence);
SmartDashboard.putString("Detected Color", colorString);
}
/**
* This function is called periodically during test mode.
*/
@Override
public void testPeriodic() {
}
}
I think the problem is in your robotInit(), you have a global ColorMatch object and inside the method you create a local ColorMatch object with the same exact variable name. Because of this when you add colors to match. It adds it to the local ColorMatch object, and not the global colorMatch. so just delete that line so it should look like
@Override
public void robotInit() {
m_colorMatcher.addColorMatch(kBlueTarget);
m_colorMatcher.addColorMatch(kGreenTarget);
m_colorMatcher.addColorMatch(kRedTarget);
m_colorMatcher.addColorMatch(kYellowTarget);
}