Archive

Posts Tagged ‘swig’

Finally publishing DS1M12 python code

November 26th, 2008
Well it’s done, I have published my work on the DS1M12 Python Swig Wrapper.

It’s located on the mercurial repos, it’s hosting two files that maybe I will have to delete since they come from the original source that I do not own. Also everything is build around Python 2.5, going to try building it for Python 2.6

It requires two downloads one from USB Instruments itself and another from a thirdparty driver

You have to use these before compiling anything of the code and also remember to update your LD_LIBRARY_PATH to poing where you placed the .so files. On my code everything is supposed to be on the same directory as the project. You will also need the .rbf files, the linux packages comes with one file that means it will use 1K buffer but the windows package comes with several files for 1K,2K,4K and 8 K buffers. These files are used to program some FPGA inside the osciloscope and set the buffer size for the capture and output, one should do some testing to find the optimum buffer for the application.

I have some bad luck now since I no longer own one of these osciloscopes and can’t continue testing and improving the code.

What I found is that there’s still some bugs, I think there’s a buffer overun that leads to a segfault when the code it’s run over continued periods of time but I don’t know what is causing it exactly. My main suspects are the numpy arrays being passed to the ds1m12 libraries but I didn’t have enough time with the device so I could investigate it further. I also tried almost every configuration parameter and operation mode and seems to be working, then, I was able to use the signal generator, external trigger and all the options you can set for each one of the channels even turning on/off each channel individually.

I did several testing and found that the kernel I was using had some really big impacts, 2.6.26 kernels worked fine for me, with 2.6.27 kernels it didn’t work at all. So if you experience some problems with the code try downgrading or upgrading the linux kernel you are using.

Good luck and please let me know how it goes for you.

Enlaces y noticias , , , , , ,

DS1M12 usb osciloscope in linux and python

October 1st, 2008
Well, today in the afternoon I was given a task, make the Usb Instruments DS1M12 usb osciloscope in linux, it was quite a surprise
to find out it had some kind of support for Linux and I started my work with their sources.

I found it requires two downloads one from USB Instruments itself and another from a thirdparty driver

Well, those two comes with some .so files you need to link your programs so you need to make sure
they have proper names like libDS1M12.so, libftd2xx.so and they can be found in the LD_LIBRARY_PATH or something like that, this is important.

After you get this solved it’s pretty straightforward with the USB Instruments package comes a good example you can use to understand their library, this took a bit of time since we(called some one to help me pinpoint some problems) could get it working and seeing the data flow quite fast, yet, it took us some time to discover that the sample code enabled the testmode and instead of real sampling, after that we could see the two channels sampling also the signal generator send out the example signals.

After this we agreed that it would be good to have a python interface to this osciloscope and I started working with swig to generare a proper wrapper around the USB Instruments library, around an hour ago I finished what we could call the first release of the interface and I’m being able to plot some data using matlplot lib. It took me a bit to figure how to manage to wrap some of the functions yet in the end swig and numpy solved all my issues.

I’m not really sure If I can release everything at the moment, I think I’ll have to ask USB Instruments since I use one of their headers to build the swig wrapper and it requires some minor modifications to make it work nicely so I would have to publish the modified header. If i get permission or any idea how I should publish the repository, it will be here at valkertown Mercurial Repositories

For now you have to beleive me that now the use of this osciloscope it’s reduced to something like this

  1.  
  2. """
  3.   Author: Carlos Andres Perilla
  4.   Centro Internacional de Física, Bogotá-Colombia.
  5.  
  6.   This program is free software: you can redistribute it and/or
  7.   modify it under the terms of the GNU Affero General Public License
  8.   as published by the Free Software Foundation, either version 3 of
  9.   the License, or (at your option) any later version.
  10.  
  11.   This program is distributed in the hope that it will be useful, but
  12.   WITHOUT ANY WARRANTY; without even the implied warranty of
  13.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.   Affero General Public License for more details.
  15.  
  16.   You should have received a copy of the GNU Affero General Public
  17.   License along with this program.  If not, see
  18.   <http://www.gnu.org/licenses/>.
  19.  
  20. """
  21.  
  22. from Osciloscope import UsbOsciloscope
  23. from pylab import *
  24. import time
  25.  
  26. o = UsbOsciloscope()
  27. # Load the firmare into the osciloscope FPGA, this the RFB that comes in the USBI pack.
  28. o.program()
  29. # Enables both channels for sampling
  30. o.set_channels()
  31.  
  32. while True:
  33.     # Start signal, it gets some other params like
  34.     # triggering configuration, port conf and a lot of other things
  35.     o.start_scan()
  36.     status = -1
  37.     while status != 0:
  38.         # Do the actual reading of a single frame
  39.         retval = o.get_data()
  40.         status = retval[0]
  41.         if status != 0:
  42.             # Since the buffers can be large and the according
  43.             # to the sampling rate we may need to wait a bit
  44.             time.sleep(0.01)
  45.         else:
  46.             # If we got the data successfully we can now plot what we got using
  47.             # matplot lib.
  48.             values = retval[5].dwNumValuesReturned
  49.             x_axis = arange(0,values,1)
  50.             plot(x_axis ,retval[2][0:values],‘r–’,x_axis,retval[3][0:values],‘bs’)
  51.             show()
  52.  

Enlaces y noticias , , , , , , ,

Swig Python

January 21st, 2008

Bueno, hace poco jugue un poco con Swig,
para generar modulos de python desde c en particular.

Resulta una tarea muy simple

Escribi un Makefile que ilustra los pasos de una forma más o menos
simple, este Makefile esta más bien orientado a Slackware 12 pero
se puede saber facilmente que partes deben ser modificadas para otros
*nix.

#Makefile

MODULE=anything

all:
	swig -Wall -python $(MODULE).i
	cc -c $(MODULE).c $(MODULE)_wrap.c -I/usr/include/python2.5/ -I/usr/lib/python2.5/config
	ld -shared $(MODULE).o $(MODULE)_wrap.o -o _$(MODULE).so
clean:
	-rm *.o	$(MODULE)_wrap.c _$(MODULE).so $(MODULE).py $(MODULE).pyc

En mi caso utilice swig para generar una interface para una libreria
muy simple que comprendia unas cuantas funciones.

Fuera de anything.c y anything.h que son especificas de la libreria en
C, se necesita anything.i que le indica a swig algunas cosas sobre el
“wrapper” que debe generar, un ejemplo:

// anything.i
y%module anything
%{
#include "anything.h"
%}
%apply (char *STRING, int LENGTH) { (char * message, int nBytes) };
%include anything.h

Este ejemplo sirve tal cual para librerias de C que no tengan tipos de
datos propios y otras restricciones más, sin embargo esto cubre un
pequeño hueco que tenia en mis programas de python.

La linea:

%apply (char *STRING, int LENGTH) { (char * message, int nBytes) };

Permite pasar cadenas de python a funciones de c que reciben un
apuntador(message) y su longitud(nBytes) como argumentos. Tiene que
recibirlos exactamente igual a como se le indican a Swig en la
directiva, pero no importa si recibe más argumentos antes o despues
del patrón.

Existen muchas otras sutilezas que se encuentra uno al hacer wrappers
pero eso ya es de leer el manual en la
página oficial de Swig
.

EOC

Enlaces y noticias , ,