45 #include <visp3/core/vpConfig.h> 47 #ifdef VISP_HAVE_BICLOPS 49 #include <visp3/core/vpTime.h> 50 #include <visp3/core/vpExponentialMap.h> 51 #include <visp3/core/vpIoTools.h> 52 #include <visp3/robot/vpBiclops.h> 53 #include <visp3/robot/vpRobotBiclops.h> 54 #include <visp3/robot/vpRobotException.h> 58 #include <visp3/core/vpDebug.h> 64 bool vpRobotBiclops::robotAlreadyCreated =
false;
67 static pthread_mutex_t vpEndThread_mutex;
68 static pthread_mutex_t vpShm_mutex;
69 static pthread_mutex_t vpMeasure_mutex;
113 :
vpBiclops(),
vpRobot(), control_thread(), controller(), positioningVelocity(defaultPositioningVelocity),
114 q_previous(), controlThreadCreated(false)
118 vpRobotBiclops::robotAlreadyCreated =
false;
122 pthread_mutex_init(&vpShm_mutex, NULL);
123 pthread_mutex_init(&vpEndThread_mutex, NULL);
124 pthread_mutex_init(&vpMeasure_mutex, NULL);
161 q_previous(), controlThreadCreated(false)
165 vpRobotBiclops::robotAlreadyCreated =
false;
169 pthread_mutex_init(&vpShm_mutex, NULL);
170 pthread_mutex_init(&vpEndThread_mutex, NULL);
171 pthread_mutex_init(&vpMeasure_mutex, NULL);
188 vpDEBUG_TRACE(12,
"Start vpRobotBiclops::~vpRobotBiclops()");
192 pthread_mutex_unlock(&vpEndThread_mutex);
197 if (controlThreadCreated ==
true) {
198 int code = pthread_join(control_thread, NULL);
200 vpCERROR <<
"Cannot terminate the control thread: " << code <<
" strErr=" << strerror(errno)
201 <<
" strCode=" << strerror(code) << std::endl;
205 pthread_mutex_destroy(&vpShm_mutex);
206 pthread_mutex_destroy(&vpEndThread_mutex);
207 pthread_mutex_destroy(&vpMeasure_mutex);
209 vpRobotBiclops::robotAlreadyCreated =
false;
241 FILE *fd = fopen(configfile.c_str(),
"r");
243 vpCERROR <<
"Cannot open biclops config file: " << configfile << std::endl;
249 controller.init(configfile);
258 vpRobotBiclops::robotAlreadyCreated =
true;
264 controlThreadCreated =
false;
289 vpRobotBiclopsController *controller =
static_cast<vpRobotBiclopsController *
>(arg);
294 vpRobotBiclopsController::shmType shm;
315 pthread_mutex_lock(&vpShm_mutex);
317 shm = controller->readShm();
320 pthread_mutex_unlock(&vpShm_mutex);
323 prev_q_dot[i] = shm.q_dot[i];
324 new_q_dot[i] =
false;
325 change_dir[i] =
false;
326 force_halt[i] =
false;
327 enable_limit[i] =
true;
331 mes_q = controller->getActualPosition();
332 mes_q_dot = controller->getActualVelocity();
335 pthread_mutex_lock(&vpShm_mutex);
337 shm = controller->readShm();
340 shm.actual_q[i] = mes_q[i];
341 shm.actual_q_dot[i] = mes_q_dot[i];
344 controller->writeShm(shm);
347 pthread_mutex_unlock(&vpShm_mutex);
350 pthread_mutex_unlock(&vpMeasure_mutex);
352 while (!controller->isStopRequested()) {
355 mes_q = controller->getActualPosition();
356 mes_q_dot = controller->getActualVelocity();
359 pthread_mutex_lock(&vpShm_mutex);
361 shm = controller->readShm();
365 shm.actual_q[i] = mes_q[i];
366 shm.actual_q_dot[i] = mes_q_dot[i];
376 if (mes_q[i] < -softLimit[i]) {
378 shm.status[i] = vpRobotBiclopsController::STOP;
379 shm.jointLimit[i] =
true;
380 }
else if (mes_q[i] > softLimit[i]) {
382 shm.status[i] = vpRobotBiclopsController::STOP;
383 shm.jointLimit[i] =
true;
385 shm.status[i] = vpRobotBiclopsController::SPEED;
386 shm.jointLimit[i] =
false;
391 if (std::fabs(shm.q_dot[i] - prev_q_dot[i]) >
392 std::fabs(
vpMath::maximum(shm.q_dot[i], prev_q_dot[i])) * std::numeric_limits<double>::epsilon())
395 new_q_dot[i] =
false;
398 if ((shm.q_dot[i] * prev_q_dot[i]) < 0.)
399 change_dir[i] =
true;
401 change_dir[i] =
false;
403 vpDEBUG_TRACE(13,
"status : %d %d", shm.status[0], shm.status[1]);
404 vpDEBUG_TRACE(13,
"joint : %d %d", shm.jointLimit[0], shm.jointLimit[1]);
405 vpDEBUG_TRACE(13,
"new q_dot : %d %d", new_q_dot[0], new_q_dot[1]);
406 vpDEBUG_TRACE(13,
"new dir : %d %d", change_dir[0], change_dir[1]);
407 vpDEBUG_TRACE(13,
"force halt : %d %d", force_halt[0], force_halt[1]);
408 vpDEBUG_TRACE(13,
"enable limit: %d %d", enable_limit[0], enable_limit[1]);
410 bool updateVelocity =
false;
415 if (shm.status[i] == vpRobotBiclopsController::STOP) {
417 if (change_dir[i] ==
false) {
420 if (enable_limit[i] ==
true) {
423 if (force_halt[i] ==
false) {
425 force_halt[i] =
true;
426 updateVelocity =
true;
431 q_dot[i] = shm.q_dot[i];
432 shm.status[i] = vpRobotBiclopsController::SPEED;
433 force_halt[i] =
false;
434 updateVelocity =
true;
438 if (enable_limit[i] ==
true) {
440 q_dot[i] = shm.q_dot[i];
441 shm.status[i] = vpRobotBiclopsController::SPEED;
442 force_halt[i] =
false;
443 enable_limit[i] =
false;
444 updateVelocity =
true;
448 if (force_halt[i] ==
false) {
450 force_halt[i] =
true;
451 enable_limit[i] =
true;
452 updateVelocity =
true;
460 q_dot[i] = shm.q_dot[i];
461 shm.status[i] = vpRobotBiclopsController::SPEED;
462 enable_limit[i] =
true;
463 updateVelocity =
true;
468 if (shm.status[i] == vpRobotBiclopsController::STOP) {
469 if (enable_limit[i] ==
true) {
472 if (force_halt[i] ==
false) {
475 force_halt[i] =
true;
476 updateVelocity =
true;
481 enable_limit[i] =
true;
486 controller->writeShm(shm);
489 pthread_mutex_unlock(&vpShm_mutex);
491 if (updateVelocity) {
495 controller->setVelocity(q_dot);
500 prev_q_dot[i] = shm.q_dot[i];
519 controller->stopRequest(
false);
521 vpDEBUG_TRACE(10,
"End of the control thread: stop the robot");
523 controller->setVelocity(q_dot);
528 delete[] enable_limit;
530 pthread_mutex_unlock(&vpEndThread_mutex);
565 pthread_mutex_lock(&vpEndThread_mutex);
571 vpCERROR <<
"Cannot create speed biclops control thread: " << code <<
" strErr=" << strerror(errno)
572 <<
" strCode=" << strerror(code) << std::endl;
575 controlThreadCreated =
true;
597 controller.setVelocity(q_dot);
600 controller.stopRequest(
true);
684 if (velocity < 0 || velocity > 100) {
689 positioningVelocity = velocity;
720 "Modification of the robot state");
754 pthread_mutex_lock(&vpEndThread_mutex);
755 controller.setPosition(q, positioningVelocity);
757 pthread_mutex_unlock(&vpEndThread_mutex);
858 q = controller.getPosition();
867 pthread_mutex_lock(&vpMeasure_mutex);
869 vpRobotBiclopsController::shmType shm;
872 pthread_mutex_lock(&vpShm_mutex);
874 shm = controller.readShm();
877 pthread_mutex_unlock(&vpShm_mutex);
880 q[i] = shm.actual_q[i];
883 vpCDEBUG(11) <<
"++++++++ Measure actuals: " << q.
t();
886 pthread_mutex_unlock(&vpMeasure_mutex);
922 "use setRobotState(vpRobot::STATE_VELOCITY_CONTROL) first) ");
924 "Cannot send a velocity to the robot " 925 "use setRobotState(vpRobot::STATE_VELOCITY_CONTROL) first) ");
931 "in the camera frame: " 932 "functionality not implemented");
934 "in the camera frame:" 935 "functionality not implemented");
939 vpERROR_TRACE(
"Bad dimension fo speed vector in articular frame");
941 "in articular frame");
947 "in the reference frame:" 948 "functionality not implemented");
953 "functionality not implemented");
957 "in the end-effector frame:" 958 "functionality not implemented");
977 if (fabs(q_dot[i]) > max) {
979 max = fabs(q_dot[i]);
988 q_dot_sat = q_dot * max;
991 vpCDEBUG(12) <<
"send velocity: " << q_dot_sat.
t() << std::endl;
993 vpRobotBiclopsController::shmType shm;
996 pthread_mutex_lock(&vpShm_mutex);
998 shm = controller.readShm();
1001 shm.q_dot[i] = q_dot[i];
1003 controller.writeShm(shm);
1006 pthread_mutex_unlock(&vpShm_mutex);
1058 q_dot = controller.getVelocity();
1067 pthread_mutex_lock(&vpMeasure_mutex);
1069 vpRobotBiclopsController::shmType shm;
1072 pthread_mutex_lock(&vpShm_mutex);
1074 shm = controller.readShm();
1077 pthread_mutex_unlock(&vpShm_mutex);
1080 q_dot[i] = shm.actual_q_dot[i];
1083 vpCDEBUG(11) <<
"++++++++ Velocity actuals: " << q_dot.
t();
1086 pthread_mutex_unlock(&vpMeasure_mutex);
1132 std::ifstream fd(filename.c_str(), std::ios::in);
1134 if (!fd.is_open()) {
1139 std::string key(
"R:");
1140 std::string id(
"#PTU-EVI - Position");
1141 bool pos_found =
false;
1146 while (std::getline(fd, line)) {
1149 if (!(line.compare(0,
id.size(), id) == 0)) {
1150 std::cout <<
"Error: this position file " << filename <<
" is not for Biclops robot" << std::endl;
1154 if ((line.compare(0, 1,
"#") == 0)) {
1157 if ((line.compare(0, key.size(), key) == 0)) {
1165 std::istringstream ss(line);
1181 std::cout <<
"Error: unable to find a position for Biclops robot in " << filename << std::endl;
1219 d = q_current - q_previous;
1231 c_previousMc_current = fMc_previous.
inverse() * fMc_current;
1240 "functionality not implemented");
1244 "functionality not implemented");
1248 "functionality not implemented");
1252 q_previous = q_current;
1255 #elif !defined(VISP_BUILD_SHARED_LIBS) 1258 void dummy_vpRobotBiclops(){};
Jacobian, geometric model functionnalities... for biclops, pan, tilt head.
vpHomogeneousMatrix get_cMe() const
static vpColVector inverse(const vpHomogeneousMatrix &M)
virtual ~vpRobotBiclops()
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
void setPositioningVelocity(double velocity)
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
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)
Stops robot motion especially in velocity and acceleration control.
void resize(unsigned int i, bool flagNullify=true)
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)