1 #ifndef __ddLCMSubscriber_h
2 #define __ddLCMSubscriber_h
11 #include <QMutexLocker>
12 #include <QWaitCondition>
15 #include <lcm/lcm-cpp.hpp>
35 this->mEmitMessages =
true;
36 this->mNotifyAllMessages =
false;
37 this->mRequiredElapsedMilliseconds = 0;
38 this->connect(
this, SIGNAL(messageReceivedInQueue(
const QString&)), SLOT(onMessageInQueue(
const QString&)));
52 lcmHandle->unsubscribe(mSubscription);
63 this->mEmitMessages = enabled;
68 return this->mEmitMessages;
74 this->mNotifyAllMessages = enabled;
88 return this->mNotifyAllMessages;
95 this->mRequiredElapsedMilliseconds = 0;
99 this->mRequiredElapsedMilliseconds =
static_cast<int>(1000.0 / hertz);
105 return this->mFPSCounter.averageFPS();
111 QMutexLocker locker(&this->mMutex);
113 QByteArray msg = this->mLastMessage;
114 this->mLastMessage.clear();
122 bool haveNewMessage = this->mWaitCondition.wait(&this->mMutex, timeout);
129 msg = this->mLastMessage;
130 this->mLastMessage.clear();
137 void messageReceived(
const QByteArray& messageData,
const QString& channel);
138 void messageReceivedInQueue(
const QString& channel);
144 QByteArray msg = this->getNextMessage(0);
145 emit this->messageReceived(msg, channel);
156 QByteArray messageBytes = QByteArray((
char*)rbuf->data, rbuf->data_size);
158 mFPSCounter.update();
160 if (this->mEmitMessages)
162 if (this->mRequiredElapsedMilliseconds == 0 || mTimer.elapsed() > this->mRequiredElapsedMilliseconds)
164 this->mTimer.restart();
166 if (this->mNotifyAllMessages)
168 emit this->messageReceived(messageBytes, QString(channel.c_str()));
173 bool doEmit = !this->mLastMessage.size();
174 this->mLastMessage = messageBytes;
175 this->mMutex.unlock();
179 emit this->messageReceivedInQueue(QString(channel.c_str()));
188 this->mLastMessage = messageBytes;
189 this->mMutex.unlock();
190 this->mWaitCondition.wakeAll();
QByteArray getNextMessage(int timeout)
void messageHandler(const lcm::ReceiveBuffer *rbuf, const std::string &channel)
void setSpeedLimit(double hertz)
void setNotifyAllMessagesEnabled(bool enabled)
virtual void unsubscribe(lcm::LCM *lcmHandle)
bool callbackIsEnabled() const
const QString & channel() const
virtual void subscribe(lcm::LCM *lcmHandle)
lcm::Subscription * mSubscription
void setCallbackEnabled(bool enabled)
QWaitCondition mWaitCondition
virtual ~ddLCMSubscriber()
bool notifyAllMessagesIsEnabled() const
ddLCMSubscriber(const QString &channel, QObject *parent=NULL)
int mRequiredElapsedMilliseconds
void onMessageInQueue(const QString &channel)