⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ptzimpl.c

📁 PTZ云台控制code,实用与所有的云台控制
💻 C
字号:
/*-------------------------- [ Comment ] --------------------------------*\
	File name	: ptzimpl.c
	Project		: NVP3100
	Description : 
				  
	Copyright(c) 1997-2007, NEXTCHIP CO., LTD. All rights reserved.
  -----------------------------------------------------------------------
	C0000 - Created by application_team@nextchip.com		2006-01-05
	C0001 - 
\*-----------------------------------------------------------------------*/

#include <pthread.h>
#include <termios.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>

#include <stdio.h>
#include <stdlib.h>
#include "ptzimpl.h"
//#include "configdata.h"
//#include "serial.h"

////////////////////////////////////////////////////////////////////////////////
//extern int fd_nvp3100;

static const char *		s_dev_485 = "/dev/tts/1";
static int				s_fd_485 = -1;
static NC_PORTPARAMS	s_serial_param = { B9600, 8, PAR_NONE, 1, 0, 0 };
static int s_baud_rate;

static struct termios 	s_old_tio;
struct termios tio;

BYTE rts_mode;
//int nPTZProtocol=0;

////////////////////////////////////////////////////////////////////////////////

#define	DEFAULT_PTZ_SPEED			10	//	1 ~ 10

////////////////////////////////////////////////////////////////////////////////

void set_debug(void)
{
	tio.c_cflag = ((tio.c_cflag & ~B9600) & ~B115200) | B115200;
	tcsetattr(s_fd_485, TCSADRAIN, &tio );
}

void set_ptz(void)
{
	tio.c_cflag = ((tio.c_cflag & ~B115200) & ~B9600) | B9600;
	tcsetattr(s_fd_485, TCSANOW, &tio );
}
int GetFD485(void)
{
	return s_fd_485;
}

//modify by kelvin 20070706
int Init485( void )
{
	BYTE nChannel;
	int  nPTZProtocol;	
		
	if( (s_fd_485=open(s_dev_485, O_WRONLY | O_NOCTTY | O_NDELAY)) < 0 )
	{
		printf("ERROR. Open %s device...\n",s_dev_485);		
		return -1;
	}
	else
		printf("OK. Open %s device...\n",s_dev_485);

	nChannel = 0;
	//nPTZProtocol = (int)ReadCameraPTZProtocol( nChannel );
	nPTZProtocol=0; //jily modify 2008.12.06
	s_baud_rate = s_serial_param.nBaudRate = GetRS485BaudRate( nPTZProtocol );

	if( InitPTZPort(s_fd_485, &s_serial_param) < 0 )
	{
		printf1("Error. Init RS-485 device...\n");
		Deinit485();
		return -1;
	}
	else
		printf1("OK. Init RS-485 device...\n");

	
	//rts_mode.data=0x00; //jily modify 
	//ioctl( fd_nvp3100, RTS_MODE, &rts_mode );
	
	return 0;
}


void Deinit485(void)
{
	
	if( s_fd_485 > 0 )
	{
		if( tcsetattr(s_fd_485, TCSANOW, &s_old_tio) < 0 )
			printf1("ERROR. Restore tio value...\n");
	}
	
	close( s_fd_485 );
	s_fd_485 = -1;
	
	//rts_mode.data=0x03; //jily modify 081206
	//ioctl( fd_nvp3100, RTS_MODE, &rts_mode );
}



