recorder.cpp

A data recorder example

/*  recorder.cpp

 Copyright(c) 1998-2009 by Robotic Systems Integration, Inc. All rights reserved.
 This software contains proprietary and confidential information of Robotic 
 Systems Integration, Inc. (RSI) and its suppliers. Except as may be set forth 
 in the license agreement under which this software is supplied, disclosure, 
 reproduction, or use with controls other than those provided by RSI or suppliers
 for RSI is strictly prohibited without the prior express written consent of 
 Robotic Systems Integration.

 This sample code presumes that the user has set the tuning paramters(PID, PIV, etc.) 
 prior to running this program so that the motor can rotate in a stable manner.

 For any questions regarding this sample please visit our documentation at www.roboticsys.com

 Warning!  This is a sample program to assist in the integration of your motion 
 controller with your application.  It may not contain all of the logic and safety
 features that your application requires.
*/

#include "rsi.h"

using namespace RSI::RapidCode::SynqNet;

// how many values to store in each record
#define VALUES_PER_RECORD   3

// how often to record data (samples between consecutive records)
#define RECORD_PERIOD_SAMPLES 100

// how long to record (in milliseconds)
#define RECORD_TIME       3000


#define RSI_BOARD                   (0)


void recorderMain()
{
  long axis0ActualPositionAddr;
  long axis0CommandVelocityAddr; 
  long axis1ActualPositionAddr;
    
  long recordsAvailable; 
  long *recordDataPtr;
  long recordData[VALUES_PER_RECORD];


  try
  {
    MotionController  *controller;

    // Create and initialize RsiController class (PCI board).
    controller = MotionController::CreateFromBoard(RSI_BOARD);  
    
    // configure recorder to record every 'n' samples
    controller->RecorderPeriodSet(RECORD_PERIOD_SAMPLES);
    
    // do not use a circular buffer
    controller->RecorderCircularBufferSet(false);


    // configure the number of values for each record
    controller->RecorderDataCountSet(VALUES_PER_RECORD);

    // get the host controller addresses for the values we want to record
    axis0ActualPositionAddr = controller->AddressFromStringGet("Axis[0].ActualPosition", "c:\\synqnet\\stdmei.map");
    axis0CommandVelocityAddr = controller->AddressFromStringGet("Axis[0].CommandVelocity", "c:\\synqnet\\stdmei.map");
    axis1ActualPositionAddr = controller->AddressFromStringGet("Axis[1].ActualPosition", "c:\\synqnet\\stdmei.map");

    // configure the recoder to record values from these addresses
    controller->RecorderDataAddressSet(0, axis0ActualPositionAddr);
    controller->RecorderDataAddressSet(1, axis0CommandVelocityAddr);
    controller->RecorderDataAddressSet(2, axis1ActualPositionAddr);

    // start recording
    controller->RecorderStart();

    // put this thread to sleep for some milliseconds
    controller->OS->Sleep(RECORD_TIME);

    // stop recording
    controller->RecorderStop();
    
    // find out how many records were recorded
    long recordsAvailable = controller->RecorderRecordCountGet();
    printf("There are %ld Records available.\n", recordsAvailable);

    // print all the records
    for(long i = 0; i < recordsAvailable  ; i++)
    {
      // get the pointer to the record data
      recordDataPtr = controller->RecorderRecordDataGet();
           
      // copy the recorded data into an array
      memcpy(&recordData, recordDataPtr, sizeof(recordData));

      // print first data value (long)
      printf("Record %ld: Axis 0 ActPos: %ld ", i, recordData[0]);
      
      // print second data value (float)
      printf("Axis 0 CmdVel: %f ", (float)*((float*)&recordData[1]));

      // print third data value (long)
      printf("Axis 1 ActPos: %ld\n", recordData[2]);
    }

  }
  catch (RsiError *err)
  {
    printf("%s\n", err->text);
  }
}