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");
759 pthread_mutex_lock(&vpEndThread_mutex);
760 controller.setPosition(q, positioningVelocity);
762 pthread_mutex_unlock(&vpEndThread_mutex);
863 q = controller.getPosition();
872 pthread_mutex_lock(&vpMeasure_mutex);
874 vpRobotBiclopsController::shmType shm;
877 pthread_mutex_lock(&vpShm_mutex);
879 shm = controller.readShm();
882 pthread_mutex_unlock(&vpShm_mutex);
885 q[i] = shm.actual_q[i];
888 vpCDEBUG(11) <<
"++++++++ Measure actuals: " << q.
t();
891 pthread_mutex_unlock(&vpMeasure_mutex);
927 "use setRobotState(vpRobot::STATE_VELOCITY_CONTROL) first) ");
929 "Cannot send a velocity to the robot " 930 "use setRobotState(vpRobot::STATE_VELOCITY_CONTROL) first) ");
936 "in the camera frame: " 937 "functionality not implemented");
939 "in the camera frame:" 940 "functionality not implemented");
944 vpERROR_TRACE(
"Bad dimension fo speed vector in articular frame");
946 "in articular frame");
952 "in the reference frame:" 953 "functionality not implemented");
958 "functionality not implemented");
962 "in the end-effector frame:" 963 "functionality not implemented");
982 if (fabs(q_dot[i]) > max) {
984 max = fabs(q_dot[i]);
993 q_dot_sat = q_dot * max;
996 vpCDEBUG(12) <<
"send velocity: " << q_dot_sat.
t() << std::endl;
998 vpRobotBiclopsController::shmType shm;
1001 pthread_mutex_lock(&vpShm_mutex);
1003 shm = controller.readShm();
1006 shm.q_dot[i] = q_dot[i];
1008 controller.writeShm(shm);
1011 pthread_mutex_unlock(&vpShm_mutex);
1063 q_dot = controller.getVelocity();
1072 pthread_mutex_lock(&vpMeasure_mutex);
1074 vpRobotBiclopsController::shmType shm;
1077 pthread_mutex_lock(&vpShm_mutex);
1079 shm = controller.readShm();
1082 pthread_mutex_unlock(&vpShm_mutex);
1085 q_dot[i] = shm.actual_q_dot[i];
1088 vpCDEBUG(11) <<
"++++++++ Velocity actuals: " << q_dot.
t();
1091 pthread_mutex_unlock(&vpMeasure_mutex);
1137 std::ifstream fd(filename.c_str(), std::ios::in);
1139 if (!fd.is_open()) {
1144 std::string key(
"R:");
1145 std::string id(
"#PTU-EVI - Position");
1146 bool pos_found =
false;
1151 while (std::getline(fd, line)) {
1154 if (!(line.compare(0,
id.size(), id) == 0)) {
1155 std::cout <<
"Error: this position file " << filename <<
" is not for Biclops robot" << std::endl;
1159 if ((line.compare(0, 1,
"#") == 0)) {
1162 if ((line.compare(0, key.size(), key) == 0)) {
1170 std::istringstream ss(line);
1186 std::cout <<
"Error: unable to find a position for Biclops robot in " << filename << std::endl;
1224 d = q_current - q_previous;
1236 c_previousMc_current = fMc_previous.
inverse() * fMc_current;
1245 "functionality not implemented");
1249 "functionality not implemented");
1253 "functionality not implemented");
1257 q_previous = q_current;
1260 #elif !defined(VISP_BUILD_SHARED_LIBS) 1263 void dummy_vpRobotBiclops(){};
Jacobian, geometric model functionnalities... for biclops, pan, tilt head.
void get_cVe(vpVelocityTwistMatrix &_cVe) 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
Initialize the position controller.
void setConfigFile(const std::string &filename="/usr/share/BiclopsDefault.cfg")
Class that defines a generic virtual robot.
void get_fJe(vpMatrix &_fJe)
void get_fMc(const vpColVector &q, vpHomogeneousMatrix &fMc) const
static const double defaultPositioningVelocity
No copy constructor allowed.
virtual ~vpRobotBiclops(void)
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
vpRobot::vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
static Type maximum(const Type &a, const Type &b)
static const float panJointLimit
Initialize the velocity controller.
Initialize the acceleration controller.
vpVelocityTwistMatrix buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
unsigned int getRows() const
static double rad(double deg)
void get_fJe(const vpColVector &q, vpMatrix &fJe) const
void get_eJe(const vpColVector &q, vpMatrix &eJe) const
static double deg(double rad)
Implementation of column vector and the associated operations.
void getDisplacement(const vpRobot::vpControlFrameType frame, vpColVector &d)
vpHomogeneousMatrix inverse() const
virtual vpRobotStateType getRobotState(void) const
double getPositioningVelocity(void)
void setPosition(const vpRobot::vpControlFrameType frame, const vpColVector &q)
static void * vpRobotBiclopsSpeedControlLoop(void *arg)
vpHomogeneousMatrix get_cMe() const
void setPositioningVelocity(const double velocity)
void resize(const unsigned int i, const bool flagNullify=true)