NemoInterface.cpp 15.6 KB
Newer Older
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1
#include "NemoInterface.h"
2
#include "SnakeTilesLocal.h"
Valentin Platzgummer's avatar
Valentin Platzgummer committed
3 4

#include "QGCApplication.h"
5
#include "QGCLoggingCategory.h"
Valentin Platzgummer's avatar
Valentin Platzgummer committed
6 7 8 9 10 11 12 13 14 15 16
#include "QGCToolbox.h"
#include "SettingsFact.h"
#include "SettingsManager.h"
#include "WimaSettings.h"

#include <shared_mutex>

#include <QTimer>

#include "QNemoHeartbeat.h"
#include "QNemoProgress.h"
17 18 19
#include "Wima/Geometry/WimaMeasurementArea.h"
#include "Wima/Snake/SnakeTile.h"
#include "Wima/Snake/snake.h"
Valentin Platzgummer's avatar
Valentin Platzgummer committed
20 21 22 23 24 25 26 27 28 29

#include "ros_bridge/include/messages/geographic_msgs/geopoint.h"
#include "ros_bridge/include/messages/jsk_recognition_msgs/polygon_array.h"
#include "ros_bridge/include/messages/nemo_msgs/heartbeat.h"
#include "ros_bridge/include/messages/nemo_msgs/progress.h"
#include "ros_bridge/include/ros_bridge.h"
#include "ros_bridge/rapidjson/include/rapidjson/document.h"
#include "ros_bridge/rapidjson/include/rapidjson/ostreamwrapper.h"
#include "ros_bridge/rapidjson/include/rapidjson/writer.h"

30 31
QGC_LOGGING_CATEGORY(NemoInterfaceLog, "NemoInterfaceLog")

Valentin Platzgummer's avatar
Valentin Platzgummer committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#define EVENT_TIMER_INTERVAL 100 // ms
auto static timeoutInterval = std::chrono::milliseconds(3000);

using ROSBridgePtr = std::unique_ptr<ros_bridge::ROSBridge>;
using JsonDocUPtr = ros_bridge::com_private::JsonDocUPtr;
using UniqueLock = std::unique_lock<std::shared_timed_mutex>;
using SharedLock = std::shared_lock<std::shared_timed_mutex>;
using JsonDocUPtr = ros_bridge::com_private::JsonDocUPtr;

class NemoInterface::Impl {
  using TimePoint = std::chrono::time_point<std::chrono::high_resolution_clock>;

public:
  Impl(NemoInterface *p);

  void start();
  void stop();
49 50
  void setTileData(const TileData &tileData);
  bool hasTileData(const TileData &tileData) const;
51 52
  void setAutoPublish(bool ap);
  void setHoldProgress(bool hp);
53

54 55 56
  void publishTileData();

  NemoInterface::STATUS status();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
57
  QVector<int> progress();
58
  bool running();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
59 60

private:
61
  void doTopicServiceSetup();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
62
  void loop();
63 64
  static STATUS heartbeatToStatus(
      const ros_bridge::messages::nemo_msgs::heartbeat::Heartbeat &hb);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
65 66 67 68 69 70 71 72 73 74
  //!
  //! \brief Publishes tilesENU
  //! \pre this->tilesENUMutex must be locked
  //!
  void publishTilesENU();
  //!
  //! \brief Publishes ENUOrigin
  //! \pre this->ENUOriginMutex must be locked
  //!
  void publishENUOrigin();
75
  bool setStatus(NemoInterface::STATUS s);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
76 77 78 79 80 81

  // Data.
  SnakeTilesLocal tilesENU;
  mutable std::shared_timed_mutex tilesENUMutex;
  QGeoCoordinate ENUOrigin;
  mutable std::shared_timed_mutex ENUOriginMutex;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
82
  QNemoProgress qProgress;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
83 84
  mutable std::shared_timed_mutex progressMutex;
  TimePoint nextTimeout;
85 86
  mutable std::shared_timed_mutex timeoutMutex;
  std::atomic<NemoInterface::STATUS> status_;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
87

88 89 90
  // Not protected data.
  TileData tileData;

Valentin Platzgummer's avatar
Valentin Platzgummer committed
91
  // Internals
92
  std::atomic_bool running_;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
93 94 95 96 97 98
  std::atomic_bool topicServiceSetupDone;
  ROSBridgePtr pRosBridge;
  QTimer loopTimer;
  NemoInterface *parent;
};

