I need a serial port for read camera, and other serial port for write control command.
Now I can read frame, but can't write command to serial port.
And the error: QSocketNotifier: Can only be used with threads started with QThread.


ViSP and UEyeOpenCV(camera) are Third-party libraries.
Qt Code:
  1. /*****main.cpp*****/
  2. #include <iostream>
  3. #include "UEyeOpenCV.hpp"
  4. #include "opencv2/opencv.hpp"
  5. #include <servocontroller.h>
  6.  
  7. #include <visp/vpFeatureBuilder.h>
  8. #include <visp/vpFeatureDepth.h>
  9. #include <visp/vpFeaturePoint.h>
  10. #include <visp/vpHomogeneousMatrix.h>
  11. #include <visp/vpPlot.h>
  12. #include <visp/vpServo.h>
  13. #include <visp/vpVelocityTwistMatrix.h>
  14. #include <visp/vpDot2.h>
  15. #include <visp/vpDisplay.h>
  16. #include <visp/vpImageIo.h>
  17. #include <visp/vpImageTools.h>
  18. #include <visp/vpDisplayOpenCV.h>
  19. #include <visp/vpConfig.h>
  20. #include <visp/vpRobotCamera.h>
  21.  
  22. int main(){
  23. /* set undistort */
  24. cv::FileStorage fs("out_camera_data.xml", cv::FileStorage::READ);
  25. cv::Mat cameraMatrix, distCoeffs;
  26. fs["Camera_Matrix"] >> cameraMatrix;
  27. fs["Distortion_Coefficients"] >> distCoeffs;
  28. std::cout << "camera matrix: " << cameraMatrix << std::endl
  29. << "distortion coeffs: " << distCoeffs << std::endl;
  30. cv::Mat image;
  31.  
  32. /* open camera */
  33. UeyeOpencvCam cam = UeyeOpencvCam(752,480);
  34.  
  35. /* define a wondow of image*/
  36. vpImage<unsigned char> I(480, 752); // Create a gray level image
  37. cv::Mat grayFrame;
  38. vpDisplayOpenCV d;
  39. d.init(I, 0, 0, "Visual servoing on dot");
  40.  
  41. /* set dot*/
  42. vpDot2 blob;
  43. blob.setGraphics(true);
  44. blob.setGraphicsThickness(2);
  45. vpImagePoint germ;
  46. bool init_done = false;
  47.  
  48. /* set com*/
  49. unsigned short zoom=1500;
  50. ServoController com;
  51. com.ServoController::createSerialPort();
  52. com.setTarget(zoom*4);
  53.  
  54. while(true) {
  55. cv::Mat frame= cam.getFrame();
  56. cv::cvtColor(frame,grayFrame, CV_BGR2GRAY);
  57. cv::undistort(grayFrame,image,cameraMatrix,distCoeffs);
  58. cv::Mat image2;
  59. cv::flip(image,image2,-1);
  60. vpImageConvert::convert(image2, I);
  61. vpDisplay::display(I);
  62.  
  63. if (! init_done) {
  64. vpDisplay::displayText(I, vpImagePoint(10,10), "Click in the blob to initialize the tracker", vpColor::red);
  65. if (vpDisplay::getClick(I, germ, false)) {
  66. blob.initTracking(I, germ);
  67. init_done = true;
  68. }
  69. }
  70. else {
  71. blob.track(I);
  72. vpImagePoint mcog;
  73. mcog=blob.getCog();
  74. int x=mcog.get_i();
  75. if (x<240){
  76. zoom=zoom+10;
  77. com.setTarget(zoom*4);
  78. cv::waitKey(33);
  79. }
  80. else{
  81. zoom=zoom-10;
  82. com.setTarget(zoom*4);
  83. cv::waitKey(33);
  84. }
  85. }
  86. vpDisplay::flush(I);
  87. }
  88. }
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. //servocontroller.h
  2. /*****servocontroller.h*****/
  3. #ifndef SERVOCONTROLLER_H
  4. #define SERVOCONTROLLER_H
  5.  
  6. #include <string>
  7. #include <QtSerialPort/QSerialPort>
  8. #include <QtSerialPort/QSerialPortInfo>
  9. #include <termios.h>
  10. #include <QDebug>
  11. #include <QString>
  12.  
  13. class ServoController
  14. {
  15. public:
  16. virtual ~ServoController();
  17. void createSerialPort();
  18. void setTarget( unsigned short target );
  19.  
  20. private:
  21. static const unsigned short mMinChannelValue = 4000;
  22. static const unsigned short mMaxChannelValue = 8000;
  23. unsigned short target;
  24. unsigned char channelNumber;
  25. QSerialPort* myCom;
  26. QByteArray command;
  27. };
  28.  
  29. #endif // SERVOCONTROLLER_H
  30.  
  31.  
  32.  
  33. /*****servocontroller.cpp*****/
  34. #include "servocontroller.h"
  35. #include <QtSerialPort/QSerialPort>
  36. #include <QtSerialPort/QSerialPortInfo>
  37. #include <iostream>
  38.  
  39. void ServoController::createSerialPort()
  40. {
  41. myCom= new QSerialPort();
  42. myCom->setPortName("/dev/ttyACM0");
  43. myCom->open(QIODevice::WriteOnly);
  44. myCom->setBaudRate(9600);
  45. myCom->setDataBits(QSerialPort::Data8);
  46. myCom->setParity(QSerialPort::NoParity);
  47. myCom->setStopBits(QSerialPort::OneStop);
  48. myCom->setFlowControl(QSerialPort::NoFlowControl);
  49. }
  50.  
  51. ServoController::~ServoController()
  52. {
  53. myCom->close();
  54. }
  55.  
  56. void ServoController::setTarget(unsigned short target )
  57. {
  58. if (target<mMinChannelValue ){
  59. target=mMinChannelValue;
  60. }
  61. if(target>mMaxChannelValue){
  62. target=mMaxChannelValue;
  63. }
  64.  
  65. command.resize(4);
  66. command[0]=0x84;
  67. command[1]=0x01;
  68. command[2]=(char)(target & 0x7F);
  69. command[3]=(char)((target >> 7) & 0x7F);
  70. myCom-> QSerialPort::write(command);
  71. }
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. HEADERS += \
  2. UEyeOpenCV.hpp \
  3. servocontroller.h
  4.  
  5. SOURCES += \
  6. main.cpp \
  7. ueyeopencv.cpp \
  8. servocontroller.cpp
  9.  
  10. # important
  11. # library of QSerialport
  12. QT += serialport
  13.  
  14. # library of OpenCV
  15. INCLUDEPATH += /usr/local/include \
  16. /usr/local/include/opencv \
  17. /usr/local/include/opencv2
  18. LIBS += /usr/local/lib/libopencv_core.so \
  19. /usr/local/lib/libopencv_highgui.so \
  20. /usr/local/lib/libopencv_imgproc.so
  21.  
  22. # library of UEye
  23. win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../../usr/lib/release/ -lueye_api
  24. else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../../usr/lib/debug/ -lueye_api
  25. else:unix: LIBS += -L$$PWD/../../../../usr/lib/ -lueye_api
  26. INCLUDEPATH += $$PWD/../../../../usr/include
  27. DEPENDPATH += $$PWD/../../../../usr/include
  28.  
  29. # libreria de ViSP
  30. win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../ViSP-build-release/lib/x86_64-linux-gnu/release/ -lvisp
  31. else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../ViSP-build-release/lib/x86_64-linux-gnu/debug/ -lvisp
  32. else:unix: LIBS += -L$$PWD/../../ViSP-build-release/lib/x86_64-linux-gnu/ -lvisp
  33. INCLUDEPATH += $$PWD/../../ViSP-build-release/lib/x86_64-linux-gnu
  34. DEPENDPATH += $$PWD/../../ViSP-build-release/lib/x86_64-linux-gnu
To copy to clipboard, switch view to plain text mode