diff --git a/.gitignore b/.gitignore index 3adb44e..999255c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ *.o *.pyc rpm_meter_test +test_wavegen spaxxpos diff --git a/dro.ini b/dro.ini deleted file mode 100644 index b95a0ce..0000000 --- a/dro.ini +++ /dev/null @@ -1,16 +0,0 @@ -[RESOLUTION] -#micron/step -x_step_size = 0.5 -y_step_size = 0.5 -z_step_size = 0.5 - -[GENERAL] -use_x = Yes -use_y = Yes -use_z = No -;mode = simulation -;gui_mode: lathe/mill -gui_mode = lathe -comport = /dev/ttyAMA0 -baudrate = 115200 - diff --git a/make_test_wavegen b/make_test_wavegen new file mode 100644 index 0000000..f4dc8f7 --- /dev/null +++ b/make_test_wavegen @@ -0,0 +1 @@ +gcc -std=gnu99 test_wavegen.c -o test_wavegen -lpigpio -lrt diff --git a/poslib.py b/poslib.py index 9425571..d6a5fcb 100644 --- a/poslib.py +++ b/poslib.py @@ -8,33 +8,35 @@ class axisPos(ctypes.Structure): ("xposition",ctypes.c_float), ("yposition",ctypes.c_float)] class LinearPositionComm: - pos_receiver_lib = ctypes.CDLL(os.path.join(myfolder,'pos_decode.so')) - - def __enter__(self): - print('Opening DRO port ' + self.port + ' at ' + str(self.baudrate) + ' baud') - pos_receiver_lib = self.pos_receiver_lib - pos_receiver_lib.init_comm.argtypes = ( ctypes.POINTER(ctypes.c_char), ctypes.c_int) - pos_receiver_lib.get_x_pos.restype = ctypes.c_float - pos_receiver_lib.get_y_pos.restype = ctypes.c_float - pos_receiver_lib.get_axis_stat.restype = axisPos - pos_receiver_lib.init_comm(self.port, self.baudrate) - return self + pos_receiver_lib = ctypes.CDLL(os.path.join(myfolder,'pos_decode.so')) - def __exit__(self, type, value, traceback): - self.shutdown() + def __enter__(self): + print('Opening DRO port ' + self.port + ' at ' + str(self.baudrate) + ' baud') + pos_receiver_lib = self.pos_receiver_lib + pos_receiver_lib.init_comm.argtypes = ( ctypes.POINTER(ctypes.c_char), ctypes.c_int) + pos_receiver_lib.get_x_pos.restype = ctypes.c_float + pos_receiver_lib.get_y_pos.restype = ctypes.c_float + pos_receiver_lib.get_axis_stat.restype = axisPos + pos_receiver_lib.init_comm(self.port, self.baudrate) + pos_receiver_lib.rpmmeterInitialize.argtype = ctypes.c_int + pos_receiver_lib.gpioCfgInterfaces(7) + pos_receiver_lib.rpmmeterInitialize(self.rpm_sensor_pin) + return self + + def __exit__(self, type, value, traceback): + self.pos_receiver_lib.shutdown() + self.pos_receiver_lib.rpmmeterShutdown() - def __init__(self, port, baudrate): - self.port = port - self.baudrate = int(baudrate) + def __init__(self, port, baudrate, rpm_sensor_pin): + self.rpm_sensor_pin = rpm_sensor_pin + self.port = port + self.baudrate = int(baudrate) - def shutdown(self): - self.pos_receiver_lib.shutdown() - + def getThreadId(): - """Returns OS thread id - Specific to Linux""" - libc = ctypes.cdll.LoadLibrary('libc.so.6') - SYS_gettid = 224 # System dependent, see e.g. /usr/include/x86_64-linux-gnu/asm/unistd_64.h - #224 is the value on raspbian - return libc.syscall(SYS_gettid) - -print "Thread ID: %i"%getThreadId() + """Returns OS thread id - Specific to Linux""" + libc = ctypes.cdll.LoadLibrary('libc.so.6') + SYS_gettid = 224 # System dependent, see e.g. /usr/include/x86_64-linux-gnu/asm/unistd_64.h + #224 is the value on raspbian + return libc.syscall(SYS_gettid) + diff --git a/rpm_meter.c b/rpm_meter.c index f5bd89a..c242d41 100644 --- a/rpm_meter.c +++ b/rpm_meter.c @@ -86,6 +86,7 @@ int rpmmeterInitialize(int gpio_pin) //cfgPeripheral: 0 (PWM), 1 (PCM) //cfgSource: deprecated, value is ignored + printf("Initializing pigpio\n"); if (gpioInitialise()<0) return 1; printf("Installing ISR on gpio %i \n", gpio_pin); fflush(stdout); diff --git a/test_rpm_sensor.py b/test_rpm_sensor.py new file mode 100644 index 0000000..0b94745 --- /dev/null +++ b/test_rpm_sensor.py @@ -0,0 +1,44 @@ +import serial +import ConfigParser +import time +import os +import sys +import struct +import poslib +from poslib import LinearPositionComm +import threading + +myfolder = os.path.dirname(os.path.realpath(__file__)) + +configfile = os.path.join(myfolder,"..","dro.ini") +os.chdir(myfolder) +print "Reading config file %s"%configfile +config = ConfigParser.ConfigParser() +config.read(configfile) + +port, baudrate , rpm_meter_pin = None, None, None +try: + port = config.get('GENERAL', 'comport') + baudrate = config.get('GENERAL', 'baudrate') + rpm_meter_pin = int(config.get('GENERAL', 'rpm_sensor_pin')) +except Exception, e: + print "Invalid config file: %s"%(str(e)) + exit(1) + +#time.sleep(2) + +#time.sleep(2) #not working after the DLL init_commm has been called. Interference with the signal handler? +try: + with LinearPositionComm(port, baudrate, rpm_meter_pin) as comm: + for i in range(1,1000): + #print "X%.3f "%comm.pos_receiver_lib.get_x_pos() + #print "Y%.3f "%comm.pos_receiver_lib.get_y_pos() + rpm = comm.pos_receiver_lib.get_rpm() + j=0 + while(j < 1000): + j+=1 + sys.stdout.write("RPM %i \r"%(rpm)) +except Exception , e: + print "Exception in the LinearPositionComm: %s"%str(e) + + diff --git a/test_wavegen.c b/test_wavegen.c new file mode 100644 index 0000000..3901f80 --- /dev/null +++ b/test_wavegen.c @@ -0,0 +1,181 @@ +#include +#include +#include +#include +#include +#include +#include + +int gpio=21; + +#define DUR_OFF 10 +#define RAMP_STEP 1 //each pulse during the ramp up period will be shorter by this value + +int start_wave(int on); + +/* +int rampup_wave(int duration){ + printf("Rampup to %i", duration); +// gpioWaveClear(); + + int ramp_up = 1000; + char wid[10*ramp_up/RAMP_STEP]; + int wave_no = 0; + while (ramp_up > duration) { + + gpioPulse_t pulse[2]; + pulse[0].gpioOn = (1< 100){ + //too many waves, need to start sending them + //fprintf(stderr, "Will send a set of %i \n", wave_no); + gpioWaveChain(wid, wave_no); + while (gpioWaveTxBusy()) time_sleep(0.001); + for (int i=0; i duration) { + + #define NO_OF_PULSES_PER_RAMPSTEP 10 + gpioPulse_t pulse[NO_OF_PULSES_PER_RAMPSTEP*2]; + for (int i= 0; i < 2*NO_OF_PULSES_PER_RAMPSTEP-1; i++){ + pulse[i].gpioOn = (1< 3) + { + gpioInitialise(); + gpioWaveClear(); + fprintf(stderr, "Too many params"); + return 1; + } + else if (argc > 2) + { + on = atoi(argv[2]); + gpio = atoi(argv[1]); + } + else if (argc > 1) + { + gpio = atoi(argv[1]); + } + + printf("gpio=%d on=%d\n", gpio, on); + + if (gpioInitialise()<0) return -1; + + gpioSetMode(gpio, PI_OUTPUT); + + rampup_wave(on); + + //start_wave(on); + while (1) { + int var = getchar(); + if(var == 's') break; + if(var == '+') { + on += 1; + start_wave(on); + } + if(var == '-') { + on -= 1; + start_wave(on); + } + time_sleep(0.01); + } + gpioWaveClear(); + gpioTerminate(); +}