Skip to content

Commit

Permalink
More WIP splitting out settables
Browse files Browse the repository at this point in the history
  • Loading branch information
gerth2 committed Jul 12, 2024
1 parent 2f04dfd commit a70972a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
package org.photonvision.vision.camera.USBCameras;

import edu.wpi.first.cscore.UsbCamera;
import edu.wpi.first.cscore.VideoException;
import edu.wpi.first.cscore.VideoMode;
import edu.wpi.first.cscore.VideoProperty;
import edu.wpi.first.util.PixelFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.photonvision.common.configuration.CameraConfiguration;
import org.photonvision.common.configuration.ConfigManager;
import org.photonvision.common.util.math.MathUtils;
import org.photonvision.vision.camera.CameraQuirk;
import org.photonvision.vision.processes.VisionSourceSettables;

import edu.wpi.first.cscore.UsbCamera;
import edu.wpi.first.cscore.VideoException;
import edu.wpi.first.cscore.VideoMode;
import edu.wpi.first.cscore.VideoProperty;
import edu.wpi.first.util.PixelFormat;

public class GenericUSBCameraSettables extends VisionSourceSettables {
// We need to remember the last exposure set when exiting
// auto exposure mode so we can restore it
Expand All @@ -42,8 +40,7 @@ public class GenericUSBCameraSettables extends VisionSourceSettables {
protected CameraConfiguration configuration;

/**
* Forgiving "set this property" action. Produces a debug message but skips
* properties if they
* Forgiving "set this property" action. Produces a debug message but skips properties if they
* aren't supported Errors if the property exists but the set fails.
*
* @param property
Expand All @@ -63,8 +60,7 @@ protected void softSet(String property, int value) {
}

/**
* Returns the first property with a name in the list. Useful to find gandolf
* property that goes
* Returns the first property with a name in the list. Useful to find gandolf property that goes
* by many names in different os/releases/whatever
*
* @param options
Expand Down Expand Up @@ -92,10 +88,12 @@ protected Optional<VideoProperty> findProperty(String... options) {
return Optional.ofNullable(retProp);
}

protected void setUpExposureProperties(){
protected void setUpExposureProperties() {
// Photonvision needs to be able to control absolute exposure. Make sure we can
// first.
var expProp = findProperty("raw_exposure_absolute", "raw_exposure_time_absolute", "exposure", "raw_Exposure");
var expProp =
findProperty(
"raw_exposure_absolute", "raw_exposure_time_absolute", "exposure", "raw_Exposure");

// Photonvision needs to be able to control auto exposure. Make sure we can
// first.
Expand Down Expand Up @@ -136,7 +134,6 @@ public GenericUSBCameraSettables(CameraConfiguration configuration, UsbCamera ca
PROP_AUTO_EXPOSURE_ENABLED = 0;
PROP_AUTO_EXPOSURE_DISABLED = 1;
}

}

public void setAllCamDefaults() {
Expand Down Expand Up @@ -198,10 +195,10 @@ public void setExposureRaw(double exposureRaw) {

int propVal = (int) MathUtils.limit(exposureRaw, minExposure, maxExposure);

//if (configuration.cameraQuirks.hasQuirk(CameraQuirk.LifeCamExposure)) {
// if (configuration.cameraQuirks.hasQuirk(CameraQuirk.LifeCamExposure)) {
// // Lifecam only allows certain settings for exposure
// propVal = MathUtils.quantize(propVal, CameraQuirkConstants.LifecamAllowableExposures);
//}
// }

logger.debug(
"Setting property "
Expand Down Expand Up @@ -296,14 +293,16 @@ public HashMap<Integer, VideoMode> getAllVideoModes() {
}

// Sort by resolution
var sortedList = videoModesList.stream()
.distinct() // remove redundant video mode entries
.sorted(((a, b) -> (b.width + b.height) - (a.width + a.height)))
.collect(Collectors.toList());
var sortedList =
videoModesList.stream()
.distinct() // remove redundant video mode entries
.sorted(((a, b) -> (b.width + b.height) - (a.width + a.height)))
.collect(Collectors.toList());
Collections.reverse(sortedList);

// On vendor cameras, respect blacklisted indices
var indexBlacklist = ConfigManager.getInstance().getConfig().getHardwareConfig().blacklistedResIndices;
var indexBlacklist =
ConfigManager.getInstance().getConfig().getHardwareConfig().blacklistedResIndices;
for (int badIdx : indexBlacklist) {
sortedList.remove(badIdx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import edu.wpi.first.cscore.VideoException;
import edu.wpi.first.cscore.VideoProperty;
import edu.wpi.first.util.RuntimeDetector;

import java.util.*;
import org.photonvision.common.configuration.CameraConfiguration;
import org.photonvision.common.logging.LogGroup;
Expand All @@ -42,7 +41,6 @@ public class GenericUSBCameraSource extends VisionSource {
protected FrameProvider usbFrameProvider;
private final CvSink cvSink;


public GenericUSBCameraSource(CameraConfiguration config) {
super(config);

Expand Down Expand Up @@ -72,8 +70,7 @@ public GenericUSBCameraSource(CameraConfiguration config) {
// in
printCameraProperaties();

var cameraBroken =
getCameraConfiguration().cameraQuirks.hasQuirk(CameraQuirk.CompletelyBroken);
var cameraBroken = getCameraConfiguration().cameraQuirks.hasQuirk(CameraQuirk.CompletelyBroken);

if (cameraBroken) {
// Known issues - Disable this camera
Expand All @@ -98,24 +95,24 @@ public GenericUSBCameraSource(CameraConfiguration config) {
// Functional camera, set up the frame provider and configure defaults
usbFrameProvider = new USBFrameProvider(cvSink, settables);
settables.setAllCamDefaults();

}
}
}

/**
* Factory for making appropriate settables
*
* @param config
* @param camera
* @return
*/
private GenericUSBCameraSettables createSettables(CameraConfiguration config, UsbCamera camera){
private GenericUSBCameraSettables createSettables(CameraConfiguration config, UsbCamera camera) {

var quirks = getCameraConfiguration().cameraQuirks;

GenericUSBCameraSettables settables;

if(quirks.hasQuirk(CameraQuirk.LifeCamExposure) && RuntimeDetector.isWindows()){
if (quirks.hasQuirk(CameraQuirk.LifeCamExposure) && RuntimeDetector.isWindows()) {
logger.debug("Using Microsoft Lifecam 3000 Windows-Specific Settables");
settables = new LifeCam3kWindowsCameraSettables(config, camera);
} else {
Expand All @@ -126,10 +123,8 @@ private GenericUSBCameraSettables createSettables(CameraConfiguration config, Us
settables.setUpExposureProperties();

return settables;

}


private void printCameraProperaties() {
VideoProperty[] cameraProperties = null;
try {
Expand Down Expand Up @@ -176,7 +171,6 @@ public VisionSourceSettables getSettables() {
return this.settables;
}


@Override
public boolean isVendorCamera() {
return false; // Vendors do not supply USB Cameras
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package org.photonvision.vision.camera.USBCameras;
import org.photonvision.common.configuration.CameraConfiguration;
import org.photonvision.common.util.math.MathUtils;

import edu.wpi.first.cscore.UsbCamera;
import edu.wpi.first.cscore.VideoException;

import org.photonvision.common.configuration.CameraConfiguration;
import org.photonvision.common.util.math.MathUtils;

public class LifeCam3kWindowsCameraSettables extends GenericUSBCameraSettables {

Expand All @@ -17,9 +16,9 @@ public LifeCam3kWindowsCameraSettables(CameraConfiguration configuration, UsbCam
}

@Override
protected void setUpExposureProperties(){
protected void setUpExposureProperties() {

autoExposureProp = null; //Not Used
autoExposureProp = null; // Not Used

exposureAbsProp = camera.getProperty("raw_Exposure");
this.minExposure = exposureAbsProp.getMin();
Expand Down Expand Up @@ -54,7 +53,6 @@ public void setExposureRaw(double exposureRaw) {
if (lastBrightness >= 0) {
setBrightness(lastBrightness - 1);
}


} catch (VideoException e) {
logger.error("Failed to set camera exposure!", e);
Expand Down Expand Up @@ -82,10 +80,9 @@ public void setAutoExposure(boolean cameraAutoExposure) {
public void setAllCamDefaults() {
// Common settings for all cameras to attempt to get their image
// as close as possible to what we want for image processing
softSet("raw_Contrast", 5);
softSet("raw_Saturation", 85);
softSet("raw_Sharpness", 25);
softSet("WhiteBalance", 4000);
softSet("raw_Contrast", 5);
softSet("raw_Saturation", 85);
softSet("raw_Sharpness", 25);
softSet("WhiteBalance", 4000);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@

package org.photonvision.vision.processes;

import edu.wpi.first.cscore.UsbCamera;
import org.photonvision.common.configuration.CameraConfiguration;
import org.photonvision.common.util.TestUtils;
import org.photonvision.vision.camera.QuirkyCamera;
import org.photonvision.vision.camera.USBCameras.GenericUSBCameraSettables;
import org.photonvision.vision.camera.USBCameras.GenericUSBCameraSource;
import org.photonvision.vision.frame.provider.FileFrameProvider;

import edu.wpi.first.cscore.UsbCamera;

public class MockUsbCameraSource extends GenericUSBCameraSource {
/** Used for unit tests to better simulate a usb camera without a camera being present. */
public MockUsbCameraSource(CameraConfiguration config, int pid, int vid) {
Expand Down

0 comments on commit a70972a

Please sign in to comment.