Skip to content

Commit

Permalink
Modified DeviceBootloader::Config to retain previous values
Browse files Browse the repository at this point in the history
  • Loading branch information
themarpe committed Sep 16, 2022
1 parent a130826 commit e96b549
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 44 deletions.
9 changes: 9 additions & 0 deletions include/depthai/device/DeviceBootloader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ class DeviceBootloader {
void setUsbMaxSpeed(UsbSpeed speed);
/// Get maxUsbSpeed
UsbSpeed getUsbMaxSpeed();

/// To JSON
nlohmann::json toJson() const;

/// from JSON
static Config fromJson(nlohmann::json);

private:
nlohmann::json data;
};

/// Bootloader version structure
Expand Down
34 changes: 25 additions & 9 deletions src/device/DeviceBootloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1134,13 +1134,11 @@ std::tuple<bool, std::string> DeviceBootloader::flashConfigFile(const dai::Path&

DeviceBootloader::Config DeviceBootloader::readConfig(Memory memory, Type type) {
auto json = readConfigData(memory, type);
// Implicit parse from json to Config
return json;
return Config::fromJson(json);
}

std::tuple<bool, std::string> DeviceBootloader::flashConfig(const Config& config, Memory memory, Type type) {
// Implicit parse from Config to json
return flashConfigData(config, memory, type);
return flashConfigData(config.toJson(), memory, type);
}

// Boot memory
Expand Down Expand Up @@ -1403,11 +1401,13 @@ UsbSpeed DeviceBootloader::Config::getUsbMaxSpeed() {
}

void DeviceBootloader::Config::setMacAddress(std::string mac) {
std::array<uint8_t, 6> a;
int last = -1;
int rc = std::sscanf(mac.c_str(), "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx%n", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &last);
if(rc != 6 || static_cast<long>(mac.size()) != last) {
throw std::invalid_argument("Invalid MAC address format " + mac);
std::array<uint8_t, 6> a = {0, 0, 0, 0, 0, 0};
if(mac != "") {
int last = -1;
int rc = std::sscanf(mac.c_str(), "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx%n", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &last);
if(rc != 6 || static_cast<long>(mac.size()) != last) {
throw std::invalid_argument("Invalid MAC address format " + mac);
}
}

// Set the parsed mac address
Expand All @@ -1429,4 +1429,20 @@ std::string DeviceBootloader::Config::getMacAddress() {
return std::string(macStr.data());
}

nlohmann::json DeviceBootloader::Config::toJson() const {
// Get current config & add data (but don't override)
nlohmann::json configValues = *this;
auto dataCopy = data;
dataCopy.update(configValues);
return dataCopy;
}

DeviceBootloader::Config DeviceBootloader::Config::fromJson(nlohmann::json json) {
// Parse out json (implicitly) and
Config cfg = json;
// save json data as is (to retain unknown values)
cfg.data = json;
return cfg;
}

} // namespace dai
52 changes: 51 additions & 1 deletion tests/src/bootloader_config_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,54 @@ TEST_CASE("Bootloader Config") {
// std::cout << "Orig mac address: " << mac << " len: " << mac.length() << std::endl;
// std::cout << "Get mac address: " << config.getMacAddress() << " len: " << config.getMacAddress().length() << std::endl;
REQUIRE(config.getMacAddress() == mac);
}
}

TEST_CASE("Bootloader Config retain") {
auto config = dai::DeviceBootloader::Config::fromJson({{"my_custom_value", "hi"}});

// By default IPv4 is 0.0.0.0 (invalid)
REQUIRE("0.0.0.0" == config.getIPv4());

std::string ipv4 = "192.168.1.150";
std::string ipv4Mask = "255.255.255.0";
std::string ipv4Gateway = "192.168.1.1";

config.setStaticIPv4(ipv4, ipv4Mask, ipv4Gateway);

std::array<uint8_t, 4> ipv4InMemory = {192, 168, 1, 150};
for(size_t i = 0; i < ipv4InMemory.size(); i++) {
REQUIRE(ipv4InMemory[i] == reinterpret_cast<uint8_t*>(&config.network.ipv4)[i]);
}

REQUIRE(ipv4 == config.getIPv4());
REQUIRE(ipv4Mask == config.getIPv4Mask());
REQUIRE(ipv4Gateway == config.getIPv4Gateway());
REQUIRE(config.isStaticIPV4() == true);

std::string dns = "1.1.1.1";
std::string dnsAlt = "8.8.8.8";

config.setDnsIPv4(dns);

REQUIRE(config.getDnsIPv4() == dns);
REQUIRE(config.network.ipv4DnsAlt == 0);

config.setDnsIPv4(dns, dnsAlt);

REQUIRE(config.getDnsIPv4() == dns);
REQUIRE(config.getDnsAltIPv4() == dnsAlt);

// MAC address
std::string mac = "FF:AA:BB:CC:00:11";
config.setMacAddress(mac);
// std::cout << "Orig mac address: " << mac << " len: " << mac.length() << std::endl;
// std::cout << "Get mac address: " << config.getMacAddress() << " len: " << config.getMacAddress().length() << std::endl;
REQUIRE(config.getMacAddress() == mac);

// At the end check that value is retained
REQUIRE(config.toJson()["my_custom_value"].get<std::string>() == "hi");

// Check roundtrip
auto j1 = config.toJson();
REQUIRE(config.fromJson(j1).toJson() == j1);
}
65 changes: 31 additions & 34 deletions tests/src/stability_stress_test.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// std
#include <chrono>
#include <csignal>
#include <iostream>
#include <unordered_map>
#include <chrono>

// Includes common necessary includes for development using depthai library
#include "depthai/depthai.hpp"

// #define DEPTHAI_STABILITY_TEST_SCRIPT

#ifdef DEPTHAI_STABILITY_TEST_DEBUG
#include <opencv2/opencv.hpp>
#include <opencv2/opencv.hpp>
#endif

static const std::vector<std::string> labelMap = {
Expand Down Expand Up @@ -42,7 +42,7 @@ int main(int argc, char** argv) {
// nnPath = std::string(argv[1]);
// }

seconds TEST_TIMEOUT{24*60*60};
seconds TEST_TIMEOUT{24 * 60 * 60};
if(argc > 1) {
TEST_TIMEOUT = seconds{stoi(argv[1])};
}
Expand All @@ -62,13 +62,13 @@ int main(int argc, char** argv) {
auto edgeDetectorLeft = pipeline.create<dai::node::EdgeDetector>();
auto edgeDetectorRight = pipeline.create<dai::node::EdgeDetector>();
auto edgeDetectorRgb = pipeline.create<dai::node::EdgeDetector>();
#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
auto script1 = pipeline.create<dai::node::Script>();
auto script2 = pipeline.create<dai::node::Script>();
auto script3 = pipeline.create<dai::node::Script>();
auto script4 = pipeline.create<dai::node::Script>();
auto scriptBurn = pipeline.create<dai::node::Script>();
#endif
#endif

// TODO(themarpe) - enable specific parts separatelly, to control load
// auto featureTrackerLeft = pipeline.create<dai::node::FeatureTracker>();
Expand All @@ -84,10 +84,10 @@ int main(int argc, char** argv) {
auto xoutEdgeLeft = pipeline.create<dai::node::XLinkOut>();
auto xoutEdgeRight = pipeline.create<dai::node::XLinkOut>();
auto xoutEdgeRgb = pipeline.create<dai::node::XLinkOut>();
#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
auto scriptOut = pipeline.create<dai::node::XLinkOut>();
auto scriptOut2 = pipeline.create<dai::node::XLinkOut>();
#endif
#endif
// auto xoutTrackedFeaturesLeft = pipeline.create<dai::node::XLinkOut>();
// auto xoutTrackedFeaturesRight = pipeline.create<dai::node::XLinkOut>();

Expand All @@ -105,14 +105,13 @@ int main(int argc, char** argv) {
xoutEdgeLeft->setStreamName(edgeLeftStr);
xoutEdgeRight->setStreamName(edgeRightStr);
xoutEdgeRgb->setStreamName(edgeRgbStr);
#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
scriptOut->setStreamName("script");
scriptOut2->setStreamName("script2");
#endif
#endif
// xoutTrackedFeaturesLeft->setStreamName("trackedFeaturesLeft");
// xoutTrackedFeaturesRight->setStreamName("trackedFeaturesRight");


// Properties
camRgb->setBoardSocket(dai::CameraBoardSocket::RGB);
camRgb->setResolution(dai::ColorCameraProperties::SensorResolution::THE_4_K);
Expand Down Expand Up @@ -152,7 +151,7 @@ int main(int argc, char** argv) {

edgeDetectorRgb->setMaxOutputFrameSize(8294400);

#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
std::string source1 = R"(
import time
import json
Expand Down Expand Up @@ -219,7 +218,7 @@ int main(int argc, char** argv) {
time.sleep(0.001)
)");
scriptBurn->setProcessor(dai::ProcessorType::LEON_MSS);
#endif
#endif

// // By default the least mount of resources are allocated
// // increasing it improves performance when optical flow is enabled
Expand Down Expand Up @@ -254,12 +253,12 @@ int main(int argc, char** argv) {
edgeDetectorLeft->outputImage.link(xoutEdgeLeft->input);
edgeDetectorRight->outputImage.link(xoutEdgeRight->input);

#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
script1->outputs["out"].link(script2->inputs["in"]);
script2->outputs["out"].link(scriptOut->input);
script3->outputs["out"].link(script4->inputs["in"]);
script4->outputs["out"].link(scriptOut2->input);
#endif
#endif

// monoLeft->out.link(featureTrackerLeft->inputImage);
// featureTrackerLeft->outputFeatures.link(xoutTrackedFeaturesLeft->input);
Expand All @@ -284,10 +283,10 @@ int main(int argc, char** argv) {
auto edgeRightQueue = device.getOutputQueue(edgeRightStr, 8, false);
auto edgeRgbQueue = device.getOutputQueue(edgeRgbStr, 8, false);

#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
auto scriptQueue = device.getOutputQueue("script", 8, false);
auto script2Queue = device.getOutputQueue("script2", 8, false);
#endif
#endif

// auto outputFeaturesLeftQueue = device.getOutputQueue("trackedFeaturesLeft", 8, false);
// auto outputFeaturesRightQueue = device.getOutputQueue("trackedFeaturesRight", 8, false);
Expand All @@ -299,11 +298,10 @@ int main(int argc, char** argv) {
auto color = cv::Scalar(255, 255, 255);
#endif


mutex countersMtx;
unordered_map<std::string, int> counters;

thread countingThread([&countersMtx, &counters, &device, TEST_TIMEOUT](){
thread countingThread([&countersMtx, &counters, &device, TEST_TIMEOUT]() {
// Initial delay
this_thread::sleep_for(5s);

Expand All @@ -314,9 +312,10 @@ int main(int argc, char** argv) {
unique_lock<mutex> l(countersMtx);

bool failed = counters.size() == 0;
cout << "[" << duration_cast<seconds>(steady_clock::now() - timeoutStopwatch).count() << "s] " << "FPS: ";
for(const auto& kv : counters){
if(kv.second == 0){
cout << "[" << duration_cast<seconds>(steady_clock::now() - timeoutStopwatch).count() << "s] "
<< "FPS: ";
for(const auto& kv : counters) {
if(kv.second == 0) {
failed = true;
}

Expand All @@ -333,7 +332,7 @@ int main(int argc, char** argv) {
fiveFpsCounter = steady_clock::now();
}

if(steady_clock::now() - timeoutStopwatch > TEST_TIMEOUT){
if(steady_clock::now() - timeoutStopwatch > TEST_TIMEOUT) {
alive = false;
break;
}
Expand All @@ -357,10 +356,10 @@ int main(int argc, char** argv) {
auto edgeLefts = edgeLeftQueue->tryGetAll<dai::ImgFrame>();
auto edgeRights = edgeRightQueue->tryGetAll<dai::ImgFrame>();

#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
auto script = scriptQueue->tryGetAll<dai::Buffer>();
auto script2 = script2Queue->tryGetAll<dai::Buffer>();
#endif
#endif

// auto edgeRgbs = edgeRgbQueue->getAll<dai::ImgFrame>();

Expand All @@ -378,15 +377,15 @@ int main(int argc, char** argv) {
counters["edgeLefts"] += edgeLefts.size();
counters["edgeRights"] += edgeRights.size();

#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
counters["script"] += script.size();
counters["script2"] += script2.size();
#endif
#endif
// counters["trackedLefts"] += trackedLefts.size();
// counters["trackedRights"] += trackedRigths.size();
}

#ifdef DEPTHAI_STABILITY_TEST_DEBUG
#ifdef DEPTHAI_STABILITY_TEST_DEBUG

/// DISPLAY & OPENCV Section
for(const auto& edgeLeft : edgeLefts) {
Expand All @@ -402,7 +401,6 @@ int main(int argc, char** argv) {
// cv::imshow(edgeRgbStr, edgeRgbFrame);
// }


cv::Mat frame = imgFrame->getCvFrame();
cv::Mat depthFrame = depth->getFrame(); // depthFrame values are in millimeters

Expand Down Expand Up @@ -475,27 +473,26 @@ int main(int argc, char** argv) {
cv::imshow("depth", depthFrameColor);
cv::imshow("rgb", frame);

#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
for(auto json : script) {
if(json != nullptr) {
std::cout << "Script: " << std::string((const char*) json->getData().data(), json->getData().size()) << std::endl;
std::cout << "Script: " << std::string((const char*)json->getData().data(), json->getData().size()) << std::endl;
}
}
for(auto json : script2) {
if(json != nullptr) {
std::cout << "Script2: " << std::string((const char*) json->getData().data(), json->getData().size()) << std::endl;
std::cout << "Script2: " << std::string((const char*)json->getData().data(), json->getData().size()) << std::endl;
}
}
#endif
#endif

int key = cv::waitKey(1);
if(key == 'q' || key == 'Q') {
alive = false;
break;
}

#endif

#endif
}

// Clean up the counting thread
Expand Down

0 comments on commit e96b549

Please sign in to comment.