99 100 101 102 103 104 105 106 107 108 109 110 111
using StatusMap = std::map<NemoInterface::STATUS, QString>;
StatusMap statusMap{
    std::make_pair<NemoInterface::STATUS, QString>(
        NemoInterface::STATUS::NOT_CONNECTED, "Not Connected"),
    std::make_pair<NemoInterface::STATUS, QString>(
        NemoInterface::STATUS::HEARTBEAT_DETECTED, "Heartbeat Detected"),
    std::make_pair<NemoInterface::STATUS, QString>(
        NemoInterface::STATUS::TIMEOUT, "Timeout"),
    std::make_pair<NemoInterface::STATUS, QString>(
        NemoInterface::STATUS::INVALID_HEARTBEAT, "Error"),
    std::make_pair<NemoInterface::STATUS, QString>(
        NemoInterface::STATUS::WEBSOCKET_DETECTED, "Websocket Detected")};

Valentin Platzgummer's avatar
Valentin Platzgummer committed
112
NemoInterface::Impl::Impl(NemoInterface *p)
113
    : nextTimeout(TimePoint::max()), status_(STATUS::NOT_CONNECTED),
114
      running_(false), topicServiceSetupDone(false), parent(p) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

  // ROS Bridge.
  WimaSettings *wimaSettings =
      qgcApp()->toolbox()->settingsManager()->wimaSettings();
  auto connectionStringFact = wimaSettings->rosbridgeConnectionString();
  auto setConnectionString = [connectionStringFact, this] {
    auto connectionString = connectionStringFact->rawValue().toString();
    if (ros_bridge::isValidConnectionString(
            connectionString.toLocal8Bit().data())) {
      this->pRosBridge.reset(
          new ros_bridge::ROSBridge(connectionString.toLocal8Bit().data()));
    } else {
      QString defaultString("localhost:9090");
      qgcApp()->showMessage("ROS Bridge connection string invalid: " +
                            connectionString);
      qgcApp()->showMessage("Resetting connection string to: " + defaultString);
      connectionStringFact->setRawValue(
          QVariant(defaultString)); // calls this function recursively
    }
  };
  connect(connectionStringFact, &SettingsFact::rawValueChanged,
          setConnectionString);
  setConnectionString();

  // Periodic.
Valentin Platzgummer's avatar
Valentin Platzgummer committed
140
  connect(&this->loopTimer, &QTimer::timeout, [this] { this->loop(); });
Valentin Platzgummer's avatar
Valentin Platzgummer committed
141 142 143
  this->loopTimer.start(EVENT_TIMER_INTERVAL);
}

144 145 146 147
void NemoInterface::Impl::start() {
  this->running_ = true;
  emit this->parent->runningChanged();
}
Valentin Platzgummer's avatar
Valentin Platzgummer committed
148

149 150 151 152
void NemoInterface::Impl::stop() {
  this->running_ = false;
  emit this->parent->runningChanged();
}
Valentin Platzgummer's avatar
Valentin Platzgummer committed
153

154 155 156 157 158 159 160 161 162 163 164
void NemoInterface::Impl::setTileData(const TileData &tileData) {
  this->tileData = tileData;
  if (tileData.tiles.count() > 0) {
    std::lock(this->ENUOriginMutex, this->tilesENUMutex);
    UniqueLock lk1(this->ENUOriginMutex, std::adopt_lock);
    UniqueLock lk2(this->tilesENUMutex, std::adopt_lock);

    const auto *obj = tileData.tiles.get(0);
    const auto *tile = qobject_cast<const SnakeTile *>(obj);
    if (tile != nullptr) {
      if (tile->coordinateList().size() > 0) {
165 166 167 168 169 170 171 172 173 174 175 176
        if (tile->coordinateList().first().isValid()) {
          this->ENUOrigin = tile->coordinateList().first();
          const auto &origin = this->ENUOrigin;
          this->tilesENU.polygons().clear();
          for (int i = 0; i < tileData.tiles.count(); ++i) {
            obj = tileData.tiles.get(i);
            tile = qobject_cast<const SnakeTile *>(obj);
            if (tile != nullptr) {
              SnakeTileLocal tileENU;
              snake::areaToEnu(origin, tile->coordinateList(), tileENU.path());
              this->tilesENU.polygons().push_back(std::move(tileENU));
            } else {
177
              qCDebug(NemoInterfaceLog) << "Impl::setTileData(): nullptr.";
178 179
              break;
            }
180
          }
181
        } else {
182
          qCDebug(NemoInterfaceLog) << "Impl::setTileData(): Origin invalid.";
183 184
        }
      } else {
185
        qCDebug(NemoInterfaceLog) << "Impl::setTileData(): tile empty.";
186 187
      }
    }
188 189 190 191 192 193 194 195
  } else {
    this->tileData.clear();

    std::lock(this->ENUOriginMutex, this->tilesENUMutex);
    UniqueLock lk1(this->ENUOriginMutex, std::adopt_lock);
    UniqueLock lk2(this->tilesENUMutex, std::adopt_lock);
    this->ENUOrigin = QGeoCoordinate(0, 0, 0);
    this->tilesENU = SnakeTilesLocal();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
196 197 198
  }
}

