aggiunto topic interface, cambiato modo di subscribe mqtt
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
#include "mqtt_callback.h"
|
||||
|
||||
mqtt_callback::mqtt_callback(mqtt::async_client *cli, std::shared_ptr<spdlog::logger> log) {
|
||||
mqtt_callback::mqtt_callback(mqtt::async_client *cli, std::shared_ptr<spdlog::logger> log, RoboGlue_SHARED *mem) {
|
||||
//initialize client pointer
|
||||
cli_=cli;
|
||||
log_=log;
|
||||
mem_= mem;
|
||||
}
|
||||
|
||||
void mqtt_callback::connected(const mqtt::string &cause) {
|
||||
@@ -17,6 +18,17 @@ void mqtt_callback::connection_lost(const mqtt::string &cause) {
|
||||
void mqtt_callback::message_arrived(mqtt::const_message_ptr msg) {
|
||||
log_->debug("Message Arrived: topic->{} - message->{}",
|
||||
msg->get_topic(), msg->to_string());
|
||||
if(msg->get_topic() == mem_->comSettings.subMap.find("interface")->second) {
|
||||
log_->debug("MQTT: Interface Message: {}", msg->get_payload().c_str());
|
||||
} else if (msg->get_topic() == mem_->comSettings.subMap.find("state")->second){
|
||||
log_->debug("MQTT: State Message: {}", msg->get_payload().c_str());
|
||||
} else if (msg->get_topic() == mem_->comSettings.subMap.find("commands")->second){
|
||||
log_->debug("MQTT: Command Message: {}", msg->get_payload().c_str());
|
||||
} else if (msg->get_topic() == mem_->comSettings.subMap.find("coordinates")->second){
|
||||
log_->debug("MQTT: Coordinate Message: {}", msg->get_payload().c_str());
|
||||
} else {
|
||||
log_->error("Unrecognized topic");
|
||||
}
|
||||
}
|
||||
|
||||
void mqtt_callback::delivery_complete(mqtt::delivery_token_ptr tok) {
|
||||
|
||||
@@ -4,15 +4,17 @@
|
||||
#include <mqtt/client.h>
|
||||
#include <mqtt/callback.h>
|
||||
#include <spdlog/logger.h>
|
||||
#include <shared/roboglue_shared.h>
|
||||
|
||||
class mqtt_callback : public virtual mqtt::callback {
|
||||
|
||||
private:
|
||||
RoboGlue_SHARED *mem_;
|
||||
mqtt::async_client *cli_ = nullptr;
|
||||
std::shared_ptr<spdlog::logger> log_;
|
||||
|
||||
public:
|
||||
mqtt_callback(mqtt::async_client *cli, std::shared_ptr<spdlog::logger> log);
|
||||
mqtt_callback(mqtt::async_client *cli, std::shared_ptr<spdlog::logger> log,RoboGlue_SHARED *m);
|
||||
|
||||
void connected(const mqtt::string &cause) override;
|
||||
void connection_lost(const mqtt::string &cause) override;
|
||||
|
||||
@@ -5,8 +5,8 @@ RoboGlue_COM::RoboGlue_COM (RoboGlue_SHARED *mem): m(mem) {
|
||||
//////// SETUP LOGGER //////////
|
||||
liblog = spdlog::stdout_logger_mt("URcom_liblog");
|
||||
modlog = spdlog::stdout_logger_mt("RoboGlue_comlog");
|
||||
liblog->set_level(spdlog::level::debug);
|
||||
modlog->set_level(spdlog::level::debug);
|
||||
liblog->set_level(spdlog::level::warn);
|
||||
modlog->set_level(spdlog::level::info);
|
||||
|
||||
///////// INITIALIZE TCP SERVER ///////////
|
||||
server = new QTcpServer(this);
|
||||
@@ -16,7 +16,7 @@ RoboGlue_COM::RoboGlue_COM (RoboGlue_SHARED *mem): m(mem) {
|
||||
|
||||
//////// INITIALIZE MQTT CONNECTOR ///////
|
||||
client = new mqtt::async_client(std::string("tcp://localhost:1883"),"");
|
||||
callback = new mqtt_callback(client,modlog);////////////////////////////////////////////////
|
||||
callback = new mqtt_callback(client,modlog,m);////////////////////////////////////////////////
|
||||
//////// END EXTERNAL PUBLIC SLOTS /////////////
|
||||
////////////////////////////////////////////////
|
||||
baseMsg = m->commonSettings.baseMessage;
|
||||
@@ -35,9 +35,8 @@ void RoboGlue_COM::run() {
|
||||
client->set_callback(*callback);
|
||||
client->connect()->wait();
|
||||
// subscrbe to mqtt topics
|
||||
std::vector<std::string> subList = m->comSettings.subList;
|
||||
for (uint i=0; i < subList.size(); i++){
|
||||
client->subscribe(subList[i],0);
|
||||
for (auto it=m->comSettings.subMap.begin(); it != m->comSettings.subMap.end(); ++it){
|
||||
client->subscribe(it->second,0);
|
||||
}
|
||||
client->start_consuming();
|
||||
|
||||
@@ -270,7 +269,7 @@ void RoboGlue_COM::timed_sendCoordinates(){
|
||||
baseMsg["value"]["rx"] = sp[3];
|
||||
baseMsg["value"]["ry"] = sp[4];
|
||||
baseMsg["value"]["rz"] = sp[5];
|
||||
MQTTpublish(baseMsg, m->comSettings.pubList[m->comSettings.COORD]);
|
||||
MQTTpublish(baseMsg, m->comSettings.pubMap.find("coordinates")->second);
|
||||
}
|
||||
}
|
||||
////////////////////////////////////////////////
|
||||
@@ -329,7 +328,7 @@ void RoboGlue_COM::on_sendROScommand(QString command, QVariantMap params) {
|
||||
baseMsg["command"] = command.toStdString();
|
||||
QJsonDocument jparam = QJsonDocument(QJsonObject::fromVariantMap(params));
|
||||
baseMsg["params"] = json::parse(jparam.toJson().data());
|
||||
MQTTpublish(baseMsg, m->comSettings.pubList[m->comSettings.COMM]);
|
||||
MQTTpublish(baseMsg, m->comSettings.pubMap.find("commands")->second);
|
||||
}
|
||||
|
||||
void RoboGlue_COM::on_sendROScoordinates(QList<double> sp) {
|
||||
@@ -350,41 +349,3 @@ void RoboGlue_COM::on_sendROSstate(QMap<std::string,bool> digital, QMap<std::str
|
||||
////////////////////////////////////////////////
|
||||
//////// END EXTERNAL PUBLIC SLOTS /////////////
|
||||
////////////////////////////////////////////////
|
||||
//void RoboGlue_COM::on_sendSetpointOLD(QList<double> sp) {
|
||||
// std::string setpoint;
|
||||
// struct {
|
||||
// union{
|
||||
// struct{
|
||||
// uint32_t x,y,z,a,b,c,keepalive;
|
||||
// };
|
||||
// char bytes[28];
|
||||
// };
|
||||
// } setPointBytes;
|
||||
// setPointBytes.keepalive=1;
|
||||
// int32_t v;
|
||||
// setpoint = "(";
|
||||
// for(uchar i=0; i<JOINT_NUMBER; i++){
|
||||
// v=static_cast<int32_t>(sp[i]*1000);
|
||||
// if (i>2) v=boost::algorithm::clamp(v, -2680,2680);
|
||||
// setpoint.append(std::to_string(v));
|
||||
// boost::algorithm::replace_all(setpoint, ",",".");
|
||||
// if(i<JOINT_NUMBER) setpoint.append("|");
|
||||
// }
|
||||
// setpoint.append("1");
|
||||
// boost::algorithm::replace_all(setpoint,"|",",");
|
||||
// setpoint.append(")");
|
||||
|
||||
// setPointBytes.x=htonl(static_cast<uint32_t>(sp[0]*1000));
|
||||
// setPointBytes.y=htonl(static_cast<uint32_t>(sp[1]*1000));
|
||||
// setPointBytes.z=htonl(static_cast<uint32_t>(sp[2]*1000));
|
||||
// setPointBytes.a=htonl(static_cast<uint32_t>(sp[3]*1000));
|
||||
// setPointBytes.b=htonl(static_cast<uint32_t>(sp[4]*1000));
|
||||
// setPointBytes.c=htonl(static_cast<uint32_t>(sp[5]*1000));
|
||||
// setPointBytes.keepalive=htonl(setPointBytes.keepalive);
|
||||
|
||||
// if(robotIN->write(setPointBytes.bytes,28))
|
||||
// modlog->trace("Sent Setpoint {}",setpoint.c_str());
|
||||
// robotIN->flush();
|
||||
//}
|
||||
|
||||
|
||||
|
||||
@@ -44,7 +44,8 @@
|
||||
* della coda di destinazione.
|
||||
* 20190311 - La pubblicazione dei messaggi di coordinate verso ros deve essere a rateo costante
|
||||
* indipendentemente dalla frequenza di ingresso
|
||||
*/
|
||||
* 20191030 - Spostata la documentazione su GIT
|
||||
*/
|
||||
|
||||
#include <QObject>
|
||||
#include <QThread>
|
||||
|
||||
Reference in New Issue
Block a user