奥特QT DDS 插件库
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

100 řádky
2.5KB

  1. #include "AQTPackageMachinePlug.hpp"
  2. #include <QDebug>
  3. #include <fastdds/dds/domain/DomainParticipantFactory.hpp>
  4. #include "HelloWorld.hpp"
  5. #include "HelloWorldPubSubTypes.hpp"
  6. AQTPackageMachinePlug::AQTPackageMachinePlug(QObject *parent)
  7. : QObject(parent)
  8. , participant_(nullptr)
  9. , publisher_(nullptr)
  10. , topic_(nullptr)
  11. , writer_(nullptr)
  12. , message_index_(0)
  13. {
  14. type_ = TypeSupport(new HelloWorldPubSubType());
  15. }
  16. AQTPackageMachinePlug::~AQTPackageMachinePlug()
  17. {
  18. if (writer_ != nullptr) {
  19. publisher_->delete_datawriter(writer_);
  20. }
  21. if (topic_ != nullptr) {
  22. participant_->delete_topic(topic_);
  23. }
  24. if (publisher_ != nullptr) {
  25. participant_->delete_publisher(publisher_);
  26. }
  27. if (participant_ != nullptr) {
  28. DomainParticipantFactory::get_instance()->delete_participant(participant_);
  29. }
  30. }
  31. bool AQTPackageMachinePlug::init()
  32. {
  33. DomainParticipantQos participantQos;
  34. participantQos.name("Participant_publisher");
  35. participant_ = DomainParticipantFactory::get_instance()->create_participant(0, participantQos);
  36. if (participant_ == nullptr) {
  37. qDebug() << "Error creating participant";
  38. return false;
  39. }
  40. type_.register_type(participant_);
  41. publisher_ = participant_->create_publisher(PUBLISHER_QOS_DEFAULT);
  42. if (publisher_ == nullptr) {
  43. qDebug() << "Error creating publisher";
  44. return false;
  45. }
  46. topic_ = participant_->create_topic(
  47. "HelloWorldTopic",
  48. "HelloWorld",
  49. TOPIC_QOS_DEFAULT);
  50. if (topic_ == nullptr) {
  51. qDebug() << "Error creating topic";
  52. return false;
  53. }
  54. writer_ = publisher_->create_datawriter(
  55. topic_,
  56. DATAWRITER_QOS_DEFAULT);
  57. if (writer_ == nullptr) {
  58. qDebug() << "Error creating data writer";
  59. return false;
  60. }
  61. qDebug() << "DDS initialization completed";
  62. return true;
  63. }
  64. void AQTPackageMachinePlug::publishOnce()
  65. {
  66. HelloWorld st;
  67. st.index(message_index_);
  68. st.message("Hello World " + std::to_string(message_index_));
  69. if (writer_ != nullptr) {
  70. writer_->write(&st);
  71. emit messagePublished(QString::fromStdString(st.message()), message_index_);
  72. } else {
  73. qDebug() << "DataWriter not initialized";
  74. }
  75. message_index_++;
  76. }
  77. QString AQTPackageMachinePlug::echo(const QString& message)
  78. {
  79. return QString("Plugin Echo: %1").arg(message);
  80. }
  81. int AQTPackageMachinePlug::calculate(int a, int b)
  82. {
  83. return a + b;
  84. }