Click here to Skip to main content
15,887,485 members
Articles / Desktop Programming / MFC

Matlab 7.1+ and Visual Studio 2005

Rate me:
Please Sign up or sign in to vote.
4.60/5 (9 votes)
11 Jul 2007CPOL3 min read 96.4K   1.4K   39  
An article describing how to compile Matlab 7.1-7.4 mexw32 files using Visual Studio 2005
// MatlabVS.cpp : Defines the entry point for the console application.
// by A. Bryant Nichols Jr. 2007

#include "stdafx.h"
#include <Windows.h>
#include "mex.h"
#include "matrix.h"
//#include <AerSys.h>

void StartDevice();

int iCase=0;
double dist1=0;
double dist2=0;
double dXPos, dYPos;
double *data1;
TCHAR szProgram[_MAX_PATH];
const double AZCOUNTS=87466.666667;
const double ELCOUNTS=-65777.777778;

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
	if (nrhs>0)
	{
		data1 = mxGetPr(prhs[0]);
		iCase = (int)data1[0];
	}
	if (nrhs>1)
	{
		data1 = mxGetPr(prhs[1]);
		dist1 = data1[0];
		if (iCase==4)
		{
			char buf[128];
			mwSize buflen;
			/* Allocate enough memory to hold the converted string. */ 
			buflen = mxGetNumberOfElements(prhs[1]) + 1;		
			mxGetString(prhs[1], buf, buflen);
			sprintf_s(szProgram,buf);
		}
	}
	if (nrhs>2)
	{
		data1 = mxGetPr(prhs[2]);
		dist2 = data1[0];
	}
	StartDevice();
	if (iCase==3)
	{
		if (nlhs>=2)
		{
			plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
			plhs[1] = mxCreateDoubleMatrix(1, 1, mxREAL);
			data1 = mxGetPr(plhs[0]);
			data1[0] = dXPos/AZCOUNTS;
			data1 = mxGetPr(plhs[1]);
			data1[0] = dYPos/ELCOUNTS;
		}
	}
	return;
}

// This function is just an example, it will not compile without the correct libraries.
void StartDevice()
{
/*	HAERCTRL          hAerCtrl = NULL;
	AERERR_CODE       eRc = AERERR_NOERR;
	HCOMPILER         hCompiler = NULL;
	DWORD             dwSpeeds[2];
	LONG              lDists[2];
	DWORD			  dwMode;

	if (iCase==1)
	{
		printf("Initializing...");
		eRc = AerSysInitialize( 0, NULL, 1, &hAerCtrl, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
		eRc = AerMoveEnable( hAerCtrl, AXISINDEX_1);
		eRc = AerMoveWaitDone( hAerCtrl, AXISINDEX_1, 1000, 0 );
		eRc = AerMoveEnable( hAerCtrl, AXISINDEX_2);
		eRc = AerMoveWaitDone( hAerCtrl, AXISINDEX_2, 1000, 0 );
		Sleep(1000);
	}
	else
	{
		eRc = AerSysOpen( AER_UNIDEX_DEFAULT, AER_CARD_DEFAULT, &hAerCtrl );
		if( hAerCtrl )
		{
			switch (iCase)
			{
			case 0:
				// Exit activities
				printf("Shutting Down NDrives...");
				lDists[0]=0;
				lDists[1]=0;
				dwSpeeds[0]=(DWORD)AZCOUNTS*10;
				dwSpeeds[1]=(DWORD)AZCOUNTS*10;
				eRc = AerMoveMAbsolute( hAerCtrl, (AXISMASK_1 | AXISMASK_2), lDists, dwSpeeds);
				eRc = AerMoveMWaitDone( hAerCtrl, (AXISMASK_1 | AXISMASK_2), 1000, 0 );
				// Reset Drive hardware (kills SMC also)
				eRc = AerSysStop( hAerCtrl );
				break;
			case 2:
				//Move Absolute Function
				lDists[0]=AZCOUNTS*dist1;
				lDists[1]=ELCOUNTS*dist2;
				dwSpeeds[0]=(DWORD)AZCOUNTS*10;
				dwSpeeds[1]=(DWORD)AZCOUNTS*10;
				eRc = AerMoveMAbsolute( hAerCtrl, (AXISMASK_1 | AXISMASK_2), lDists, dwSpeeds);
				eRc = AerMoveMWaitDone( hAerCtrl, (AXISMASK_1 | AXISMASK_2), 1000, 0 );
				Sleep(1000);
				break;
			case 3:
				//Position Return
				eRc = AerParmGetValue( hAerCtrl, AER_PARMTYPE_AXIS, AXISINDEX_1, AXISPARM_PositionCnts, 0, &dXPos);
				eRc = AerParmGetValue( hAerCtrl, AER_PARMTYPE_AXIS, AXISINDEX_2, AXISPARM_PositionCnts, 0, &dYPos);
				printf( "AZ Pos: %f", dXPos/AZCOUNTS);
				printf( "EL Pos: %f", dYPos/ELCOUNTS);
				break;
			case 4:
				//Run GCode Program
				printf("Running Program...");
				dwMode = AERPROG_RUN_FILE_CMPL;
				eRc = AerProgramRun( hAerCtrl, szProgram, dwMode, TASKINDEX_1);
				break;
			case 5:
				//Move Incremental Function
				lDists[0]=AZCOUNTS*dist1;
				lDists[1]=ELCOUNTS*dist2;
				dwSpeeds[0]=(DWORD)AZCOUNTS*10;
				dwSpeeds[1]=(DWORD)AZCOUNTS*10;
				eRc = AerMoveMIncremental( hAerCtrl, (AXISMASK_1 | AXISMASK_2), lDists, dwSpeeds);
				Sleep(1000);
				break;
			case 6:
				//Home
				printf("Homing...");
				eRc = AerMoveHome( hAerCtrl, AXISINDEX_1 );
				eRc = AerMoveWaitDone( hAerCtrl, AXISINDEX_1, 1000, 0 );
				Sleep(25000);
				eRc = AerMoveHome( hAerCtrl, AXISINDEX_2 );
				eRc = AerMoveWaitDone( hAerCtrl, AXISINDEX_2, 1000, 0 );
				Sleep(25000);	
				printf("Homing Complete.");
			}
		}
		else
			printf("NMotion Handle Not Found.");
	}
	printf("\n");
	return;*/
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
I mainly use Visual Studio 2005 for programming. I am an electrical engineer in Washington DC.

Comments and Discussions