diff options
| author | Nikita Kostovsky <luntik2012@gmail.com> | 2025-01-12 17:57:45 +0100 |
|---|---|---|
| committer | Nikita Kostovsky <luntik2012@gmail.com> | 2025-01-12 17:57:45 +0100 |
| commit | 201d98f63131242bb8871ed0c4a3ae9ebd4ef030 (patch) | |
| tree | 763a52710b3c8021c62b93535692b53de90265e5 /main.cpp | |
| parent | 4782e81c32392c78025aae1acb4b2ed1d1395908 (diff) | |
start refactoring. non-working commit
Diffstat (limited to 'main.cpp')
| -rw-r--r-- | main.cpp | 471 |
1 files changed, 190 insertions, 281 deletions
@@ -9,6 +9,7 @@ #include "LibCamera.h" #include "calibration.h" +#include "camera/ov9281.h" #include "dumps.h" #include "fuck_intel.h" #include "genetic_algos.h" @@ -59,10 +60,10 @@ 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; +// 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 @@ -71,12 +72,12 @@ 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); +// 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) { for (size_t i = (img_width - 10) / 2; @@ -88,6 +89,21 @@ auto printPixels = [](const auto& pixels) { std::cout << std::endl; }; +void onNewPixels(std::shared_ptr<Pixels> pixels) +{ + if (!*pixels) + { + qDebug() << "got empty pixels"; + } + + ::pixels = *pixels; + + if (!::pixels) + { + qDebug() << "empty pixels after copy"; + } +}; + bool initLaser(); int main(int argc, char* argv[]) @@ -98,7 +114,8 @@ int main(int argc, char* argv[]) qDebug() << "size of raw profile" << sizeof(Pixels); if (true) { - if (false) { + if (true) + { // z // if (!openCalibrationTable( // "/home/user/dumps/binz.calibration_table", @@ -166,14 +183,15 @@ int main(int argc, char* argv[]) std::cout << std::endl; } - exit(EXIT_SUCCESS); + // exit(EXIT_SUCCESS); interpolate(::calibrationTableX); // calibrationTableToImage(::calibrationTableX) // .save("/home/user/dumps/imageX_interpolated.png"); } - if (true) { + if (false) + { auto rawProfiles = openDump("/home/user/dumps/binz"); // auto rawProfiles = openDump("/home/user/dumps/z"); qDebug() << "raw z-profiles count is" << rawProfiles.size(); @@ -211,7 +229,8 @@ int main(int argc, char* argv[]) qDebug() << "--------------------------------------------------------"; - if (true) { + if (false) + { auto rawProfiles = openDump("/home/user/dumps/binx"); qDebug() << "raw x-profiles count is" << rawProfiles.size(); // qDebug() << "height" << calibrationColumnHeight; @@ -269,27 +288,40 @@ int main(int argc, char* argv[]) std::make_unique<libcamera::CameraManager>(); cm->start(); - const auto cameras = cm->cameras(); - - // openDump(); + // const auto cameras = cm->cameras(); + const auto cameras = OV9281::search(cm); if (cameras.empty()) { - std::cout << "No cameras were identified on the system." << std::endl; + std::cerr << "No cameras were identified on the system." << std::endl; cm->stop(); return EXIT_FAILURE; } + auto camera = cameras.at(0); + + camera->printControls(); + + camera->newPixels.connect(&onNewPixels); + + if (!camera->startStream()) + { + cm->stop(); + + return EXIT_FAILURE; + } + + /* std::string cameraId = cameras[0]->id(); std::cout << "using " << cameraId << std::endl; - /* - * Note that `camera` may not compare equal to `cameras[0]`. - * In fact, it might simply be a `nullptr`, as the particular - * device might have disappeared (and reappeared) in the meantime. - */ + // + // Note that `camera` may not compare equal to `cameras[0]`. + // In fact, it might simply be a `nullptr`, as the particular + // device might have disappeared (and reappeared) in the meantime. + // // std::shared_ptr<Camera> camera = cm->get(cameraId); camera = cm->get(cameraId); @@ -304,8 +336,7 @@ int main(int argc, char* argv[]) // FIXME: nullptr // std::unique_ptr<CameraConfiguration> config = // camera->generateConfiguration( { StreamRole::Viewfinder } ); - /*std::unique_ptr<CameraConfiguration> */ config = - camera->generateConfiguration({libcamera::StreamRole::Raw}); + config = camera->generateConfiguration({libcamera::StreamRole::Raw}); if (config->empty()) { @@ -481,8 +512,7 @@ int main(int argc, char* argv[]) { camera->queueRequest(request.get()); } - - printControls(); +*/ // std::this_thread::sleep_for(2s); // TODO: move to thread @@ -490,11 +520,13 @@ int main(int argc, char* argv[]) QHttpServer qHttpServer; qHttpServer.route("/v1/sensor/image", [&]() { + std::cout << "http: image" << std::endl; std::lock_guard<std::mutex> lg(pgm_image_mtx); // qDebug() << "image"; return QByteArray((const char*)pgm_image, pgm_image_size); }); qHttpServer.route("/v1/sensor/image2", [&]() { + std::cout << "http: image2" << std::endl; std::lock_guard<std::mutex> lg(pgm_image_mtx); // qDebug() << "image"; return QByteArray((const char*)pgm_image, pgm_image_size); @@ -504,6 +536,7 @@ int main(int argc, char* argv[]) // return "123"; // }); qHttpServer.route("/v1/pixels", [&]() { + std::cout << "http: pixels" << std::endl; std::lock_guard<std::mutex> lg(pgm_image_mtx); QJsonArray pixels; @@ -540,6 +573,7 @@ int main(int argc, char* argv[]) }); qHttpServer.route("/v1/profile", [&]() { + std::cout << "http: profile" << std::endl; std::lock_guard<std::mutex> lg(pgm_image_mtx); const Profile profile(::pixels, @@ -554,7 +588,9 @@ int main(int argc, char* argv[]) qHttpServer .route("/v1/commands/resetEncoder", [&](const QHttpServerRequest& request) -> QHttpServerResponse { - if (request.method() != QHttpServerRequest::Method::Post) { + std::cout << "http: resetEncoder" << std::endl; + if (request.method() != QHttpServerRequest::Method::Post) + { return QHttpServerResponse::StatusCode::NotFound; } @@ -565,91 +601,92 @@ int main(int argc, char* argv[]) return QHttpServerResponse::StatusCode::Ok; }); - qHttpServer.route( - "/v1/commands/startCalibration", - [&](const QHttpServerRequest& request) -> QHttpServerResponse { - if (request.method() != QHttpServerRequest::Method::Post) - { - return QHttpServerResponse::StatusCode::NotFound; - } + qHttpServer + .route("/v1/commands/startCalibration", + [&](const QHttpServerRequest& request) -> QHttpServerResponse { + std::cout << "http: startCalibration" << std::endl; + if (request.method() != QHttpServerRequest::Method::Post) + { + return QHttpServerResponse::StatusCode::NotFound; + } - qDebug() << "start calibration"; + qDebug() << "start calibration"; - // TODO: use flags - scanningModeFlags = ScanningModeFlags::Calibration; - calibrationTimer.start(); + // TODO: use flags + scanningModeFlags = ScanningModeFlags::Calibration; + calibrationTimer.start(); - return QHttpServerResponse::StatusCode::Ok; - } - ); + return QHttpServerResponse::StatusCode::Ok; + }); - qHttpServer.route( - "/v1/commands/gCode", - [&](const QHttpServerRequest& request) -> QHttpServerResponse { - 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/commands/startCalibration", - [&](const QHttpServerRequest& request) -> QHttpServerResponse { - if (request.method() != QHttpServerRequest::Method::Post) - { - return QHttpServerResponse::StatusCode::NotFound; - } + // qHttpServer + // .route("/v1/commands/startCalibration", + // [&](const QHttpServerRequest& request) -> QHttpServerResponse { + // std::cout << "http: startCalibration" << 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 { + std::cout << "http: params" << std::endl; switch (request.method()) { case QHttpServerRequest::Method::Get: { std::lock_guard<std::mutex> lg(pgm_image_mtx); QJsonObject json; - const libcamera::ControlIdMap& ctrlIdMap = - camera->controls().idmap(); + // const libcamera::ControlIdMap& ctrlIdMap = + // camera->controls().idmap(); - qDebug() << "readParams:" << lastControls.size(); - qDebug() << request.method(); + // qDebug() << "readParams:" << lastControls.size(); + // qDebug() << request.method(); - for (const auto& [id, value] : lastControls) - { - const libcamera::ControlId* controlId = ctrlIdMap.at(id); - auto name = QString::fromStdString(controlId->name()); - const auto valueStr = - QString::fromStdString(value.toString()); - qDebug() - << "\t param:" << controlId->id() << name << valueStr; + // for (const auto& [id, value] : lastControls) + // { + // const libcamera::ControlId* controlId = ctrlIdMap.at(id); + // auto name = QString::fromStdString(controlId->name()); + // const auto valueStr = + // QString::fromStdString(value.toString()); + // qDebug() + // << "\t param:" << controlId->id() << name << valueStr; - name[0] = name[0].toLower(); - json[name] = valueStr; - } + // name[0] = name[0].toLower(); + // json[name] = valueStr; + // } - json[laserLevelKey] = requested_params.laserLevel; + // json[laserLevelKey] = requested_params.laserLevel; - qDebug() << "response body:" << json; + // qDebug() << "response body:" << json; // QHttpServerResponse return QHttpServerResponse(QJsonDocument(json).toJson()); @@ -715,8 +752,7 @@ int main(int argc, char* argv[]) ); } } - } - ); + }); qDebug() << "listen: " << qHttpServer.listen(QHostAddress::Any, 8081); @@ -745,97 +781,24 @@ int main(int argc, char* argv[]) future.cancel(); future.waitForFinished(); - for (auto& [fd, mem] : mappedBuffers_) - { - munmap(mem.first, mem.second); - } + // for (auto& [fd, mem] : mappedBuffers_) + // { + // munmap(mem.first, mem.second); + // } // FIXME: crash somewhere here. proper libcamera finishing needed - requests.clear(); - mappedBuffers_.clear(); + // requests.clear(); + // mappedBuffers_.clear(); - camera->stop(); - config.reset(); - allocator->free(stream); - allocator.reset(); - camera->release(); - camera.reset(); + // camera->stop(); + // config.reset(); + // allocator->free(stream); + // allocator.reset(); + // camera->release(); + // camera.reset(); cm->stop(); return result; - - // time_t start_time = time(0); - // int frame_count = 0; - - // LibCamera cam; - // uint32_t width = 1280; - // uint32_t height = 800; - // uint32_t stride; - // char key; - - // ret = cam.initCamera(); - - // if (ret != EXIT_SUCCESS) - // { - // std::cerr << "cannot open camera" << std::endl; - - // return EXIT_FAILURE; - // } - - // cam.configureStill(width, height, formats::R8, 1, 0); - // // ControlList controls_; - // int64_t frame_time = 1000000 / 10; - // // Set frame rate - // // controls_.set( controls::FrameDurationLimits, libcamera::Span<const - // int64_t, 2>( - // // { frame_time, frame_time } )); - // // Adjust the brightness of the output images, in the range -1.0 to 1.0 - // // controls_.set(controls::Brightness, 0.5); - // // Adjust the contrast of the output image, where 1.0 = normal contrast - // // controls_.set(controls::Contrast, 1.5); - // // Set the exposure time - // // controls_.set(controls::ExposureTime, 20000); - // // cam.set(controls_); - - // std::cout << std::flush; - - // // NOTE: already checked - // if (ret == EXIT_SUCCESS) { - // bool flag; - // LibcameraOutData frameData; - // cam.startCamera(); - // cam.VideoStream(&width, &height, &stride); - - // while (true) { - // flag = cam.readFrame(&frameData); - // if (!flag) - // continue; - - // // key = waitKey(1); - // // if (key == 'q') { - // // break; - // // } else if (key == 'f') { - // // ControlList controls; - // // controls.set(controls::AfMode, controls::AfModeAuto); - // // controls.set(controls::AfTrigger, 0); - // // cam.set(controls); - // // } - - // frame_count++; - // if ((time(0) - start_time) >= 1){ - // printf("fps: %d\n", frame_count); - // frame_count = 0; - // start_time = time(0); - // } - // cam.returnFrameBuffer(frameData); - // } - - // cam.stopCamera(); - // } - - // cam.closeCamera(); - - // return EXIT_SUCCESS; } /* @@ -843,6 +806,7 @@ int main(int argc, char* argv[]) * important not to block the thread for a long time, as this blocks internal * processing of the camera pipelines, and can affect realtime performance. */ +/* void onRequestCompleted(libcamera::Request* completed_request) { using namespace libcamera; @@ -879,13 +843,6 @@ void onRequestCompleted(libcamera::Request* completed_request) const FrameMetadata& metadata = buffer->metadata(); - // if (verbose) - // { - // std::cout << " seq: " << std::setw(6) << std::setfill('0') - // << metadata.sequence - // << " bytesused: "; - // } - for (size_t i = 0; i < buffer->planes().size(); ++i) { const FrameBuffer::Plane& plane = buffer->planes()[i]; @@ -913,7 +870,7 @@ void onRequestCompleted(libcamera::Request* completed_request) // unpack_16bit((uint8_t*)img.data, img, (uint16_t*)&unpacked); // img.data = unpacked; // img.dataSize = img.width * img.height * sizeof(uint16_t); - rotate(img); + img.rotate(); Pixels pixels = process_columns(img); ::pixels = pixels; @@ -1040,120 +997,72 @@ void onRequestCompleted(libcamera::Request* completed_request) // qDebug() << "-------------------------------------------"; } +*/ -static bool applyConfig( - const std::unique_ptr<libcamera::CameraConfiguration>& config -) -{ - using namespace libcamera; - - auto status = config->validate(); - - // WARNING: unsafe - libcamera::StreamConfiguration& streamConfig = config->at(0); - - switch (status) - { - case CameraConfiguration::Status::Valid: - std::cout << "config is valid" << std::endl; - break; - case CameraConfiguration::Status::Adjusted: - std::cout << "\tpixelFormat: " << streamConfig.pixelFormat.toString() - << std::endl; - std::cout << "\tbufferCount: " << streamConfig.bufferCount << std::endl; - std::cout << "\torientation: " << config->orientation << std::endl; - break; - case CameraConfiguration::Status::Invalid: - std::cout << "config is invalid, quit." << std::endl; - - return false; - } - - return true; -} - -static void printControls() -{ - using namespace libcamera; - const libcamera::ControlInfoMap& control_map = camera->controls(); +// static bool applyConfig( +// const std::unique_ptr<libcamera::CameraConfiguration>& config +// ) +// { +// using namespace libcamera; - // for (const auto & [id, info]: control_map) - for (const std::pair<const ControlId*, ControlInfo>& pair : control_map) - { - const ControlId* const& id = pair.first; - const ControlInfo& info = pair.second; +// auto status = config->validate(); - std::cout << "\tc " << id->name() << " (" << id->id() - << "): " << info.toString() - << (info.def().isNone() - ? "" - : " (dflt:" + info.def().toString() + ")"); +// // WARNING: unsafe +// libcamera::StreamConfiguration& streamConfig = config->at(0); - if (!info.values().size()) - { - std::cout << std::endl; - continue; - } +// switch (status) +// { +// case CameraConfiguration::Status::Valid: +// std::cout << "config is valid" << std::endl; +// break; +// case CameraConfiguration::Status::Adjusted: +// std::cout << "\tpixelFormat: " << streamConfig.pixelFormat.toString() +// << std::endl; +// std::cout << "\tbufferCount: " << streamConfig.bufferCount << std::endl; +// std::cout << "\torientation: " << config->orientation << std::endl; +// break; +// case CameraConfiguration::Status::Invalid: +// std::cout << "config is invalid, quit." << std::endl; - std::cout << " - ["; +// return false; +// } - for (const auto& v : info.values()) - { - std::cout << " " << v.toString(); - } +// return true; +// } - std::cout << " ]\n"; - } -} - -static QList<Pixels> filter(const QList<Pixels>& rawProfiles) -{ - QList<Pixels> result; +// static void printControls() +// { +// using namespace libcamera; +// const libcamera::ControlInfoMap& control_map = camera->controls(); - QList<Pixels>::const_iterator it = rawProfiles.constBegin(); +// // for (const auto & [id, info]: control_map) +// for (const std::pair<const ControlId*, ControlInfo>& pair : control_map) +// { +// const ControlId* const& id = pair.first; +// const ControlInfo& info = pair.second; - // for (size_t i = 0; i < 10; ++i) - // { - // std::cout << "pos - " << rawProfiles.at(i).counters.encoderPosition - // << std::endl; - // } +// std::cout << "\tc " << id->name() << " (" << id->id() +// << "): " << info.toString() +// << (info.def().isNone() +// ? "" +// : " (dflt:" + info.def().toString() + ")"); - // ++it; - while (it != rawProfiles.constEnd()) - { - Pixels sum = *it; - // std::cout << "current pos is " << sum.counters.encoderPosition - // << std::endl; +// if (!info.values().size()) +// { +// std::cout << std::endl; +// continue; +// } - // printPixels(sum.pixels); +// std::cout << " - ["; - size_t count{1}; - ++it; +// for (const auto& v : info.values()) +// { +// std::cout << " " << v.toString(); +// } - // std::cout << "here\n"; - while (it != rawProfiles.constEnd() && - it->counters.encoderPosition == sum.counters.encoderPosition) - { - // std::cout << "here2\n"; - // std::cout << "\tadd to pos " << it->counters.encoderPosition - // << std::endl; - sum += *it; - // std::cout << "\tadded" << std::endl; - ++count; - ++it; - } - - // std::cout << "here3\n"; - sum /= float(count); - // printPixels(sum.pixels); - - result << sum; - - // return result; - } - - return result; -} +// std::cout << " ]\n"; +// } +// } bool initLaser() { |
