/*****main.cpp*****/
#include <iostream>
#include "UEyeOpenCV.hpp"
#include "opencv2/opencv.hpp"
#include <servocontroller.h>
#include <visp/vpFeatureBuilder.h>
#include <visp/vpFeatureDepth.h>
#include <visp/vpFeaturePoint.h>
#include <visp/vpHomogeneousMatrix.h>
#include <visp/vpPlot.h>
#include <visp/vpServo.h>
#include <visp/vpVelocityTwistMatrix.h>
#include <visp/vpDot2.h>
#include <visp/vpDisplay.h>
#include <visp/vpImageIo.h>
#include <visp/vpImageTools.h>
#include <visp/vpDisplayOpenCV.h>
#include <visp/vpConfig.h>
#include <visp/vpRobotCamera.h>
int main(){
/* set undistort */
cv::FileStorage fs("out_camera_data.xml", cv::FileStorage::READ);
cv::Mat cameraMatrix, distCoeffs;
fs["Camera_Matrix"] >> cameraMatrix;
fs["Distortion_Coefficients"] >> distCoeffs;
std::cout << "camera matrix: " << cameraMatrix << std::endl
<< "distortion coeffs: " << distCoeffs << std::endl;
cv::Mat image;
/* open camera */
UeyeOpencvCam cam = UeyeOpencvCam(752,480);
/* define a wondow of image*/
vpImage<unsigned char> I(480, 752); // Create a gray level image
cv::Mat grayFrame;
vpDisplayOpenCV d;
d.init(I, 0, 0, "Visual servoing on dot");
/* set dot*/
vpDot2 blob;
blob.setGraphics(true);
blob.setGraphicsThickness(2);
vpImagePoint germ;
bool init_done = false;
/* set com*/
unsigned short zoom=1500;
ServoController com;
com.ServoController::createSerialPort();
com.setTarget(zoom*4);
while(true) {
cv::Mat frame= cam.getFrame();
cv::cvtColor(frame,grayFrame, CV_BGR2GRAY);
cv::undistort(grayFrame,image,cameraMatrix,distCoeffs);
cv::Mat image2;
cv::flip(image,image2,-1);
vpImageConvert::convert(image2, I);
vpDisplay::display(I);
if (! init_done) {
vpDisplay::displayText(I, vpImagePoint(10,10), "Click in the blob to initialize the tracker", vpColor::red);
if (vpDisplay::getClick(I, germ, false)) {
blob.initTracking(I, germ);
init_done = true;
}
}
else {
blob.track(I);
vpImagePoint mcog;
mcog=blob.getCog();
int x=mcog.get_i();
if (x<240){
zoom=zoom+10;
com.setTarget(zoom*4);
cv::waitKey(33);
}
else{
zoom=zoom-10;
com.setTarget(zoom*4);
cv::waitKey(33);
}
}
vpDisplay::flush(I);
}
}
/*****main.cpp*****/
#include <iostream>
#include "UEyeOpenCV.hpp"
#include "opencv2/opencv.hpp"
#include <servocontroller.h>
#include <visp/vpFeatureBuilder.h>
#include <visp/vpFeatureDepth.h>
#include <visp/vpFeaturePoint.h>
#include <visp/vpHomogeneousMatrix.h>
#include <visp/vpPlot.h>
#include <visp/vpServo.h>
#include <visp/vpVelocityTwistMatrix.h>
#include <visp/vpDot2.h>
#include <visp/vpDisplay.h>
#include <visp/vpImageIo.h>
#include <visp/vpImageTools.h>
#include <visp/vpDisplayOpenCV.h>
#include <visp/vpConfig.h>
#include <visp/vpRobotCamera.h>
int main(){
/* set undistort */
cv::FileStorage fs("out_camera_data.xml", cv::FileStorage::READ);
cv::Mat cameraMatrix, distCoeffs;
fs["Camera_Matrix"] >> cameraMatrix;
fs["Distortion_Coefficients"] >> distCoeffs;
std::cout << "camera matrix: " << cameraMatrix << std::endl
<< "distortion coeffs: " << distCoeffs << std::endl;
cv::Mat image;
/* open camera */
UeyeOpencvCam cam = UeyeOpencvCam(752,480);
/* define a wondow of image*/
vpImage<unsigned char> I(480, 752); // Create a gray level image
cv::Mat grayFrame;
vpDisplayOpenCV d;
d.init(I, 0, 0, "Visual servoing on dot");
/* set dot*/
vpDot2 blob;
blob.setGraphics(true);
blob.setGraphicsThickness(2);
vpImagePoint germ;
bool init_done = false;
/* set com*/
unsigned short zoom=1500;
ServoController com;
com.ServoController::createSerialPort();
com.setTarget(zoom*4);
while(true) {
cv::Mat frame= cam.getFrame();
cv::cvtColor(frame,grayFrame, CV_BGR2GRAY);
cv::undistort(grayFrame,image,cameraMatrix,distCoeffs);
cv::Mat image2;
cv::flip(image,image2,-1);
vpImageConvert::convert(image2, I);
vpDisplay::display(I);
if (! init_done) {
vpDisplay::displayText(I, vpImagePoint(10,10), "Click in the blob to initialize the tracker", vpColor::red);
if (vpDisplay::getClick(I, germ, false)) {
blob.initTracking(I, germ);
init_done = true;
}
}
else {
blob.track(I);
vpImagePoint mcog;
mcog=blob.getCog();
int x=mcog.get_i();
if (x<240){
zoom=zoom+10;
com.setTarget(zoom*4);
cv::waitKey(33);
}
else{
zoom=zoom-10;
com.setTarget(zoom*4);
cv::waitKey(33);
}
}
vpDisplay::flush(I);
}
}
To copy to clipboard, switch view to plain text mode
//servocontroller.h
/*****servocontroller.h*****/
#ifndef SERVOCONTROLLER_H
#define SERVOCONTROLLER_H
#include <string>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <termios.h>
#include <QDebug>
#include <QString>
class ServoController
{
public:
virtual ~ServoController();
void createSerialPort();
void setTarget( unsigned short target );
private:
static const unsigned short mMinChannelValue = 4000;
static const unsigned short mMaxChannelValue = 8000;
unsigned short target;
unsigned char channelNumber;
QSerialPort* myCom;
};
#endif // SERVOCONTROLLER_H
/*****servocontroller.cpp*****/
#include "servocontroller.h"
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <iostream>
void ServoController::createSerialPort()
{
myCom= new QSerialPort();
myCom->setPortName("/dev/ttyACM0");
myCom->setBaudRate(9600);
myCom->setDataBits(QSerialPort::Data8);
myCom->setParity(QSerialPort::NoParity);
myCom->setStopBits(QSerialPort::OneStop);
myCom->setFlowControl(QSerialPort::NoFlowControl);
}
ServoController::~ServoController()
{
myCom->close();
}
void ServoController::setTarget(unsigned short target )
{
if (target<mMinChannelValue ){
target=mMinChannelValue;
}
if(target>mMaxChannelValue){
target=mMaxChannelValue;
}
command.resize(4);
command[0]=0x84;
command[1]=0x01;
command[2]=(char)(target & 0x7F);
command[3]=(char)((target >> 7) & 0x7F);
myCom-> QSerialPort::write(command);
}
//servocontroller.h
/*****servocontroller.h*****/
#ifndef SERVOCONTROLLER_H
#define SERVOCONTROLLER_H
#include <string>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <termios.h>
#include <QDebug>
#include <QString>
class ServoController
{
public:
virtual ~ServoController();
void createSerialPort();
void setTarget( unsigned short target );
private:
static const unsigned short mMinChannelValue = 4000;
static const unsigned short mMaxChannelValue = 8000;
unsigned short target;
unsigned char channelNumber;
QSerialPort* myCom;
QByteArray command;
};
#endif // SERVOCONTROLLER_H
/*****servocontroller.cpp*****/
#include "servocontroller.h"
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <iostream>
void ServoController::createSerialPort()
{
myCom= new QSerialPort();
myCom->setPortName("/dev/ttyACM0");
myCom->open(QIODevice::WriteOnly);
myCom->setBaudRate(9600);
myCom->setDataBits(QSerialPort::Data8);
myCom->setParity(QSerialPort::NoParity);
myCom->setStopBits(QSerialPort::OneStop);
myCom->setFlowControl(QSerialPort::NoFlowControl);
}
ServoController::~ServoController()
{
myCom->close();
}
void ServoController::setTarget(unsigned short target )
{
if (target<mMinChannelValue ){
target=mMinChannelValue;
}
if(target>mMaxChannelValue){
target=mMaxChannelValue;
}
command.resize(4);
command[0]=0x84;
command[1]=0x01;
command[2]=(char)(target & 0x7F);
command[3]=(char)((target >> 7) & 0x7F);
myCom-> QSerialPort::write(command);
}
To copy to clipboard, switch view to plain text mode
HEADERS += \
UEyeOpenCV.hpp \
servocontroller.h
SOURCES += \
main.cpp \
ueyeopencv.cpp \
servocontroller.cpp
# important
# library of QSerialport
QT += serialport
# library of OpenCV
INCLUDEPATH += /usr/local/include \
/usr/local/include/opencv \
/usr/local/include/opencv2
LIBS += /usr/local/lib/libopencv_core.so \
/usr/local/lib/libopencv_highgui.so \
/usr/local/lib/libopencv_imgproc.so
# library of UEye
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../../usr/lib/release/ -lueye_api
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../../usr/lib/debug/ -lueye_api
else:unix: LIBS += -L$$PWD/../../../../usr/lib/ -lueye_api
INCLUDEPATH += $$PWD/../../../../usr/include
DEPENDPATH += $$PWD/../../../../usr/include
# libreria de ViSP
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../ViSP-build-release/lib/x86_64-linux-gnu/release/ -lvisp
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../ViSP-build-release/lib/x86_64-linux-gnu/debug/ -lvisp
else:unix: LIBS += -L$$PWD/../../ViSP-build-release/lib/x86_64-linux-gnu/ -lvisp
INCLUDEPATH += $$PWD/../../ViSP-build-release/lib/x86_64-linux-gnu
DEPENDPATH += $$PWD/../../ViSP-build-release/lib/x86_64-linux-gnu
HEADERS += \
UEyeOpenCV.hpp \
servocontroller.h
SOURCES += \
main.cpp \
ueyeopencv.cpp \
servocontroller.cpp
# important
# library of QSerialport
QT += serialport
# library of OpenCV
INCLUDEPATH += /usr/local/include \
/usr/local/include/opencv \
/usr/local/include/opencv2
LIBS += /usr/local/lib/libopencv_core.so \
/usr/local/lib/libopencv_highgui.so \
/usr/local/lib/libopencv_imgproc.so
# library of UEye
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../../usr/lib/release/ -lueye_api
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../../usr/lib/debug/ -lueye_api
else:unix: LIBS += -L$$PWD/../../../../usr/lib/ -lueye_api
INCLUDEPATH += $$PWD/../../../../usr/include
DEPENDPATH += $$PWD/../../../../usr/include
# libreria de ViSP
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../ViSP-build-release/lib/x86_64-linux-gnu/release/ -lvisp
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../ViSP-build-release/lib/x86_64-linux-gnu/debug/ -lvisp
else:unix: LIBS += -L$$PWD/../../ViSP-build-release/lib/x86_64-linux-gnu/ -lvisp
INCLUDEPATH += $$PWD/../../ViSP-build-release/lib/x86_64-linux-gnu
DEPENDPATH += $$PWD/../../ViSP-build-release/lib/x86_64-linux-gnu
To copy to clipboard, switch view to plain text mode
Bookmarks