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

📄 pciio.c

📁 一个机器人的源代码.软件设计得超级好!是商业级代码.
💻 C
字号:
// PCIio.cpp : Defines the entry point for the console application.
//

#define PMD_PCI_INTERFACE

#include "c-motion.h"
#include "PMDutil.h"
#include "PMDpci.h"
#include "PMDconio.h"

#include <stdlib.h>
#include <stdio.h>

#ifdef _LINUX
#include "ConsFunc.h"
#endif

PMDuint16 PMDReadBufferPCI(PMDAxisHandle* hAxis, PMDuint32* pbuffer, PMDuint16 bufferID, PMDuint16 dwords_to_read);
#ifndef _LINUX
void InterruptExample(PMDAxisHandle* hAxis);
#endif

// the axis handle object
PMDAxisHandle hAxis1,hAxis2;


int main(int argc, char* argv[])
{
#define bufsize 1024
	PMDuint8 ui8major, ui8minor;
	PMDuint16 generation, motorType, numberAxes, special, custom, major, minor;
	PMDuint16 status;
    int i;

	PMDuint32 buffer[bufsize];
	PMDuint16 bufferID = 0;

#ifdef _LINUX
	ConsoleInitialize();
   	Cls();
#endif
	if (PMD_NOERROR != PMDSetupAxisInterface_PCI( &hAxis1, PMDAxis1, 0 ))
    {
		PMDprintf("Board initialization failed\n");
#ifdef _LINUX 
        	while(!PMDkbhit()); 
   		ConsoleEnd();
    		exit(0);
#endif
		return 1;
    }

    //((PMDPCIIOTransportData*)hAxis1.transport_data)->bDiagnostics = 1;

	// use the same transport for Axis#2 because it resides on the same chip
	// so must use the same interface



	
	if (PMD_NOERROR != PMDChipsetReset( &hAxis1 ))
	{
  	free( hAxis1.transport_data );
		PMDprintf("Reset failed\n");
#ifdef _LINUX 
        	while(!PMDkbhit()); 
   		ConsoleEnd();
    		exit(0);
#endif		
		return 1;
	}

	PMDGetCMotionVersion( &ui8major, &ui8minor );

	PMDprintf("C-Motion Version %d.%d \n", ui8major, ui8minor);

	// just do some easy gets to make sure comms are working
	PMDGetVersion(&hAxis1, &generation, &motorType, &numberAxes, &special, &custom, &major, &minor);

	PMDprintf("MC%d%d%d%d Version %d.%d\n\n", generation, motorType, numberAxes, custom, major, minor);

	PMDGetEventStatus(&hAxis1, &status);
	PMDprintf("Axis#1 Event Status: %4X\n",status);
	
	if(numberAxes>1)
	{
		PMDCopyAxisInterface( &hAxis2, &hAxis1, PMDAxis2 );
		PMDGetEventStatus(&hAxis2, &status);
		PMDprintf("Axis#2 Event Status: %4X\n",status);
	}

#ifndef _LINUX
	InterruptExample(&hAxis1);
#endif


    	memset(buffer, 0, sizeof(PMDuint32)*bufsize);

	// setup up some on chip ram and write to it
	PMDSetBufferLength( &hAxis1, bufferID, bufsize );

	for (i=0;i<bufsize;i++)
		PMDWriteBuffer( &hAxis1, bufferID, i );

	// read some memory
	PMDReadBufferPCI( &hAxis1, &buffer[0], bufferID, bufsize);

	// free any axis handle memory that was allocated
	PMDCloseAxisInterface(&hAxis1);

#ifdef _LINUX 
        while(!PMDkbhit()); 
   	ConsoleEnd();
    	exit(0);
#endif
	return 0;
}


/***********************************************************************************

  PMDReadBufferPCI

  Implements the same functionality as the chip command ReadBuffer except it reads the data
  through the PCI interface directly from the DPRAM.  It is at least 100 times faster than
  calling ReadBuffer for the same number of data reads.
  To further improve performance the PMDGet... commands can be removed and the values stored
  in local variables

/***********************************************************************************/
PMDuint16 PMDReadBufferPCI(PMDAxisHandle* hAxis, PMDuint32* pbuffer, PMDuint16 bufferID, PMDuint16 dwords_to_read)
{
	PMDuint32 index,length,start;

	PMDGetBufferReadIndex(hAxis, bufferID, &index);
	PMDGetBufferLength(hAxis, bufferID, &length);
	PMDGetBufferStart(hAxis, bufferID, &start);

	// can't read more data than the buffer can hold!
	if (dwords_to_read>length)
		return 1;



	// check if this is a wrap-around condition
	if (dwords_to_read > (length-index))
	{
		// read the first segment
		PMDPCI_ReadDPRAM( hAxis->transport_data, pbuffer, index+start, (length-index));
		// read the next segment
		PMDPCI_ReadDPRAM( hAxis->transport_data, pbuffer+(length-index), start, dwords_to_read-(length-index));
	}
	else
		PMDPCI_ReadDPRAM( hAxis->transport_data, pbuffer, index+start, dwords_to_read);

	PMDSetBufferReadIndex(hAxis, bufferID, dwords_to_read%length);

	return 0;
}



#ifndef _LINUX
void InterruptExample(PMDAxisHandle* hAxis)
{
	HANDLE EventHdl = NULL;
	DWORD EventStatus;

	PMDResetEventStatus(hAxis, 0);
	PMDClearInterrupt(hAxis);
	PMDSetInterruptMask(hAxis, PMDEventMotionCompleteMask);

	PMDPCI_SetInterruptEvent(hAxis, &EventHdl);

	PMDSetAcceleration(hAxis, 1000);
	PMDSetVelocity(hAxis, 10000);
	PMDSetPosition(hAxis, 1000);
	PMDUpdate(hAxis);

	// interrupt should occur once motion is complete
	EventStatus = WaitForSingleObject(EventHdl, 10000); // 10sec timeout
	ResetEvent(EventHdl);

	switch (EventStatus)
	{
	case WAIT_OBJECT_0:
		// Interrupt occurred
        PMDprintf("*Interrupt Received*\n");
		break;

	case WAIT_TIMEOUT:
		// ERROR - Timeout waiting for Interrupt Event
        PMDprintf("*Timeout waiting for interrupt*\n");
		break;

	case WAIT_FAILED:
		// ERROR - Failed while waiting for interrupt
        PMDprintf("*ERROR* - Failed while waiting for interrupt\n");
		break;
	}
}
#endif

⌨️ 快捷键说明

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