38 #include <visp3/ustk_gui/usRectangleVisualServoingController.h>
40 #if (defined(USTK_HAVE_VTK_QT) || defined(USTK_HAVE_QT5)) && defined(VISP_HAVE_MODULE_USTK_TEMPLATE_TRACKING)
45 usRectangleVisualServoingController::usRectangleVisualServoingController(QObject *parent)
46 : QObject(parent), m_tracker(), m_confidenceProcess(), m_converter(), m_imagePreScan(), m_confidencePreScan(),
47 m_imagePostScan(), m_trackerActivated(false), m_controllerActivated(false)
51 usRectangleVisualServoingController::~usRectangleVisualServoingController() {}
56 m_imagePreScan = image;
59 m_converter.convert(m_imagePreScan, m_imagePostScan);
60 emit(newPostScanFrame(m_imagePostScan));
61 qApp->processEvents();
64 if (m_tracker.isInit()) {
66 double xtarget, ytarget;
69 m_confidenceProcess.run(m_confidencePreScan, m_imagePreScan);
70 m_tracker.update(m_imagePostScan);
71 vpRectOriented rectangle = m_tracker.getTarget();
73 if (m_trackerActivated)
74 emit(newRectTracked(rectangle));
79 double ttarget = atan2(xtarget, ytarget);
82 emit(trackerXError(xtarget));
84 unsigned int height(m_confidencePreScan.getHeight()), width(m_confidencePreScan.getWidth());
87 for (
unsigned int i = 0; i < height; ++i)
88 for (
unsigned int j = 0; j < width; ++j)
89 I_sum +=
static_cast<double>(m_confidencePreScan[i][j]);
92 for (
unsigned int x = 0; x < height; ++x)
93 for (
unsigned int y = 0; y < width; ++y) {
94 yc += y * m_confidencePreScan[x][y];
98 double tc = yc * m_confidencePreScan.getScanLinePitch() - m_confidencePreScan.getFieldOfView() / 2.0;
101 emit(confidenceMap(m_confidencePreScan));
102 emit(confidenceBarycenterAngle(yc));
104 double lambda_t = 1.2;
105 double lambda_c = 0.8;
108 double xControlVelocity = -lambda_t * xtarget + lambda_c * (tc - ttarget) * ytarget;
109 double thetaZControlVelocity = lambda_c * (tc - ttarget);
112 if (m_controllerActivated) {
113 emit(updateProbeZOrientation(vpMath::deg(thetaZControlVelocity) * 10));
114 emit(updatePobeXVelocity(xControlVelocity * 1000));
119 void usRectangleVisualServoingController::initTracker(vpRectOriented rect)
121 if (m_imagePostScan.getSize() != 0)
122 m_tracker.init(m_imagePostScan, rect);
124 throw(vpException(vpException::fatalError,
"No frames sent to controller, cannot init tracker"));
125 m_trackerActivated =
true;
128 void usRectangleVisualServoingController::activateController(
bool activate)
130 m_controllerActivated = activate;
132 emit(updateProbeZOrientation(0));
133 emit(updatePobeXVelocity(0));
137 void usRectangleVisualServoingController::activateController() { activateController(
true); }
139 void usRectangleVisualServoingController::disactivateController() { activateController(
false); }
141 void usRectangleVisualServoingController::stopTracking()
143 m_trackerActivated =
false;
144 m_controllerActivated =
false;
145 emit(updateProbeZOrientation(0));
146 emit(updatePobeXVelocity(0));
static void convert(const usImagePostScan2D< unsigned char > &image, const double &u, const double &v, double &x, double &y)
Conversion for 2D post-scan images.