finished integration between new client and backend

commands needs to be implemented
This commit is contained in:
ori agranat
2019-10-16 14:13:12 +03:00
parent 0828d2290a
commit dc2b738190
14 changed files with 75 additions and 45 deletions

View File

@@ -1,14 +1,12 @@
package com.chameleonvision.network;
import com.chameleonvision.settings.ConnectionType;
import com.chameleonvision.settings.NetworkSettings;
import com.chameleonvision.settings.Platform;
import com.chameleonvision.settings.SettingsManager;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class NetworkManager {
@@ -86,7 +84,7 @@ public class NetworkManager {
}
var genSettings = SettingsManager.GeneralSettings;
boolean isStatic = genSettings.connectionType.equals(ConnectionType.Static);
boolean isStatic = genSettings.connectionType.equals(NetworkIPMode.STATIC);
if (isStatic) {
var splitIPAddr = genSettings.ip.split("\\.");

View File

@@ -1,5 +0,0 @@
package com.chameleonvision.settings;
public enum ConnectionType {
DHCP,Static
}

View File

@@ -1,8 +1,10 @@
package com.chameleonvision.settings;
import com.chameleonvision.network.NetworkIPMode;
public class GeneralSettings {
public int teamNumber = 1577;
public ConnectionType connectionType = ConnectionType.DHCP;
public NetworkIPMode connectionType = NetworkIPMode.DHCP;
public String ip = "";
public String gateway = "";
public String netmask = "";

View File

@@ -19,7 +19,7 @@ public class Pipeline {
public SortMode sortMode = SortMode.Largest;
public TargetGroup targetGroup = TargetGroup.Single;
public TargetIntersection targetIntersection = TargetIntersection.Up;
public double M = 1;
public double B = 0;
public double m = 1;
public double b = 0;
public boolean isCalibrated = false;
}

View File

@@ -13,7 +13,8 @@ import java.util.stream.IntStream;
public class Camera {
private static final float DEFAULT_FOV = 60.8f;
private static final double DEFAULT_FOV = 60.8;
private static final StreamDivisor DEFAULT_STREAMDIVISOR = StreamDivisor.none;
private static final int MINIMUM_FPS = 30;
private static final int MINIMUM_WIDTH = 320;
private static final int MINIMUM_HEIGHT = 200;
@@ -29,8 +30,8 @@ public class Camera {
private final CvSink cvSink;
private final Object cvSourceLock = new Object();
private CvSource cvSource;
private float FOV;
private int streamDivisor;
private Double FOV;
private StreamDivisor streamDivisor;
private CameraValues camVals;
private CamVideoMode camVideoMode;
private int currentPipelineIndex;
@@ -40,27 +41,29 @@ public class Camera {
this(cameraName, DEFAULT_FOV);
}
public Camera(String cameraName, float fov) {
public Camera(String cameraName, double fov) {
this(cameraName,CameraManager.AllUsbCameraInfosByName.get(cameraName), fov);
}
public Camera(String cameraName, UsbCameraInfo usbCamInfo, float fov) {
this(cameraName ,usbCamInfo, fov, new HashMap<>(), 0);
public Camera(String cameraName, UsbCameraInfo usbCameraInfo, double fov) {
this(cameraName,usbCameraInfo, fov, DEFAULT_STREAMDIVISOR);
}
public Camera(String cameraName, UsbCameraInfo usbCamInfo, double fov,StreamDivisor divisor) {
this(cameraName ,usbCamInfo, fov, new HashMap<>(), 0, divisor);
}
public Camera(String cameraName, float fov, int videoModeIndex) {
this(cameraName, fov, new HashMap<>(), videoModeIndex);
public Camera(String cameraName, double fov, int videoModeIndex , StreamDivisor divisor) {
this(cameraName, fov, new HashMap<>(), videoModeIndex, divisor);
}
public Camera(String cameraName, float fov, HashMap<Integer, Pipeline> pipelines, int videoModeIndex) {
this(cameraName, CameraManager.AllUsbCameraInfosByName.get(cameraName), fov, pipelines, videoModeIndex);
public Camera(String cameraName, double fov, HashMap<Integer, Pipeline> pipelines, int videoModeIndex , StreamDivisor divisor) {
this(cameraName, CameraManager.AllUsbCameraInfosByName.get(cameraName), fov, pipelines, videoModeIndex, divisor);
}
public Camera(String cameraName, UsbCameraInfo usbCamInfo, float fov, HashMap<Integer, Pipeline> pipelines, int videoModeIndex) {
public Camera(String cameraName, UsbCameraInfo usbCamInfo, double fov, HashMap<Integer, Pipeline> pipelines, int videoModeIndex, StreamDivisor divisor) {
FOV = fov;
name = cameraName;
path = usbCamInfo.path;
streamDivisor = divisor;
UsbCam = new UsbCamera(name, path);
this.pipelines = pipelines;
@@ -143,11 +146,11 @@ public class Camera {
if (pipelineNumber - 1 > pipelines.size()) return;
currentPipelineIndex = pipelineNumber;
}
public int getStreamDivisor(){
public StreamDivisor getStreamDivisor(){
return streamDivisor;
}
public void setStreamDivisor(int divisor){
streamDivisor = divisor;
streamDivisor = StreamDivisor.values()[divisor];
}
public HashMap<Integer, Pipeline> getPipelines() {
@@ -165,12 +168,12 @@ public class Camera {
.orElse(-1);
}
public float getFOV() {
public double getFOV() {
return FOV;
}
public void setFOV(float fov) {
FOV = fov;
public void setFOV(Number fov) {
FOV = fov.doubleValue();
camVals = new CameraValues(this);
}

View File

@@ -14,9 +14,10 @@ public class CameraDeserializer implements JsonDeserializer<Camera> {
@Override
public Camera deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException {
var jsonObj = jsonElement.getAsJsonObject();
var camFOV = jsonObj.get("FOV").getAsFloat();
var camFOV = jsonObj.get("FOV").getAsDouble();
var camName = jsonObj.get("name").getAsString();
var videoModeIndex = jsonObj.get("resolution").getAsInt();
var divisor = StreamDivisor.values()[jsonObj.get("streamDivisor").getAsInt()];
var pipelines = jsonObj.get("pipelines");
@@ -30,6 +31,6 @@ public class CameraDeserializer implements JsonDeserializer<Camera> {
e.printStackTrace();
}
return actualPipelines != null ? new Camera(camName, camFOV, actualPipelines, videoModeIndex) : new Camera(camName, camFOV, videoModeIndex);
return actualPipelines != null ? new Camera(camName, camFOV, actualPipelines, videoModeIndex,divisor) : new Camera(camName, camFOV, videoModeIndex, divisor);
}
}

View File

@@ -10,6 +10,7 @@ public class CameraSerializer implements JsonSerializer<Camera> {
obj.addProperty("FOV", camera.getFOV());
obj.addProperty("path", camera.path);
obj.addProperty("name", camera.name);
obj.addProperty("streamDivisor", camera.getStreamDivisor().ordinal());
var pipelines = context.serialize(camera.getPipelines());
obj.add("pipelines", pipelines);

View File

@@ -7,7 +7,7 @@ import org.apache.commons.math3.util.FastMath;
public class CameraValues {
public final int ImageWidth;
public final int ImageHeight;
public final float FOV;
public final double FOV;
public final double ImageArea;
public final double CenterX;
public final double CenterY;
@@ -24,7 +24,7 @@ public class CameraValues {
this(camera.getVideoMode().width, camera.getVideoMode().height, camera.getFOV());
}
public CameraValues(int imageWidth, int imageHeight, float fov) {
public CameraValues(int imageWidth, int imageHeight, double fov) {
ImageWidth = imageWidth;
ImageHeight = imageHeight;
FOV = fov;

View File

@@ -0,0 +1,14 @@
package com.chameleonvision.vision.camera;
public enum StreamDivisor {
none(1),
half(2),
quarter(4),
sixth(6);
public final Integer value;
StreamDivisor(int value) {
this.value = value;
}
}

View File

@@ -4,7 +4,6 @@ import com.chameleonvision.settings.SettingsManager;
import com.chameleonvision.vision.Orientation;
import com.chameleonvision.vision.Pipeline;
import com.chameleonvision.vision.camera.Camera;
import com.chameleonvision.vision.camera.CameraException;
import com.chameleonvision.web.ServerHandler;
import edu.wpi.cscore.VideoException;
import edu.wpi.first.networktables.*;
@@ -169,8 +168,8 @@ public class VisionProcess implements Runnable {
pipelineResult.CalibratedX = camera.getCamVals().CenterX;
pipelineResult.CalibratedY = camera.getCamVals().CenterY;
} else {
pipelineResult.CalibratedX = (finalRect.center.y - currentPipeline.B) / currentPipeline.M;
pipelineResult.CalibratedY = (finalRect.center.x * currentPipeline.M) + currentPipeline.B;
pipelineResult.CalibratedX = (finalRect.center.y - currentPipeline.b) / currentPipeline.m;
pipelineResult.CalibratedY = (finalRect.center.x * currentPipeline.m) + currentPipeline.b;
}
pipelineResult.Pitch = camera.getCamVals().CalculatePitch(finalRect.center.y, pipelineResult.CalibratedY);
pipelineResult.Yaw = camera.getCamVals().CalculateYaw(finalRect.center.x, pipelineResult.CalibratedX);

View File

@@ -55,7 +55,7 @@ public class ServerHandler {
}
case "cameraSettings": {
HashMap camSettings = (HashMap)entry.getValue();
CameraManager.getCurrentCamera().setFOV((float)camSettings.get("fov"));
CameraManager.getCurrentCamera().setFOV((Number)camSettings.get("fov"));
CameraManager.getCurrentCamera().setStreamDivisor((Integer) camSettings.get("streamDivisor"));
CameraManager.getCurrentCamera().setCamVideoMode((Integer) camSettings.get("resolution"),true);
break;
@@ -85,7 +85,12 @@ public class ServerHandler {
setField(CameraManager.getCurrentCamera().getCurrentPipeline(),entry.getKey(),entry.getValue());
switch (entry.getKey()){
case "exposure":{
CameraManager.getCurrentCamera().setExposure((Integer) entry.getValue());
try{
CameraManager.getCurrentCamera().setExposure((Integer) entry.getValue());
} catch (Exception e){
System.err.println("Camera Does not support exposure change");
}
}
case "brightness":{
CameraManager.getCurrentCamera().setBrightness((Integer) entry.getValue());
@@ -133,13 +138,12 @@ public class ServerHandler {
}
}
public static void broadcastMessage(Object obj) {//TODO fix sending for msgpack
public static void broadcastMessage(Object obj) {
broadcastMessage(obj, null);//Broadcasts the message to every user
}
private static HashMap<String,Object> getOrdinalPipeline() throws CameraException, IllegalAccessException {
HashMap<String,Object> tmp = new HashMap<>();
for (Field f : Pipeline.class.getFields()){
if (!f.getType().isEnum()){
tmp.put(f.getName(),f.get(CameraManager.getCurrentCamera().getCurrentPipeline()));
@@ -150,7 +154,7 @@ public class ServerHandler {
}
return tmp;
}
private static HashMap<String,Object> getOrdinalSettings() throws IllegalAccessException {
private static HashMap<String,Object> getOrdinalSettings(){
HashMap<String,Object> tmp = new HashMap<>();
tmp.put("teamNumber",SettingsManager.GeneralSettings.teamNumber);
tmp.put("connectionType",SettingsManager.GeneralSettings.connectionType.ordinal());
@@ -160,11 +164,24 @@ public class ServerHandler {
tmp.put("hostname",SettingsManager.GeneralSettings.hostname);
return tmp;
}
private static HashMap<String ,Object> getOrdinalCameraSettings(){
HashMap<String,Object> tmp = new HashMap<>();
try {
var currentCamera = CameraManager.getCurrentCamera();
tmp.put("fov",currentCamera.getFOV());
tmp.put("streamDivisor",currentCamera.getStreamDivisor().ordinal());
tmp.put("resolution",currentCamera.getVideoModeIndex());
} catch (CameraException e) {
e.printStackTrace();
}
return tmp;
}
public static void sendFullSettings() {
//General settings
Map<String, Object> fullSettings = new HashMap<>();
try {
fullSettings.put("settings", getOrdinalSettings());
fullSettings.put("cameraSettings",getOrdinalCameraSettings());
fullSettings.put("cameraList", CameraManager.getAllCamerasByName().keySet());
var currentCamera = CameraManager.getCurrentCamera();
fullSettings.put("pipeline", getOrdinalPipeline());