int InitPTZPort( int fd, const NC_PORTPARAMS *param )
{
	int ret = -1;

	bzero( &tio, sizeof(tio) );//set tio all word=0

	if( tcgetattr(fd, &tio) < 0 )
	{
		perror("tcgetattr");		
		goto done;
	}

	memcpy( &s_old_tio, &tio, sizeof(tio) );


#if 1
	printf2("nBaudRate = %d\n",param->nBaudRate );
	if( param->nBaudRate > 0 )
	{	
		// Baudrate
		cfsetispeed( &tio, param->nBaudRate ); //set in/out boudrate
		cfsetospeed( &tio, param->nBaudRate );
	}
	else
	{
		printf1("Invalid BaudRate Value : %d\n", param->nBaudRate );
		return ret;
	}

	// Character size
	tio.c_cflag &= ~CSIZE;
	switch( param->nCharSize )
	{
		case 5	:
			tio.c_cflag |= CS5;
			break;
		case 6	:
			tio.c_cflag |= CS6;
			break;
		case 7	: 
			tio.c_cflag |= CS7;
			break;
		case 8	: 	
		default	:
			tio.c_cflag |= CS8;
			break;
	}

	// Parity bit
	switch( param->nParityBit )
	{
		case PAR_NONE	:
			tio.c_cflag &= ~PARENB;
			break;
		case PAR_EVEN	:	
			tio.c_cflag |= PARENB;	
			tio.c_cflag &= ~PARODD;
			break;
		case PAR_ODD	:	
			tio.c_cflag |= PARENB;	
			tio.c_cflag |= PARODD;	
			break;
	}

	// Stop bit
	if( param->nStopBit == 2 )
		tio.c_cflag |= CSTOPB;		// 2 Stop Bit
	else
		tio.c_cflag &= ~CSTOPB;		// 1 Stop Bit

	tio.c_cflag &= ~CRTSCTS;
	tio.c_cflag |= (CLOCAL | CREAD);

	tio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
	tio.c_lflag |= FLUSHO;	// Output flush
	
	tio.c_oflag &= ~OPOST;
	tio.c_cc[VMIN] = 1;
	tio.c_cc[VTIME] = 0;

#if 1
	tio.c_iflag = IGNBRK | IGNPAR;
#else
	tio.c_iflag = IGNBRK | IGNPAR | IXON | IXOFF;	
	tcflush( fd, TCIFLUSH );
#endif

	ret = tcsetattr( fd, TCSANOW, &tio );//reset ttys1 attr
	if( ret != 0 )
		perror( "tcsetattr" );
#else
	tio.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
	tio.c_oflag = 0;
	tio.c_lflag = 0;		/* non-canon, no echo */
	tio.c_cc[VMIN] = 0;		/* blocking read until 1 char */
	tio.c_cc[VTIME] = 0;	/* timer unused */
	ret = tcsetattr( fd, TCSANOW, &tio );
	
	set_debug();
	
/*
	tio.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
	tio.c_cflag &= ~PARENB;
	tio.c_cflag &= ~PARODD;
	tio.c_cflag &= ~CSTOPB;
	tio.c_iflag = IGNPAR;
	tio.c_oflag = 0;
	tio.c_lflag = 0;		// non-canon, no echo
	tio.c_cc[VMIN] = 0;		// blocking read until 1 char
	tio.c_cc[VTIME] = 0;	// timer unused
	tcflush ( fd, TCIFLUSH );
*/
	if( ret != 0 )
	{
		perror("Unable to initialize serial device");
		fprintf( stdout, "Unable to initialize serial device\n");
		ret = -1;
	}
#endif
	
done:

	return ret;
}

int GetRS485BaudRate( int nPTZDevID )
{
	int nBoudRate = 0;

	switch( nPTZDevID )
	{
		case PANTILTDEVICE_PELCO_P	:	nBoudRate = B9600;	break;
		case PANTILTDEVICE_PELCO_D	:	nBoudRate = B9600;	break;
	}

	return nBoudRate;
}

