void InitVectors() { const mxArray *mx_comm; char *str; /* Check if the Matlab global variable sundials_MPI_comm exists (mpirun and mpiruns set it) */ mx_comm = mexGetVariable("global", "sundials_MPI_comm"); if (mx_comm == NULL) { /* If it does not exist, set vector type to 1 (serial) */ sundials_VecType = 1; } else { /* If it does exist, set vector type to 2 (parallel) and set the MPI communicator */ sundials_VecType = 2; str = mxArrayToString(mx_comm); if (!strcmp(str,"NULL" )) sundials_comm = MPI_COMM_NULL ; else if (!strcmp(str,"WORLD" )) sundials_comm = MPI_COMM_WORLD ; else if (!strcmp(str,"SELF" )) sundials_comm = MPI_COMM_SELF ; else sundials_comm = *(MPI_Comm*)mxGetData(mx_comm); } }
void BERCalculationSIMO_api(const mxArray * const prhs[2], const mxArray *plhs[1]) { emxArray_real_T *bitsRX; real_T type; const mxArray *tmp; emlrtHeapReferenceStackEnterFcnR2012b(emlrtRootTLSGlobal); emxInit_real_T(&bitsRX, 1, &b_emlrtRTEI, TRUE); /* Marshall function inputs */ c_emlrt_marshallIn(emlrtAlias(prhs[0]), "bitsRX", bitsRX); type = emlrt_marshallIn(emlrtAliasP(prhs[1]), "type"); /* Marshall in global variables */ tmp = mexGetVariable("global", "d"); if (tmp) { e_emlrt_marshallIn(tmp, "d", &d); d_dirty = 0U; } /* Invoke the target function */ type = BERCalculationSIMO(bitsRX, type); /* Marshall out global variables */ mexPutVariable("global", "d", b_emlrt_marshallOut(&d)); /* Marshall function outputs */ plhs[0] = emlrt_marshallOut(type); bitsRX->canFreeData = FALSE; emxFree_real_T(&bitsRX); emlrtHeapReferenceStackLeaveFcnR2012b(emlrtRootTLSGlobal); }
void _testNamespaces_RTTIRegister() { const mxArray *alreadyCreated = mexGetVariablePtr("global", "gtsam_testNamespaces_rttiRegistry_created"); if(!alreadyCreated) { std::map<std::string, std::string> types; mxArray *registry = mexGetVariable("global", "gtsamwrap_rttiRegistry"); if(!registry) registry = mxCreateStructMatrix(1, 1, 0, NULL); typedef std::pair<std::string, std::string> StringPair; for(const StringPair& rtti_matlab: types) { int fieldId = mxAddField(registry, rtti_matlab.first.c_str()); if(fieldId < 0) mexErrMsgTxt("gtsam wrap: Error indexing RTTI types, inheritance will not work correctly"); mxArray *matlabName = mxCreateString(rtti_matlab.second.c_str()); mxSetFieldByNumber(registry, 0, fieldId, matlabName); } if(mexPutVariable("global", "gtsamwrap_rttiRegistry", registry) != 0) mexErrMsgTxt("gtsam wrap: Error indexing RTTI types, inheritance will not work correctly"); mxDestroyArray(registry); mxArray *newAlreadyCreated = mxCreateNumericMatrix(0, 0, mxINT8_CLASS, mxREAL); if(mexPutVariable("global", "gtsam_testNamespaces_rttiRegistry_created", newAlreadyCreated) != 0) mexErrMsgTxt("gtsam wrap: Error indexing RTTI types, inheritance will not work correctly"); mxDestroyArray(newAlreadyCreated); } }
/* Function: mdlStart ======================================================= * Abstract: * This function is called once at * start of model execution. If you * have states that should be initialized once, this is the place * to do it. */ static void mdlStart(SimStruct *S) { mxArray *array_ptr; double *tmp; char COM; if (verbose) printf("URG: call mdlStart()\n"); // Leemos el COM definido por el usuario array_ptr = mexGetVariable("caller", "COM"); if (array_ptr == NULL ){ printf("URG: No se encontro la variable COM. Se intentara usar COM1\n"); verbose = 0; } else { tmp=(double*)(mxGetData(array_ptr)); COM=(unsigned char)(*tmp); printf("URG: Usando COM%d\n",COM); } // Leemos el flag Verbose array_ptr = mexGetVariable("caller", "Verbose"); if (array_ptr == NULL ){ printf("URG: No se encontro la variable Verbose. Se desactiva Verbose"); COM=1; } else { tmp=(double*)(mxGetData(array_ptr)); verbose=(int)(*tmp); if (verbose) { printf("URG: Verbose activado\n"); } else { printf("URG: Verbose desactivado\n"); } } /* Destroy array */ mxDestroyArray(array_ptr); memset(buf, 0, sizeof(buf)); memset(res, 0, sizeof(res)); openURG(COM); urg_start_single_scan(); if (verbose) printf("URG: return mdlStart()\n"); }
void _geometry_RTTIRegister() { const mxArray *alreadyCreated = mexGetVariablePtr("global", "gtsam_geometry_rttiRegistry_created"); if(!alreadyCreated) { std::map<std::string, std::string> types; mxArray *registry = mexGetVariable("global", "gtsamwrap_rttiRegistry"); if(!registry) registry = mxCreateStructMatrix(1, 1, 0, NULL); typedef std::pair<std::string, std::string> StringPair; BOOST_FOREACH(const StringPair& rtti_matlab, types) { int fieldId = mxAddField(registry, rtti_matlab.first.c_str()); if(fieldId < 0) mexErrMsgTxt("gtsam wrap: Error indexing RTTI types, inheritance will not work correctly"); mxArray *matlabName = mxCreateString(rtti_matlab.second.c_str()); mxSetFieldByNumber(registry, 0, fieldId, matlabName); }
void addflops(unsigned fl) { #if 0 mxArray *flopcount = mexGetArray("flopcount","global"); if(flopcount && !mxIsEmpty(flopcount))) { *mxGetPr(flopcount) += fl; mexPutArray(flopcount,"global"); } #else /* Matlab 6.5 */ mxArray *flopcount = mexGetVariable("global","flopcount"); if(flopcount && !mxIsEmpty(flopcount)) { *mxGetPr(flopcount) += fl; mexPutVariable("global","flopcount",flopcount); } #endif }
/* Function: mdlInitializeSampleTimes ========================================= * Abstract: * This function is used to specify the sample time(s) for your * S-function. You must register the same number of sample times as * specified in ssSetNumSampleTimes. */ static void mdlInitializeSampleTimes(SimStruct *S) { mxArray *array_ptr; double sampleTime; // printf("URG: 2222\n"); // Leemos el sample time definido por el usuario array_ptr = mexGetVariable("caller", "SampleTime"); if (array_ptr == NULL ){ printf("URG: No se encontro la variable SampleTime. Se usará 0.5\n"); sampleTime = 0.5; } else { sampleTime=*((double*)(mxGetData(array_ptr))); printf("URG: Usando variable SampleTime con valor = %f\n", sampleTime); } //sampleTime = *sampleTimeTmp; /* Destroy array */ mxDestroyArray(array_ptr); ssSetSampleTime(S, 0, sampleTime); ssSetOffsetTime(S, 0, 0.0); }
/* Function: mdlInitializeSampleTimes ========================================= * Abstract: * Specifiy that we inherit our sample time from the driving block. */ static void mdlInitializeSampleTimes(SimStruct *S) { InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0); mxArray *array_ptr; double stTmp; // Leemos el sample time definido por el usuario array_ptr = mexGetVariable("caller", "SampleTime"); if (array_ptr == NULL ){ mexPrintf("No se encontro la variable SampleTime. Se usará 0.001\n"); stTmp = 0.001; } else { stTmp=*((double*)(mxGetData(array_ptr))); mexPrintf("Usando variable SampleTime con valor = %f\n", sampleTime); } sampleTime = stTmp; /* Destroy array */ mxDestroyArray(array_ptr); ssSetSampleTime(S, 0, sampleTime); ssSetOffsetTime(S, 0, 0.0); }
//Gateway routine void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { //Read input arguments char outputFormat[10]; char outputVarName[MAXVARNAMESIZE]; int outputVarSampsPerChan; double timeout; int numSampsPerChan; bool outputData; //Indicates whether to return an outputData argument mxClassID outputDataClass; uInt32 bufSize; bool writeDigitalLines; uInt32 bytesPerChan=0; TaskHandle taskID, *taskIDPtr; int32 status; //Get TaskHandle taskIDPtr = (TaskHandle*)mxGetData(mxGetProperty(prhs[0],0, "taskID")); taskID = *taskIDPtr; //Determine if this is a buffered read operation status = DAQmxGetBufInputBufSize(taskID, &bufSize); if (status) handleDAQmxError(status, "DAQmxGetBufInputBufSize"); //Handle input arguments if ((nrhs < 2) || mxIsEmpty(prhs[1]) || mxIsInf(mxGetScalar(prhs[1]))) { if (bufSize==0) numSampsPerChan = 1; else numSampsPerChan = DAQmx_Val_Auto; } else numSampsPerChan = (int) mxGetScalar(prhs[1]); if ((nrhs < 3) || mxIsEmpty(prhs[2])) { //Automatic determination of read type bool isLineBased = (bool) mxGetScalar(mxGetProperty(prhs[0],0,"isLineBasedDigital")); if ((bufSize==0) && isLineBased) //This is a non-buffered, line-based Task: return data as a double array outputDataClass = mxDOUBLE_CLASS; else { status = DAQmxGetReadDigitalLinesBytesPerChan(taskID,&bytesPerChan); //This actually returns the number of bytes required to represent one sample of Channel data if (status) handleDAQmxError(status, "DAQmxGetReadDigitalLinesBytesPerChan"); if (bytesPerChan <= 8) outputDataClass = mxUINT8_CLASS; else if (bytesPerChan <= 16) outputDataClass = mxUINT16_CLASS; else if (bytesPerChan <= 32) outputDataClass = mxUINT32_CLASS; else mexErrMsgTxt("It is not currently possible to read integer values from Task with greater than 32 lines per sample value"); } } else { mxGetString(prhs[2], outputFormat, 10); if (_strcmpi(outputFormat,"uint8")) outputDataClass = mxUINT8_CLASS; else if (_strcmpi(outputFormat,"uint16")) outputDataClass = mxUINT16_CLASS; else if (_strcmpi(outputFormat,"uint32")) outputDataClass = mxUINT32_CLASS; else if (_strcmpi(outputFormat,"double")) outputDataClass = mxDOUBLE_CLASS; else if (_strcmpi(outputFormat,"logical")) outputDataClass = mxLOGICAL_CLASS; else mexErrMsgTxt("The specified 'outputFormat' value (case-sensitive) is not recognized."); } if ((outputDataClass == mxDOUBLE_CLASS) || (outputDataClass == mxLOGICAL_CLASS)) { writeDigitalLines = true; if (bytesPerChan == 0) { status = DAQmxGetReadDigitalLinesBytesPerChan(taskID,&bytesPerChan); //This actually returns the number of bytes required to represent one sample of Channel data if (status) handleDAQmxError(status, "DAQmxGetReadDigitalLinesBytesPerChan"); } } else writeDigitalLines = false; if ((nrhs < 4) || mxIsEmpty(prhs[3]) || mxIsInf(mxGetScalar(prhs[3]))) timeout = DAQmx_Val_WaitInfinitely; else timeout = mxGetScalar(prhs[3]); if ((nrhs < 5) || mxIsEmpty(prhs[4])) //OutputVarSizeOrName argument { outputData = true; outputVarSampsPerChan = numSampsPerChan; //If value is DAQmx_Val_Auto, then the # of samples available will be queried before allocting array } else { outputData = mxIsNumeric(prhs[4]); if (outputData) { if (nlhs < 2) mexErrMsgTxt("There must be two output arguments specified if a preallocated MATLAB variable is not specified"); outputVarSampsPerChan = (int) mxGetScalar(prhs[4]); } else mxGetString(prhs[4], outputVarName, MAXVARNAMESIZE); } //Determin # of output channels uInt32 numChannels; DAQmxGetReadNumChans(taskID, &numChannels); //Reflects number of channels in Task, or the number of channels specified by 'ReadChannelsToRead' property //Determine output buffer/size (creating if needed) mxArray *outputDataBuf, *outputDataBufTrue; void *outputDataPtr; //float64 *outputDataPtr; if (outputData) { mwSize numRows; if (outputVarSampsPerChan == DAQmx_Val_Auto) { status = DAQmxGetReadAvailSampPerChan(taskID, (uInt32 *)&outputVarSampsPerChan); if (status) { handleDAQmxError(status, "DAQmxGetReadAvailSampPerChan"); return; } } if (writeDigitalLines) numRows = (mwSize) (outputVarSampsPerChan * bytesPerChan); else numRows = (mwSize) outputVarSampsPerChan; if (outputDataClass == mxDOUBLE_CLASS) { outputDataBuf = mxCreateNumericMatrix(numRows,numChannels,mxUINT8_CLASS,mxREAL); outputDataBufTrue = mxCreateDoubleMatrix(numRows,numChannels,mxREAL); } else outputDataBuf = mxCreateNumericMatrix(numRows,numChannels,outputDataClass,mxREAL); } else //I don't believe this is working { outputDataBuf = mexGetVariable("caller", outputVarName); outputVarSampsPerChan = mxGetM(outputDataBuf); //TODO: Add check to ensure WS variable is of correct class } outputDataPtr = mxGetData(outputDataBuf); //Read data int32 numSampsRead; int32 numBytesPerSamp; switch (outputDataClass) { case mxUINT8_CLASS: status = DAQmxReadDigitalU8(taskID, numSampsPerChan, timeout, fillMode, (uInt8*) outputDataPtr, outputVarSampsPerChan * numChannels, &numSampsRead, NULL); break; case mxUINT16_CLASS: status = DAQmxReadDigitalU16(taskID, numSampsPerChan, timeout, fillMode, (uInt16*) outputDataPtr, outputVarSampsPerChan * numChannels, &numSampsRead, NULL); break; case mxUINT32_CLASS: status = DAQmxReadDigitalU32(taskID, numSampsPerChan, timeout, fillMode, (uInt32*) outputDataPtr, outputVarSampsPerChan * numChannels, &numSampsRead, NULL); break; case mxLOGICAL_CLASS: case mxDOUBLE_CLASS: status = DAQmxReadDigitalLines(taskID, numSampsPerChan, timeout, fillMode, (uInt8*) outputDataPtr, outputVarSampsPerChan * numChannels * bytesPerChan, &numSampsRead, &numBytesPerSamp, NULL); break; default: mexErrMsgTxt("There must be two output arguments specified if a preallocated MATLAB variable is not specified"); } //Return output data if (!status) { //mexPrintf("Successfully read %d samples of data\n", numSampsRead); if (outputData) { if (nlhs > 0) { if (outputDataClass == mxDOUBLE_CLASS) { //Convert logical data to double type double *outputDataTruePtr = mxGetPr(outputDataBufTrue); for (size_t i=0;i < mxGetNumberOfElements(outputDataBuf);i++) *(outputDataTruePtr+i) = (double) *((uInt8 *)outputDataPtr+i); mxDestroyArray(outputDataBuf); plhs[0] = outputDataBufTrue; } else plhs[0] = outputDataBuf; } else mxDestroyArray(outputDataBuf); //If you don't read out, all the reading was done for naught } else //I don't believe this is working { mexErrMsgTxt("invalid branch"); mexPutVariable("caller", outputVarName, outputDataBuf); if (nlhs > 0) //Return empty value for output data plhs[0] = mxCreateDoubleMatrix(0,0,mxREAL); } if (nlhs > 1) //Return number of samples actually read { double *sampsReadOutput; plhs[1] = mxCreateDoubleScalar(0); sampsReadOutput = mxGetPr(plhs[1]); *sampsReadOutput = (double)numSampsRead; } } else //Read failed handleDAQmxError(status, mexFunctionName()); }
void storeMBSdata(MBSdataStruct *s, char *s_name){ mxArray *s_ptr, *field_value_ptr; int i,j; /* Get pointer to MBSdataStruct */ #ifdef OLDVER s_ptr = mexGetArray(s_name,"base"); //v6 #else s_ptr = mexGetVariable("base", s_name); #endif if (s_ptr == NULL){ mexErrMsgTxt("Could not get structure variable.\n"); } //=== Work Variables ==========================================================// /* q */ field_value_ptr = mxGetField(s_ptr, 0, "q"); for(i=1;i<=s->njoint;i++) { mxGetPr(field_value_ptr)[i-1] = s->q[i]; } mxSetField(s_ptr, 0, "q", field_value_ptr); /* qd */ field_value_ptr = mxGetField(s_ptr, 0, "qd"); for(i=1;i<=s->njoint;i++) { mxGetPr(field_value_ptr)[i-1] = s->qd[i]; } mxSetField(s_ptr, 0, "qd", field_value_ptr); /* qdd */ field_value_ptr = mxGetField(s_ptr, 0, "qdd"); for(i=1;i<=s->njoint;i++) { mxGetPr(field_value_ptr)[i-1] = s->qdd[i]; } mxSetField(s_ptr, 0, "qdd", field_value_ptr); /* frc */ field_value_ptr = mxGetField(s_ptr, 0, "frc"); for(i=1;i<=3;i++) { for(j=1;j<=s->nbody;j++) { mxGetPr(field_value_ptr)[(i-1)+3*(j-1)] = s->frc[i][j]; } } mxSetField(s_ptr, 0, "frc", field_value_ptr); /* trq */ field_value_ptr = mxGetField(s_ptr, 0, "trq"); for(i=1;i<=3;i++) { for(j=1;j<=s->nbody;j++) { mxGetPr(field_value_ptr)[(i-1)+3*(j-1)] = s->trq[i][j]; } } mxSetField(s_ptr, 0, "trq", field_value_ptr); /* Qq */ field_value_ptr = mxGetField(s_ptr, 0, "Qq"); for(i=1;i<=s->njoint;i++) { mxGetPr(field_value_ptr)[i-1] = s->Qq[i]; } mxSetField(s_ptr, 0, "Qq", field_value_ptr); /* Fl */ //field_value_ptr = mxGetField(s_ptr, 0, "Fl"); field_value_ptr = mxCreateDoubleMatrix(1,s->Nlink,mxREAL); for(i=1;i<=s->Nlink;i++) { mxGetPr(field_value_ptr)[i-1] = s->Fl[i]; } mxSetField(s_ptr, 0, "Fl", field_value_ptr); /* Z */ //field_value_ptr = mxGetField(s_ptr, 0, "Z"); field_value_ptr = mxCreateDoubleMatrix(1,s->Nlink,mxREAL); for(i=1;i<=s->Nlink;i++) { mxGetPr(field_value_ptr)[i-1] = s->Z[i]; } mxSetField(s_ptr, 0, "Z", field_value_ptr); /* Zd */ //field_value_ptr = mxGetField(s_ptr, 0, "Zd"); field_value_ptr = mxCreateDoubleMatrix(1,s->Nlink,mxREAL); for(i=1;i<=s->Nlink;i++) { mxGetPr(field_value_ptr)[i-1] = s->Zd[i]; } mxSetField(s_ptr, 0, "Zd", field_value_ptr); /* ux */ field_value_ptr = mxGetField(s_ptr, 0, "ux"); for(i=1;i<=s->Nux;i++) { mxGetPr(field_value_ptr)[i-1] = s->ux[i]; } mxSetField(s_ptr, 0, "ux", field_value_ptr); /* uxd */ field_value_ptr = mxGetField(s_ptr, 0, "uxd"); for(i=1;i<=s->Nux;i++) { mxGetPr(field_value_ptr)[i-1] = s->uxd[i]; } mxSetField(s_ptr, 0, "uxd", field_value_ptr); #ifdef OLDVER mexPutArray(s_ptr,"base"); //v6 #else mexPutVariable("base", s_name,s_ptr); #endif }