199
bool NemoInterface::Impl::hasTileData(const TileData &tileData) const {
200
  return this->tileData == tileData;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
201 202
}

203 204 205 206 207 208 209 210 211 212 213 214
void NemoInterface::Impl::publishTileData() {
  std::lock(this->ENUOriginMutex, this->tilesENUMutex);
  UniqueLock lk1(this->ENUOriginMutex, std::adopt_lock);
  UniqueLock lk2(this->tilesENUMutex, std::adopt_lock);

  if (this->tilesENU.polygons().size() > 0 && this->running_ &&
      this->topicServiceSetupDone) {
    this->publishENUOrigin();
    this->publishTilesENU();
  }
}

215
NemoInterface::STATUS NemoInterface::Impl::status() { return status_.load(); }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
216 217 218

QVector<int> NemoInterface::Impl::progress() {
  SharedLock lk(this->progressMutex);
219
  return this->qProgress.progress();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
220 221
}

222 223
bool NemoInterface::Impl::running() { return this->running_.load(); }

224
void NemoInterface::Impl::doTopicServiceSetup() {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
225 226
  using namespace ros_bridge::messages;

227
  // snake tiles.
Valentin Platzgummer's avatar
Valentin Platzgummer committed
228 229 230 231 232 233 234
  {
    SharedLock lk(this->tilesENUMutex);
    this->pRosBridge->advertiseTopic(
        "/snake/tiles",
        jsk_recognition_msgs::polygon_array::messageType().c_str());
  }

235
  // snake origin.
Valentin Platzgummer's avatar
Valentin Platzgummer committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
  {
    SharedLock lk(this->ENUOriginMutex);
    this->pRosBridge->advertiseTopic(
        "/snake/origin", geographic_msgs::geo_point::messageType().c_str());
  }

  // Subscribe nemo progress.
  this->pRosBridge->subscribe(
      "/nemo/progress",
      /* callback */ [this](JsonDocUPtr pDoc) {
        std::lock(this->progressMutex, this->tilesENUMutex,
                  this->ENUOriginMutex);
        UniqueLock lk1(this->progressMutex, std::adopt_lock);
        UniqueLock lk2(this->tilesENUMutex, std::adopt_lock);
        UniqueLock lk3(this->ENUOriginMutex, std::adopt_lock);

        int requiredSize = this->tilesENU.polygons().size();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
253
        auto &progressMsg = this->qProgress;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
254 255 256 257
        if (!nemo_msgs::progress::fromJson(*pDoc, progressMsg) ||
            progressMsg.progress().size() !=
                requiredSize) { // Some error occured.
          progressMsg.progress().clear();
258
          qgcApp()->showMessage("Invalid progress message received.");
Valentin Platzgummer's avatar
Valentin Platzgummer committed
259
        }
260
        emit this->parent->progressChanged();
261

Valentin Platzgummer's avatar
Valentin Platzgummer committed
262 263 264 265 266 267 268 269 270
        lk1.unlock();
        lk2.unlock();
        lk3.unlock();
      });

  // Subscribe /nemo/heartbeat.
  this->pRosBridge->subscribe(
      "/nemo/heartbeat",
      /* callback */ [this](JsonDocUPtr pDoc) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
271
        //        auto start = std::chrono::high_resolution_clock::now();
272
        nemo_msgs::heartbeat::Heartbeat heartbeatMsg;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
273
        if (!nemo_msgs::heartbeat::fromJson(*pDoc, heartbeatMsg)) {
274
          this->setStatus(STATUS::INVALID_HEARTBEAT);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
275
        } else {
276
          this->setStatus(heartbeatToStatus(heartbeatMsg));
277
        }
278 279
        if (this->status_ == STATUS::INVALID_HEARTBEAT) {
          UniqueLock lk(this->timeoutMutex);
280
          this->nextTimeout = TimePoint::max();
281 282
        } else if (this->status_ == STATUS::HEARTBEAT_DETECTED) {
          UniqueLock lk(this->timeoutMutex);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
283 284 285 286
          this->nextTimeout =
              std::chrono::high_resolution_clock::now() + timeoutInterval;
        }

Valentin Platzgummer's avatar
Valentin Platzgummer committed
287 288 289 290 291 292
        //        auto delta =
        //        std::chrono::duration_cast<std::chrono::milliseconds>(
        //            std::chrono::high_resolution_clock::now() - start);
        //        std::cout << "/nemo/heartbeat callback time: " <<
        //        delta.count() << " ms"
        //                  << std::endl;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
293 294 295 296 297 298 299
      });

  // Advertise /snake/get_origin.
  this->pRosBridge->advertiseService(
      "/snake/get_origin", "snake_msgs/GetOrigin",
      [this](JsonDocUPtr) -> JsonDocUPtr {
        using namespace ros_bridge::messages;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
300
        SharedLock lk(this->ENUOriginMutex);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
301 302 303 304 305 306

        JsonDocUPtr pDoc(
            std::make_unique<rapidjson::Document>(rapidjson::kObjectType));
        auto &origin = this->ENUOrigin;
        rapidjson::Value jOrigin(rapidjson::kObjectType);
        lk.unlock();
307 308 309 310 311 312 313 314 315 316
        if (geographic_msgs::geo_point::toJson(origin, jOrigin,
                                               pDoc->GetAllocator())) {
          lk.unlock();
          pDoc->AddMember("origin", jOrigin, pDoc->GetAllocator());
        } else {
          lk.unlock();
          qCWarning(NemoInterfaceLog)
              << "/snake/get_origin service: could not create json document.";
        }

Valentin Platzgummer's avatar
Valentin Platzgummer committed
317 318 319 320 321 322 323
        return pDoc;
      });

  // Advertise /snake/get_tiles.
  this->pRosBridge->advertiseService(
      "/snake/get_tiles", "snake_msgs/GetTiles",
      [this](JsonDocUPtr) -> JsonDocUPtr {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
324
        SharedLock lk(this->tilesENUMutex);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
325 326 327 328

        JsonDocUPtr pDoc(
            std::make_unique<rapidjson::Document>(rapidjson::kObjectType));
        rapidjson::Value jSnakeTiles(rapidjson::kObjectType);
329 330 331 332 333 334 335 336 337 338 339

        if (jsk_recognition_msgs::polygon_array::toJson(
                this->tilesENU, jSnakeTiles, pDoc->GetAllocator())) {
          lk.unlock();
          pDoc->AddMember("tiles", jSnakeTiles, pDoc->GetAllocator());
        } else {
          lk.unlock();
          qCWarning(NemoInterfaceLog)
              << "/snake/get_tiles service: could not create json document.";
        }

Valentin Platzgummer's avatar
Valentin Platzgummer committed
340 341 342 343 344 345
        return pDoc;
      });
}

