46 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
53 #include <sys/types.h>
57 #include <visp3/core/vpDebug.h>
58 #include <visp3/core/vpTime.h>
59 #include <visp3/robot/vpRobotException.h>
60 #include <visp3/robot/vpServolens.h>
69 vpServolens::vpServolens() : remfd(0), isinit(false) { }
79 vpServolens::vpServolens(
const char *port) : remfd(0), isinit(false) { this->open(port); }
88 vpServolens::~vpServolens() { this->close(); }
104 void vpServolens::open(
const char *port)
109 printf(
"\nOpen the Servolens serial port \"%s\"\n", port);
111 if ((this->remfd = ::open(port, O_RDWR | O_NONBLOCK)) < 0) {
112 vpERROR_TRACE(
"Cannot open Servolens serial port.");
117 if (tcgetattr(this->remfd, &info) < 0) {
118 ::close(this->remfd);
119 vpERROR_TRACE(
"Error using TCGETS in ioctl.");
130 info.c_iflag |= INLCR;
140 info.c_cflag |= CREAD;
141 info.c_cflag |= B9600 | CS7 | PARENB;
147 if (tcsetattr(this->remfd, TCSANOW, &info) < 0) {
148 ::close(this->remfd);
149 vpERROR_TRACE(
"Error using TCGETS in ioctl.");
154 tcflush(this->remfd, TCIFLUSH);
163 if (this->getPosition(vpServolens::ZOOM, izoom) ==
false) {
164 vpERROR_TRACE(
"Cannot dial with the servolens. Check if the serial "
165 "link is connected.");
167 "serial link is connected.");
176 void vpServolens::close()
179 printf(
"\nClose the serial connection with Servolens\n");
180 ::close(this->remfd);
191 void vpServolens::reset()
const
194 vpERROR_TRACE(
"Cannot dial with Servolens.");
201 this->write(cmd.c_str());
205 this->write(cmd.c_str());
213 this->write(cmd.c_str());
217 this->write(cmd.c_str());
227 void vpServolens::init()
const
230 vpERROR_TRACE(
"Cannot dial with Servolens.");
238 this->write(cmd.c_str());
242 this->write(cmd.c_str());
258 void vpServolens::enablePrompt(
bool active)
const
261 vpERROR_TRACE(
"Cannot dial with Servolens.");
272 this->write(cmd.c_str());
283 void vpServolens::setController(vpControllerType controller)
const
286 vpERROR_TRACE(
"Cannot dial with Servolens.");
291 switch (controller) {
295 this->write(cmd.c_str());
300 this->write(cmd.c_str());
303 this->write(cmd.c_str());
307 this->write(cmd.c_str());
310 this->write(cmd.c_str());
321 void vpServolens::setAutoIris(
bool enable)
const
324 vpERROR_TRACE(
"Cannot dial with Servolens.");
334 this->write(cmd.c_str());
347 void vpServolens::setPosition(vpServoType servo,
unsigned int position)
const
350 vpERROR_TRACE(
"Cannot dial with Servolens.");
353 std::stringstream command;
364 this->enableCommandComplete();
377 if (position < ZOOM_MIN)
384 if (position > FOCUS_MAX)
385 position = FOCUS_MAX;
391 if (position > IRIS_MAX)
400 command <<
"ZD" << position;
403 command <<
"FD" << position;
406 command <<
"DD" << position;
411 printf(
"\ncommand: %s", command.str());
414 this->write(command.str().c_str());
432 bool vpServolens::getPosition(vpServoType servo,
unsigned int &position)
const
435 vpERROR_TRACE(
"Cannot dial with Servolens.");
439 char posit[10], *pt_posit;
441 short fin_lect_posit;
443 short lecture_posit_en_cours;
468 this->write(cmd.c_str());
474 lecture_posit_en_cours = 0;
476 if (this->read(&c, 1) ==
true) {
513 if (c >=
'0' && c <=
'9') {
515 lecture_posit_en_cours = 1;
517 else if (lecture_posit_en_cours) {
531 }
while (!fin_lect_posit);
536 this->clean(posit, posit);
539 position = (
unsigned int)atoi(posit);
577 vpERROR_TRACE(
"Cannot dial with Servolens.");
581 double pix_size = 7.4e-6;
582 double px = 1000, py = 1000, u0 = 320, v0 = 240;
585 double subsample_factor = 1.;
589 if (width > 300 && width < 340 && height > 220 && height < 260)
590 subsample_factor = 2;
591 else if (width > 140 && width < 1800 && height > 100 && height < 140)
592 subsample_factor = 4;
595 getPosition(vpServolens::ZOOM, zoom);
602 double focale = zoom * 1.0e-5;
603 px = focale / (double)(subsample_factor * pix_size);
604 py = focale / (double)(subsample_factor * pix_size);
620 char vpServolens::wait()
const
623 vpERROR_TRACE(
"Cannot dial with Servolens.");
628 r = ::write(this->remfd,
"\r\n", strlen(
"\r\n"));
629 if (r != (ssize_t)(strlen(
"\r\n"))) {
634 r = ::read(this->remfd, &c, 1);
653 void vpServolens::wait(vpServoType servo)
const
656 vpERROR_TRACE(
"Cannot dial with Servolens.");
680 if (::read(this->remfd, &c, 1) != 1) {
686 if (c == fin_mvt.c_str()[0]) {
688 if (::read(this->remfd, &c, 1) != 1) {
693 if (c == fin_mvt.c_str()[1]) {
713 bool vpServolens::read(
char *c,
long timeout_s)
const
716 vpERROR_TRACE(
"Cannot dial with Servolens.");
721 struct timeval timeout = { timeout_s, 0 };
724 FD_SET(
static_cast<unsigned int>(this->remfd), &readfds);
726 if (select(FD_SETSIZE, &readfds, (fd_set *)
nullptr, (fd_set *)
nullptr, &timeout) > 0) {
727 ssize_t n = ::read(this->remfd, c, 1);
746 void vpServolens::write(
const char *s)
const
749 vpERROR_TRACE(
"Cannot dial with Servolens.");
753 r = ::write(this->remfd,
"\r", strlen(
"\r"));
754 r += ::write(this->remfd, s, strlen(s));
755 r += ::write(this->remfd,
"\r", strlen(
"\r"));
756 if (r != (ssize_t)(2 * strlen(
"\r") + strlen(s))) {
781 bool vpServolens::clean(
const char *in,
char *out)
const
789 while (*(in) ==
'0' && i++ < nb_car) {
798 while (i++ <= nb_car) {
Generic class defining intrinsic camera parameters.
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
unsigned int getWidth() const
unsigned int getHeight() const
void init(unsigned int h, unsigned int w, Type value)
Error that can be emitted by the vpRobot class and its derivatives.
@ communicationError
Unable to communicate.
VISP_EXPORT int wait(double t0, double t)