30 bool CPtuDPerception::moveToAbsPos(
char axis,
double nRad)
32 if (!radAsign(axis,
'P', nRad))
return false;
41 bool CPtuDPerception::absPosQ(
char axis,
double& nRad)
43 return radQuerry(axis,
'P', nRad);
50 bool CPtuDPerception::moveToOffPos(
char axis,
double nRad)
52 if (!radAsign(axis,
'O', nRad))
return false;
61 bool CPtuDPerception::offPosQ(
char axis,
double& nRad)
63 return radQuerry(axis,
'O', nRad);
70 bool CPtuDPerception::maxPosQ(
char axis,
double& nRad)
72 return radQuerry(axis,
'X', nRad);
79 bool CPtuDPerception::minPosQ(
char axis,
double& nRad)
81 return radQuerry(axis,
'N', nRad);
88 bool CPtuDPerception::speed(
char axis,
double radSec)
90 return radAsign(axis,
'S', radSec);
97 bool CPtuDPerception::speedQ(
char axis,
double& radSec)
99 return radQuerry(axis,
'S', radSec);
106 bool CPtuDPerception::aceleration(
char axis,
double radSec2)
108 return radAsign(axis,
'A', radSec2);
115 bool CPtuDPerception::acelerationQ(
char axis,
double& radSec2)
117 return radQuerry(axis,
'A', radSec2);
124 bool CPtuDPerception::baseSpeed(
char axis,
double radSec)
126 return radAsign(axis,
'B', radSec);
133 bool CPtuDPerception::baseSpeedQ(
char axis,
double& radSec)
135 return radQuerry(axis,
'B', radSec);
142 bool CPtuDPerception::upperSpeed(
char axis,
double radSec)
144 return radAsign(axis,
'U', radSec);
151 bool CPtuDPerception::upperSpeedQ(
char axis,
double& radSec)
153 return radQuerry(axis,
'U', radSec);
160 bool CPtuDPerception::lowerSpeed(
char axis,
double radSec)
162 return radAsign(axis,
'L', radSec);
169 bool CPtuDPerception::lowerSpeedQ(
char axis,
double& radSec)
171 return radQuerry(axis,
'L', radSec);
178 bool CPtuDPerception::enableLimitsQ(
bool& enable)
182 if (!transmit(
"L") || !receive(
"L", response))
return false;
184 if (strstr(
upperCase(response).c_str(),
"ENABLE") !=
nullptr)
196 bool CPtuDPerception::enableLimits(
bool set)
199 return (transmit(
"LE") && receive(
"LE",
nullptr));
201 return (transmit(
"LD") && receive(
"LD",
nullptr));
208 bool CPtuDPerception::inmediateExecution(
bool set)
211 return (transmit(
"I") && receive(
"I",
nullptr));
213 return (transmit(
"S") && receive(
"S",
nullptr));
220 bool CPtuDPerception::aWait()
222 return (transmit(
"A") && receive(
"A",
nullptr));
229 bool CPtuDPerception::haltAll()
231 return (transmit(
"H") && receive(
"H",
nullptr));
238 bool CPtuDPerception::halt(
char axis)
245 return (transmit(sTrans) && receive(sTrans,
nullptr));
252 bool CPtuDPerception::reset()
254 if (!transmit(
"R"))
return false;
255 receive(
"R",
nullptr);
257 return panTiltHitError();
264 bool CPtuDPerception::save()
266 return (transmit(
"DS") && receive(
"DS",
nullptr));
273 bool CPtuDPerception::restoreDefaults()
275 return (transmit(
"DR") && receive(
"DR",
nullptr));
282 bool CPtuDPerception::restoreFactoryDefaults()
284 return (transmit(
"DF") && receive(
"DF",
nullptr));
291 bool CPtuDPerception::version(
char* sVersion)
293 return (transmit(
"V") && receive(
"V", sVersion));
300 bool CPtuDPerception::powerModeQ(
bool transit,
char& mode)
302 const char* response =
"";
308 else if (!transmit(
"PH"))
312 if (strstr(
upperCase(response).c_str(),
"REGULAR") !=
nullptr)
314 else if (strstr(
upperCase(response).c_str(),
"HIGH") !=
nullptr)
316 else if (strstr(
upperCase(response).c_str(),
"LOW") !=
nullptr)
328 bool CPtuDPerception::powerMode(
bool transit,
char mode)
332 sTrans[1] = transit ?
'M' :
'H';
336 return (transmit(sTrans) && receive(sTrans,
nullptr));
343 bool CPtuDPerception::init(
const string& port)
349 cout << endl <<
"[INFO] Start PTU communication config:" << endl;
351 cout <<
"[PTU::OpenSerialPort] Opening serial port...";
353 if (serPort.isOpen())
358 cout <<
" Error opening serial port";
362 cout <<
"OK" << endl;
364 cout <<
"[PTU::SetTimeouts] Setting timeouts...";
365 serPort.setTimeouts(1000, 1, 1000, 1, 1000);
366 cout <<
"OK" << endl;
368 cout <<
"[PTU::setBaudRate] Setting baud rate...";
369 serPort.setConfig(9600);
370 cout <<
"OK" << endl;
373 cout <<
"[PTU::setInitialConfiguration] Setting initial " 376 (!resolution()) || (!echoMode(
true)) || (!inmediateExecution(
true)))
378 cout <<
" Error setting initial configuration";
385 <<
"[INFO] Pan Resolution: " << panResolution <<
" radians | " 386 <<
RAD2DEG(panResolution) <<
"degrees";
388 <<
"[INFO] TitlResolution: " << tiltResolution <<
" radians | " 389 <<
RAD2DEG(tiltResolution) <<
"degrees" << endl
394 cerr << e.what() << endl;
405 void CPtuDPerception::close() { serPort.close(); }
410 double CPtuDPerception::radError(
char axis,
double nRadMoved)
415 div = nRadMoved - long(nRadMoved / panResolution) * panResolution;
417 div = nRadMoved - long(nRadMoved / tiltResolution) * tiltResolution;
426 bool CPtuDPerception::transmit(
const char* command)
433 size_t written = serPort.Write(str, strlen(str));
447 bool CPtuDPerception::receive(
const char* command,
char* response)
450 unsigned long nReaden;
456 nReaden = serPort.Read(&str[cnt], 1);
457 if (nReaden != 0) cnt++;
458 }
while ((nReaden != 0) && (((tmp = strstr(str, command)) ==
nullptr) ||
459 (str[cnt - 1] !=
'\n')));
463 nError = nError * TimeoutError;
467 if (response !=
nullptr)
475 if (strstr(tmp,
"!") ==
nullptr)
477 nError = nError * NoError;
481 if ((strstr(tmp,
"!P") !=
nullptr) && (strstr(tmp,
"!T") !=
nullptr))
482 nError = nError * PanTiltHitError;
483 else if (strstr(tmp,
"!T") !=
nullptr)
484 nError = nError * TiltHitError;
485 else if (strstr(tmp,
"!P") !=
nullptr)
486 nError = nError * PanHitError;
487 else if (strstr(tmp,
"! Maximum") !=
nullptr)
488 nError = nError * MaxLimitError;
489 else if (strstr(tmp,
"! Minimum") !=
nullptr)
490 nError = nError * MinLimitError;
491 else if (strstr(tmp,
"! Value") !=
nullptr)
492 nError = nError * OutOfRange;
493 else if (strstr(tmp,
"! Illegal") !=
nullptr)
494 nError = nError * IllegalCommandError;
496 nError = nError * UnExpectedError;
505 bool CPtuDPerception::verboseQ(
bool& mode)
509 if (!transmit(
"F") || !receive(
"F", response))
return false;
511 if (strstr(response,
"VERBOSE") !=
nullptr)
526 return (transmit(
"FV") && (receive(
"FV",
nullptr)));
528 return (transmit(
"FT") && (receive(
"FT",
nullptr)));
535 bool CPtuDPerception::echoModeQ(
bool& mode)
539 if (!transmit(
"E") || !receive(
"E", response))
return false;
541 if (strstr(
upperCase(response).c_str(),
"ENABLE") !=
nullptr)
553 bool CPtuDPerception::echoMode(
bool mode)
556 return (transmit(
"EE") && receive(
"EE",
nullptr));
558 return (transmit(
"ED") && receive(
"ED",
nullptr));
565 bool CPtuDPerception::resolution()
569 if ((!transmit(
"PR")) || (!receive(
"PR", response)))
return false;
570 panResolution =
DEG2RAD(convertToDouble(response) / 3600);
572 if ((!transmit(
"TR")) || (!receive(
"TR", response)))
return false;
573 tiltResolution =
DEG2RAD(convertToDouble(response) / 3600);
582 bool CPtuDPerception::radQuerry(
char axis,
char command,
double& rad)
591 if ((!transmit(sTrans)) || (!receive(sTrans, response)))
return false;
602 bool CPtuDPerception::radAsign(
char axis,
char command,
double nRad)
609 os::sprintf(sPos,
sizeof(sPos),
"%li", radToPos(axis, nRad));
615 return (transmit(sTrans) && receive(sTrans, response));
622 bool CPtuDPerception::scan(
623 char axis,
int tWait,
float initial,
float final,
double radPre)
634 moveToAbsPos(axis, initial);
637 std::this_thread::sleep_for(500ms);
642 long steps = radToPos(axis, radPre);
646 int initialPos = radToPos(axis, initial);
647 int finalPos = radToPos(axis,
final);
649 totalSteps = std::abs(initialPos - finalPos);
652 for (
int i = 0; i < totalSteps / steps; i++)
656 moveToOffPos(axis, -radPre);
660 moveToOffPos(axis, radPre);
663 std::this_thread::sleep_for(std::chrono::milliseconds(tWait));
667 moveToOffPos(axis, radPre / 2);
671 for (
int i = 0; i < (totalSteps / steps) - 1; i++)
675 moveToOffPos(axis, radPre);
679 moveToOffPos(axis, -radPre);
682 std::this_thread::sleep_for(std::chrono::milliseconds(tWait));
688 moveToAbsPos(axis, 0);
697 long CPtuDPerception::radToPos(
char axis,
double nrad)
700 return (
long)(nrad / panResolution);
702 return (
long)(nrad / tiltResolution);
709 double CPtuDPerception::posToRad(
char axis,
long nPos)
712 return (
double)nPos * panResolution;
714 return (
double)nPos * tiltResolution;
723 char* result = strpbrk(sLong,
"-0123456789");
726 return strtol(result, &stop, 10);
733 double CPtuDPerception::convertToDouble(
char* sDouble)
735 char* result = strpbrk(sDouble,
"-0123456789");
738 return strtod(result, &stop);
745 int CPtuDPerception::checkErrors()
760 else if (timeoutError())
764 else if (initError())
768 else if (panTiltHitError())
772 else if (panHitError())
776 else if (tiltHitError())
780 else if (maxLimitError())
784 else if (minLimitError())
788 else if (outOfRange())
792 else if (illegalCommandError())
796 else if (unExpectedError())
809 void CPtuDPerception::nversion(
double& nVersion)
811 cout <<
"[ERROR] Function not defined for this PTU model";
819 bool CPtuDPerception::setLimits(
820 [[maybe_unused]]
char axis, [[maybe_unused]]
double& l,
821 [[maybe_unused]]
double& u)
823 cout <<
"[ERROR] Function not defined for this PTU model";
831 bool CPtuDPerception::changeMotionDir()
833 cout <<
"[ERROR] Function not defined for this PTU model";
841 bool CPtuDPerception::rangeMeasure()
843 cout <<
"[ERROR] Function not defined for this PTU model";
char * strcat(char *dest, size_t destSize, const char *source) noexcept
An OS-independent version of strcat.
Contains classes for various device interfaces.
char * strcpy(char *dest, size_t destSize, const char *source) noexcept
An OS-independent version of strcpy.
long convertToLong(char *sLong)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
std::string upperCase(const std::string &str)
Returns a upper-case version of a string.
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...