Hi Brad,
Thank you for this example. Below is the code I have been
running, which is essentially identical. The issue I am having is that the text file that is generated is all zeroes, though the actual gather buffer contains true non-zero data. The file size is as expected, so I think the issue is within the case statements. Any help will be appreciated.
Nadir
#include // Global Gp Shared memory pointer
//----------------------------------------------------------------------------------
// pp_proj.h is the C header for accessing PMAC Global, CSGlobal, Ptr vars
// _PPScriptMode_ for Pmac Script like access global & csglobal
// global Mypvar - access with "Mypvar"
// global Myparray(32) - access with "Myparray(i)"
// csglobal Myqvar - access with "Myqvar(i)" where "i" is Coord #
// csglobal Myqarray(16) - access with "Myqvar(i,j)" where "j" is index
// _EnumMode_ for Pmac enum data type checking on Set & Get global functions
// Example
// global Mypvar
// csglobal Myqvar
// "SetGlobalVar(Myqvar, data)" will give a compile error because its a csglobal var.
// "SetCSGlobalVar(Mypvar, data)" will give a compile error because its a global var.
//------------------------------------------------------------------------------------
// #define _PPScriptMode_ // uncomment for Pmac Script type access
//#define _EnumMode_ // uncomment for Pmac enum data type checking on Set & Get global functions // uncomment for Pmac Script type access
//----------------------------------------------------------------------------------
// To use the functions defined in the Libraries, create a prototype of the function
// in this file or in a header file that is included in this file.
// For Example:
// If a Library project has been created with the following function and you intend to use
// that function in this C file:
// int MyFunction(int MyVar)
// {
// return MyVar*10;
// }
// Then a prototype of this function must be created in this c file or in a
// header file that is being included in this file. The prototype is the following:
// int MyFunction(int);
//------------------------------------------------------------------------------------
#include "../../Include/pp_proj.h"
int main(void)
{
//---------------------------------------------------------------------
// Required Startup Code: Insures that this APP is run as an RT or NON
// RT APP otherwise depending upon how it is started it will inherit
// the scheduling priority and policy of the task that started it.
//---------------------------------------------------------------------
//----------------------------------------------
// Uncomment the below #define to run as a RT Linux APP
// #define RUN_AS_RT_APP
// For older F/W uncomment the following if you get a compile error:
// #define BACKGROUND_RT_PRIORITY 50
// #define NANO_5MSEC 5000000
//----------------------------------------------
struct sched_param param;
int done = 0;
int iGatItems;
int iLineLength;
int iMaxLines;
int i,j,k, sleep;
int iTemp;
double dTemp;
int *pGatBuffer;
#ifndef RUN_AS_RT_APP
//-----------------------------
// Runs as a NON RT Linux APP
//-----------------------------
param.__sched_priority = 0;
pthread_setschedparam(pthread_self(), SCHED_OTHER, ¶m);
#else
//---------------------------------------------------------------
// Runs as a RT Linux APP with the same scheduling policy as the
// Background script PLCs
// To run at a recommended lower priority use BACKGROUND_SCRIPT_PLC_PRIORITY - 10
// To run at a higher priority use BACKGROUND_SCRIPT_PLC_PRIORITY + 1
//---------------------------------------------------------------------
param.__sched_priority = BACKGROUND_RT_PRIORITY - 10;
pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m);
#endif
InitLibrary(); // open access to library
FILE *pOutputFile;
iGatItems = (int) pshm->Gather.Items;
iLineLength = (int) pshm->Gather.LineLength;
iMaxLines = (int) pshm->Gather.MaxLines;
pOutputFile = fopen("/var/ftp/gather/eventlog.txt","w+");
if(pOutputFile != NULL)
{
isleep = 0;
k = 0;
pGatBuffer = (int *) pshm->Gather.Buffer[0];
for (i = 0 ; i < iMaxLines ; i++)
{
for (j = 0 ; j < iGatItems ; j++)
{
iTemp = (int) pshm->Gather.Type[j];
switch (iTemp)
{
case 0:
fprintf(pOutputFile, "%u", (int) pshm->Gather.Buffer[k]);
k = k + 1;
break;
case 1:
fprintf(pOutputFile, "%d", (int) pshm->Gather.Buffer[k]);
k = k + 1;
break;
case 2:
fprintf(pOutputFile, "%u", (int) pshm->Gather.Buffer[k]);
k = k + 1;
break;
case 3:
fprintf(pOutputFile, "%d", (int) pshm->Gather.Buffer[k]);
k = k + 1;
break;
case 4:
fprintf(pOutputFile, "%31.20f", (float) pshm->Gather.Buffer[k]);
k = k + 1;
break;
case 5:
pGatBuffer = & (pshm->Gather.Buffer[k]);
dTemp = * ((double *) pGatBuffer);
fprintf(pOutputFile, "%31.20f", dTemp);
k = k + 2;
break;
} // end of switch iTemp
} //end of j loop
fprintf(pOutputFile, "\n");
pshm->P[100]++;
iSleep++;
if(iSleep > 100)
{
iSleep = 0;
nanosleep(&sleeptime,NULL); // sleep and yield to LINUX scheduler
}
} // end of i loop
fclose(pOutputFile);
} // if(pOutputFile != NULL)
CloseLibrary();
return 0;
} // int main (void)
// Application Overview:
// 1. Gather position data on four motors at the servo rate, waiting for a position corruption event.
// 2. Run 24/7 using the rotary gather mode and a large gather buffer allocated at the User Buffer.
// 3. When the event occurs, stop gathering after a few seconds delay.
// 4. Upload the complete data from the gather buffer, both pre-event and post-event, into a text file for further analysis.
//
Gather.UserBufStart = Sys.Idata[4096].a // This will start the servo gather storage at 4096 * 4 = 16,384 bytes from the start of the user shared memory buffer
Gather.UserBufSize = 10000 // for testing only
Gather.Addr[0]= Sys.ServoCount.a // Gather.Type = 0:32-bit integer, unsigned. 4 bytes.
Gather.Addr[1]= Motor[4].ActPos.a // for testing only. Gather.Type = 5:64-bit floating-point (double-precision). 8 bytes
Gather.Items = 2 // for testing only !
Gather.Period = 1 // 10KHz sampling rate
Gather.Enable = 1 // causes Power PMAC to calculate the number of words required to store each sample (Gather.LineLength)
// and the number of samples that can be stored (Gather.MaxLines).
Gather.MaxSamples=Gather.MaxLines // collect maximum possible number of samples
Gather.Enable = 3 // enable gathering in rotary mode
//
//
// When the event occurs, a monitoring PLC issues Gather.Enable = 0, and the user is prompted to execute the followind C app.
//
//
//******************************************************************************
//
// The issue is that the first gathered item (an int, Gather.Addr[0]= Sys.ServoCount.a) is written properly to the file,
// but the second gathered item (a double Gather.Addr[1]= Motor[4].ActPos.a) writes only zeroes to the file, even though
// that item looks correct when viewd via Sys.Data at the IDE terminal.
//
#include // Global Gp Shared memory pointer
//----------------------------------------------------------------------------------
// pp_proj.h is the C header for accessing PMAC Global, CSGlobal, Ptr vars
//
// _PPScriptMode_ for Pmac Script like access global & csglobal
// global Mypvar - access with "Mypvar"
// global Myparray(32) - access with "Myparray(i)"
// csglobal Myqvar - access with "Myqvar(i)" where "i" is Coord #
// csglobal Myqarray(16) - access with "Myqvar(i,j)" where "j" is index
//
// _EnumMode_ for Pmac enum data type checking on Set & Get global functions
// Example
// global Mypvar
// csglobal Myqvar
// "SetGlobalVar(Myqvar, data)" will give a compile error because its a csglobal var.
// "SetCSGlobalVar(Mypvar, data)" will give a compile error because its a global var.
//------------------------------------------------------------------------------------
#define _PPScriptMode_ // uncomment for Pmac Script type access
//#define _EnumMode_ // uncomment for Pmac enum data type checking on Set & Get global functions // uncomment for Pmac Script type access
//----------------------------------------------------------------------------------
// To use the functions defined in the Libraries, create a prototype of the function
// in this file or in a header file that is included in this file.
// For Example:
// If a Library project has been created with the following function and you intend to use
// that function in this C file:
// int MyFunction(int MyVar)
// {
// return MyVar*10;
// }
// Then a prototype of this function must be created in this c file or in a
// header file that is being included in this file. The prototype is the following:
// int MyFunction(int);
//------------------------------------------------------------------------------------
#include "../../Include/pp_proj.h"
int main(void)
{
//---------------------------------------------------------------------
// Required Startup Code: Insures that this APP is run as an RT or NON
// RT APP otherwise depending upon how it is started it will inherit
// the scheduling priority and policy of the task that started it.
//---------------------------------------------------------------------
//----------------------------------------------
// Uncomment the below #define to run as a RT Linux APP
// #define RUN_AS_RT_APP
// For older F/W uncomment the following if you get a compile error:
// #define BACKGROUND_RT_PRIORITY 50
// #define NANO_5MSEC 5000000
//----------------------------------------------
struct sched_param param;
//int done = 0;
int iGatItems;
int iLineLength;
int iMaxLines;
int i,j,k, iSleep;
int iTemp;
double dTemp;
int *pGatBuffer;
int pGatBuffer1;
double pGatBuffer2;
struct timespec sleeptime = {0};
sleeptime.tv_nsec = NANO_5MSEC;
//int *MyUshmIntVar;
//#ifndef RUN_AS_RT_APP
//-----------------------------
// Runs as a NON RT Linux APP
//-----------------------------
param.__sched_priority = 0;
pthread_setschedparam(pthread_self(), SCHED_OTHER, ¶m);
// #else
//---------------------------------------------------------------
// Runs as a RT Linux APP with the same scheduling policy as the
// Background script PLCs
// To run at a recommended lower priority use BACKGROUND_SCRIPT_PLC_PRIORITY - 10
// To run at a higher priority use BACKGROUND_SCRIPT_PLC_PRIORITY + 1
//---------------------------------------------------------------------
// param.__sched_priority = BACKGROUND_RT_PRIORITY - 10;
// pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m);
// #endif
InitLibrary(); // open access to library
FILE *pOutputFile;
iGatItems = (int) pshm->Gather.Items;
pTest10 = (double) iGatItems; // for testing
iLineLength = (int) pshm->Gather.LineLength;
pTest11 = (double) iLineLength; // for testing
iMaxLines = (int) pshm->Gather.MaxLines;
pTest12 = (double) iMaxLines; // for testing
pOutputFile = fopen("/var/ftp/gather/eventlog.txt","w+");
pTest13 = (double) 0; // for testing, i loop
pTest14 = (double) 0; // for testing, j loop
pTest15 = (double) 0; // for testing, k
pTest17 = (double) 0; // for testing
pTest18 = (double) 0; // for testing
if(pOutputFile != NULL)
{
iSleep = 0;
k = 0;
for (i = 0 ; i < iMaxLines ; i++)
{
pTest13 = (int) i; // for testing
for (j = 0 ; j < iGatItems ; j++)
{
pTest14 = (int) j; // for testing
iTemp = (int) pshm->Gather.Type[j];
switch (iTemp)
{
case 0:
pGatBuffer1 = * ((int *) ((( int *) pushm + 4096 + k)));
printf( "%u , ", (int) pGatBuffer1);
fprintf(pOutputFile, "%u ,", (int) pGatBuffer1);
k = k + 1;
pTest15 = (double) k;
break;
case 1:
pGatBuffer1 = * ((int *) ((( int *) pushm + 4096 + k)));
printf( "%u , ", (int) pGatBuffer1);
fprintf(pOutputFile, "%u ,", (int) pGatBuffer1);
k = k + 1;
pTest15 = (double) k;
break;
case 2:
pGatBuffer1 = * ((int *) ((( int *) pushm + 4096 + k)));
printf( "%u , ", (int) pGatBuffer1);
fprintf(pOutputFile, "%u ,", (int) pGatBuffer1);
k = k + 1;
pTest15 = (double) k;
break;
case 3:
pGatBuffer1 = * ((int *) ((( int *) pushm + 4096 + k)));
printf( "%u , ", (int) pGatBuffer1);
fprintf(pOutputFile, "%u ,", (int) pGatBuffer1);
k = k + 1;
pTest15 = (double) k;
break;
case 4:
pGatBuffer1 = * ((int *) ((( int *) pushm + 4096 + k)));
printf( "%u , ", (int) pGatBuffer1);
fprintf(pOutputFile, "%u ,", (int) pGatBuffer1);
k = k + 1;
pTest15 = (double) k;;
break;
case 5:
pGatBuffer2 = * ((double *) ((( double *) pushm + 4096 + k)));
printf( "%u , ", (double) pGatBuffer2);
printf( "%30.10f \n", (double) pGatBuffer2);
fprintf(pOutputFile, "%u ,", (double) pGatBuffer2);
k = k + 2;
pTest15 = (double) k;
break;
} // end of switch iTemp
} //end of j loop
fprintf(pOutputFile, "\n");
iSleep++;
if(iSleep > 100)
{
iSleep = 0;
nanosleep(&sleeptime,NULL); // sleep and yield to LINUX scheduler
}
} // end of i loop
fclose(pOutputFile);
} // if(pOutputFile != NULL)
CloseLibrary();
return 0;
} // int main (void)