diff options
| author | Nikita Kostovsky <luntik2012@gmail.com> | 2025-01-26 11:55:53 +0100 |
|---|---|---|
| committer | Nikita Kostovsky <luntik2012@gmail.com> | 2025-01-26 11:55:53 +0100 |
| commit | 90503471c49ef6ab795a61a2dc7df1e35761fa7e (patch) | |
| tree | c919bc493838185eae947a80e16b6f593d825fbc /main.cpp | |
| parent | 626f08162b07a49e8683bfb04a71ac02faa9b12d (diff) | |
use callbacks for pixels/image, update http resps, load calibration tables in separate threads
Diffstat (limited to 'main.cpp')
| -rw-r--r-- | main.cpp | 180 |
1 files changed, 113 insertions, 67 deletions
@@ -38,7 +38,8 @@ #include <QtConcurrent/QtConcurrent> #define try_apply_config() \ - if (!applyConfig(config)) { \ + if (!applyConfig(config)) \ + { \ camera->release(); \ cm->stop(); \ \ @@ -83,17 +84,19 @@ CalibrationTablePtr calibrationTableX; auto printPixels = [](const auto& pixels) { for (size_t i = (img_width - 10) / 2; i < img_width - ((img_width - 10) / 2); - ++i) { + ++i) + { std::cout << pixels[i] << " "; } std::cout << std::endl; }; -void onNewImage( - std::shared_ptr<Image> image) +void onNewImage(std::shared_ptr<Image> image) { - std::cout << __func__ << std::endl << std::flush; - if (!image) { + // std::cout << __func__ << std::endl << std::flush; + + if (!image) + { qDebug() << __func__ << "no image"; return; } @@ -101,18 +104,22 @@ void onNewImage( ::img = image; } -void onNewPixels( - std::shared_ptr<Pixels> pixels) +void onNewPixels(std::shared_ptr<Pixels> pixels) { - if (!pixels) { + // std::cout << __func__ << std::endl << std::flush; + + if (!pixels) + { qDebug() << __func__ << "got null pixels"; } - if (!*pixels) { + if (!*pixels) + { // qDebug() << __func__ << "got empty pixels"; } - for (size_t i = 640 - 5; i < 640 + 5; i++) { + for (size_t i = 640 - 5; i < 640 + 5; i++) + { // std::cout << pixels->pixels[i] << " "; } @@ -127,12 +134,15 @@ int main(int argc, char* argv[]) { QCoreApplication app(argc, argv); + QList<QFuture<void>> initializers; + #ifdef INNO_MAKER - if (false) { + if (false) + { std::cout << std::boolalpha; InnoMakerOV9281 innoMakerCam; qDebug() << "init:" << innoMakerCam.init(); - qDebug() << "set exposure:" << innoMakerCam.setExposureTimeMs(3000); + qDebug() << "set exposure:" << innoMakerCam.setExposureTimeUs(3000); qDebug() << "set gain:" << innoMakerCam.setGain(3000); innoMakerCam.startStream(); @@ -155,22 +165,32 @@ int main(int argc, char* argv[]) // if (false) qDebug() << "size of raw profile" << sizeof(Pixels); - if (true) { + if (true) + { // open binary calibration table - if (true) { - if (!openCalibrationTable("/home/user/dumps/binz.calibration_table", - ::calibrationTableZ)) { - exit(EXIT_FAILURE); - } - // interpolate(::calibrationTableZ); - if (!openCalibrationTable("/home/user/dumps/binx.calibration_table", - ::calibrationTableX)) { - exit(EXIT_FAILURE); - } - // interpolate(::calibrationTableX); + if (true) + { + initializers << QtConcurrent::run([&]() { + if (!openCalibrationTable( + "/home/user/dumps/binz.calibration_table", + ::calibrationTableZ)) + { + exit(EXIT_FAILURE); + } + }); + + initializers << QtConcurrent::run([&]() { + if (!openCalibrationTable( + "/home/user/dumps/binx.calibration_table", + ::calibrationTableX)) + { + exit(EXIT_FAILURE); + } + }); } - if (false) { + if (false) + { // z // if (!openCalibrationTable( // "/home/user/dumps/binz.calibration_table", @@ -246,8 +266,10 @@ int main(int argc, char* argv[]) } // load binary calibration dumps and calibrate - if (false) { - if (true) { + if (false) + { + if (true) + { auto rawProfiles = openDump("/home/user/dumps/binz"); // auto rawProfiles = openDump("/home/user/dumps/z"); qDebug() << "raw z-profiles count is" << rawProfiles.size(); @@ -272,7 +294,8 @@ int main(int argc, char* argv[]) interpolate(::calibrationTableZ); if (!dump(::calibrationTableZ, - "/home/user/dumps/binz.calibration_table")) { + "/home/user/dumps/binz.calibration_table")) + { qApp->exit(EXIT_FAILURE); } // calibrationTableToImage(::calibrationTableZ) @@ -283,7 +306,8 @@ int main(int argc, char* argv[]) qDebug() << "--------------------------------------------------------"; - if (true) { + if (true) + { auto rawProfiles = openDump("/home/user/dumps/binx"); qDebug() << "raw x-profiles count is" << rawProfiles.size(); // qDebug() << "height" << calibrationColumnHeight; @@ -305,7 +329,9 @@ int main(int argc, char* argv[]) interpolate(::calibrationTableX); - if (!dump(::calibrationTableX, "/home/user/dumps/binx.calibration_table")) { + if (!dump(::calibrationTableX, + "/home/user/dumps/binx.calibration_table")) + { qApp->exit(EXIT_FAILURE); } @@ -315,14 +341,6 @@ int main(int argc, char* argv[]) } } - QCryptographicHash hashZ(QCryptographicHash::Sha256); - hashZ.addData(QByteArrayView((char *) ::calibrationTableZ.data(), sizeof(CalibrationTable))); - qDebug() << "hash z" << sizeof(::calibrationTableZ) << hashZ.result().toHex(); - - QCryptographicHash hashX(QCryptographicHash::Sha256); - hashX.addData(QByteArrayView((char *) ::calibrationTableX.data(), sizeof(CalibrationTable))); - qDebug() << "hash x" << sizeof(::calibrationTableX) << hashX.result().toHex(); - // exit(EXIT_SUCCESS); // if (!initLaser()) { @@ -351,7 +369,8 @@ int main(int argc, char* argv[]) const auto cameras = InnoMakerOV9281::search(); // const auto cameras = - if (cameras.empty()) { + if (cameras.empty()) + { std::cerr << "No cameras were identified on the system." << std::endl; #ifndef INNO_MAKER cm->stop(); @@ -369,8 +388,14 @@ int main(int argc, char* argv[]) std::cout << "connect everything" << std::endl; camera->newPixels.connect(&onNewPixels); camera->newImage.connect(&onNewImage); + camera->newImageCallback = &onNewImage; + camera->newPixelsCallback = &onNewPixels; + + for (auto& i : initializers) + i.waitForFinished(); - if (!camera->startStream()) { + if (!camera->startStream()) + { #ifndef INNO_MAKER cm->stop(); #endif @@ -380,22 +405,22 @@ int main(int argc, char* argv[]) QHttpServer qHttpServer; qHttpServer.route("/v1/sensor/image", [&]() { - std::cout << "http: image" << std::endl << std::flush; + // std::cout << "http: image" << std::endl << std::flush; // FILE *f = fopen("/tmp/img.pgm", "w"); // static bool save = false; pgm_save(::img); // save = false; std::lock_guard<std::mutex> lg(pgm_image_mtx); - qDebug() << "mutex locked"; - qDebug() << "image saved to array"; + // qDebug() << "mutex locked"; + // qDebug() << "image saved to array"; return QByteArray((const char*) pgm_image, pgm_image_size); }); qHttpServer.route("/v1/sensor/image2", [&]() { - std::cout << "http: image2" << std::endl; + // std::cout << "http: image2" << std::endl; pgm_save(::img); std::lock_guard<std::mutex> lg(pgm_image_mtx); - qDebug() << "image2"; + // qDebug() << "image2"; return QByteArray((const char*) pgm_image, pgm_image_size); }); // qHttpServer.route("/v1/sensor/exposureTimeUs", [&]() { @@ -403,12 +428,13 @@ int main(int argc, char* argv[]) // return "123"; // }); qHttpServer.route("/v1/pixels", [&]() { - std::cout << "http: pixels" << std::endl; + // std::cout << "http: pixels" << std::endl; std::lock_guard<std::mutex> lg(pgm_image_mtx); QJsonArray pixels; - for (size_t i = 0; i < img_width; ++i) { + for (size_t i = 0; i < img_width; ++i) + { // pixels << img_height - img.pixels[i]; pixels << ::pixels.pixels[i]; } @@ -426,7 +452,8 @@ int main(int argc, char* argv[]) QJsonArray jsonLines; - for (const auto& l : lines) { + for (const auto& l : lines) + { jsonLines << QJsonArray{QJsonArray{l.p1().x(), l.p1().y()}, QJsonArray{l.p2().x(), l.p2().y()}}; } @@ -436,10 +463,13 @@ int main(int argc, char* argv[]) return QHttpServerResponse(QJsonDocument(json).toJson()); }); - qHttpServer.route("/v1/profile", [&]() { - std::cout << "http: profile" << std::endl; + qHttpServer.route("/v1/profile", [&]() -> QHttpServerResponse { + // std::cout << "http: profile" << std::endl; std::lock_guard<std::mutex> lg(pgm_image_mtx); + if (!::calibrationTableZ || !::calibrationTableX) + return QHttpServerResponse::StatusCode::ServiceUnavailable; + const Profile profile(::pixels, ::calibrationTableZ, ::calibrationTableX); @@ -453,7 +483,8 @@ int main(int argc, char* argv[]) .route("/v1/commands/resetEncoder", [&](const QHttpServerRequest& request) -> QHttpServerResponse { std::cout << "http: resetEncoder" << std::endl; - if (request.method() != QHttpServerRequest::Method::Post) { + if (request.method() != QHttpServerRequest::Method::Post) + { return QHttpServerResponse::StatusCode::NotFound; } @@ -468,7 +499,8 @@ int main(int argc, char* argv[]) .route("/v1/commands/startCalibration", [&](const QHttpServerRequest& request) -> QHttpServerResponse { std::cout << "http: startCalibration" << std::endl; - if (request.method() != QHttpServerRequest::Method::Post) { + if (request.method() != QHttpServerRequest::Method::Post) + { return QHttpServerResponse::StatusCode::NotFound; } @@ -485,7 +517,8 @@ int main(int argc, char* argv[]) .route("/v1/commands/gCode", [&](const QHttpServerRequest& request) -> QHttpServerResponse { std::cout << "http: gCode" << std::endl; - if (request.method() != QHttpServerRequest::Method::Post) { + if (request.method() != QHttpServerRequest::Method::Post) + { return QHttpServerResponse::StatusCode::NotFound; } @@ -520,7 +553,8 @@ int main(int argc, char* argv[]) "/v1/sensor/params", [&](const QHttpServerRequest& request) -> QHttpServerResponse { std::cout << "http: params" << std::endl; - switch (request.method()) { + switch (request.method()) + { case QHttpServerRequest::Method::Get: { std::lock_guard<std::mutex> lg(pgm_image_mtx); QJsonObject json; @@ -557,21 +591,28 @@ int main(int argc, char* argv[]) auto json = QJsonDocument::fromJson(request.body()).object(); - if (json.contains(exposureTimeKey)) { + if (json.contains(exposureTimeKey)) + { const int32_t value{json[exposureTimeKey].toInt()}; - if (value == 0) { - return QHttpServerResponse::StatusCode::NotFound; - } + if (value == 0) + return QHttpServerResponse::StatusCode:: + RequestRangeNotSatisfiable; qDebug() << "set new exposure time:" << value; - requested_params.exposureTime = value; + + // requested_params.exposureTime = value; + if (!camera->setExposureTimeUs(value)) + return QHttpServerResponse::StatusCode:: + RequestRangeNotSatisfiable; } - if (json.contains(laserLevelKey)) { + if (json.contains(laserLevelKey)) + { const int32_t value{json[laserLevelKey].toInt()}; - if (value == 0) { + if (value == 0) + { return QHttpServerResponse::StatusCode::NotFound; } @@ -582,7 +623,8 @@ int main(int argc, char* argv[]) "/sys/class/pwm/pwmchip2/pwm1/duty_cycle"}; QFile f{laserLevelFile}; - if (!f.open(QFile::ReadWrite)) { + if (!f.open(QFile::ReadWrite)) + { qDebug() << "cannot open laser level file:" << f.errorString(); qDebug() << "file path is" << f.fileName(); @@ -667,7 +709,8 @@ bool initLaser() QFile f{pwmExportFile}; - if (!f.open(QFile::WriteOnly)) { + if (!f.open(QFile::WriteOnly)) + { qWarning() << "cannot open" << f.fileName() << "for writing"; qWarning() << "error:" << f.errorString(); @@ -684,7 +727,8 @@ bool initLaser() f.close(); f.setFileName(periodFilename); - if (!f.open(QFile::WriteOnly)) { + if (!f.open(QFile::WriteOnly)) + { qWarning() << "cannot open" << f.fileName() << "for writing"; qWarning() << "error:" << f.errorString(); @@ -699,7 +743,8 @@ bool initLaser() f.close(); f.setFileName(dutyCycleFilename); - if (!f.open(QFile::WriteOnly)) { + if (!f.open(QFile::WriteOnly)) + { qWarning() << "cannot open" << f.fileName() << "for writing"; qWarning() << "error:" << f.errorString(); @@ -714,7 +759,8 @@ bool initLaser() f.close(); f.setFileName(enableFilename); - if (!f.open(QFile::WriteOnly)) { + if (!f.open(QFile::WriteOnly)) + { qWarning() << "cannot open" << f.fileName() << "for writing"; qWarning() << "error:" << f.errorString(); |
