已为我提供了使用QT Creator创建的GUI,该GUI可以读取和更新四足机器人的扭矩。但是,我需要能够通过发布者将该扭矩变量发布给ros,以便可以在控制器中使用它。
我是ROS,QT和C ++的新手。
我尝试查看ros_qt,但不确定这是否对我的代码有效。
main.cpp
#include "mainwindow.hh
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.hh
#include "ServoTable.hh"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void timerEvent(QTimerEvent *event) override;
.....
(这是我要发布的扭矩值的地方(随附图片)
ServoTable.hh
#include <QAbstractTableModel>
#include "dogbot/Servo.hh"
class ServoTable
: public QAbstractTableModel
{
QVariant data(const QModelIndex &index, int role) const override;
....
private slots:
//! Queue an update for a row
void queueDataUpdate();
...
};
ServoTable.cc
#include "ServoTable.hh"
....
QVariant ServoTable::data(const QModelIndex &index, int role)
const
{
auto it = m_row2joint.find(index.row());
if(it == m_row2joint.end()) {
return QVariant(); // Doesn't seem to exist.
}
const DogBotN::JointC *joint = it->second;
const DogBotN::ServoC *servo = dynamic_cast<const DogBotN::ServoC *>(joint);
if (role == Qt::DisplayRole) {
switch(index.column())
{
case ColumnTorque: {
QString numStr;
numStr.setNum(joint->Torque(),'f',2);
return numStr;
}
}
return QVariant();
}
function:Torque()在另一个文件中定义
float Torque() const
{ return m_torque; }
基本上从另一个类返回一个私有变量
我尝试的发布者:
#include "GUI/Servo.hh"
#include "std_msgs/String.h"
#include <ros/ros.h>
#include <sstream>
int main(int argc, char **argv)
{
ros::init(argc, argv, "talker");
ros::NodeHandle n;
ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);
ros::Rate loop_rate(10);
int count = 0;
while (ros::ok())
{
std_msgs::String msg;
std::stringstream ss;
ss << DogBotN::JointC::Torque() << count;
msg.data = ss.str();
ROS_INFO("%s", msg.data.c_str());
chatter_pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
++count;
}
return 0;
}
我得到的错误是DogBotN :: JointC :: Torque()无法在没有对象的情况下调用。我不能只从负责更新的GUI线程中读取它吗?