snap7/Qt/ros-------ubantu14.04下杂记
1.snap7在qt中使用:
類似Windows下配置類似
#------------------------------------------------- # # Project created by QtCreator 2019-08-17T14:12:19 # #-------------------------------------------------QT += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = snap7_c TEMPLATE = app# The following define makes your compiler emit warnings if you use # any feature of Qt which has been marked as deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS# You can also make your code fail to compile if you use deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0INCLUDEPATH += ../snap7-full-1.4.1/release/Wrappers/c-cpp/ \../snap7-full-1.4.1/src/lib/ \../snap7-full-1.4.1/src/core/ \../snap7-full-1.4.1/src/sys/ SOURCES += \main.cpp \mainwindow.cpp \../snap7-full-1.4.1/release/Wrappers/c-cpp/snap7.cpp \../snap7-full-1.4.1/src/core/s7_client.cpp \../snap7-full-1.4.1/src/core/s7_isotcp.cpp \../snap7-full-1.4.1/src/core/s7_micro_client.cpp \../snap7-full-1.4.1/src/core/s7_partner.cpp \../snap7-full-1.4.1/src/core/s7_peer.cpp \../snap7-full-1.4.1/src/core/s7_server.cpp \../snap7-full-1.4.1/src/core/s7_text.cpp \../snap7-full-1.4.1/src/lib/snap7_libmain.cpp \../snap7-full-1.4.1/src/sys/snap_msgsock.cpp \../snap7-full-1.4.1/src/sys/snap_sysutils.cpp \../snap7-full-1.4.1/src/sys/snap_tcpsrvr.cpp \../snap7-full-1.4.1/src/sys/snap_threads.cpp \joythread.cppHEADERS += \mainwindow.h \../snap7-full-1.4.1/release/Wrappers/c-cpp/snap7.h \../snap7-full-1.4.1/src/core/s7_client.h \../snap7-full-1.4.1/src/core/s7_firmware.h \../snap7-full-1.4.1/src/core/s7_isotcp.h \../snap7-full-1.4.1/src/core/s7_micro_client.h \../snap7-full-1.4.1/src/core/s7_partner.h \../snap7-full-1.4.1/src/core/s7_peer.h \../snap7-full-1.4.1/src/core/s7_server.h \../snap7-full-1.4.1/src/core/s7_text.h \../snap7-full-1.4.1/src/core/s7_types.h \../snap7-full-1.4.1/src/lib/snap7_libmain.h \../snap7-full-1.4.1/src/sys/snap_msgsock.h \../snap7-full-1.4.1/src/sys/snap_platform.h \../snap7-full-1.4.1/src/sys/snap_sysutils.h \../snap7-full-1.4.1/src/sys/snap_tcpsrvr.h \../snap7-full-1.4.1/src/sys/snap_threads.h \../snap7-full-1.4.1/src/sys/sol_threads.h \../snap7-full-1.4.1/src/sys/unix_threads.h \../snap7-full-1.4.1/src/sys/win_threads.h \joythread.hCONFIG +=C++11FORMS += \mainwindow.uiDISTFILES += \../snap7-full-1.4.1/release/Linux/X86_64/glibc_2.21/libsnap7.soQMAKE_CXXFLAGS += -Wno-unused-parameterQMAKE_CXXFLAGS += -Wno-unused-variable在qt的main.cpp測試如下:并連接PLC,通過測試,讀寫的時(shí)候和python接口有點(diǎn)區(qū)別。
#include "mainwindow.h" #include <QApplication>#include <QString> #include <QDebug> #include "snap7.h"int main(int argc, char *argv[]) {QApplication a(argc, argv);MainWindow w;w.show();// //test // TS7Client *MyS7Client=new TS7Client(); // QString ip="192.168.0.2"; // MyS7Client->ConnectTo(ip.toStdString().c_str(),0,1); // if(!MyS7Client->Connected()) // { // qDebug()<<"if=false: not connected "; // } // else // { // qDebug()<<"if=true: is connected"; // } // //int readint; // // bool readbool; // // byte DB_buffer[200]; // // int res; // // res=MyS7Client->DBRead(1,0,30,&DB_buffer); // // if(res!=0) // // { // // qDebug()<<"error read from DB"<<"1"<<QString::number(res); // // } // // qDebug()<<"read from DB:1 "<<QString::number(res); // // byte value; // // byte value2; // // MyS7Client->DBRead(1,8,1,&value);//1 8 // // qDebug()<<"read from DB is"<<value;// // value2=1; // // MyS7Client->DBWrite(1,8,1,&value2);// // MyS7Client->DBRead(1,8,1,&value);//1 8 // // qDebug()<<"read from DB is"<<value;// byte value; // byte value2;// //int write // MyS7Client->DBRead(1,2,1,&value); // qDebug()<<"read from DB is"<<value; // MyS7Client->DBRead(1,3,1,&value); // qDebug()<<"read from DB is"<<value;// int abb; // abb=23453;// value=abb>>8; //hight // value2=(abb&255); //low// MyS7Client->DBWrite(1,2,1,&value); // MyS7Client->DBWrite(1,3,1,&value2);// MyS7Client->DBRead(1,2,1,&value); // qDebug()<<"read from DB is"<<value; // MyS7Client->DBRead(1,3,1,&value); // qDebug()<<"read from DB is"<<value;// //bool write // value=22; // MyS7Client->DBWrite(1,0,1,&value);// value=3; // MyS7Client->DBWrite(1,8,1,&value);return a.exec(); }2.snap7在ros中使用
類似ros調(diào)用第三方庫,在snap7的包找到bulid文件夾,然后選擇linux,進(jìn)去后而根據(jù)自己的系統(tǒng)選擇make文件進(jìn)行make。兩句make -f all? 和make install。具體可見readme文件。生成的.so文件在build/bin下對應(yīng)的問價(jià)夾下。
然后在新建的rospakage包的include文件下包含類似QT的頭文件,在package下再創(chuàng)建lib文件夾,把剛才生成的.so文件放lib下。
?
類似這樣,其實(shí)只需要把snap7.c和.h文件放進(jìn)去,這是我嘗試時(shí)候加的,懶得改了
具體代碼,我寫在control.cpp中:c中的讀和pythoy類似,但是寫的時(shí)候方便一些,可以直接寫一個(gè)八位以上的十六進(jìn)制數(shù),書寫順序也更加人性化,不用高位和地位顛倒。
#include "snap7.h" #include "ros/ros.h" #include "msgbag/joy_msg.h" #include <string.h> #include "snap7.cpp" #include <stdio.h> using namespace std;TS7Client plc;void chatterCallback(const msgbag::joy_msg::ConstPtr& msg) {// 將接收到的消息打印出來// ROS_INFO("%d %d %d %d %d %d %d %d %d %d %d %d %d",msg->A_botton,msg->B_botton,msg->X_botton,msg->Y_botton,msg->LB_botton,msg->RB_botton,// msg->LT_botton,msg->RT_botton,msg->X_Axis,msg->Y_Axis,msg->U_Axis,msg->R_Axis,msg->Z_Axis);//ROS_INFO("%d %d %d %d %d ",msg->A_botton,msg->B_botton,msg->X_botton,msg->Y_botton,msg->X_Axis); // int a; // int* p=&a; // if(msg->A_botton==1) // { // a=1; // plc.AsDBRead(1, 0, 1, p); // } // int AsDBRead(int DBNumber, int Start, int Size, void * pUsrData); // int AsDBWrite(int DBNumber, int Start, int Size, void * pUsrData); int buff[8]={0}; if(msg->A_botton==1 && msg->X_botton==1) { buff[1]=5; plc.DBWrite(2,0,1,buff+1); //plc.DBRead(1,0,1,buff[1]); // ROS_INFO("%d %d %d %d %d %d %d %d",buff[0],buff[1],buff[2],buff[3],buff[4],buff[5],buff[6],buff // [7]); }else if(msg->A_botton==0 && msg->X_botton==1) { buff[1]=4; plc.DBWrite(2,0,1,buff+1); // ROS_INFO("%d %d %d %d %d %d %d %d",buff[0],buff[1],buff[2],buff[3],buff[4],buff[5],buff[6],buff // [7]); } else if(msg->A_botton==1) {buff[1]=1;plc.DBWrite(2,0,1,buff+1); } else {buff[1]=0;plc.DBWrite(2,0,1,buff+1); }//backward spin if(msg->B_botton==1 && msg->X_botton==1) { buff[1]=6; plc.DBWrite(2,0,1,buff+1); //plc.DBRead(1,0,1,buff[1]); // ROS_INFO("%d %d %d %d %d %d %d %d",buff[0],buff[1],buff[2],buff[3],buff[4],buff[5],buff[6],buff // [7]); }else if(msg->B_botton==0 && msg->X_botton==1) { buff[1]=4; plc.DBWrite(2,0,1,buff+1); // ROS_INFO("%d %d %d %d %d %d %d %d",buff[0],buff[1],buff[2],buff[3],buff[4],buff[5],buff[6],buff // [7]); } else if(msg->B_botton==1) {buff[1]=2;plc.DBWrite(2,0,1,buff+1); } else {buff[1]=0;plc.DBWrite(2,0,1,buff+1); }//-32768-32767 if(msg->X_Axis!=0) {buff[1]=msg->X_Axis; plc.DBWrite(2,2,2,buff+1); //plc.DBRead(1,2,1,buff); // ROS_INFO("%d %d %d %d %d %d %d %d",buff[0],buff[1],buff[2],buff[3],buff[4],buff[5],buff[6],buff // [7]); }else {buff[1]=0; plc.DBWrite(2,2,2,buff+1); }// if(msg->Y_botton==1) // { // buff[1]=5; // plc.DBWrite(1,3,1,buff+1); // //plc.DBRead(1,3,1,buff); // // ROS_INFO("%d %d %d %d %d %d %d %d",buff[0],buff[1],buff[2],buff[3],buff[4],buff[5],buff[6],buff // // [7]); // }}int main(int argc, char *argv[]) {string ip="192.168.1.1";plc.ConnectTo(ip.c_str(),0,1);if (!plc.Connected()){printf("PLC not connect successful");}else{printf("PLC connect successful");}// 初始化ROS節(jié)點(diǎn)ros::init(argc, argv, "listener");// 創(chuàng)建節(jié)點(diǎn)句柄ros::NodeHandle n;// 創(chuàng)建一個(gè)Subscriber,訂閱名為chatter的topic,注冊回調(diào)函數(shù)chatterCallbackros::Subscriber sub = n.subscribe("/joymsg_topic", 1000, chatterCallback);// 循環(huán)等待回調(diào)函數(shù)ros::spin();return 0; }這里需要提醒:需要include snap7.c文件,否則報(bào)錯(cuò)。這個(gè)問題很奇怪,我研究了好久才發(fā)現(xiàn),一開始一直以為是庫的版本不對,或者庫錯(cuò)了。其實(shí)報(bào)錯(cuò)的就是:調(diào)用構(gòu)造函數(shù)時(shí)候,默認(rèn)的構(gòu)造函數(shù)沒有參數(shù),他會(huì)提醒沒有定義的構(gòu)造函數(shù)。我查了好久,也沒搞明白,外網(wǎng)一個(gè)哥們,也是只知道解決辦法,但是不知道為什么,C++原理應(yīng)該沒錯(cuò),可能是ros不支持默認(rèn)無參數(shù)的構(gòu)造函數(shù)吧。
3.在qt中使用ros
基本上我看到的比較簡單的是catkin_creat_qt_pkg創(chuàng)建ros包,然后再在qt里面創(chuàng)建個(gè)項(xiàng)目,再把ros包的四個(gè)目錄包含進(jìn)來,在修改pro文件。增加下面配置:
INCLUDEPATH += /opt/ros/indigo/includeDEPENDPATH += /opt/ros/indigo/includeINCLUDEPATH += /home/ys/catkin_ws/devel/includeLIBS += -l:/usr/lib/x86_64-linux-gnu/libboost_thread.soLIBS += -l:/usr/lib/x86_64-linux-gnu/libpthread.soLIBS += -L/opt/ros/indigo/lib -lroscpp -lrospack -lpthread -lrosconsole -lrosconsole_log4cxx -lrosconsole_backend_interface -lxmlrpcpp -lroscpp_serialization -lrostime -lcpp_common -lroslib -ltf -lyaml-cpp -lkdl_conversionsLIBS += -l:/opt/ros/indigo/lib/libroslib.soCONFIG +=C++11?
再刪除創(chuàng)建qt所產(chǎn)生的幾個(gè)文件,其實(shí)就是用catkin_creat_qt_pkg創(chuàng)建ros包代替qt生成的文件,用qt的pro文件代替CMakeList文件和packxml。
代碼結(jié)果如下
不過這個(gè)自帶ros節(jié)點(diǎn)發(fā)布者,我正常寫了個(gè)訂閱著,可是收不到消息,但是ros的rqt_graph可以看到節(jié)點(diǎn)已經(jīng)鏈接成功的。
然后我自己沒用catkin_creat_qt_pkg。直接在配置好的qt的main.cpp文件寫訂閱者,再寫個(gè)發(fā)布者。這里需要提醒,要先開roscore,并且是通過終端開啟的qtcreator_ros。這樣才可以實(shí)現(xiàn)通訊,否則報(bào)錯(cuò)找不到roscore。成功實(shí)現(xiàn)通訊,但是顯示的差距很大,終端hello 100,qt中hello50左右。
未知問題還有很多,先記錄下。
代碼詳細(xì)見? 資源處:linux/ros/qt/snap7.tar.gz
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的snap7/Qt/ros-------ubantu14.04下杂记的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CentOS6.4安装Vbox增强包
- 下一篇: MSP430F5529 DriverLi