28 bool CPtuDPerception::moveToAbsPos(
char axis,
double nRad)
30 if (!radAsign(axis,
'P', nRad))
return false;
39 bool CPtuDPerception::absPosQ(
char axis,
double& nRad)
41 return radQuerry(axis,
'P', nRad);
48 bool CPtuDPerception::moveToOffPos(
char axis,
double nRad)
50 if (!radAsign(axis,
'O', nRad))
return false;
59 bool CPtuDPerception::offPosQ(
char axis,
double& nRad)
61 return radQuerry(axis,
'O', nRad);
68 bool CPtuDPerception::maxPosQ(
char axis,
double& nRad)
70 return radQuerry(axis,
'X', nRad);
77 bool CPtuDPerception::minPosQ(
char axis,
double& nRad)
79 return radQuerry(axis,
'N', nRad);
86 bool CPtuDPerception::speed(
char axis,
double radSec)
88 return radAsign(axis,
'S', radSec);
95 bool CPtuDPerception::speedQ(
char axis,
double& radSec)
97 return radQuerry(axis,
'S', radSec);
104 bool CPtuDPerception::aceleration(
char axis,
double radSec2)
106 return radAsign(axis,
'A', radSec2);
113 bool CPtuDPerception::acelerationQ(
char axis,
double& radSec2)
115 return radQuerry(axis,
'A', radSec2);
122 bool CPtuDPerception::baseSpeed(
char axis,
double radSec)
124 return radAsign(axis,
'B', radSec);
131 bool CPtuDPerception::baseSpeedQ(
char axis,
double& radSec)
133 return radQuerry(axis,
'B', radSec);
140 bool CPtuDPerception::upperSpeed(
char axis,
double radSec)
142 return radAsign(axis,
'U', radSec);
149 bool CPtuDPerception::upperSpeedQ(
char axis,
double& radSec)
151 return radQuerry(axis,
'U', radSec);
158 bool CPtuDPerception::lowerSpeed(
char axis,
double radSec)
160 return radAsign(axis,
'L', radSec);
167 bool CPtuDPerception::lowerSpeedQ(
char axis,
double& radSec)
169 return radQuerry(axis,
'L', radSec);
176 bool CPtuDPerception::enableLimitsQ(
bool& enable)
180 if (!transmit(
"L") || !receive(
"L", response))
return false;
182 if (strstr(
upperCase(response).c_str(),
"ENABLE") !=
nullptr)
194 bool CPtuDPerception::enableLimits(
bool set)
197 return (transmit(
"LE") && receive(
"LE",
nullptr));
199 return (transmit(
"LD") && receive(
"LD",
nullptr));
206 bool CPtuDPerception::inmediateExecution(
bool set)
209 return (transmit(
"I") && receive(
"I",
nullptr));
211 return (transmit(
"S") && receive(
"S",
nullptr));
218 bool CPtuDPerception::aWait(
void)
220 return (transmit(
"A") && receive(
"A",
nullptr));
227 bool CPtuDPerception::haltAll()
229 return (transmit(
"H") && receive(
"H",
nullptr));
236 bool CPtuDPerception::halt(
char axis)
243 return (transmit(sTrans) && receive(sTrans,
nullptr));
252 if (!transmit(
"R"))
return false;
253 receive(
"R",
nullptr);
255 return panTiltHitError();
262 bool CPtuDPerception::save(
void)
264 return (transmit(
"DS") && receive(
"DS",
nullptr));
271 bool CPtuDPerception::restoreDefaults(
void)
273 return (transmit(
"DR") && receive(
"DR",
nullptr));
280 bool CPtuDPerception::restoreFactoryDefaults(
void)
282 return (transmit(
"DF") && receive(
"DF",
nullptr));
289 bool CPtuDPerception::version(
char* sVersion)
291 return (transmit(
"V") && receive(
"V", sVersion));
298 bool CPtuDPerception::powerModeQ(
bool transit,
char&
mode)
300 const char* response =
"";
306 else if (!transmit(
"PH"))
310 if (strstr(
upperCase(response).c_str(),
"REGULAR") !=
nullptr)
312 else if (strstr(
upperCase(response).c_str(),
"HIGH") !=
nullptr)
314 else if (strstr(
upperCase(response).c_str(),
"LOW") !=
nullptr)
326 bool CPtuDPerception::powerMode(
bool transit,
char mode)
330 sTrans[1] = transit ?
'M' :
'H';
334 return (transmit(sTrans) && receive(sTrans,
nullptr));
341 bool CPtuDPerception::init(
const string& port)
347 cout << endl <<
"[INFO] Start PTU comunication config:" << endl;
349 cout <<
"[PTU::OpenSerialPort] Opening serial port...";
351 if (serPort.isOpen())
356 cout <<
" Error opening serial port";
360 cout <<
"OK" << endl;
362 cout <<
"[PTU::SetTimeouts] Setting timeouts...";
363 serPort.setTimeouts(1000, 1, 1000, 1, 1000);
364 cout <<
"OK" << endl;
366 cout <<
"[PTU::setBaudRate] Setting baud rate...";
367 serPort.setConfig(9600);
368 cout <<
"OK" << endl;
371 cout <<
"[PTU::setInitialConfiguration] Setting initial " 373 if ((!verbose(
true)) ||
374 (!resolution()) || (!echoMode(
true)) || (!inmediateExecution(
true)))
376 cout <<
" Error setting initial configuration";
383 <<
"[INFO] Pan Resolution: " << panResolution <<
" radians | " 384 <<
RAD2DEG(panResolution) <<
"degrees";
386 <<
"[INFO] TitlResolution: " << tiltResolution <<
" radians | " 387 <<
RAD2DEG(tiltResolution) <<
"degrees" << endl
392 cerr << e.what() << endl;
403 void CPtuDPerception::close() { serPort.close(); }
408 double CPtuDPerception::radError(
char axis,
double nRadMoved)
413 div = nRadMoved - long(nRadMoved / panResolution) * panResolution;
415 div = nRadMoved - long(nRadMoved / tiltResolution) * tiltResolution;
424 bool CPtuDPerception::transmit(
const char* command)
431 size_t written = serPort.Write(str, strlen(str));
445 bool CPtuDPerception::receive(
const char* command,
char* response)
448 unsigned long nReaden;
454 nReaden = serPort.Read(&str[cnt], 1);
455 if (nReaden != 0) cnt++;
456 }
while ((nReaden != 0) && (((tmp = strstr(str, command)) ==
nullptr) ||
457 (str[cnt - 1] !=
'\n')));
461 nError = nError * TimeoutError;
465 if (response !=
nullptr)
473 if (strstr(tmp,
"!") ==
nullptr)
475 nError = nError * NoError;
479 if ((strstr(tmp,
"!P") !=
nullptr) && (strstr(tmp,
"!T") !=
nullptr))
480 nError = nError * PanTiltHitError;
481 else if (strstr(tmp,
"!T") !=
nullptr)
482 nError = nError * TiltHitError;
483 else if (strstr(tmp,
"!P") !=
nullptr)
484 nError = nError * PanHitError;
485 else if (strstr(tmp,
"! Maximum") !=
nullptr)
486 nError = nError * MaxLimitError;
487 else if (strstr(tmp,
"! Minimum") !=
nullptr)
488 nError = nError * MinLimitError;
489 else if (strstr(tmp,
"! Value") !=
nullptr)
490 nError = nError * OutOfRange;
491 else if (strstr(tmp,
"! Illegal") !=
nullptr)
492 nError = nError * IllegalCommandError;
494 nError = nError * UnExpectedError;
503 bool CPtuDPerception::verboseQ(
bool&
mode)
507 if (!transmit(
"F") || !receive(
"F", response))
return false;
509 if (strstr(response,
"VERBOSE") !=
nullptr)
521 bool CPtuDPerception::verbose(
bool set)
524 return (transmit(
"FV") && (receive(
"FV",
nullptr)));
526 return (transmit(
"FT") && (receive(
"FT",
nullptr)));
533 bool CPtuDPerception::echoModeQ(
bool&
mode)
537 if (!transmit(
"E") || !receive(
"E", response))
return false;
539 if (strstr(
upperCase(response).c_str(),
"ENABLE") !=
nullptr)
551 bool CPtuDPerception::echoMode(
bool mode)
554 return (transmit(
"EE") && receive(
"EE",
nullptr));
556 return (transmit(
"ED") && receive(
"ED",
nullptr));
563 bool CPtuDPerception::resolution(
void)
567 if ((!transmit(
"PR")) || (!receive(
"PR", response)))
return false;
568 panResolution =
DEG2RAD(convertToDouble(response) / 3600);
570 if ((!transmit(
"TR")) || (!receive(
"TR", response)))
return false;
571 tiltResolution =
DEG2RAD(convertToDouble(response) / 3600);
580 bool CPtuDPerception::radQuerry(
char axis,
char command,
double& rad)
589 if ((!transmit(sTrans)) || (!receive(sTrans, response)))
return false;
600 bool CPtuDPerception::radAsign(
char axis,
char command,
double nRad)
607 os::sprintf(sPos,
sizeof(sPos),
"%li", radToPos(axis, nRad));
613 return (transmit(sTrans) && receive(sTrans, response));
620 bool CPtuDPerception::scan(
621 char axis,
int tWait,
float initial,
float final,
double radPre)
632 moveToAbsPos(axis, initial);
635 std::this_thread::sleep_for(500ms);
640 long steps = radToPos(axis, radPre);
644 int initialPos = radToPos(axis, initial);
645 int finalPos = radToPos(axis,
final);
647 totalSteps = abs(initialPos - finalPos);
650 for (
int i = 0; i < totalSteps / steps; i++)
654 moveToOffPos(axis, -radPre);
658 moveToOffPos(axis, radPre);
661 std::this_thread::sleep_for(std::chrono::milliseconds(tWait));
665 moveToOffPos(axis, radPre / 2);
669 for (
int i = 0; i < (totalSteps / steps) - 1; i++)
673 moveToOffPos(axis, radPre);
677 moveToOffPos(axis, -radPre);
680 std::this_thread::sleep_for(std::chrono::milliseconds(tWait));
686 moveToAbsPos(axis, 0);
695 long CPtuDPerception::radToPos(
char axis,
double nrad)
698 return (
long)(nrad / panResolution);
700 return (
long)(nrad / tiltResolution);
707 double CPtuDPerception::posToRad(
char axis,
long nPos)
710 return (
double)nPos * panResolution;
712 return (
double)nPos * tiltResolution;
721 char* result = strpbrk(sLong,
"-0123456789");
724 return strtol(result, &stop, 10);
731 double CPtuDPerception::convertToDouble(
char* sDouble)
733 char* result = strpbrk(sDouble,
"-0123456789");
736 return strtod(result, &stop);
743 int CPtuDPerception::checkErrors()
758 else if (timeoutError())
762 else if (initError())
766 else if (panTiltHitError())
770 else if (panHitError())
774 else if (tiltHitError())
778 else if (maxLimitError())
782 else if (minLimitError())
786 else if (outOfRange())
790 else if (illegalCommandError())
794 else if (unExpectedError())
807 void CPtuDPerception::nversion(
double& nVersion)
809 cout <<
"[ERROR] Function not defined for this PTU model";
817 bool CPtuDPerception::setLimits(
char axis,
double& l,
double& u)
822 cout <<
"[ERROR] Function not defined for this PTU model";
830 bool CPtuDPerception::changeMotionDir()
832 cout <<
"[ERROR] Function not defined for this PTU model";
840 bool CPtuDPerception::rangeMeasure()
842 cout <<
"[ERROR] Function not defined for this PTU model";
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Contains classes for various device interfaces.
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
long convertToLong(char *sLong)
char * strcat(char *dest, size_t destSize, const char *source) noexcept
An OS-independent version of strcat.
std::string upperCase(const std::string &str)
Returns a upper-case version of a string.
char * strcpy(char *dest, size_t destSize, const char *source) noexcept
An OS-independent version of strcpy.
int sprintf(char *buf, size_t bufSize, const char *format,...) noexcept MRPT_printf_format_check(3
An OS-independent version of sprintf (Notice the bufSize param, which may be ignored in some compiler...