Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix grayscale passthrough #1083

Merged
merged 18 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.photonvision.vision.camera;

import edu.wpi.first.cscore.VideoMode;
import edu.wpi.first.math.MathUtil;
import edu.wpi.first.math.Pair;
import java.util.HashMap;
import org.photonvision.common.configuration.CameraConfiguration;
Expand Down Expand Up @@ -133,23 +134,29 @@ public void setExposure(double exposure) {
return;
}

// Map exposure from 0 to 100 (UI Values) to 0.1 to 100 since 0 in libcamera is auto exposure.
// 0.1 since it is the next lowest value available through the UI.
MathUtils.map(exposure, 0.0, 100.0, 0.1, 100);

// HACKS!
// If we set exposure too low, libcamera crashes or slows down
// Very weird and smelly
// For now, band-aid this by just not setting it lower than the "it breaks" limit
// is different depending on camera.
// is different depending on camera.
if (sensorModel == LibCameraJNI.SensorModel.OV9281) {
if (exposure < 6.0) {
exposure = 6.0;
}
// Map and clamp exposure to 6 to 100 since setting exposure on ov9281 lower
// than 6 will crash libcamera at resolutions lower than max.
exposure = MathUtils.map(exposure, 0.1, 100.0, 6.0, 100);
BytingBulldogs3539 marked this conversation as resolved.
Show resolved Hide resolved
exposure = MathUtil.clamp(exposure, 0.1, 100.0);
} else if (sensorModel == LibCameraJNI.SensorModel.OV5647) {
if (exposure < 0.7) {
exposure = 0.7;
}
// Map and clamp exposure to 0.7 to 100 since setting exposure on ov5647 lower
// than 0.7 will crash libcamera at resolutions lower than max.
exposure = MathUtils.map(exposure, 0.1, 100.0, 0.7, 100);
exposure = MathUtil.clamp(exposure, 0.1, 100.0);
}

lastManualExposure = exposure;
var success = LibCameraJNI.setExposure(r_ptr, (int) Math.round(exposure) * 800);
var success = LibCameraJNI.setExposure(r_ptr, (int) (exposure * 800));
if (!success) LibcameraGpuSource.logger.warn("Couldn't set Pi Camera exposure");
}

Expand All @@ -164,8 +171,12 @@ public void setBrightness(int brightness) {
@Override
public void setGain(int gain) {
lastGain = gain;
// TODO units here seem odd -- 5ish seems legit? So divide by 10
var success = LibCameraJNI.setAnalogGain(r_ptr, gain / 10.0);

// Map and clamp gain to values between 1 and 10 (libcamera min and gain that just seems higher
// than ever needed) from 0 to 100 (UI values).
var success =
LibCameraJNI.setAnalogGain(
r_ptr, MathUtil.clamp(MathUtils.map(gain, 0.0, 100.0, 1.0, 10.0), 1.0, 10.0));
if (!success) LibcameraGpuSource.logger.warn("Couldn't set Pi Camera gain");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.opencv.core.Mat;
import org.photonvision.common.util.math.MathUtils;
import org.photonvision.raspi.LibCameraJNI;
import org.photonvision.raspi.LibCameraJNI.SensorModel;
import org.photonvision.vision.camera.LibcameraGpuSettables;
import org.photonvision.vision.frame.Frame;
import org.photonvision.vision.frame.FrameProvider;
Expand Down Expand Up @@ -91,9 +90,7 @@ public Frame get() {

@Override
public void requestFrameThresholdType(FrameThresholdType type) {
if (settables.getModel() == SensorModel.OV9281 && type.equals(FrameThresholdType.GREYSCALE))
LibCameraJNI.setGpuProcessType(settables.r_ptr, 4); // 4 = Grayscale pass through.
else LibCameraJNI.setGpuProcessType(settables.r_ptr, type.ordinal());
LibCameraJNI.setGpuProcessType(settables.r_ptr, type.ordinal());
}

@Override
Expand Down
Binary file not shown.