45 #include <visp3/core/vpTime.h> 47 #include <visp3/core/vpConfig.h> 49 #ifdef VISP_HAVE_BICLOPS 51 #include <visp3/core/vpExponentialMap.h> 52 #include <visp3/core/vpIoTools.h> 53 #include <visp3/robot/vpBiclops.h> 54 #include <visp3/robot/vpRobotBiclops.h> 55 #include <visp3/robot/vpRobotException.h> 59 #include <visp3/core/vpDebug.h> 65 bool vpRobotBiclops::robotAlreadyCreated =
false;
68 static pthread_mutex_t vpEndThread_mutex;
69 static pthread_mutex_t vpShm_mutex;
70 static pthread_mutex_t vpMeasure_mutex;
115 :
vpBiclops(),
vpRobot(), control_thread(), controller(), positioningVelocity(defaultPositioningVelocity),
116 q_previous(), controlThreadCreated(false)
120 vpRobotBiclops::robotAlreadyCreated =
false;
124 pthread_mutex_init(&vpShm_mutex, NULL);
125 pthread_mutex_init(&vpEndThread_mutex, NULL);
126 pthread_mutex_init(&vpMeasure_mutex, NULL);
166 q_previous(), controlThreadCreated(false)
170 vpRobotBiclops::robotAlreadyCreated =
false;
174 pthread_mutex_init(&vpShm_mutex, NULL);
175 pthread_mutex_init(&vpEndThread_mutex, NULL);
176 pthread_mutex_init(&vpMeasure_mutex, NULL);
193 vpDEBUG_TRACE(12,
"Start vpRobotBiclops::~vpRobotBiclops()");
197 pthread_mutex_unlock(&vpEndThread_mutex);
202 if (controlThreadCreated ==
true) {
203 int code = pthread_join(control_thread, NULL);
205 vpCERROR <<
"Cannot terminate the control thread: " << code <<
" strErr=" << strerror(errno)
206 <<
" strCode=" << strerror(code) << std::endl;
210 pthread_mutex_destroy(&vpShm_mutex);
211 pthread_mutex_destroy(&vpEndThread_mutex);
212 pthread_mutex_destroy(&vpMeasure_mutex);
214 vpRobotBiclops::robotAlreadyCreated =
false;
246 FILE *fd = fopen(configfile.c_str(),
"r");
248 vpCERROR <<
"Cannot open biclops config file: " << configfile << std::endl;
254 controller.init(configfile);
263 vpRobotBiclops::robotAlreadyCreated =
true;
269 controlThreadCreated =
false;
294 vpRobotBiclopsController *controller =
static_cast<vpRobotBiclopsController *
>(arg);
299 vpRobotBiclopsController::shmType shm;
320 pthread_mutex_lock(&vpShm_mutex);
322 shm = controller->readShm();
325 pthread_mutex_unlock(&vpShm_mutex);
328 prev_q_dot[i] = shm.q_dot[i];
329 new_q_dot[i] =
false;
330 change_dir[i] =
false;
331 force_halt[i] =
false;
332 enable_limit[i] =
true;
336 mes_q = controller->getActualPosition();
337 mes_q_dot = controller->getActualVelocity();
340 pthread_mutex_lock(&vpShm_mutex);
342 shm = controller->readShm();
345 shm.actual_q[i] = mes_q[i];
346 shm.actual_q_dot[i] = mes_q_dot[i];
349 controller->writeShm(shm);
352 pthread_mutex_unlock(&vpShm_mutex);
355 pthread_mutex_unlock(&vpMeasure_mutex);
357 while (!controller->isStopRequested()) {
360 mes_q = controller->getActualPosition();
361 mes_q_dot = controller->getActualVelocity();
364 pthread_mutex_lock(&vpShm_mutex);
366 shm = controller->readShm();
370 shm.actual_q[i] = mes_q[i];
371 shm.actual_q_dot[i] = mes_q_dot[i];
381 if (mes_q[i] < -softLimit[i]) {
383 shm.status[i] = vpRobotBiclopsController::STOP;
384 shm.jointLimit[i] =
true;
385 }
else if (mes_q[i] > softLimit[i]) {
387 shm.status[i] = vpRobotBiclopsController::STOP;
388 shm.jointLimit[i] =
true;
390 shm.status[i] = vpRobotBiclopsController::SPEED;
391 shm.jointLimit[i] =
false;
396 if (std::fabs(shm.q_dot[i] - prev_q_dot[i]) >
397 std::fabs(
vpMath::maximum(shm.q_dot[i], prev_q_dot[i])) * std::numeric_limits<double>::epsilon())
400 new_q_dot[i] =
false;
403 if ((shm.q_dot[i] * prev_q_dot[i]) < 0.)
404 change_dir[i] =
true;
406 change_dir[i] =
false;
408 vpDEBUG_TRACE(13,
"status : %d %d", shm.status[0], shm.status[1]);
409 vpDEBUG_TRACE(13,
"joint : %d %d", shm.jointLimit[0], shm.jointLimit[1]);
410 vpDEBUG_TRACE(13,
"new q_dot : %d %d", new_q_dot[0], new_q_dot[1]);
411 vpDEBUG_TRACE(13,
"new dir : %d %d", change_dir[0], change_dir[1]);
412 vpDEBUG_TRACE(13,
"force halt : %d %d", force_halt[0], force_halt[1]);
413 vpDEBUG_TRACE(13,
"enable limit: %d %d", enable_limit[0], enable_limit[1]);
415 bool updateVelocity =
false;
420 if (shm.status[i] == vpRobotBiclopsController::STOP) {
422 if (change_dir[i] ==
false) {
425 if (enable_limit[i] ==
true) {
428 if (force_halt[i] ==
false) {
430 force_halt[i] =
true;
431 updateVelocity =
true;
436 q_dot[i] = shm.q_dot[i];
437 shm.status[i] = vpRobotBiclopsController::SPEED;
438 force_halt[i] =
false;
439 updateVelocity =
true;
443 if (enable_limit[i] ==
true) {
445 q_dot[i] = shm.q_dot[i];
446 shm.status[i] = vpRobotBiclopsController::SPEED;
447 force_halt[i] =
false;
448 enable_limit[i] =
false;
449 updateVelocity =
true;
453 if (force_halt[i] ==
false) {
455 force_halt[i] =
true;
456 enable_limit[i] =
true;
457 updateVelocity =
true;
465 q_dot[i] = shm.q_dot[i];
466 shm.status[i] = vpRobotBiclopsController::SPEED;
467 enable_limit[i] =
true;
468 updateVelocity =
true;
473 if (shm.status[i] == vpRobotBiclopsController::STOP) {
474 if (enable_limit[i] ==
true) {
477 if (force_halt[i] ==
false) {
480 force_halt[i] =
true;
481 updateVelocity =
true;
486 enable_limit[i] =
true;
491 controller->writeShm(shm);
494 pthread_mutex_unlock(&vpShm_mutex);
496 if (updateVelocity) {
500 controller->setVelocity(q_dot);
505 prev_q_dot[i] = shm.q_dot[i];
524 controller->stopRequest(
false);
526 vpDEBUG_TRACE(10,
"End of the control thread: stop the robot");
528 controller->setVelocity(q_dot);
533 delete[] enable_limit;
535 pthread_mutex_unlock(&vpEndThread_mutex);
570 pthread_mutex_lock(&vpEndThread_mutex);
576 vpCERROR <<
"Cannot create speed biclops control thread: " << code <<
" strErr=" << strerror(errno)
577 <<
" strCode=" << strerror(code) << std::endl;
580 controlThreadCreated =
true;
602 controller.setVelocity(q_dot);
605 controller.stopRequest(
true);
689 if (velocity < 0 || velocity > 100) {
694 positioningVelocity = velocity;
725 "Modification of the robot state");
761 pthread_mutex_lock(&vpEndThread_mutex);
762 controller.setPosition(q, positioningVelocity);
764 pthread_mutex_unlock(&vpEndThread_mutex);
840 vpERROR_TRACE(
"Cannot get position in camera frame: not implemented");
866 q = controller.getPosition();
875 pthread_mutex_lock(&vpMeasure_mutex);
877 vpRobotBiclopsController::shmType shm;
880 pthread_mutex_lock(&vpShm_mutex);
882 shm = controller.readShm();
885 pthread_mutex_unlock(&vpShm_mutex);
888 q[i] = shm.actual_q[i];
891 vpCDEBUG(11) <<
"++++++++ Measure actuals: " << q.
t();
894 pthread_mutex_unlock(&vpMeasure_mutex);
930 "use setRobotState(vpRobot::STATE_VELOCITY_CONTROL) first) ");
932 "Cannot send a velocity to the robot " 933 "use setRobotState(vpRobot::STATE_VELOCITY_CONTROL) first) ");
939 "in the camera frame: " 940 "functionality not implemented");
942 "in the camera frame:" 943 "functionality not implemented");
947 vpERROR_TRACE(
"Bad dimension fo speed vector in articular frame");
949 "in articular frame");
955 "in the reference frame: " 956 "functionality not implemented");
958 "in the reference frame:" 959 "functionality not implemented");
963 "in the mixt frame: " 964 "functionality not implemented");
967 "functionality not implemented");
971 "Case not taken in account.");
988 if (fabs(q_dot[i]) > max) {
990 max = fabs(q_dot[i]);
999 q_dot_sat = q_dot * max;
1002 vpCDEBUG(12) <<
"send velocity: " << q_dot_sat.
t() << std::endl;
1004 vpRobotBiclopsController::shmType shm;
1007 pthread_mutex_lock(&vpShm_mutex);
1009 shm = controller.readShm();
1012 shm.q_dot[i] = q_dot[i];
1014 controller.writeShm(shm);
1017 pthread_mutex_unlock(&vpShm_mutex);
1044 vpERROR_TRACE(
"Cannot get position in camera frame: not implemented");
1070 q_dot = controller.getVelocity();
1079 pthread_mutex_lock(&vpMeasure_mutex);
1081 vpRobotBiclopsController::shmType shm;
1084 pthread_mutex_lock(&vpShm_mutex);
1086 shm = controller.readShm();
1089 pthread_mutex_unlock(&vpShm_mutex);
1092 q_dot[i] = shm.actual_q_dot[i];
1095 vpCDEBUG(11) <<
"++++++++ Velocity actuals: " << q_dot.
t();
1098 pthread_mutex_unlock(&vpMeasure_mutex);
1144 std::ifstream fd(filename.c_str(), std::ios::in);
1146 if (!fd.is_open()) {
1151 std::string key(
"R:");
1152 std::string id(
"#PTU-EVI - Position");
1153 bool pos_found =
false;
1158 while (std::getline(fd, line)) {
1161 if (!(line.compare(0,
id.size(), id) == 0)) {
1162 std::cout <<
"Error: this position file " << filename <<
" is not for Biclops robot" << std::endl;
1166 if ((line.compare(0, 1,
"#") == 0)) {
1169 if ((line.compare(0, key.size(), key) == 0)) {
1177 std::istringstream ss(line);
1193 std::cout <<
"Error: unable to find a position for Biclops robot in " << filename << std::endl;
1231 d = q_current - q_previous;
1243 c_previousMc_current = fMc_previous.
inverse() * fMc_current;
1251 vpERROR_TRACE(
"Cannot get a velocity in the reference frame: " 1252 "functionality not implemented");
1254 "functionality not implemented");
1258 "functionality not implemented");
1260 "functionality not implemented");
1264 q_previous = q_current;
1267 #elif !defined(VISP_BUILD_SHARED_LIBS) 1270 void dummy_vpRobotBiclops(){};
Jacobian, geometric model functionnalities... for biclops, pan, tilt head.
vpHomogeneousMatrix get_cMe() const
static vpColVector inverse(const vpHomogeneousMatrix &M)
Implementation of a matrix and operations on matrices.
bool readPositionFile(const std::string &filename, vpColVector &q)
VISP_EXPORT int wait(double t0, double t)
Error that can be emited by the vpRobot class and its derivates.
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &q_dot)
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &q)
static const unsigned int ndof
void get_eJe(vpMatrix &_eJe)
Implementation of an homogeneous matrix and operations on such kind of matrices.
static const float tiltJointLimit
void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &q_dot)
static const float speedLimit
void get_eJe(const vpColVector &q, vpMatrix &eJe) const
Initialize the position controller.
void setConfigFile(const std::string &filename="/usr/share/BiclopsDefault.cfg")
unsigned int getRows() const
vpHomogeneousMatrix inverse() const
Class that defines a generic virtual robot.
void get_fJe(vpMatrix &_fJe)
static const double defaultPositioningVelocity
No copy constructor allowed.
virtual ~vpRobotBiclops(void)
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
void get_cVe(vpVelocityTwistMatrix &_cVe) const
vpRobot::vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
static Type maximum(const Type &a, const Type &b)
static const float panJointLimit
Initialize the velocity controller.
virtual vpRobotStateType getRobotState(void) const
Initialize the acceleration controller.
vpVelocityTwistMatrix buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
void get_fMc(const vpColVector &q, vpHomogeneousMatrix &fMc) const
static double rad(double deg)
static double deg(double rad)
Implementation of column vector and the associated operations.
void getDisplacement(const vpRobot::vpControlFrameType frame, vpColVector &d)
double getPositioningVelocity(void)
void setPosition(const vpRobot::vpControlFrameType frame, const vpColVector &q)
void get_fJe(const vpColVector &q, vpMatrix &fJe) const
static void * vpRobotBiclopsSpeedControlLoop(void *arg)
void setPositioningVelocity(const double velocity)
void resize(const unsigned int i, const bool flagNullify=true)