45 #include <sys/types.h>
70 fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
72 fprintf(stderr,
"Could not open serial device %s\n",port);
78 tcgetattr(fd, &oldtio);
84 struct termios newtio;
85 memset(&newtio, 0,
sizeof(newtio));
86 newtio.c_cflag = CS8 | CLOCAL | CREAD;
87 newtio.c_iflag = IGNPAR;
89 newtio.c_lflag = ICANON;
139 fprintf(stderr,
"Failed to set serial baud rate: %d\n", rate);
144 if (cfsetispeed(&newtio, sp) < 0 || cfsetospeed(&newtio, sp) < 0) {
145 fprintf(stderr,
"Failed to set serial baud rate: %d\n", rate);
150 tcflush(fd, TCIFLUSH);
151 tcsetattr(fd, TCSANOW, &newtio);
156 tcflush(fd, TCIFLUSH);
164 tcflush(fd, TCIFLUSH);
179 if (tr <= 0 || pr <= 0 || PMin == 0 || PMax == 0 || TMin == 0 || TMax == 0) {
186 char response[10] =
"!T!T!P!P*";
188 for (
int i = 0; i < 9; ++i) {
189 while ((len = read(fd, &temp, 1 )) == 0) {};
190 if ((len != 1) || (temp != response[i])) {
191 fprintf(stderr,
"Error Resetting Pan Tilt unit\n");
192 fprintf(stderr,
"Stopping access to pan-tilt unit\n");
199 tcflush(fd, TCIFLUSH);
215 if (tr <= 0 || pr <= 0 || PMin == 0 || PMax == 0 || TMin == 0 || TMax == 0) {
217 fprintf(stderr,
"Error getting pan-tilt resolution...is the serial port correct?\n");
218 fprintf(stderr,
"Stopping access to pan-tilt unit\n");
229 void PTU46::Disconnect() {
232 tcsetattr(fd, TCSANOW, &oldtio);
239 int PTU46::Write(
const char * data,
int length) {
246 length = strlen(data);
249 if (write(fd, data, length) < length) {
250 fprintf(stderr,
"Error writing to Pan Tilt Unit, disabling\n");
259 float PTU46::GetRes(
char type) {
270 if (len < 3 || buffer[0] !=
'*') {
271 fprintf(stderr,
"Error getting pan-tilt res\n");
276 double z = strtod(&buffer[2],NULL);
282 int PTU46::GetLimit(
char type,
char LimType) {
294 if (len < 3 || buffer[0] !=
'*') {
295 fprintf(stderr,
"Error getting pan-tilt limit\n");
300 return strtol(&buffer[2],NULL,0);
305 float PTU46::GetPosition (
char type) {
317 if (len < 3 || buffer[0] !=
'*') {
318 fprintf(stderr,
"Error getting pan-tilt pos\n");
324 return strtod (&buffer[2],NULL) * GetResolution(type);
329 bool PTU46::SetPosition (
char type,
float pos,
bool Block) {
334 int Count =
static_cast<int> (pos/GetResolution(type));
338 fprintf (stderr,
"Pan Tilt Value out of Range: %c %f(%d) (%d-%d)\n", type, pos, Count, (type ==
PTU46_TILT ? TMin : PMin),(type ==
PTU46_TILT ? TMax : PMax));
343 snprintf (cmd,16,
"%cp%d ",type,Count);
350 if (len <= 0 || buffer[0] !=
'*') {
351 fprintf(stderr,
"Error setting pan-tilt pos\n");
356 while (GetPosition (type) != pos) {};
362 float PTU46::GetSpeed (
char type) {
374 if (len < 3 || buffer[0] !=
'*') {
375 fprintf (stderr,
"Error getting pan-tilt speed\n");
381 return strtod(&buffer[2],NULL) * GetResolution(type);
387 bool PTU46::SetSpeed (
char type,
float pos) {
392 int Count =
static_cast<int> (pos/GetResolution(type));
394 if (abs(Count) < (type ==
PTU46_TILT ? TSMin : PSMin) || abs(Count) > (type ==
PTU46_TILT ? TSMax : PSMax)) {
395 fprintf (stderr,
"Pan Tilt Speed Value out of Range: %c %f(%d) (%d-%d)\n", type, pos, Count, (type ==
PTU46_TILT ? TSMin : PSMin),(type ==
PTU46_TILT ? TSMax : PSMax));
400 snprintf (cmd,16,
"%cs%d ",type,Count);
407 if (len <= 0 || buffer[0] !=
'*') {
408 fprintf (stderr,
"Error setting pan-tilt speed\n");
416 bool PTU46::SetMode (
char type) {
428 if (len <= 0 || buffer[0] !=
'*') {
429 fprintf (stderr,
"Error setting pan-tilt move mode\n");
436 char PTU46::GetMode () {
445 if (len < 3 || buffer[0] !=
'*') {
446 fprintf (stderr,
"Error getting pan-tilt pos\n");
450 if (buffer[2] ==
'p')
452 else if (buffer[2] ==
'i')
PTU46 Pan Tilt Unit Driver Copyright (C) 2009 Erik Karulf (erik@cse.wustl.edu)
ptu control header. Defines commands for ptu control.