UsTK : Ultrasound ToolKit  version 2.0.1 under development (2024-05-20)
usRectangleVisualServoingController.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ustk software.
4  * Copyright (C) 2016 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ustk with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * This software was developed at:
17  * Inria Rennes - Bretagne Atlantique
18  * Campus Universitaire de Beaulieu
19  * 35042 Rennes Cedex
20  * France
21  *
22  * If you have questions regarding the use of this file, please contact
23  * Inria at ustk@inria.fr
24  *
25  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27  *
28  * Authors:
29  * Marc Pouliquen
30  *
31  *****************************************************************************/
32 
38 #include <visp3/ustk_gui/usRectangleVisualServoingController.h>
39 
40 #if (defined(USTK_HAVE_VTK_QT) || defined(USTK_HAVE_QT5)) && defined(VISP_HAVE_MODULE_USTK_TEMPLATE_TRACKING)
41 
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)
48 {
49 }
50 
51 usRectangleVisualServoingController::~usRectangleVisualServoingController() {}
52 
53 void usRectangleVisualServoingController::updateImage(usImagePreScan2D<unsigned char> image)
54 {
55 
56  m_imagePreScan = image;
57 
58  // convert to post-scan
59  m_converter.convert(m_imagePreScan, m_imagePostScan);
60  emit(newPostScanFrame(m_imagePostScan));
61  qApp->processEvents();
62 
63  // run the tracking process and get the result
64  if (m_tracker.isInit()) {
65 
66  double xtarget, ytarget;
67 
68  // run confidence map process
69  m_confidenceProcess.run(m_confidencePreScan, m_imagePreScan);
70  m_tracker.update(m_imagePostScan);
71  vpRectOriented rectangle = m_tracker.getTarget();
72 
73  if (m_trackerActivated)
74  emit(newRectTracked(rectangle));
75 
76  usPixelMeterConversion::convert(m_imagePostScan, rectangle.getCenter().get_j(), rectangle.getCenter().get_i(),
77  xtarget, ytarget);
78 
79  double ttarget = atan2(xtarget, ytarget);
80 
81  // emit results for display puropose
82  emit(trackerXError(xtarget));
83 
84  unsigned int height(m_confidencePreScan.getHeight()), width(m_confidencePreScan.getWidth());
85 
86  double I_sum = 0.0;
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]);
90 
91  double yc = 0.0;
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];
95  }
96  yc /= I_sum;
97 
98  double tc = yc * m_confidencePreScan.getScanLinePitch() - m_confidencePreScan.getFieldOfView() / 2.0;
99 
100  // emit results for display puropose
101  emit(confidenceMap(m_confidencePreScan));
102  emit(confidenceBarycenterAngle(yc));
103 
104  double lambda_t = 1.2;
105  double lambda_c = 0.8;
106 
107  // computing velocities
108  double xControlVelocity = -lambda_t * xtarget + lambda_c * (tc - ttarget) * ytarget; // meters per second
109  double thetaZControlVelocity = lambda_c * (tc - ttarget); // radians per second
110 
111  // send command
112  if (m_controllerActivated) {
113  emit(updateProbeZOrientation(vpMath::deg(thetaZControlVelocity) * 10)); // 10-1 deg per second
114  emit(updatePobeXVelocity(xControlVelocity * 1000)); // mm per second
115  }
116  }
117 }
118 
119 void usRectangleVisualServoingController::initTracker(vpRectOriented rect)
120 {
121  if (m_imagePostScan.getSize() != 0)
122  m_tracker.init(m_imagePostScan, rect);
123  else
124  throw(vpException(vpException::fatalError, "No frames sent to controller, cannot init tracker"));
125  m_trackerActivated = true;
126 }
127 
128 void usRectangleVisualServoingController::activateController(bool activate)
129 {
130  m_controllerActivated = activate;
131  if (!activate) {
132  emit(updateProbeZOrientation(0));
133  emit(updatePobeXVelocity(0));
134  }
135 }
136 
137 void usRectangleVisualServoingController::activateController() { activateController(true); }
138 
139 void usRectangleVisualServoingController::disactivateController() { activateController(false); }
140 
141 void usRectangleVisualServoingController::stopTracking()
142 {
143  m_trackerActivated = false;
144  m_controllerActivated = false;
145  emit(updateProbeZOrientation(0));
146  emit(updatePobeXVelocity(0));
147 }
148 #endif
static void convert(const usImagePostScan2D< unsigned char > &image, const double &u, const double &v, double &x, double &y)
Conversion for 2D post-scan images.