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

📄 p1240ptp.cpp

📁 基于PCI-1240运动控制卡的示例5-PTP
💻 CPP
📖 第 1 页 / 共 2 页
字号:

//*****************************************************************************
// Program        : P1240PTP.CPP                                                        
// Description    : PCI-1240 demo program for XYZU-Axis 
//                  point to point function.        
// APIs used      : P1240MotDevOpen,
//                  P1240MotAxisParaSet,P1240MotPtp,P1240AxisBusy,      
//                  P1240MotDevClose                                                   
// Author         : xianfeng.yan                                              
// Revision       : 1.10                                                      
// Created        : 01/19/2005           Advantech Co., Ltd..                     
//*****************************************************************************

#include <windows.h>
#include <windef.h>
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
#include <winreg.h>

#include "ADS1240.h"

#include "resource.h"

void main()
{
   DWORD dwReturnCode;

   BYTE byBoard_ID = 0;
   BYTE byTS = TCurveAcceleration;
   BYTE byRA = RelativeCoordinate;
   BYTE byArcDirection = CCW;
//   BYTE byStopMode;
   DWORD dwSV = 1000;   //Start Velocity, 1000 pulse per second
   DWORD dwDV = 0;   //Drive Speed, 8000 pulse per second
   DWORD dwAC = 200000; //Acceleration Speed, 480000 pulse per second
   DWORD dwAK = 2000000; //Acceleration Rate, 960000 pulse per second
   DWORD dwMDV = 800000;//Maximum Drive Speed, 800000 pulse per second
   LONG dwDVY=0;
   LONG dwDVZ=0;
   int i=0;
   int a;
   char back;
//   USHORT Value;
   //the unit for the following variable is pulse
   long lPulseX = 100000;//-200000;
   long lPulseY = 100000;//-200000;
   long lPulseZ = 100000;
   long lPulseU = 0;
   long lPulseX2,lPulseY2;

   printf(" PCI-1240 demo program for XYZU-Axis point to point function.\n\n");
   printf(" T curve acceleration \n");
   printf(" Relative coordinate \n");
   printf(" MDV(Maximum Drive Speed) : %7d\n", dwMDV);
   printf(" SV(Start Velocity)       : %7d\n", dwSV);
   printf(" DV(Drive Speed)          : %7d\n", dwDV);
   printf(" AC(Acceleration Speed)   : %7d\n", dwAC);
   printf(" AK(Acceleration Rate)    : %7d\n", dwAK);
   printf(" X PULSE : %6d\n",lPulseX);
   printf(" Y PULSE : %6d\n",lPulseY);
   printf(" Z PULSE : %6d\n",lPulseZ);
   printf(" U PULSE : %6d\n",lPulseU);

   //If PCI-1240 drives a servo motor,
   //a "SERVO-ON" signal should be sent to the driver before moving.
   //If this signal is not auto set when power on, you can follow these steps:
   //1. Power Off.
   //2. Connect "nOut6" to the driver's "SERVO-ON" signal.(n stand for X,Y,Z,U)
   //3. Power On.
   //4. Call "P1240SetOutputType" to set general output type. 
   //5. Call "P1240MotDO" to enable "SERVO-ON".
   printf(" If you use servo motor, Please make sure that the Servo is on!\n");

   printf(" Please input BoardID (the range is from 0 to 15)\n");
   int tempID;
   cin>>tempID;
   while ( (tempID < 0 ) || (tempID > 15) )
   {
      printf("BoardID out of range, please enter a correct one!\n");
      scanf("%d", &tempID);
   }
   byBoard_ID = tempID;      
   
   // Device open
   dwReturnCode = P1240MotDevOpen(byBoard_ID);
   if ( dwReturnCode != ERROR_SUCCESS )
   {
      printf("\n\n Program Fail 0x%04x", dwReturnCode);
      printf("\n Board %d doesn't exsit !", byBoard_ID);
      printf("\n Press any key to exit....");
      getch();
      exit(1);
   }
   else
   {
      printf("Device open success!\n");
   }


   // Set XYZU-Axis parameter


RESTART:
   // set the mothome type
   // moter move home

/*   dwReturnCode = P1240MotHome(byBoard_ID,XY_Axis);
   if ( dwReturnCode != ERROR_SUCCESS )
	   cout<<"\n MotHome faile!"<<"    "<<dwReturnCode<<endl;

		while ( P1240MotAxisBusy(byBoard_ID, XY_Axis) != ERROR_SUCCESS )
		{
			 ;
		}
*/
   // Set XYZU-Axis parameter

   cout<<"Please choose the motion type: 0 : for and back,1 : line interpolation,"<<endl;
   cout<<"                               2 : move n points"<<endl;
   cin>>a;
   if(a==0)
   {	   
	   	dwDV = 50000;
		dwReturnCode = P1240MotAxisParaSet(
	      byBoard_ID, // Board ID number
	      XYZ_Axis,  // Assign the operation axis
	      byTS,       // Assign the acceleration type T or S 
	      dwSV,			// Assign the initial speed
	      dwDV,			// Assign the drive speed
	      dwMDV,		// Assign the Maximum drive speed
	      dwAC,       // Assign the acceleration speed
	      dwAK);      // Assign the acceleration rate
	    if ( dwReturnCode != ERROR_SUCCESS )
	      printf("\n\n Program Fail 0x%04x", dwReturnCode);

/*   		cout<<"Please input the pulse of x axis,y axis and z axis:";
		cout<<"lPulseX = ";
		cin>>lPulseX;
		cout<<"		lPulseY = ";
		cin>>lPulseY;
		cout<<lPulseX<<"      "<<lPulseY<<endl;
		cout<<"lPulseZ = ";
		cin>>lPulseZ;
		cout<<lPulseZ<<"      "<<lPulseZ<<endl;*/

		//设定各轴速度
		int k,kk,d,b,c;
		d=lPulseX;
		b=lPulseY;
		k=b/d;
		c=lPulseZ;
//		kk=k;
	 	dwDVY = dwDV*k;//40000;

		kk=c/d;
//		kk=k;
		dwDVZ = dwDV*kk;

		dwReturnCode = P1240SetDrivingSpeed(
		  byBoard_ID, // Board ID number
	      Y_Axis,  // Assign the operation axis
	      dwDVY);
	   if ( dwReturnCode != ERROR_SUCCESS )
	      printf("\n\n Program Fail--set speed 0x%04x", dwReturnCode);

		dwReturnCode = P1240SetDrivingSpeed(
		  byBoard_ID, // Board ID number
	      Z_Axis,  // Assign the operation axis
	      dwDVZ);

	   if ( dwReturnCode != ERROR_SUCCESS )
	      printf("\n\n Program Fail--SET SPEED 0x%04x", dwReturnCode);

//RESTART:

	   while(i<10)
	   {
		   i++;
// point to point
			dwReturnCode = P1240MotPtp(
				byBoard_ID, // Board ID number
				XYZ_Axis,  // Assign the operation axis
				byRA,       // Assign movement operation is relative 
           // or absolute coordinate
				lPulseX,    // The movement pulses number for X axis
				lPulseY,    // The movement pulses number for Y axis
				lPulseZ,    // The movement pulses number for Z axis
				lPulseU);   // The movement pulses number for U axis
			if ( dwReturnCode != ERROR_SUCCESS )
				printf("\n\n Program Fail 0x%04x", dwReturnCode);
			else
				printf("\n Moving......\n");
			// Program wait when axis busy
			while ( P1240MotAxisBusy(byBoard_ID, XYZ_Axis) != ERROR_SUCCESS )
			{
				 ;
			}
		  lPulseX = lPulseX * (-1);
		  lPulseY = lPulseY * (-1);	
		  lPulseZ = lPulseZ * (-1);

	   }
	   cout<<"If you want to restart,press r."<<endl;
	   cin>>back;
	   if((back == 'R') || (back == 'r'))
		  goto RESTART;
//	   else
//		   break;
	}		   
	else if(a==1)
	{
	   cout<<"Please input the DV of the line interpolation:"<<endl;
	   cin>>dwDV;
	   cout<<"Please input the pulse of x axis and y axis:";
	   cout<<"lPulseX = ";
	   cin>>lPulseX;
	   cout<<"		lPulseY = ";
	   cin>>lPulseY;
   // Set linear interpolation parameter
	   dwReturnCode = P1240MotAxisParaSet(
	      byBoard_ID, // Board ID number
	      0,          // Set 0 for Arc or Line Interpolation
	      byTS,       // Assign the acceleration type T or S 
	      dwSV,       // Assign the initial speed
	      dwDV,       // Assign the drive speed
	      dwMDV,      // Assign the Maximum drive speed
	      dwAC,       // Assign the acceleration speed
	      dwAK);      // Assign the acceleration rate
	   if ( dwReturnCode != ERROR_SUCCESS )
	      printf("\n\n Program Fail 0x%04x", dwReturnCode);
//RAETART:
   // Start line function
	   dwReturnCode = P1240MotLine(
	      byBoard_ID, //Board ID number
	      XY_Axis,    //Assign operation axis
	      byRA,       //Assign movement operation is relative 
                //or absolute coordinate
	      lPulseX,    //The new position for X axis
	      lPulseY,    //The new position for Y axis
	      0,          //The new position for Z axis
	      0);         //The new position for U axis
	   if ( dwReturnCode != ERROR_SUCCESS )
	      printf("\n\n Program Fail 0x%04x", dwReturnCode);

⌨️ 快捷键说明

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