void NemoInterface::Impl::loop() {
  // Check ROS Bridge status and do setup if necessary.
346
  if (this->running_) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
347 348
    if (!this->pRosBridge->isRunning()) {
      this->pRosBridge->start();
349
      this->loop();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
350 351
    } else if (this->pRosBridge->isRunning() && this->pRosBridge->connected() &&
               !this->topicServiceSetupDone) {
352 353 354 355
      this->doTopicServiceSetup();
      this->topicServiceSetupDone = true;

      this->setStatus(STATUS::WEBSOCKET_DETECTED);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
356 357 358 359 360
    } else if (this->pRosBridge->isRunning() &&
               !this->pRosBridge->connected() && this->topicServiceSetupDone) {
      this->pRosBridge->reset();
      this->pRosBridge->start();
      this->topicServiceSetupDone = false;
361

362
      this->setStatus(STATUS::TIMEOUT);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
363 364 365 366 367 368 369
    }
  } else if (this->pRosBridge->isRunning()) {
    this->pRosBridge->reset();
    this->topicServiceSetupDone = false;
  }

  // Check if heartbeat timeout occured.
370
  if (this->running_ && this->topicServiceSetupDone) {
371
    UniqueLock lk(this->timeoutMutex);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
372 373
    if (this->nextTimeout != TimePoint::max() &&
        this->nextTimeout < std::chrono::high_resolution_clock::now()) {
374
      lk.unlock();
375
      if (this->pRosBridge->isRunning() && this->pRosBridge->connected()) {
376
        this->setStatus(STATUS::WEBSOCKET_DETECTED);
377
      } else {
378
        this->setStatus(STATUS::TIMEOUT);
379
      }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
380 381 382 383
    }
  }
}

384 385 386 387 388 389 390 391
NemoInterface::STATUS NemoInterface::Impl::heartbeatToStatus(
    const ros_bridge::messages::nemo_msgs::heartbeat::Heartbeat &hb) {
  if (STATUS(hb.status()) == STATUS::HEARTBEAT_DETECTED)
    return STATUS::HEARTBEAT_DETECTED;
  else
    return STATUS::INVALID_HEARTBEAT;
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
392
void NemoInterface::Impl::publishTilesENU() {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
393
  using namespace ros_bridge::messages;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
394 395
  JsonDocUPtr jSnakeTiles(
      std::make_unique<rapidjson::Document>(rapidjson::kObjectType));
396 397 398 399 400 401 402
  if (jsk_recognition_msgs::polygon_array::toJson(
          this->tilesENU, *jSnakeTiles, jSnakeTiles->GetAllocator())) {
    this->pRosBridge->publish(std::move(jSnakeTiles), "/snake/tiles");
  } else {
    qCWarning(NemoInterfaceLog)
        << "Impl::publishTilesENU: could not create json document.";
  }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
403 404 405
}

void NemoInterface::Impl::publishENUOrigin() {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
406
  using namespace ros_bridge::messages;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
407 408
  JsonDocUPtr jOrigin(
      std::make_unique<rapidjson::Document>(rapidjson::kObjectType));
409 410 411 412 413 414 415
  if (geographic_msgs::geo_point::toJson(this->ENUOrigin, *jOrigin,
                                         jOrigin->GetAllocator())) {
    this->pRosBridge->publish(std::move(jOrigin), "/snake/origin");
  } else {
    qCWarning(NemoInterfaceLog)
        << "Impl::publishENUOrigin: could not create json document.";
  }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
416 417
}

418 419 420 421 422 423 424 425 426 427
bool NemoInterface::Impl::setStatus(NemoInterface::STATUS s) {
  if (s != this->status_) {
    this->status_ = s;
    emit this->parent->statusChanged();
    return true;
  } else {
    return false;
  }
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
428 429 430 431 432 433 434
// ===============================================================
// NemoInterface
NemoInterface::NemoInterface(QObject *parent)
    : QObject(parent), pImpl(std::make_unique<NemoInterface::Impl>(this)) {}

NemoInterface::~NemoInterface() {}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
435 436 437 438
void NemoInterface::start() { this->pImpl->start(); }

void NemoInterface::stop() { this->pImpl->stop(); }

439 440 441
void NemoInterface::publishTileData() { this->pImpl->publishTileData(); }

void NemoInterface::requestProgress() {
442
  qCWarning(NemoInterfaceLog) << "requestProgress(): dummy.";
443 444 445
}

void NemoInterface::setTileData(const TileData &tileData) {
446
  this->pImpl->setTileData(tileData);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
447 448
}

449 450
bool NemoInterface::hasTileData(const TileData &tileData) const {
  return this->pImpl->hasTileData(tileData);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
451 452
}

453 454 455
int NemoInterface::status() const { return integral(this->pImpl->status()); }

NemoInterface::STATUS NemoInterface::statusEnum() const {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
456 457 458
  return this->pImpl->status();
}

459 460 461 462
QString NemoInterface::statusString() const {
  return statusMap.at(this->pImpl->status());
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
463
QVector<int> NemoInterface::progress() const { return this->pImpl->progress(); }
464 465 466 467 468 469

QString NemoInterface::editorQml() {
  return QStringLiteral("NemoInterface.qml");
}

bool NemoInterface::running() { return this->pImpl->running(); }