Misc bugfixes (#39)

* Selectively send pipeline changes

* Make input and output both rotated

* Notify UI of driver mode change over NT

* Fix "show multiple"

* Rename extent to fullness, fix area filtering

This is a breaking change to docs (make sure we note area is out of 100 and is percentage)

* Apply stream divisor to both streams

Co-authored-by: Banks T <btrout.dhrs@gmail.com>
This commit is contained in:
Matt
2020-07-17 20:05:03 -07:00
committed by GitHub
parent 0d155d9c6a
commit 768964c8fc
19 changed files with 231 additions and 145 deletions

View File

@@ -17,6 +17,7 @@
package org.photonvision.common.dataflow.events;
import io.javalin.websocket.WsContext;
import java.util.HashMap;
import org.photonvision.common.dataflow.DataChangeDestination;
import org.photonvision.common.dataflow.DataChangeSource;
@@ -24,15 +25,21 @@ import org.photonvision.common.dataflow.DataChangeSource;
public class IncomingWebSocketEvent<T> extends DataChangeEvent<T> {
public final Integer cameraIndex;
public final WsContext originContext;
public IncomingWebSocketEvent(DataChangeDestination destType, String propertyName, T newValue) {
this(destType, propertyName, newValue, null);
this(destType, propertyName, newValue, null, null);
}
public IncomingWebSocketEvent(
DataChangeDestination destType, String propertyName, T newValue, Integer cameraIndex) {
DataChangeDestination destType,
String propertyName,
T newValue,
Integer cameraIndex,
WsContext originContext) {
super(DataChangeSource.DCS_WEBSOCKET, destType, propertyName, newValue);
this.cameraIndex = cameraIndex;
this.originContext = originContext;
}
@SuppressWarnings("unchecked")

View File

@@ -17,6 +17,7 @@
package org.photonvision.common.dataflow.events;
import io.javalin.websocket.WsContext;
import java.util.HashMap;
import org.photonvision.common.dataflow.DataChangeDestination;
import org.photonvision.common.dataflow.DataChangeSource;
@@ -24,17 +25,24 @@ import org.photonvision.server.UIUpdateType;
public class OutgoingUIEvent<T> extends DataChangeEvent<T> {
public final UIUpdateType updateType;
public final WsContext originContext;
public OutgoingUIEvent(UIUpdateType updateType, String propertyName, T newValue) {
public OutgoingUIEvent(
UIUpdateType updateType, String propertyName, T newValue, WsContext originContext) {
super(DataChangeSource.DCS_WEBSOCKET, DataChangeDestination.DCD_UI, propertyName, newValue);
this.updateType = updateType;
this.originContext = originContext;
}
public static OutgoingUIEvent<HashMap<String, Object>> wrappedOf(
UIUpdateType uiUpdateType, String commandName, String propertyName, Object value) {
UIUpdateType uiUpdateType,
String commandName,
String propertyName,
Object value,
WsContext originContext) {
HashMap<String, Object> data = new HashMap<>();
data.put(propertyName, value);
return new OutgoingUIEvent<>(uiUpdateType, commandName, data);
return new OutgoingUIEvent<>(uiUpdateType, commandName, data, originContext);
}
}

View File

@@ -0,0 +1,81 @@
/*
* Copyright (C) 2020 Photon Vision.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.photonvision.common.dataflow.websocket;
import com.fasterxml.jackson.core.JsonProcessingException;
import edu.wpi.first.wpilibj.MedianFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import org.photonvision.common.dataflow.CVPipelineResultConsumer;
import org.photonvision.common.logging.LogGroup;
import org.photonvision.common.logging.Logger;
import org.photonvision.server.SocketHandler;
import org.photonvision.vision.pipeline.result.CVPipelineResult;
public class UIDataPublisher implements CVPipelineResultConsumer {
private static final Logger logger = new Logger(UIDataPublisher.class, LogGroup.VisionModule);
// TODO check if this is the right spot to do FPS calculation
private final MedianFilter fpsAverager = new MedianFilter(10);
private final int index;
private long lastRunTime = 0;
private long lastUIResultUpdateTime = 0;
public UIDataPublisher(int index) {
this.index = index;
}
@Override
public void accept(CVPipelineResult result) {
var now = System.currentTimeMillis();
var fps = fpsAverager.calculate(1000.0 / (now - lastRunTime));
lastRunTime = now;
// only update the UI at 15hz
if (lastUIResultUpdateTime + 1000.0 / 15.0 > now) return;
var uiMap = new HashMap<Integer, HashMap<String, Object>>();
var dataMap = new HashMap<String, Object>();
dataMap.put("fps", fps);
dataMap.put("latency", result.getLatencyMillis());
var targets = result.targets;
var uiTargets = new ArrayList<HashMap<String, Object>>();
for (var t : targets) {
uiTargets.add(t.toHashMap());
}
dataMap.put("targets", uiTargets);
uiMap.put(index, dataMap);
var retMap = new HashMap<String, Object>();
retMap.put("updatePipelineResult", uiMap);
try {
SocketHandler.getInstance().broadcastMessage(retMap, null);
} catch (JsonProcessingException e) {
logger.error(e.getMessage());
logger.error(Arrays.toString(e.getStackTrace()));
}
lastUIResultUpdateTime = now;
}
}

View File

@@ -30,6 +30,7 @@ import java.util.List;
import java.util.function.Supplier;
import org.photonvision.common.dataflow.DataChangeService;
import org.photonvision.common.dataflow.events.OutgoingUIEvent;
import org.photonvision.server.SocketHandler;
import org.photonvision.server.UIUpdateType;
public class Logger {
@@ -122,7 +123,7 @@ public class Logger {
for (var a : currentAppenders) {
var shouldColor = a instanceof ConsoleLogAppender;
var formattedMessage = format(message, level, group, clazz, shouldColor);
a.log(formattedMessage);
a.log(formattedMessage, level);
}
}
@@ -183,23 +184,24 @@ public class Logger {
}
private interface LogAppender {
void log(String message);
void log(String message, LogLevel level);
}
private static class ConsoleLogAppender implements LogAppender {
@Override
public void log(String message) {
public void log(String message, LogLevel level) {
System.out.println(message);
}
}
private static class UILogAppender implements LogAppender {
@Override
public void log(String message) {
var message_ = new HashMap<>();
message_.put("logMessage", message);
public void log(String message, LogLevel level) {
var messageMap = new SocketHandler.UIMap();
messageMap.put("logMessage", message);
messageMap.put("logLevel", level.code);
DataChangeService.getInstance()
.publishEvent(new OutgoingUIEvent<>(UIUpdateType.BROADCAST, "log", message_));
.publishEvent(new OutgoingUIEvent<>(UIUpdateType.BROADCAST, "log", messageMap, null));
}
}
@@ -211,7 +213,7 @@ public class Logger {
}
@Override
public void log(String message) {
public void log(String message, LogLevel level) {
try (AsynchronousFileChannel asyncFile =
AsynchronousFileChannel.open(
filePath, StandardOpenOption.WRITE, StandardOpenOption.CREATE)) {