Skip to content

Commit

Permalink
Update for 2022 (#216)
Browse files Browse the repository at this point in the history
- Use apt package for ML dependency
- Upgrade to 2022.3.1
  • Loading branch information
PeterJohnson authored Feb 13, 2022
1 parent e771374 commit f3ec878
Show file tree
Hide file tree
Showing 29 changed files with 460 additions and 426 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ RUN apt-get -y update && \
quilt coreutils qemu-user-static debootstrap zerofree zip dosfstools \
bsdtar libcap2-bin rsync grep udev xz-utils curl xxd file kmod bc\
binfmt-support ca-certificates \
build-essential cmake python3 python3-distutils ant sudo openjdk-11-jdk \
build-essential cmake python3 python3-distutils python3-jinja2 ant sudo openjdk-11-jdk \
&& rm -rf /var/lib/apt/lists/*

COPY . /pi-gen/
Expand Down
2 changes: 1 addition & 1 deletion azure-docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ RUN apt-get -y update && \
quilt coreutils qemu-user-static debootstrap zerofree zip dosfstools \
bsdtar libcap2-bin rsync grep udev xz-utils curl xxd file kmod bc \
binfmt-support ca-certificates \
build-essential cmake python3 python3-distutils ant sudo openjdk-11-jdk \
build-essential cmake python3 python3-distutils python3-jinja2 ant sudo openjdk-11-jdk \
&& rm -rf /var/lib/apt/lists/*

LABEL "com.azure.dev.pipelines.agent.handler.node.path"="/usr/bin/node"
55 changes: 26 additions & 29 deletions deps/examples/cpp-multiCameraServer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

#include <cstdio>
#include <string>
#include <string_view>
#include <thread>
#include <vector>

#include <fmt/format.h>
#include <networktables/NetworkTableInstance.h>
#include <vision/VisionPipeline.h>
#include <vision/VisionRunner.h>
#include <wpi/StringRef.h>
#include <wpi/StringExtras.h>
#include <wpi/json.h>
#include <wpi/raw_istream.h>
#include <wpi/raw_ostream.h>
Expand Down Expand Up @@ -83,8 +85,8 @@ std::vector<CameraConfig> cameraConfigs;
std::vector<SwitchedCameraConfig> switchedCameraConfigs;
std::vector<cs::VideoSource> cameras;

wpi::raw_ostream& ParseError() {
return wpi::errs() << "config error in '" << configFile << "': ";
void ParseError(std::string_view msg) {
fmt::print(stderr, "config error in '{}': {}\n", configFile, msg);
}

bool ReadCameraConfig(const wpi::json& config) {
Expand All @@ -94,16 +96,15 @@ bool ReadCameraConfig(const wpi::json& config) {
try {
c.name = config.at("name").get<std::string>();
} catch (const wpi::json::exception& e) {
ParseError() << "could not read camera name: " << e.what() << '\n';
ParseError(fmt::format("could not read camera name: {}", e.what()));
return false;
}

// path
try {
c.path = config.at("path").get<std::string>();
} catch (const wpi::json::exception& e) {
ParseError() << "camera '" << c.name
<< "': could not read path: " << e.what() << '\n';
ParseError(fmt::format("camera '{}': could not read path: {}", c.name, e.what()));
return false;
}

Expand All @@ -123,16 +124,17 @@ bool ReadSwitchedCameraConfig(const wpi::json& config) {
try {
c.name = config.at("name").get<std::string>();
} catch (const wpi::json::exception& e) {
ParseError() << "could not read switched camera name: " << e.what() << '\n';
ParseError(fmt::format("could not read switched camera name: {}",
e.what()));
return false;
}

// key
try {
c.key = config.at("key").get<std::string>();
} catch (const wpi::json::exception& e) {
ParseError() << "switched camera '" << c.name
<< "': could not read key: " << e.what() << '\n';
ParseError(fmt::format("switched camera '{}': could not read key: {}",
c.name, e.what()));
return false;
}

Expand All @@ -155,38 +157,37 @@ bool ReadConfig() {
try {
j = wpi::json::parse(is);
} catch (const wpi::json::parse_error& e) {
ParseError() << "byte " << e.byte << ": " << e.what() << '\n';
ParseError(fmt::format("byte {}: {}", e.byte, e.what()));
return false;
}

// top level must be an object
if (!j.is_object()) {
ParseError() << "must be JSON object\n";
ParseError("must be JSON object");
return false;
}

// team number
try {
team = j.at("team").get<unsigned int>();
} catch (const wpi::json::exception& e) {
ParseError() << "could not read team number: " << e.what() << '\n';
ParseError(fmt::format("could not read team number: {}", e.what()));
return false;
}

// ntmode (optional)
if (j.count("ntmode") != 0) {
try {
auto str = j.at("ntmode").get<std::string>();
wpi::StringRef s(str);
if (s.equals_lower("client")) {
if (wpi::equals_lower(str, "client")) {
server = false;
} else if (s.equals_lower("server")) {
} else if (wpi::equals_lower(str, "server")) {
server = true;
} else {
ParseError() << "could not understand ntmode value '" << str << "'\n";
ParseError(fmt::format("could not understand ntmode value '{}'", str));
}
} catch (const wpi::json::exception& e) {
ParseError() << "could not read ntmode: " << e.what() << '\n';
ParseError(fmt::format("could not read ntmode: {}", e.what()));
}
}

Expand All @@ -196,7 +197,7 @@ bool ReadConfig() {
if (!ReadCameraConfig(camera)) return false;
}
} catch (const wpi::json::exception& e) {
ParseError() << "could not read cameras: " << e.what() << '\n';
ParseError(fmt::format("could not read cameras: {}", e.what()));
return false;
}

Expand All @@ -207,7 +208,7 @@ bool ReadConfig() {
if (!ReadSwitchedCameraConfig(camera)) return false;
}
} catch (const wpi::json::exception& e) {
ParseError() << "could not read switched cameras: " << e.what() << '\n';
ParseError(fmt::format("could not read switched cameras: {}", e.what()));
return false;
}
}
Expand All @@ -216,11 +217,9 @@ bool ReadConfig() {
}

cs::UsbCamera StartCamera(const CameraConfig& config) {
wpi::outs() << "Starting camera '" << config.name << "' on " << config.path
<< '\n';
auto inst = frc::CameraServer::GetInstance();
fmt::print("Starting camera '{}' on {}\n", config.name, config.path);
cs::UsbCamera camera{config.name, config.path};
auto server = inst->StartAutomaticCapture(camera);
auto server = frc::CameraServer::StartAutomaticCapture(camera);

camera.SetConfigJson(config.config);
camera.SetConnectionStrategy(cs::VideoSource::kConnectionKeepOpen);
Expand All @@ -232,10 +231,8 @@ cs::UsbCamera StartCamera(const CameraConfig& config) {
}

cs::MjpegServer StartSwitchedCamera(const SwitchedCameraConfig& config) {
wpi::outs() << "Starting switched camera '" << config.name << "' on "
<< config.key << '\n';
auto server =
frc::CameraServer::GetInstance()->AddSwitchedCamera(config.name);
fmt::print("Starting switched camera '{}' on {}\n", config.name, config.key);
auto server = frc::CameraServer::AddSwitchedCamera(config.name);

nt::NetworkTableInstance::GetDefault()
.GetEntry(config.key)
Expand Down Expand Up @@ -279,10 +276,10 @@ int main(int argc, char* argv[]) {
// start NetworkTables
auto ntinst = nt::NetworkTableInstance::GetDefault();
if (server) {
wpi::outs() << "Setting up NetworkTables server\n";
fmt::print("Setting up NetworkTables server\n");
ntinst.StartServer();
} else {
wpi::outs() << "Setting up NetworkTables client for team " << team << '\n';
fmt::print("Setting up NetworkTables client for team {}\n", team);
ntinst.StartClientTeam(team);
ntinst.StartDSClient();
}
Expand Down
2 changes: 1 addition & 1 deletion deps/examples/java-multiCameraServer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dependencies {
compile name: 'ntcore'
compile name: 'cscore'
compile name: 'cameraserver'
compile name: 'opencv-347'
compile name: 'opencv-452'
compile name: 'wpilibj'
compile name: 'wpiHal'
}
Expand Down
6 changes: 3 additions & 3 deletions deps/examples/java-multiCameraServer/src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import edu.wpi.cscore.MjpegServer;
import edu.wpi.cscore.UsbCamera;
import edu.wpi.cscore.VideoSource;
import edu.wpi.first.cameraserver.CameraServer;
import edu.wpi.first.cscore.MjpegServer;
import edu.wpi.first.cscore.UsbCamera;
import edu.wpi.first.cscore.VideoSource;
import edu.wpi.first.networktables.EntryListenerFlags;
import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.vision.VisionPipeline;
Expand Down
4 changes: 2 additions & 2 deletions deps/tools/configServer/gen_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
funcName = "GetResource_" + re.sub(r"[^a-zA-Z0-9]", "_", inputBase)

with open(args.outputFile, "wt") as f:
print("#include <stddef.h>\n#include <wpi/StringRef.h>\nextern \"C\" {\nstatic const unsigned char contents[] = { ", file=f, end='')
print("#include <stddef.h>\n#include <string_view>\nextern \"C\" {\nstatic const unsigned char contents[] = { ", file=f, end='')
print(", ".join("0x%02x" % x for x in data), file=f, end='')
print(" };", file=f)
print("const unsigned char* {}{}(size_t* len) {{\n *len = {};\n return contents;\n}}\n}}".format(args.prefix, funcName, fileSize), file=f)

if args.namespace:
print("namespace {} {{".format(namespace), file=f)
print("wpi::StringRef {}() {{\n return wpi::StringRef(reinterpret_cast<const char*>(contents), {});\n}}".format(funcName, fileSize), file=f)
print("std::string_view {}() {{\n return std::string_view(reinterpret_cast<const char*>(contents), {});\n}}".format(funcName, fileSize), file=f)
if args.namespace:
print("}", file=f)
46 changes: 24 additions & 22 deletions deps/tools/configServer/src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
#include <sys/stat.h>
#include <unistd.h>

#include <wpi/FileSystem.h>
#include <fmt/format.h>
#include <wpi/StringExtras.h>
#include <wpi/fs.h>
#include <wpi/json.h>
#include <wpi/raw_istream.h>
#include <wpi/raw_ostream.h>
Expand All @@ -23,11 +25,11 @@ std::shared_ptr<Application> Application::GetInstance() {
return inst;
}

void Application::Set(wpi::StringRef appType,
std::function<void(wpi::StringRef)> onFail) {
wpi::StringRef appDir;
wpi::StringRef appEnv;
wpi::StringRef appCommand;
void Application::Set(std::string_view appType,
std::function<void(std::string_view)> onFail) {
std::string_view appDir;
std::string_view appEnv;
std::string_view appCommand;

if (appType == "builtin") {
appCommand = "/usr/local/frc/bin/multiCameraServer";
Expand Down Expand Up @@ -65,7 +67,7 @@ void Application::Set(wpi::StringRef appType,
{
// write file
std::error_code ec;
wpi::raw_fd_ostream os(EXEC_HOME "/runCamera", ec, wpi::sys::fs::F_Text);
wpi::raw_fd_ostream os(EXEC_HOME "/runCamera", ec, fs::F_Text);
if (ec) {
onFail("could not write " EXEC_HOME "/runCamera");
return;
Expand All @@ -85,9 +87,9 @@ void Application::Set(wpi::StringRef appType,
UpdateStatus();
}

void Application::FinishUpload(wpi::StringRef appType, UploadHelper& helper,
std::function<void(wpi::StringRef)> onFail) {
wpi::StringRef filename;
void Application::FinishUpload(std::string_view appType, UploadHelper& helper,
std::function<void(std::string_view)> onFail) {
std::string_view filename;
if (appType == "upload-java") {
filename = "/uploaded.jar";
} else if (appType == "upload-cpp") {
Expand All @@ -111,29 +113,29 @@ void Application::FinishUpload(wpi::StringRef appType, UploadHelper& helper,

// change ownership
if (fchown(fd, APP_UID, APP_GID) == -1) {
wpi::errs() << "could not change app ownership: " << std::strerror(errno)
<< '\n';
fmt::print(stderr, "could not change app ownership: {}\n",
std::strerror(errno));
}

// set file to be executable
if (fchmod(fd, 0775) == -1) {
wpi::errs() << "could not change app permissions: " << std::strerror(errno)
<< '\n';
fmt::print(stderr, "could not change app permissions: {}\n",
std::strerror(errno));
}

// close temporary file
helper.Close();

// remove old file (need to do this as we can't overwrite a running exe)
if (unlink(pathname.c_str()) == -1) {
wpi::errs() << "could not remove app executable: " << std::strerror(errno)
<< '\n';
fmt::print(stderr, "could not remove app executable: {}\n",
std::strerror(errno));
}

// rename temporary file to new file
if (rename(helper.GetFilename(), pathname.c_str()) == -1) {
wpi::errs() << "could not rename to app executable: "
<< std::strerror(errno) << '\n';
fmt::print(stderr, "could not rename to app executable: {}\n",
std::strerror(errno));
}

// terminate vision process so it reloads
Expand All @@ -149,16 +151,16 @@ wpi::json Application::GetStatusJson() {
std::error_code ec;
wpi::raw_fd_istream is(EXEC_HOME "/runCamera", ec);
if (ec) {
wpi::errs() << "could not read " EXEC_HOME "/runCamera\n";
fmt::print(stderr, "{}", "could not read " EXEC_HOME "/runCamera\n");
return j;
}

// scan file
wpi::SmallString<256> lineBuf;
while (!is.has_error()) {
wpi::StringRef line = is.getline(lineBuf, 256).trim();
if (line.startswith(TYPE_TAG)) {
j["applicationType"] = line.substr(strlen(TYPE_TAG)).trim();
std::string_view line = wpi::trim(is.getline(lineBuf, 256));
if (wpi::starts_with(line, TYPE_TAG)) {
j["applicationType"] = wpi::trim(wpi::substr(line, strlen(TYPE_TAG)));
break;
}
}
Expand Down
10 changes: 5 additions & 5 deletions deps/tools/configServer/src/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
#include <functional>
#include <memory>
#include <string>
#include <string_view>

#include <wpi/ArrayRef.h>
#include <wpi/Signal.h>
#include <wpi/StringRef.h>

namespace wpi {
class json;
Expand All @@ -27,10 +26,11 @@ class Application {
Application(const Application&) = delete;
Application& operator=(const Application&) = delete;

void Set(wpi::StringRef appType, std::function<void(wpi::StringRef)> onFail);
void Set(std::string_view appType,
std::function<void(std::string_view)> onFail);

void FinishUpload(wpi::StringRef appType, UploadHelper& helper,
std::function<void(wpi::StringRef)> onFail);
void FinishUpload(std::string_view appType, UploadHelper& helper,
std::function<void(std::string_view)> onFail);

void UpdateStatus();

Expand Down
Loading

0 comments on commit f3ec878

Please sign in to comment.