- From: Stienen, A.H.A. (CTW) <"Stienen,>
- Date: Thu, 09 Mar 2006 12:44:48 +0100
To understand all the options of the NI_660x drivers, I combined all the files of the test programs of Klaas Gadeyne (can still be found here: http://people.mech.kuleuven.be/~kgadeyne/software/drivers/comedi/ni660x/test/) into a single script, whereby with commenting parts I could understand it step bij step. Here it is, perhaps it is useful to you. Arno. -- ____ / |_| <<< IR. ARNO H.A. STIENEN >>> |_ O | Biomechanical Engineering | http://www.bw.ctw.utwente.nl |_|__/ a.h.a.stienen_at_utwente.nl | +31-(0)53-489-4778 UTwente "There are alternatives: Firefox, Thunderbird & OpenOffice!"
// $Id: etest.c,v 1.12 2004/01/14 17:54:35 kgadeyne Exp $
/*
** Copyright (C) 2003 Klaas Gadeyne <klaas dot gadeyne at mech dot kuleuven dot ac dot be>
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**
*/
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <getopt.h>
#include <ctype.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#include <comedi.h>
#include <comedilib.h>
#define NI_660x_COUNTER_SUBD 0 // Counter subdevice on NI6602
#define MAX_SAMPLES 20 // Number of samples to read
#define ENCODER_CONFIG_DATA 4 // See above
#define INSN_LIST_NUM_INS 2//4 // The number of instructions in our list
char *filename="/dev/comedi2";
//int verbose;
//int value=0;
int subdevice=0; // 0 is for COUNTER subdevice
int channel=0;
//int aref=AREF_GROUND; // Doesn't matter for counter subd
//int range=0; // Doesn't matter for counter subd
//int n_chan=8; // For ni_6602!
//int n_scan=1000; // Currently not implemented
//double freq=1000.0; // Currently not implemented
int gpct_x=4; // Standard X1 encoding (can be 1/2/4)
static comedi_t * device;
static int ret,i;
comedi_insnlist il;
static void perform_check(char * filename,
int subdevice)
{
// Check Subdevice :-)
if (subdevice != NI_660x_COUNTER_SUBD)
{
printf("You're not using the counter subdevice (%d)!\n", NI_660x_COUNTER_SUBD);
exit(0);
}
printf("Are you sure you're using encoder %d?\n",channel);
device=comedi_open(filename);
if(!device)
{
printf("Error opening Comedi Device\n");
comedi_perror(filename);
exit(0);
}
}
int main(int argc, char *argv[])
{
comedi_insn insn[INSN_LIST_NUM_INS]; // List of instructions
struct timeval t1,t2; // Start en end time
lsampl_t config_data[ENCODER_CONFIG_DATA]; // Configuration data
lsampl_t data[MAX_SAMPLES]; // Read data
perform_check(filename, subdevice);
/* Set up a the "instruction list", which is just a pointer
* to the array of instructions and the number of instructions.
*/
il.n_insns=INSN_LIST_NUM_INS;
il.insns=insn;
/* Instruction 0: perform a gettimeofday() */
insn[0].insn=INSN_GTOD;
insn[0].n=2;
insn[0].data=(void *)&t1;
ret=comedi_do_insn(device,&(insn[0]));
if(ret<0)
{
printf("Encodertest.c: GTOD Failed\n");
comedi_perror(filename);
exit(0);
}
/* Instruction 1: Configure the GPCT for use as an encoder */
insn[1].insn=INSN_CONFIG;
insn[1].n=1; // Utterly relevant for accepting config!!
config_data[0] = INSN_CONFIG_GPCT_QUADRATURE_ENCODER;
config_data[1] = gpct_x;
config_data[2] = GPCT_IndexPhaseHighHigh;
config_data[3] = 0; // do not reset on index pulse
insn[1].data=config_data;
insn[1].subdev=subdevice;
insn[1].chanspec=CR_PACK(channel,0,0);
ret=comedi_do_insn(device,&(insn[1]));
if(ret<0)
{
printf("Encodertest.c: Config Failed\n");
comedi_perror(filename);
exit(0);
}
/* Instruction 3: Read the encoder MAXSAMPLES times */
/*
insn[2].insn=INSN_READ;
insn[2].n=MAX_SAMPLES;
insn[2].data=data;
insn[2].subdev=subdevice;
insn[2].chanspec=CR_PACK(channel,0,0);
*/
/*
ret=comedi_do_insn(device,&(insn[2]));
if(ret<0)
{
printf("Encodertest.c: Read encoder data failed\n");
comedi_perror(filename);
exit(0);
}
*/
/* Instruction 4: perform a gettimeofday() */
/*
insn[3].insn=INSN_GTOD;
insn[3].n=2;
insn[3].data=(void *)&t2;
*/
/*
ret=comedi_do_insnlist(device,&il);
if(ret<0)
{
printf("encodertest.c: Instruction list did not complete");
printf(" successfully\n");
comedi_perror(filename);
exit(0);
}
*/
/*
printf("encodertest.c: initial time: %ld.%06ld\n",
t1.tv_sec,t1.tv_usec);
printf("encodertest.c: final time: %ld.%06ld\n",
t2.tv_sec,t2.tv_usec);
printf("encodertest.c: elapsed (us): %ld\n",
(t2.tv_sec-t1.tv_sec)*1000000+(t2.tv_usec-t1.tv_usec));
printf("encodertest.c: Encoder data\n");
for (i = 0; i < MAX_SAMPLES ; i++ )
{
printf("data[%d] = %d\n",i,(int) data[i]);
}
*/
printf("encodertest.c: Trying comedi_data_read now...\n");
ret=comedi_data_read(device,NI_660x_COUNTER_SUBD,channel,0,0,data);
if(ret<0){
printf("encodertest.c: comedi_data_read did not complete\n");
comedi_perror(filename);
exit(0);
}
printf("data = %d\n",(int) data[0]);
printf("encodertest.c: Sleeping 1 seconds...\n");
usleep(1000000);
ret=comedi_data_read(device,NI_660x_COUNTER_SUBD,channel,0,0,data);
if(ret<0){
printf("encodertest.c: comedi_data_read did not complete\n");
comedi_perror(filename);
exit(0);
}
printf("data = %d\n",(int) data[0]);
printf("encodertest.c: Sleeping 1 seconds...\n");
usleep(1000000);
ret=comedi_data_read(device,NI_660x_COUNTER_SUBD,channel,0,0,data);
if(ret<0){
printf("encodertest.c: comedi_data_read did not complete\n");
comedi_perror(filename);
exit(0);
}
printf("data = %d\n",(int) data[0]);
printf("encodertest.c: Testing comedi functions...\n");
ret = comedi_get_n_channels(device,NI_660x_COUNTER_SUBD);
printf("number of channels = %d\n",(int) ret);
lsampl_t lret = comedi_get_maxdata(device,NI_660x_COUNTER_SUBD, channel);
printf("maxdata = %u\n",(lsampl_t) lret);
int subdevtest = comedi_find_subdevice_by_type(device, COMEDI_SUBD_COUNTER, 0);
printf("subdevtest = %d\n",subdevtest);
return 0;
}
Received on 2006-03-09Z11:44:48