int WriteTo485( void *pdata, int nsize )
{     printf1("write data to 485 %d\n",nsize);
	if( pdata && nsize > 0 )
	{
		//set_ptz();
		if( write(s_fd_485, pdata, nsize) != nsize )
		{
			printf1("ERROR. WRITE\n");
			return FALSE;
		}
/*char test[]="122334433433";
		int len=strlen(test);
              while(write(s_fd_485, &test,len)== len){
			printf("test =%s\n",test);
			usleep(10000);
			
              }*/
		//set_debug();
		return TRUE;
	}
	//set_debug();
	return FALSE;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int DeviceControl( int nControl)
{
	int device;
	int nAddr;


	//nAddr  = (int)ReadCameraPTZAddr( nChannel );
	//device = (int)ReadCameraPTZProtocol( nChannel );
	nAddr  = 1;
	device = 0;
	switch( device )
	{
		case PANTILTDEVICE_PELCO_P	:
			return ControlPELCO_P( 0, nAddr, nControl );
			break;
		case PANTILTDEVICE_PELCO_D	:
			return ControlPELCO_D( 0, nAddr, nControl );
			break;
	}

	return FALSE;
}

// PELCO P Protocol
int ControlPELCO_P( int nPort, int nAddr, int nControl )
{
	COMMPELCOP	Command;
	int			PTZSpeed = (0x3F / 10) * DEFAULT_PTZ_SPEED;

	
	if( nAddr <= 0 )
		return 1;
	//nAddr -= 1;//modify by kelvin 20070706

	Command.Stx		= 0xA0;
	Command.Addr	= nAddr;
	Command.Data3	= PTZSpeed;	// pan speed	: 0x00~0x3f
	Command.Data4	= PTZSpeed;	// tilt speed	: 0x00~0x3f
     printf1("port= %d,addr= %d,ncontrol=%d.\n",nPort,nAddr,nControl );
	switch (nControl)
	{
		case PANTILT_PAN_LEFT:
			Command.Data1	= 0x00;
			Command.Data2	= 0x04;		// PanLeft
			break;
		case PANTILT_PAN_RIGHT:
			Command.Data1	= 0x00;
			Command.Data2	= 0x02;		// PanRight
			break;
		case PANTILT_TILT_UP:
			Command.Data1	= 0x00;
			Command.Data2	= 0x08;		// TiltUp
			break;
		case PANTILT_TILT_DOWN:
			Command.Data1	= 0x00;
			Command.Data2	= 0x10;		// TiltDown
			break;
		case PANTILT_ZOOM_IN:
			Command.Data1	= 0x00;
			Command.Data2	= 0x20;		// ZoomIn
			break;
		case PANTILT_ZOOM_OUT:
			Command.Data1	= 0x00;
			Command.Data2	= 0x40;		// ZoomOut
			break;
		case PANTILT_FOCUS_NEAR:
			Command.Data1	= 0x02;		// FocusNear
			Command.Data2	= 0x00;
			break;
		case PANTILT_FOCUS_FAR:
			Command.Data1	= 0x01;		// FocusFar
			Command.Data2	= 0x00;
			break;
		default:
			Command.Data1	= 0x00;
			Command.Data2	= 0x00;
			break;
	}

	Command.Etx		 =	0xAF;
	Command.CheckSum =	Command.Stx	  ^
						Command.Addr  ^
						Command.Data1 ^
						Command.Data2 ^
						Command.Data3 ^
						Command.Data4 ^
						Command.Etx;

	printf1("Write PELCO-P Protocol...\n");
	//printf1("Command=%s\n",Command);
	int err=-1;
	 err=WriteTo485( &Command, sizeof(Command) );
	printf1("err=%d\n",err);
	return err;

}

// PELCO-D Protocol
int ControlPELCO_D(int nPort, int nAddr, int nControl)
{
	COMMPELCOD	Command;
	int			nPTZSpeed = (int)(((float)0x3f / 10.0) * (float)DEFAULT_PTZ_SPEED);

	Command.Synch	= 0xFF;
	Command.Addr	= nAddr;
	Command.Data1	= nPTZSpeed;	// pan speed : 0x00 ~ 0x3f
	Command.Data2	= nPTZSpeed;	// tilt speed : 0x00 ~ 0x3f

	switch (nControl)
	{
		case PANTILT_PAN_LEFT:
			Command.Cmd1	= 0x00;
			Command.Cmd2	= 0x04;		// PanLeft
			break;
		case PANTILT_PAN_RIGHT:
			Command.Cmd1	= 0x00;
			Command.Cmd2	= 0x02;		// PanRight
			break;
		case PANTILT_TILT_UP:
			Command.Cmd1	= 0x00;
			Command.Cmd2	= 0x08;		// TiltUp
			break;
		case PANTILT_TILT_DOWN:
			Command.Cmd1	= 0x00;
			Command.Cmd2	= 0x10;		// TiltDown
			break;
		case PANTILT_ZOOM_IN:
			Command.Cmd1	= 0x00;
			Command.Cmd2	= 0x20;		// ZoomIn
			break;
		case PANTILT_ZOOM_OUT:
			Command.Cmd1	= 0x00;
			Command.Cmd2	= 0x40;		// ZoomOut
			break;
		case PANTILT_FOCUS_NEAR:
			Command.Cmd1	= 0x01;		// FocusNear
			Command.Cmd2	= 0x00;
			break;
		case PANTILT_FOCUS_FAR:
			Command.Cmd1	= 0x00;		// FocusFar
			Command.Cmd2	= 0x80;
			break;
		case PANTILT_IRIS_OPEN:
			Command.Cmd1	= 0x02;		// IrisOpen
			Command.Cmd2	= 0x00;
			break;
		case PANTILT_IRIS_CLOSE:
			Command.Cmd1	= 0x04;		// IrisClose
			Command.Cmd2	= 0x00;
			break;
		default:
			Command.Cmd1	= 0x00;
			Command.Cmd2	= 0x00;		
			break;
	}

	Command.CheckSum =	Command.Addr + 
						Command.Cmd1 + 
						Command.Cmd2 + 
						Command.Data1 + 
						Command.Data2;

//	printf1("Write PELCO-D Protocol...\n");
	int err;
	err= WriteTo485( &Command, sizeof(Command) );
	printf1("err=%d\n",err);
	return err;
}

#ifdef SUPPORT_PTZ_LILIN
// MERIT LILIN PIH7000 Protocol
int ControlMERITLILIN(int nPort, int nAddr, int nControl)
{
    COMMMERITLILIN   Command;
    Command.Addr = nAddr;       // 01H ~ 40H

    switch (nControl)
	{
        case PANTILT_PAN_LEFT:
            Command.Cmd1 = 0x02;        // PanLeft
            Command.Cmd2 = 0xA4;
            break;
        case PANTILT_PAN_RIGHT:
            Command.Cmd1 = 0x01;        // PanRight
            Command.Cmd2 = 0xA4;
            break;
        case PANTILT_TILT_UP:
            Command.Cmd1 = 0x04;        // TiltUp
            Command.Cmd2 = 0xA4;
            break;
        case PANTILT_TILT_DOWN:
            Command.Cmd1 = 0x08;        // TiltDown
            Command.Cmd2 = 0xA4;
            break;
        case PANTILT_ZOOM_IN:
            Command.Cmd1 = 0x10;        // ZoomIn
            Command.Cmd2 = 0xA4;
            break;
        case PANTILT_ZOOM_OUT:
            Command.Cmd1 = 0x20;        // ZoomOut
            Command.Cmd2 = 0xA4;
            break;
        case PANTILT_FOCUS_NEAR:
            Command.Cmd1 = 0x40;        // FocusNear
            Command.Cmd2 = 0xA4;
            break;
        case PANTILT_FOCUS_FAR:
            Command.Cmd1 = 0x80;        // FocusFar
            Command.Cmd2 = 0xA4;
            break;
        default:
            return  FALSE;
	}

    return WriteTo485( &Command, sizeof(Command) );
}
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -