diff options
Diffstat (limited to 'src/main.cpp')
| -rw-r--r-- | src/main.cpp | 388 |
1 files changed, 105 insertions, 283 deletions
diff --git a/src/main.cpp b/src/main.cpp index 5a7d701..e5d6c5b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,32 +1,15 @@ +// cpp and linux #include <chrono> #include <csignal> #include <errno.h> #include <fcntl.h> -#include <fstream> #include <iostream> -#include <iterator> #include <linux/videodev2.h> #include <string.h> #include <sys/ioctl.h> #include <sys/mman.h> -#include <thread> - -#include "LibCamera.h" -#include "calibration.h" -#include "camera/veye_i2c.h" -#include "camera/veyeimx287m.h" -#include "dumps.h" -#include "fuck_intel.h" -#include "genetic_algos.h" -#include "imagealgos.h" -#include "laser.h" -#include "macro.h" -#include "protocols/httpserver.h" -// #include "pigpio.h" -#include "printerclient.h" -#include "profile.h" -#include "rotaryencoder.h" +// qt #include <QCoreApplication> #include <QDebug> #include <QDir> @@ -41,6 +24,16 @@ #include <QTimer> #include <QtConcurrent/QtConcurrent> +// orpheus +#include "LibCamera.h" +#include "calibration.h" +#include "camera/veyeimx287m.h" +#include "dumps.h" +#include "imagealgos.h" +#include "profile.h" +#include "protocols/httpserver.h" + +// #include "rapidjson/document.h" #include "rapidjson/stringbuffer.h" #include "rapidjson/writer.h" @@ -50,8 +43,7 @@ extern size_t pgm_image_size; extern std::mutex pgm_image_mtx; #define try_apply_config() \ - if (!applyConfig(config)) \ - { \ + if (!applyConfig(config)) { \ camera->release(); \ cm->stop(); \ \ @@ -67,8 +59,6 @@ extern volatile int32_t positionSteps; requested_params_t requested_params; namespace { -// std::shared_ptr<Image> img; -// Image *img = nullptr; Pixels pixels; std::vector<Pixels> calibrationPixels; QMutex calibrationPixelsMutex; @@ -76,154 +66,62 @@ QMutex calibrationPixelsMutex; using namespace std::chrono_literals; -// static std::shared_ptr<libcamera::Camera> camera; -// std::unique_ptr<libcamera::CameraConfiguration> config; -// static std::map<int, std::pair<void*, unsigned int>> mappedBuffers_; -// std::vector<std::unique_ptr<libcamera::Request>> requests; -libcamera::ControlList lastControls; - namespace { CalibrationTablePtr calibrationTableZ; CalibrationTablePtr calibrationTableX; } // namespace -// static bool applyConfig( -// const std::unique_ptr<libcamera::CameraConfiguration>& config -// ); -// static void onRequestCompleted(libcamera::Request* completed_request); -// static void printControls(); -// static QList<Pixels> filter(const QList<Pixels>& rawProfiles); - -auto printPixels = [](const auto& pixels) { +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(Image &image) -// { -// // std::cout << __func__ << std::endl << std::flush; - -// // if (!image) -// // { -// // qDebug() << __func__ << "no image"; -// // return; -// // } - -// ::img = ℑ -// } - -void onNewPixels(std::shared_ptr<Pixels> pixels) -{ - // std::cout << __func__ << std::endl << std::flush; - - if (!pixels) - { - qDebug() << __func__ << "got null pixels"; - } - - if (!*pixels) - { - // qDebug() << __func__ << "got empty pixels"; - } - - // for (size_t i = 640 - 5; i < 640 + 5; i++) - // { - // // std::cout << pixels->pixels[i] << " "; - // } - - // std::cout << std::endl - - ::pixels = *pixels; -} - bool initLaser(); bool initCam(); -int main(int argc, char* argv[]) +int main(int argc, char *argv[]) { auto sigHandler = [](int s) { std::cout << "got signal " << s << std::endl; std::signal(s, SIG_DFL); - // QTimer::singleShot(0, qApp, &QCoreApplication::quit); - // qApp->quit(); }; - // if (!initCam()) { - // return EXIT_FAILURE; - // } - - // return EXIT_SUCCESS; - - // for (int i = 2000; i >= 0; i -= 50) { - // for (int i = 0; i < 2000; i += 50) { - // if (!veye::imx287m::test(i)) { - // return EXIT_FAILURE; - // } - // } - // return EXIT_SUCCESS; - std::signal(SIGINT, sigHandler); std::signal(SIGTERM, sigHandler); QCoreApplication app(argc, argv); - // QTimer::singleShot(4000, qApp, &QCoreApplication::quit); QList<QFuture<void>> initializers; - // if (false) qDebug() << "size of raw profile" << sizeof(Pixels); if (false) { // open binary calibration table - if (true) - { + if (true) { initializers << QtConcurrent::run([&]() { - if (!openCalibrationTable("/home/user/dumps/binz.calibration_table", - // "/tmp/binz.calibration_table", - ::calibrationTableZ)) { + if (!openCalibrationTable( + "/home/user/dumps/binz.calibration_table", + // "/tmp/binz.calibration_table", + ::calibrationTableZ)) { exit(EXIT_FAILURE); } }); initializers << QtConcurrent::run([&]() { - if (!openCalibrationTable("/home/user/dumps/binx.calibration_table", - // "/tmp/binx.calibration_table", - ::calibrationTableX)) { + if (!openCalibrationTable( + "/home/user/dumps/binx.calibration_table", + // "/tmp/binx.calibration_table", + ::calibrationTableX)) { exit(EXIT_FAILURE); } }); } - if (false) - { - // z - // if (!openCalibrationTable( - // "/home/user/dumps/binz.calibration_table", - // ::calibrationTableZ - // )) - // { - // exit(EXIT_FAILURE); - // } - - // if (!calibrationTableToImage(::calibrationTableZ) - // .save("/home/user/dumps/imageZ.png")) - // { - // qDebug() << "cannot save imageZ.png"; - // exit(EXIT_FAILURE); - // } - - // interpolate(::calibrationTableZ); - // exit(EXIT_SUCCESS); - - // calibrationTableToImage(::calibrationTableZ) - // .save("/home/user/dumps/imageZ_interpolated.png"); - + if (false) { auto rawProfiles = openDump("/home/user/dumps/binx"); qDebug() << "raw x-profiles count is" << rawProfiles.size(); // qDebug() << "height" << calibrationColumnHeight; @@ -234,57 +132,14 @@ int main(int argc, char* argv[]) ::calibrationTableX = calibrateX(std::move(filteredRawProfiles)); - // for (size_t i = 9471; i < 9472; i++) { - // std::cout << "row #" << i << ": "; - - // for (size_t j = 0; j < 1280; ++j) { - // const auto& p = ::calibrationTableX->at(j).at(i); - // std::cout << p << ' '; - // } - - // std::cout << std::endl; - // } - - // x - // qDebug() << "open x table"; - // if (!openCalibrationTable("/home/user/dumps/binx.calibration_table", - // ::calibrationTableX)) { - // exit(EXIT_FAILURE); - // } - - // if (!calibrationTableToImage(::calibrationTableX) - // .save("/home/user/dumps/imageX.png")) { - // qDebug() << "cannot save imageX.png"; - // exit(EXIT_FAILURE); - // } - - // for (size_t i = 9471; i < 9472; i++) { - // std::cout << "row #" << i << ": "; - - // for (size_t j = 0; j < 1280; ++j) { - // const auto& p = ::calibrationTableX->at(j).at(i); - // std::cout << p << ' '; - // } - - // std::cout << std::endl; - // } - - // exit(EXIT_SUCCESS); interpolate(::calibrationTableX); - - // calibrationTableToImage(::calibrationTableX) - // .save("/home/user/dumps/imageX_interpolated.png"); } // 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(); - // qDebug() << "height" << calibrationColumnHeight; auto filteredRawProfiles = filter(std::move(rawProfiles)); qDebug() << "filtered z-profiles count is" @@ -293,35 +148,20 @@ int main(int argc, char* argv[]) ::calibrationTableZ = calibrateZ(std::move(filteredRawProfiles), requested_params.stepsPerMm); - // bool ok = calibrationTableToImage(::calibrationTableZ) - // .save("/home/user/dumps/z/imageZ.png"); - - // if (!ok) - // { - // qDebug() << "cannot save imageZ.png"; - // exit(EXIT_FAILURE); - // } - interpolate(::calibrationTableZ); if (!dump(::calibrationTableZ, - "/home/user/dumps/binz.calibration_table")) - { + "/home/user/dumps/binz.calibration_table")) { qApp->exit(EXIT_FAILURE); } - // calibrationTableToImage(::calibrationTableZ) - // .save("/home/user/dumps/z/imageZ_interpolated.png"); - // exit(EXIT_SUCCESS); } qDebug() << "--------------------------------------------------------"; - if (true) - { + if (true) { auto rawProfiles = openDump("/home/user/dumps/binx"); qDebug() << "raw x-profiles count is" << rawProfiles.size(); - // qDebug() << "height" << calibrationColumnHeight; auto filteredRawProfiles = filter(std::move(rawProfiles)); qDebug() << "filtered x-profiles count is" @@ -329,55 +169,25 @@ int main(int argc, char* argv[]) ::calibrationTableX = calibrateX(std::move(filteredRawProfiles)); - // bool ok = calibrationTableToImage(::calibrationTableX) - // .save("/home/user/dumps/z/imageX.png"); - - // if (!ok) - // { - // qDebug() << "cannot save imageX.png"; - // exit(EXIT_FAILURE); - // } - interpolate(::calibrationTableX); if (!dump(::calibrationTableX, - "/home/user/dumps/binx.calibration_table")) - { + "/home/user/dumps/binx.calibration_table")) { qApp->exit(EXIT_FAILURE); } - - // calibrationTableToImage(::calibrationTableX) - // .save("/home/user/dumps/z/imageX_interpolated.png"); } } } - // exit(EXIT_SUCCESS); - - // if (!initLaser()) { - // return EXIT_FAILURE; - // } - - // PrinterClient printerClient; - QElapsedTimer t; t.start(); - qDebug() << "msecs before encoder:" << t.elapsed(); - - // RotaryEncoder encoder; - - qDebug() << "msecs before camera:" << t.elapsed(); // FIXME: don't use one var for everything int ret; - // const auto cameras = cm->cameras(); - // const auto cameras = OV9281::search(cm); - // const auto cameras = InnoMakerOV9281::search(); + const auto cameras = VeyeIMX287m::search(); - // const auto cameras = - if (cameras.empty()) - { + if (cameras.empty()) { std::cerr << "No cameras were identified on the system." << std::endl; return EXIT_FAILURE; @@ -386,17 +196,12 @@ int main(int argc, char* argv[]) auto camera = cameras.at(0); std::cout << "connect everything" << std::endl; - // camera->newPixels.connect(&onNewPixels); - // camera->newImage.connect(&onNewImage); - // camera->newImageCallback = &onNewImage; - camera->newPixelsCallback = &onNewPixels; - for (auto& i : initializers) + for (auto &i : initializers) i.waitForFinished(); std::cout << "loaded calibration tables" << std::endl; - if (!camera->startStream()) - { + if (!camera->startStream()) { return EXIT_FAILURE; } @@ -419,7 +224,8 @@ int main(int argc, char* argv[]) pgm_save(&img); std::lock_guard<std::mutex> lg(pgm_image_mtx); - return QHttpServerResponse{QByteArray((const char *) pgm_image, pgm_image_size), + return QHttpServerResponse{QByteArray((const char *) pgm_image, + pgm_image_size), QHttpServerResponse::StatusCode::Ok}; }; qHttpServer.route("/v1/sensor/image", httpGetImage); @@ -450,7 +256,9 @@ int main(int argc, char* argv[]) // qt json does not allow to limit double precision const auto lines = pixelsToLines(::pixels); - const auto nan2zero = [](const auto &value) { return qIsNaN(value) ? 0 : value; }; + const auto nan2zero = [](const auto &value) { + return qIsNaN(value) ? 0 : value; + }; rapidjson::Document jd; jd.SetObject(); auto &al = jd.GetAllocator(); @@ -465,9 +273,11 @@ int main(int argc, char* argv[]) for (const auto &l : lines) { rapidjson::Value rjLineP1{rapidjson::kArrayType}; - rjLineP1.PushBack(nan2zero(l.p1().x()), al).PushBack(nan2zero(l.p1().y()), al); + rjLineP1.PushBack(nan2zero(l.p1().x()), al) + .PushBack(nan2zero(l.p1().y()), al); rapidjson::Value rjLineP2{rapidjson::kArrayType}; - rjLineP2.PushBack(nan2zero(l.p2().x()), al).PushBack(nan2zero(l.p2().y()), al); + rjLineP2.PushBack(nan2zero(l.p2().x()), al) + .PushBack(nan2zero(l.p2().y()), al); rapidjson::Value rjLinePoints{rapidjson::kArrayType}; rjLinePoints.PushBack(rjLineP1, al).PushBack(rjLineP2, al); @@ -511,10 +321,9 @@ int main(int argc, char* argv[]) qHttpServer .route("/v1/commands/resetEncoder", - [&](const QHttpServerRequest& request) -> QHttpServerResponse { + [&](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; } @@ -527,10 +336,9 @@ int main(int argc, char* argv[]) qHttpServer .route("/v1/commands/startCalibration", - [&](const QHttpServerRequest& request) -> QHttpServerResponse { + [&](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; } @@ -543,28 +351,28 @@ int main(int argc, char* argv[]) return QHttpServerResponse::StatusCode::Ok; }); - qHttpServer.route("/v1/commands/gCode", - [&](const QHttpServerRequest &request) -> QHttpServerResponse { - std::cout << "http: gCode" << std::endl; - if (request.method() != QHttpServerRequest::Method::Post) { - return QHttpServerResponse::StatusCode::NotFound; - } + qHttpServer + .route("/v1/commands/gCode", + [&](const QHttpServerRequest &request) -> QHttpServerResponse { + std::cout << "http: gCode" << std::endl; + if (request.method() != QHttpServerRequest::Method::Post) { + return QHttpServerResponse::StatusCode::NotFound; + } - const auto command = request.body(); + const auto command = request.body(); - qDebug() << "send gCode:" << command; + qDebug() << "send gCode:" << command; - // printerClient.sendCommand(command); + // printerClient.sendCommand(command); - return QHttpServerResponse::StatusCode::Ok; - }); + return QHttpServerResponse::StatusCode::Ok; + }); qHttpServer.route( "/v1/sensor/params", - [&](const QHttpServerRequest& request) -> QHttpServerResponse { + [&](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; @@ -577,8 +385,7 @@ 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) @@ -596,8 +403,7 @@ int main(int argc, char* argv[]) } } - if (json.contains(gainKey)) - { + if (json.contains(gainKey)) { const auto value = json[gainKey].toDouble(); if (value == 0) @@ -609,8 +415,7 @@ int main(int argc, char* argv[]) RequestRangeNotSatisfiable; } - if (json.contains(laserLevelKey)) - { + if (json.contains(laserLevelKey)) { const int32_t value{json[laserLevelKey].toInt()}; if (!camera->setLaserLevel(value)) @@ -652,8 +457,7 @@ 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(); @@ -670,8 +474,7 @@ 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(); @@ -686,8 +489,7 @@ 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(); @@ -702,8 +504,7 @@ 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(); @@ -725,7 +526,10 @@ bool initCam() int fd{open(devname, O_RDWR)}; if (fd < 0) { - printf("Error opening device %s: %s (%d).\n", devname, strerror(errno), errno); + printf("Error opening device %s: %s (%d).\n", + devname, + strerror(errno), + errno); fflush(stdout); return false; } @@ -736,25 +540,35 @@ bool initCam() memset(&cap, 0, sizeof cap); const auto ret = ioctl(fd, VIDIOC_QUERYCAP, &cap); if (ret < 0) { - printf("cannot query cap for %s: %s (%d).\n", devname, strerror(errno), errno); + printf("cannot query cap for %s: %s (%d).\n", + devname, + strerror(errno), + errno); fflush(stdout); return false; } std::cout << devname << ": got caps" << std::endl; - const auto caps = cap.capabilities & V4L2_CAP_DEVICE_CAPS ? cap.device_caps : cap.capabilities; + const auto caps = cap.capabilities & V4L2_CAP_DEVICE_CAPS + ? cap.device_caps + : cap.capabilities; const bool has_video = caps - & (V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_CAPTURE - | V4L2_CAP_VIDEO_OUTPUT_MPLANE | V4L2_CAP_VIDEO_OUTPUT); + & (V4L2_CAP_VIDEO_CAPTURE_MPLANE + | V4L2_CAP_VIDEO_CAPTURE + | V4L2_CAP_VIDEO_OUTPUT_MPLANE + | V4L2_CAP_VIDEO_OUTPUT); const bool has_meta = caps & (V4L2_CAP_META_CAPTURE | V4L2_CAP_META_OUTPUT); const bool has_capture = caps - & (V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_CAPTURE + & (V4L2_CAP_VIDEO_CAPTURE_MPLANE + | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_META_CAPTURE); const bool has_output = caps - & (V4L2_CAP_VIDEO_OUTPUT_MPLANE | V4L2_CAP_VIDEO_OUTPUT - | V4L2_CAP_META_OUTPUT); - const bool has_mplane = caps & (V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT_MPLANE); + & (V4L2_CAP_VIDEO_OUTPUT_MPLANE + | V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_META_OUTPUT); + const bool has_mplane = caps + & (V4L2_CAP_VIDEO_CAPTURE_MPLANE + | V4L2_CAP_VIDEO_OUTPUT_MPLANE); printf("Device `%s' on `%s' (driver '%s') supports%s%s%s%s %s mplanes.\n", cap.card, @@ -768,22 +582,30 @@ bool initCam() const auto buf_type = [caps, devname]() -> int { if (caps & V4L2_CAP_VIDEO_CAPTURE_MPLANE) { - std::cout << devname << ": buf_type: V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE" << std::endl; + std::cout << devname + << ": buf_type: V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE" + << std::endl; return V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; } else if (caps & V4L2_CAP_VIDEO_OUTPUT_MPLANE) { - std::cout << devname << ": buf_type: V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE" << std::endl; + std::cout << devname + << ": buf_type: V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE" + << std::endl; return V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; } else if (caps & V4L2_CAP_VIDEO_CAPTURE) { - std::cout << devname << ": buf_type: V4L2_BUF_TYPE_VIDEO_CAPTURE" << std::endl; + std::cout << devname << ": buf_type: V4L2_BUF_TYPE_VIDEO_CAPTURE" + << std::endl; return V4L2_BUF_TYPE_VIDEO_CAPTURE; } else if (caps & V4L2_CAP_VIDEO_OUTPUT) { - std::cout << devname << ": buf_type: V4L2_BUF_TYPE_VIDEO_OUTPUT" << std::endl; + std::cout << devname << ": buf_type: V4L2_BUF_TYPE_VIDEO_OUTPUT" + << std::endl; return V4L2_BUF_TYPE_VIDEO_OUTPUT; } else if (caps & V4L2_CAP_META_CAPTURE) { - std::cout << devname << ": buf_type: V4L2_BUF_TYPE_META_CAPTURE" << std::endl; + std::cout << devname << ": buf_type: V4L2_BUF_TYPE_META_CAPTURE" + << std::endl; return V4L2_BUF_TYPE_META_CAPTURE; } else if (caps & V4L2_CAP_META_OUTPUT) { - std::cout << devname << ": buf_type: V4L2_BUF_TYPE_META_OUTPUT" << std::endl; + std::cout << devname << ": buf_type: V4L2_BUF_TYPE_META_OUTPUT" + << std::endl; return V4L2_BUF_TYPE_META_OUTPUT; } else { printf("Device supports neither capture nor output.\n"); |
