Skip to content

Commit

Permalink
Working on accurate stepper control
Browse files Browse the repository at this point in the history
  • Loading branch information
lnutiu committed Jun 10, 2019
1 parent 12f4cb5 commit cb05e15
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
*.o
*.pyc
rpm_meter_test
test_wavegen
spaxxpos
16 changes: 0 additions & 16 deletions dro.ini

This file was deleted.

1 change: 1 addition & 0 deletions make_test_wavegen
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gcc -std=gnu99 test_wavegen.c -o test_wavegen -lpigpio -lrt
54 changes: 28 additions & 26 deletions poslib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

1 change: 1 addition & 0 deletions rpm_meter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
44 changes: 44 additions & 0 deletions test_rpm_sensor.py
Original file line number Diff line number Diff line change
@@ -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)


181 changes: 181 additions & 0 deletions test_wavegen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pigpio.h>
#include <time.h>
#include <sys/ioctl.h>
#include <signal.h>

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<<gpio);
pulse[0].gpioOff = 0;
pulse[0].usDelay = ramp_up;
pulse[1].gpioOn = 0;
pulse[1].gpioOff = (1<<gpio);
pulse[1].usDelay = DUR_OFF;
gpioWaveAddGeneric(2, pulse);
int wave_id = gpioWaveCreate();
if (wave_id > 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<wave_no; i++) gpioWaveDelete(wid[i]);
wave_no = 0;
}else {
wid[wave_no] = wave_id;
ramp_up -= RAMP_STEP;
wave_no+=1;
}
//fprintf(stderr, "add wave dur %i ", ramp_up);
//fflush(stderr);
}
//gpioWaveTxSend(wave_id, PI_WAVE_MODE_ONE_SHOT_SYNC);
gpioWaveChain(wid, wave_no);
while (gpioWaveTxBusy()) time_sleep(0.001);
start_wave(duration);
for (int i=0; i<wave_no; i++) gpioWaveDelete(wid[i]);
return 0;
}
*/

int rampup_wave(int duration){
printf("Rampup to %i", duration);
// gpioWaveClear();

int ramp_up = 1000;

int wave_no = 0;
while (ramp_up > 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<<gpio);
pulse[i].gpioOff = 0;
pulse[i].usDelay = ramp_up;

pulse[i+1].gpioOn = 0;
pulse[i+1].gpioOff = (1<<gpio);
pulse[i+1].usDelay = ramp_up;
}
gpioWaveAddGeneric(NO_OF_PULSES_PER_RAMPSTEP*2, pulse);
int wave_id = gpioWaveCreate();
gpioWaveTxSend(wave_id, PI_WAVE_MODE_REPEAT_SYNC);
//while (gpioWaveTxBusy()) time_sleep(0.01);
//time_sleep(0.01);
gpioWaveDelete(wave_id);
ramp_up -= RAMP_STEP;
//fprintf(stderr, "add wave dur %i ", ramp_up);
//fflush(stderr);
}
//gpioWaveTxSend(wave_id, PI_WAVE_MODE_ONE_SHOT_SYNC);
//gpioWaveChain(wid, wave_no);
//while (gpioWaveTxBusy()) time_sleep(0.0001);
start_wave(duration);
//for (int i=0; i<wave_no; i++) gpioWaveDelete(wid[i]);
return 0;
}

int start_wave(int on){
//gpioWaveClear();
//fprintf(stderr, "start wave %i ", on);
//fflush(stderr);
// gpioWaveClear();

gpioPulse_t pulse[2];
pulse[0].gpioOn = (1<<gpio);
pulse[0].gpioOff = 0;
pulse[0].usDelay = on;

pulse[1].gpioOn = 0;
pulse[1].gpioOff = (1<<gpio);
pulse[1].usDelay = on;

gpioWaveAddGeneric(2, pulse);
int wave_id = gpioWaveCreate();
if (wave_id < 0){
fprintf(stderr, "Wave create error\n");
if (wave_id == PI_EMPTY_WAVEFORM){
fprintf(stderr, "PI_EMPTY_WAVEFORM\n");
}else if (wave_id == PI_TOO_MANY_CBS){
fprintf(stderr, "PI_TOO_MANY_CBS\n");
}else if (wave_id == PI_TOO_MANY_OOL){
fprintf(stderr, "PI_TOO_MANY_OOL\n");
}else if (wave_id == PI_NO_WAVEFORM_ID){
fprintf(stderr, "PI_NO_WAVEFORM_ID\n");
};

return 1;
}

gpioWaveTxSend(wave_id, PI_WAVE_MODE_REPEAT_SYNC);
return 0;
}

int main(int argc, char *argv[])
{
int on=1000;

if (argc > 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();
}

0 comments on commit cb05e15

Please sign in to comment.