Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
11 changes: 6 additions & 5 deletions src/Perspective.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "pattern.h"
#include "proto/vision/ssl_vision_wrapper.pb.h"
#include "log.h"
#include "config.h"

#include <cmath>

Expand All @@ -39,16 +40,16 @@ void Perspective::geometryCheck(const int width, const int height, const double

bool calibFound = false;
for(const SSL_GeometryCameraCalibration& calib : socket->getGeometry().calib()) {
if(calib.camera_id() == camId) {
if(calib.camera_id() == config.camera_id()) {
calibFound = true;
model = CameraModel(calib);
if(!calib.has_derived_camera_world_tx() || !calib.has_derived_camera_world_ty() || !calib.has_derived_camera_world_tz()) {
socket->send(model.getProto(camId));
socket->send(model.getProto(config.camera_id()));
SSL_WrapperPacket wrapper;
wrapper.set_source(SSL_SOURCE_VISION_PROCESSOR);
wrapper.mutable_geometry()->CopyFrom(socket->getGeometry());
wrapper.mutable_geometry()->clear_calib();
wrapper.mutable_geometry()->add_calib()->CopyFrom(model.getProto(camId));
wrapper.mutable_geometry()->add_calib()->CopyFrom(model.getProto(config.camera_id()));
socket->send(wrapper);
}
break;
Expand Down Expand Up @@ -105,8 +106,8 @@ void Perspective::geometryCheck(const int width, const int height, const double
}

// clamp to field boundaries
const float halfLength = (float)field.field_length()/2.0f + goalBoundaryWidth(field) + geometryTolerance;
const float halfWidth = (float)field.field_width()/2.0f + (float)field.boundary_width() + geometryTolerance;
const float halfLength = (float)field.field_length()/2.0f + goalBoundaryWidth(field) + config.thresholds().geometry_tolerance();
const float halfWidth = (float)field.field_width()/2.0f + (float)field.boundary_width() + config.thresholds().geometry_tolerance();
visibleFieldExtent[0] = std::max(visibleFieldExtent[0], -halfLength);
visibleFieldExtent[1] = std::min(visibleFieldExtent[1], halfLength);
visibleFieldExtent[2] = std::max(visibleFieldExtent[2], -halfWidth);
Expand Down
4 changes: 1 addition & 3 deletions src/Perspective.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ typedef struct __attribute__ ((packed)) {

class Perspective {
public:
Perspective(std::shared_ptr<VisionSocket> socket, int camId, float geometryTolerance): socket(std::move(socket)), camId(camId), geometryTolerance(geometryTolerance) {}
Perspective(std::shared_ptr<VisionSocket> socket): socket(std::move(socket)) {}
void geometryCheck(int width, int height, double maxBotHeight, float resamplingFactor);

Eigen::Vector2f flat2field(const Eigen::Vector2f& pos) const;
Expand All @@ -53,6 +53,4 @@ class Perspective {

private:
const std::shared_ptr<VisionSocket> socket;
const unsigned int camId;
const float geometryTolerance;
};
55 changes: 5 additions & 50 deletions src/Resources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,40 +67,16 @@ static YAML::Node getOptional(const YAML::Node& node) {
return node.IsDefined() ? node : YAML::Node();
}

Resources::Resources(const std::string& configPath) : fieldReference(), configPath(configPath) {
Resources::Resources(const std::string& configPath) : configPath(configPath) {
YAML::Node config = YAML::LoadFile(configPath);
struct stat st{};
if(stat(configPath.c_str(), &st) == 0)
configMtime = (int64_t)st.st_mtim.tv_sec * 1000000000 + st.st_mtim.tv_nsec;

openCl = std::make_shared<OpenCL>();

CameraConfig camConfig(getOptional(config["camera"]));
camera = openCamera(camConfig);
camera->setResolution(camConfig.width, camConfig.height);
camera->setExposure(camConfig.exposure);
camera->setGain(camConfig.gain);
camera->setGamma(camConfig.gamma);
camera->setWhiteBalance(camConfig.whiteBalanceType, camConfig.whiteBalanceBlue, camConfig.whiteBalanceRed);

camId = config["cam_id"].as<int>(0);
if (camId < 0 || camId > 7) {
FATAL("Invalid camera ID, must be >= 0 and <= 7: " << camId);
}

maxBlobs = getOptional(config["thresholds"])["blobs"].as<int>(2000);
geometryTolerance = getOptional(config["thresholds"])["geometry_tolerance"].as<float>(10.0f);

applyTunables(config);

orange = orangeReference;
field = fieldReference;
yellow = yellowReference;
blue = blueReference;
green = greenReference;
pink = pinkReference;
fieldLineColor = fieldReference;

YAML::Node geometry = getOptional(config["geometry"]);
cameraAmount = geometry["camera_amount"].as<int>(1);
cameraHeight = geometry["camera_height"].as<double>(0.0);
Expand All @@ -117,12 +93,10 @@ Resources::Resources(const std::string& configPath) : fieldReference(), configPa

YAML::Node network = getOptional(config["network"]);
gcSocket = std::make_shared<GCSocket>(network["gc_ip"].as<std::string>("224.5.23.1"), network["gc_port"].as<int>(10003), YAML::LoadFile(config["bot_heights_file"].as<std::string>("robot-heights.yml")).as<std::map<std::string, double>>());
socket = std::make_shared<VisionSocket>(network["vision_ip"].as<std::string>("224.5.23.2"), network["vision_port"].as<int>(10006), camId, gcSocket->defaultBotHeight);
perspective = std::make_shared<Perspective>(socket, camId, geometryTolerance);
socket = std::make_shared<VisionSocket>(network["vision_ip"].as<std::string>("224.5.23.2"), network["vision_port"].as<int>(10006), gcSocket->defaultBotHeight);
perspective = std::make_shared<Perspective>(socket);

YAML::Node stream = getOptional(config["stream"]);
rtpStreamer = std::make_shared<RTPStreamer>(stream["active"].as<bool>(true), "rtp://" + stream["ip_base_prefix"].as<std::string>("224.5.23.") + std::to_string(stream["ip_base_end"].as<int>(100) + camId) + ":" + std::to_string(stream["port"].as<int>(10100)));
rawFeed = stream["raw_feed"].as<bool>(false);
snapshotWriter = std::make_shared<SnapshotWriter>();

raw2quadKernel = openCl->compile(kernel_raw2quad_cl, camera->format().kernelOptions);
Expand Down Expand Up @@ -173,7 +147,7 @@ void Resources::rgba2blobCenter(const std::shared_ptr<CLImage>* channels, std::s
void Resources::streamQuad(std::shared_ptr<CLImage>* channels) {
std::shared_ptr<RawImage> nv12 = openCl->acquireNV12(channels[0]->width, channels[0]->height);
openCl->await(quad2nv12, cl::EnqueueArgs(cl::NDRange(channels[0]->width, channels[0]->height)), channels[0]->image, channels[1]->image, channels[2]->image, channels[3]->image, nv12->buffer);
rtpStreamer->sendFrame(nv12);
rtpStreamer.sendFrame(nv12);
}

void Resources::streamImage(CLImage &img) {
Expand All @@ -189,31 +163,12 @@ void Resources::streamImage(CLImage &img) {

std::shared_ptr<RawImage> nv12 = openCl->acquireNV12(img.width, img.height);
openCl->await(kernel, cl::EnqueueArgs(cl::NDRange(img.width, img.height)), img.image, nv12->buffer);
rtpStreamer->sendFrame(nv12);
rtpStreamer.sendFrame(nv12);
}

void Resources::applyTunables(const YAML::Node& config) {
YAML::Node thresholds = getOptional(config["thresholds"]);
minCircularity = thresholds["circularity"].as<double>(15.0);
minScore = thresholds["score"].as<double>(5.0);
minConfidence = thresholds["min_confidence"].as<float>(0.2f);
minCamEdgeDistance = thresholds["min_cam_edge_distance"].as<double>(170.0);
resamplingFactor = thresholds["resampling_factor"].as<float>(1.0f);
clippingTolerance = thresholds["clipping_tolerance"].as<float>(10.0f);

YAML::Node tracking = getOptional(config["tracking"]);
minTrackingRadius = tracking["min_tracking_radius"].as<double>(20.0);
maxBotAcceleration = 1000 * tracking["max_bot_acceleration"].as<double>(6.5);

YAML::Node color = getOptional(config["color"]);
referenceForce = color["reference_force"].as<float>(0.1f);
historyForce = color["history_force"].as<float>(0.7f);
orangeReference = color["orange"].as<Eigen::Vector3i>(Eigen::Vector3i{192, 128, 64});
fieldReference = color["field"].as<Eigen::Vector3i>(Eigen::Vector3i{128, 128, 128});
yellowReference = color["yellow"].as<Eigen::Vector3i>(Eigen::Vector3i{255, 128, 0});
blueReference = color["blue"].as<Eigen::Vector3i>(Eigen::Vector3i{0, 128, 255});
greenReference = color["green"].as<Eigen::Vector3i>(Eigen::Vector3i{0, 255, 128});
pinkReference = color["pink"].as<Eigen::Vector3i>(Eigen::Vector3i{255, 0, 128});

YAML::Node debug = getOptional(config["debug"]);
debugImages = debug["debug_images"].as<bool>(false);
Expand Down
33 changes: 0 additions & 33 deletions src/Resources.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <string>
#include <memory>
#include <yaml-cpp/node/node.h>
#include "driver/cameradriver.h"
#include "rtpstreamer.h"
#include "snapshotwriter.h"
#include "udpsocket.h"
Expand All @@ -42,37 +41,7 @@ class Resources {

void reloadConfigIfChanged();

std::unique_ptr<CameraDriver> camera = nullptr;

int camId;

double minTrackingRadius;
double maxBotAcceleration; // mm/s²

double minCircularity;
double minScore;
double minCamEdgeDistance;
int maxBlobs;
float minConfidence;
float resamplingFactor;
float clippingTolerance;
float geometryTolerance;

float referenceForce;
float historyForce;
Eigen::Vector3i orangeReference;
Eigen::Vector3i fieldReference;
Eigen::Vector3i yellowReference;
Eigen::Vector3i blueReference;
Eigen::Vector3i greenReference;
Eigen::Vector3i pinkReference;
Eigen::Vector3i orange;
Eigen::Vector3i field;
Eigen::Vector3i yellow;
Eigen::Vector3i blue;
Eigen::Vector3i green;
Eigen::Vector3i pink;
Eigen::Vector3i fieldLineColor = fieldReference;

int cameraAmount;
double cameraHeight; // Just for calibration, do not use elsewhere (0.0 as special value for automatic calibration)
Expand All @@ -86,13 +55,11 @@ class Resources {
std::string groundTruth;
bool debugImages;
int debugStreamIntervalMs;
bool rawFeed;

std::shared_ptr<GCSocket> gcSocket;
std::shared_ptr<VisionSocket> socket;
std::shared_ptr<Perspective> perspective;
std::shared_ptr<OpenCL> openCl;
std::shared_ptr<RTPStreamer> rtpStreamer;
std::shared_ptr<SnapshotWriter> snapshotWriter;

cl::Kernel raw2quadKernel;
Expand Down
6 changes: 4 additions & 2 deletions src/blob_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <yaml-cpp/node/parse.h>
#include <yaml-cpp/yaml.h>
#include "Resources.h"
#include "driver/cameradriver.h"
#include "config.h"
#include "GroundTruth.h"
#include "pattern.h"
#include "cl_kernels.h"
Expand Down Expand Up @@ -134,7 +136,7 @@ int main(int argc, char* argv[]) {

while(true) {
double startTime = getRealTime();
std::shared_ptr<RawImage> img = r.camera->readImage();
std::shared_ptr<RawImage> img = camera->readImage();
if(img == nullptr)
break;

Expand All @@ -151,7 +153,7 @@ int main(int argc, char* argv[]) {
r.rgba2blobCenter(channels, flat, gradDot, blobCenter);

//std::shared_ptr<CLImage> score = r.openCl->acquire(&PixelFormat::F32, r.perspective->reprojectedFieldSize[0], r.perspective->reprojectedFieldSize[1], img->name);
//OpenCL::await(scoreKernel, cl::EnqueueArgs(cl::NDRange(r.perspective->reprojectedFieldSize[0], r.perspective->reprojectedFieldSize[1])), flat->image, blobCenter->image, score->image, (float)r.minCircularity, (int)floor(r.minBlobRadius/r.perspective->fieldScale));
//OpenCL::await(scoreKernel, cl::EnqueueArgs(cl::NDRange(r.perspective->reprojectedFieldSize[0], r.perspective->reprojectedFieldSize[1])), flat->image, blobCenter->image, score->image, config.thresholds().circularity(), (int)floor(r.minBlobRadius/r.perspective->fieldScale));

processingTime += getRealTime() - startTime;
startTime = getRealTime();
Expand Down
47 changes: 25 additions & 22 deletions src/blobs/colorupdate.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "colorupdate.h"
#include "kmeans.h"
#include "pattern.h"
#include "config.h"
#include "proto/vision/ssl_vp_config.pb.h"
#include <eigen3/Eigen/Core>


static float sqPointLineSegmentDistance(const SSL_FieldLineSegment& line, const Eigen::Vector2f& point) {
Expand All @@ -18,7 +21,7 @@ static bool ballAtLine(const Resources& r, const BallHypothesis& ball) {
const Eigen::Vector2f imgPos = r.perspective->model.field2image({ball.pos.x(), ball.pos.y(), (float)r.gcSocket->maxBotHeight});
const Eigen::Vector2f ballPos = r.perspective->model.image2field(imgPos, r.perspective->field.ball_radius()).head<2>();

const float maxLineDistance = (float)r.perspective->field.line_thickness()/2 + r.geometryTolerance;
const float maxLineDistance = (float)r.perspective->field.line_thickness()/2 + config.thresholds().geometry_tolerance();
const float sqMaxLineDistance = maxLineDistance*maxLineDistance;

for(const auto& line : r.perspective->field.field_lines()) {
Expand Down Expand Up @@ -52,22 +55,19 @@ static void determineFieldLineBlobColor(Resources& r, const std::list<std::uniqu

//TODO median instead?
if(amount > 2)
r.fieldLineColor = colorSum /= amount;
liveFieldLineColor = colorSum /= amount;
}

static inline void updateColor(const Resources& r, const Eigen::Vector3i& reference, const Eigen::Vector3i& oldColor, Eigen::Vector3i& color) {
const float updateForce = 1.0f - r.referenceForce - r.historyForce;
color = (r.referenceForce*reference.cast<float>() + r.historyForce*oldColor.cast<float>() + updateForce*color.cast<float>()).cast<int>();
static inline void updateColor(VPColor* field, const Eigen::Vector3i& color) {
const float updateForce = 1.0f - config.color().reference_force() - config.color().history_force();
Eigen::Vector3f newColor = config.color().reference_force()*eigen_from_color(field->reference()).cast<float>() +
config.color().history_force()*eigen_from_color(field->live()).cast<float>() +
updateForce*color.cast<float>();

field->mutable_live()->CopyFrom(color_from_eigen(newColor.cast<int>()));
}

void updateColors(Resources& r, const std::list<std::unique_ptr<BotHypothesis>>& bestBotModels, const std::list<std::unique_ptr<BallHypothesis>>& ballCandidates) {
Eigen::Vector3i oldField = r.field;
Eigen::Vector3i oldOrange = r.orange;
Eigen::Vector3i oldYellow = r.yellow;
Eigen::Vector3i oldBlue = r.blue;
Eigen::Vector3i oldGreen = r.green;
Eigen::Vector3i oldPink = r.pink;

std::vector<Eigen::Vector3i> centerBlobs;
Eigen::Vector3i pink(0, 0, 0);
int pinkN = 0;
Expand All @@ -94,27 +94,30 @@ void updateColors(Resources& r, const std::list<std::unique_ptr<BotHypothesis>>&
}

if(pinkN > 0) {
r.pink = pink / pinkN;
updateColor(r, r.pinkReference, oldPink, r.pink);
updateColor(config.mutable_color()->mutable_pink(), pink / pinkN);
}
if(greenN > 0) {
r.green = green / greenN;
updateColor(r, r.greenReference, oldGreen, r.green);
updateColor(config.mutable_color()->mutable_green(), green / greenN);
}

if(kMeans(r.pink, centerBlobs, r.yellow, r.blue)) {
updateColor(r, r.yellowReference, oldYellow, r.yellow);
updateColor(r, r.blueReference, oldBlue, r.blue);
Eigen::Vector3i yellow = liveYellow;
Eigen::Vector3i blue = liveBlue;
if(kMeans(livePink, centerBlobs, yellow, blue)) {
updateColor(config.mutable_color()->mutable_yellow(), yellow);
updateColor(config.mutable_color()->mutable_blue(), blue);
}

std::vector<Eigen::Vector3i> ballBlobs;
for (const auto& ball : ballCandidates)
ballBlobs.push_back(ball->blob->center);

if(kMeans(r.blue, ballBlobs, r.orange, r.field)) {
updateColor(r, r.orangeReference, oldOrange, r.orange);
updateColor(r, r.fieldReference, oldField, r.field);
Eigen::Vector3i orange = liveOrange;
Eigen::Vector3i field = liveField;
if(kMeans(liveBlue, ballBlobs, orange, field)) {
updateColor(config.mutable_color()->mutable_orange(), orange);
updateColor(config.mutable_color()->mutable_field(), field);
}

determineFieldLineBlobColor(r, ballCandidates);
apply_color_update();
}
Loading
Loading