📄 sonyevid30.cc
字号:
//printf("storing %d bytes\n", numread-(bufptr+1)); memcpy(buffer,temp_reply+bufptr+1,numread-(bufptr+1)); numread = numread-(bufptr+1); } //PrintPacket("Really Received", temp_reply, temp); //PrintPacket("Received", temp_reply, bufptr+1); // strip off leading trash, up to start character 0x90 for(i = 0;i< bufptr;i++) { if(temp_reply[i] == 0x90 && temp_reply[i+1] != 0x90) break; } //if(i) //printf("SonyEVID30::Receive(): strip off zeros up to: %d\n", i); if(i == bufptr) return(0); memcpy(reply,temp_reply+i,bufptr+1-i); // if it's a command completion, record it, then go again if((reply[0] == 0x90) && ((reply[1] >> 4) == 0x05) && (reply[2] == 0xFF)) { //puts("got command completion"); if((reply[1] & 0x0F) == 0x01) command_pending1 = false; else if((reply[1] & 0x0F) == 0x02) command_pending2 = false; } return(bufptr+1-i);}intSonyEVID30::CancelCommand(char socket){ unsigned char command[MAX_PTZ_MESSAGE_LENGTH]; unsigned char reply[MAX_PTZ_MESSAGE_LENGTH]; int reply_len; //printf("Canceling socket %d\n", socket); command[0] = socket; command[0] |= 0x20; if((reply_len = Send(command, 1, reply)) <= 0) return(reply_len); // wait for the response while((reply[0] != 0x90) || ((reply[1] >> 4) != 0x06) || !((reply[2] == 0x04) || (reply[2] == 0x05)) || (reply_len != 4)) { if((reply[0] != 0x90) || ((reply[1] >> 4) != 0x05) || (reply[2] != 0xFF)) PrintPacket("SonyEVID30::CancelCommand(): unexpected response",reply, reply_len); //puts("CancelCommand(): calling Receive()"); if((reply_len = Receive(reply)) <= 0) return(reply_len); } if(socket == 1) command_pending1 = false; else if(socket == 2) command_pending2 = false; return(0);}int SonyEVID30::SendCommand(unsigned char* str, int len){ unsigned char reply[MAX_PTZ_PACKET_LENGTH]; int reply_len; if(command_pending1 && command_pending2) { if((command_pending1 && CancelCommand(1)) || (command_pending2 && CancelCommand(2))) return(-1); } if(command_pending1 && command_pending2) { puts("2 commands still pending. wait"); return(-1); } if((reply_len = Send(str, len, reply)) <= 0) return(reply_len); // wait for the ACK while((reply[0] != 0x90) || ((reply[1] >> 4) != 0x04) || (reply_len != 3)) { if((reply[0] != 0x90) || ((reply[1] >> 4) != 0x05) || (reply_len != 3)) { PrintPacket("SonyEVID30::SendCommand(): expected ACK, but got", reply,reply_len); } //puts("SendCommand(): calling Receive()"); if((reply_len = Receive(reply)) <= 0) return(reply_len); } if((reply[1] & 0x0F) == 0x01) command_pending1 = true; else if((reply[1] & 0x0F) == 0x02) command_pending2 = true; else fprintf(stderr,"SonyEVID30::SendCommand():got ACK for socket %d\n", reply[1] & 0x0F); return(0);}int SonyEVID30::SendRequest(unsigned char* str, int len, unsigned char* reply){ int reply_len; if((reply_len = Send(str, len, reply)) <= 0) return(reply_len); // check that it's an information return while((reply[0] != 0x90) || (reply[1] != 0x50)) { if((reply[0] != 0x90) || ((reply[1] >> 4) != 0x05) || (reply_len != 3)) { PrintPacket("SonyEVID30::SendCommand(): expected information return, but got", reply,reply_len); } //puts("SendRequest(): calling Receive()"); if((reply_len = Receive(reply)) <= 0) return(reply_len); } return(reply_len);}intSonyEVID30::SendAbsPanTilt(short pan, short tilt){ unsigned char command[MAX_PTZ_MESSAGE_LENGTH]; short convpan,convtilt; if (abs(pan)>(short)PTZ_PAN_MAX) { if(pan<(short)-PTZ_PAN_MAX) pan=(short)-PTZ_PAN_MAX; else if(pan>(short)PTZ_PAN_MAX) pan=(short)PTZ_PAN_MAX; puts("Camera pan angle thresholded"); } if(abs(tilt)>(short)PTZ_TILT_MAX) { if(tilt<(short)-PTZ_TILT_MAX) tilt=(short)-PTZ_TILT_MAX; else if(tilt>(short)PTZ_TILT_MAX) tilt=(short)PTZ_TILT_MAX; puts("Camera tilt angle thresholded"); } convpan = (short)(pan*PTZ_PAN_CONV_FACTOR); convtilt = (short)(tilt*PTZ_TILT_CONV_FACTOR); command[0] = 0x01; // absolute position command command[1] = 0x06; // absolute position command command[2] = 0x02; // absolute position command command[3] = 0x18; // MAX pan speed command[4] = 0x14; // MAX tilt speed // pan position command[5] = (unsigned char)((convpan & 0xF000) >> 12); command[6] = (unsigned char)((convpan & 0x0F00) >> 8); command[7] = (unsigned char)((convpan & 0x00F0) >> 4); command[8] = (unsigned char)(convpan & 0x000F); // tilt position command[9] = (unsigned char)((convtilt & 0xF000) >> 12); command[10] = (unsigned char)((convtilt & 0x0F00) >> 8); command[11] = (unsigned char)((convtilt & 0x00F0) >> 4); command[12] = (unsigned char)(convtilt & 0x000F); return(SendCommand(command, 13));}intSonyEVID30::GetAbsPanTilt(short* pan, short* tilt){ unsigned char command[MAX_PTZ_MESSAGE_LENGTH]; unsigned char reply[MAX_PTZ_PACKET_LENGTH]; int reply_len; short convpan, convtilt; command[0] = 0x09; command[1] = 0x06; command[2] = 0x12; if((reply_len = SendRequest(command,3,reply)) <= 0) return(reply_len); // first two bytes are header (0x90 0x50) // next 4 are pan convpan = reply[5]; convpan |= (reply[4] << 4); convpan |= (reply[3] << 8); convpan |= (reply[2] << 12); *pan = (short)(convpan / PTZ_PAN_CONV_FACTOR); // next 4 are tilt convtilt = reply[9]; convtilt |= (reply[8] << 4); convtilt |= (reply[7] << 8); convtilt |= (reply[6] << 12); *tilt = (short)(convtilt / PTZ_TILT_CONV_FACTOR); return(0);}intSonyEVID30::GetAbsZoom(short* zoom){ unsigned char command[MAX_PTZ_MESSAGE_LENGTH]; unsigned char reply[MAX_PTZ_PACKET_LENGTH]; int reply_len; command[0] = 0x09; command[1] = 0x04; command[2] = 0x47; if((reply_len = SendRequest(command,3,reply)) <= 0) return(reply_len); // first two bytes are header (0x90 0x50) // next 4 are zoom *zoom = reply[5]; *zoom |= (reply[4] << 4); *zoom |= (reply[3] << 8); *zoom |= (reply[2] << 12); return(0);}intSonyEVID30::SendAbsZoom(short zoom){ unsigned char command[MAX_PTZ_MESSAGE_LENGTH]; if(zoom<0) { zoom=0; //puts("Camera zoom thresholded"); } else if(zoom>1023){ zoom=1023; //puts("Camera zoom thresholded"); } command[0] = 0x01; // absolute position command command[1] = 0x04; // absolute position command command[2] = 0x47; // absolute position command // zoom position command[3] = (unsigned char)((zoom & 0xF000) >> 12); command[4] = (unsigned char)((zoom & 0x0F00) >> 8); command[5] = (unsigned char)((zoom & 0x00F0) >> 4); command[6] = (unsigned char)(zoom & 0x000F); return(SendCommand(command, 7));}voidSonyEVID30::PrintPacket(char* str, unsigned char* cmd, int len){ printf("%s: ", str); for(int i=0;i<len;i++) printf(" %.2x", cmd[i]); puts("");}// this function will be run in a separate threadvoid SonyEVID30::Main(){ player_ptz_data_t data; player_ptz_cmd_t command; short pan,tilt,zoom; short pandemand=0, tiltdemand=0, zoomdemand=0; bool newpantilt=true, newzoom=true; while(1) { pthread_testcancel(); GetCommand((unsigned char*)&command, sizeof(player_ptz_cmd_t)); pthread_testcancel(); if(pandemand != (short)ntohs((unsigned short)(command.pan))) { pandemand = (short)ntohs((unsigned short)(command.pan)); newpantilt = true; } if(tiltdemand != (short)ntohs((unsigned short)(command.tilt))) { tiltdemand = (short)ntohs((unsigned short)(command.tilt)); newpantilt = true; } if(zoomdemand != (short)ntohs((unsigned short)(command.zoom))) { zoomdemand = (short) ntohs((unsigned short)(command.zoom)); newzoom = true; } // Do some coordinate transformatiopns. Pan value must be // negated. The zoom value must be converted from a field of view // (in degrees) into arbitrary Sony PTZ units. pandemand = -pandemand; zoomdemand = (1024 * (zoomdemand - this->maxfov)) / (this->minfov - this->maxfov); if(newpantilt) { if(SendAbsPanTilt(pandemand,tiltdemand)) { fputs("SonyEVID30:Main():SendAbsPanTilt() errored. bailing.\n", stderr); pthread_exit(NULL); } } if(newzoom) { if(SendAbsZoom(zoomdemand)) { fputs("SonyEVID30:Main():SendAbsZoom() errored. bailing.\n", stderr); pthread_exit(NULL); } } /* get current state */ if(GetAbsPanTilt(&pan,&tilt)) { fputs("SonyEVID30:Main():GetAbsPanTilt() errored. bailing.\n", stderr); pthread_exit(NULL); } /* get current state */ if(GetAbsZoom(&zoom)) { fputs("SonyEVID30:Main():GetAbsZoom() errored. bailing.\n", stderr); pthread_exit(NULL); } // Do the necessary coordinate conversions. Camera's natural pan // coordinates increase clockwise; we want them the other way, so // we negate pan here. Zoom values are converted from arbitrary // units to a field of view (in degrees). pan = -pan; zoom = this->maxfov + (zoom * (this->minfov - this->maxfov)) / 1024; // Copy the data. data.pan = htons((unsigned short)pan); data.tilt = htons((unsigned short)tilt); data.zoom = htons((unsigned short)zoom); /* test if we are supposed to cancel */ pthread_testcancel(); PutData((unsigned char*)&data, sizeof(player_ptz_data_t),0,0); newpantilt = false; newzoom = false; usleep(PTZ_SLEEP_TIME_USEC); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -