Page 1 of 2

Startracker

Posted: Wed Jan 07, 2015 8:58 pm
by slawekw79
Hi
I try to run my first project which is star tracking device. First of all i try connect Raspberry Pi + L298n + 42shd0404-22 and run simple script but i have problems with stepper motor which shaking, lack of torque and have no power to spin. Motor is supplying by 12v max 5A supply.

Code: Select all

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)

ControlPin = (7,11,13,15)
 
for pin in ControlPin:
	GPIO.setup(pin,GPIO.OUT)
	GPIO.output(pin,0)

seq = [ [1,0,0,0],
	[1,1,0,0],
	[0,1,0,0],
	[0,1,1,0],
	[0,0,1,0],
	[0,0,1,1],
	[0,0,0,1],
	[1,0,0,1] ]
for i in range(200):
    for halfstep in range(8):
        for pin in range(4):
            GPIO.output(ControlPin[pin], seq[halfstep][pin])
        time.sleep(0.01)

GPIO.cleanup()
and this is photo of my setup
https://drive.google.com/file/d/0B6lViP ... sp=sharing
any idea what is wrong?

Re: Startracker

Posted: Wed Jan 07, 2015 9:56 pm
by danjperron
The problem could be that you are changing coil by coil the power to the servo. It's a bit slow with python.

Also if it doesn't turn always the same way . it is possible that one of the coils is missed wired. Run way slower and check the pattern. Put sleep(1) instead of 0.01. It is easier to see if all coils are wired correctly.

Don't forget that stepper is 1.8 degree per step every time it moves.

If you need more precision try micro-stepping.

I made a post about micro-stepping with a l298 i.c. I'm using word set/clear to access the gpio at once.

http://www.raspberrypi.org/forums/viewt ... 77#p610277

Daniel

Re: Startracker

Posted: Wed Jan 07, 2015 10:05 pm
by simplesi
Steppers need special treatment - the time between pulses is important and varies between different types
Try to find a spec sheet for your model

Simon

Re: Startracker

Posted: Thu Jan 08, 2015 12:45 am
by mad-hatter
Hello,
but i have problems with stepper motor which shaking
Stepper motors can not be started running at a high speed, they need to be started slowly and run up to speed. The shaking is telling you that you are trying to start at too high a speed.
The idea with steppers is to start slowly and ramp up to a higher speed. As was suggested use a longer time delay between pulses at start up then gradually decrease the time delay in software until you get to the speed you require.

Regards

Re: Startracker

Posted: Thu Jan 08, 2015 9:25 pm
by slawekw79
HI
Thank all of you for advice. I check again my setup i recon i wrong connect motor to driver :lol: , now is running with my code but i'm still not happy. Is not accurate as i expect, each step made different angle. I just wondering it's problem with motor or driver?

Re: Startracker

Posted: Fri Jan 09, 2015 12:10 am
by danjperron
Try full step first and check if the spacing is regular.

Is your stepper have a gear box? If yes than you could have a problem with the backlash.

It is possible in half step to have different current on coil. If one phase has less power than the other phase, then you will have two short step and two longer step.

Daniel

Re: Startracker

Posted: Fri Jan 09, 2015 7:51 pm
by slawekw79
Hi
I check today one more time accuracy, with coupling and set 50 seq what is exactly 1 revolution with time 0.1s. And i observed it done exactly 1 rev. So i set 200 seq and to my surprise done nice clear 4 rev. Now i try to calculate time and seq for my final stage. thanks again for your help.

Re: Startracker

Posted: Sat Jan 24, 2015 3:33 pm
by slawekw79
HI
One more question, in my code i'm using time.delay() and i heard system timing is not accurate. What if i will add RTC to my RPi? This timing will be more accurate?

Re: Startracker

Posted: Sat Jan 24, 2015 5:47 pm
by DougieLawson
slawekw79 wrote:HI
One more question, in my code i'm using time.delay() and i heard system timing is not accurate. What if i will add RTC to my RPi? This timing will be more accurate?
It won't make any difference. The clock used for time of day is not the same as the clock used for delay timing.

Re: Startracker

Posted: Sat Jan 24, 2015 6:33 pm
by danjperron
For accurate timing, you could read the free running raspberry PI counter and calculate the amount of step you need to move
using a loop.

The free running clock is 1Mhz.

Just do a loop and compare the reading to your target count.
If it is over or equal just move one step and add to your Target the exact number of usec each step is supposed to be.

B.T.W. Exact number of usec between each step mean fraction also.

You could have small inequality between step but it should be inside error range. And you won't miss step and the stepper will run at the accuracy of the Raspberry Pi crystal.

This is some code in C to access the free running counter.

https://mindplusplus.wordpress.com/2013 ... mhz-timer/

I don't know if somebody create a module to read the free running counter in python?

Daniel

Re: Startracker

Posted: Sun Jan 25, 2015 9:06 pm
by danjperron
PIGPIO has a tick timer but it is only 32 bits.

I decide to implement the 64 bits internal free running timer

This is the C code to create the python module.

freeRunningCounter.c

Code: Select all

#include <stdio.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <Python.h>

#define ST_BASE (0x20003000)
#define TIMER_OFFSET (4)

static PyObject *freeRunningCounterError;

long long int *counter; // 64 bit timer
void *st_base;
int fd;

static PyObject * freeRunningCounter(PyObject *self, PyObject * args);

static PyMethodDef freeRunningCounterMethods[] = {
  { "getCounter", freeRunningCounter,METH_NOARGS, "Get free running counter"},
  { NULL,NULL,0,NULL}
};


PyMODINIT_FUNC
initfreeRunningCounter(void)
{
   PyObject *m;
   m = Py_InitModule("freeRunningCounter", freeRunningCounterMethods);
   if ( m == NULL)
      return;

       // get access to system core memory
    if (-1 == (fd = open("/dev/mem", O_RDONLY))) {
        fprintf(stderr, "open() failed.\n");
        return;
    }

    // map a specific page into process's address space
    if (MAP_FAILED == (st_base = mmap(NULL, 4096,
                        PROT_READ, MAP_SHARED, fd, ST_BASE))) {
        fprintf(stderr, "mmap() failed.\n");
        return ;
    }
    close(fd);

    // set up pointer, based on mapped page
    counter = (long long int *)((char *)st_base + TIMER_OFFSET);

   freeRunningCounterError = PyErr_NewException("freeRunningCounter.error",NULL,NULL);
   Py_INCREF(freeRunningCounterError);
   PyModule_AddObject(m, "error", freeRunningCounterError);

}

static PyObject * freeRunningCounter(PyObject *self, PyObject * args)
{
   return PyLong_FromUnsignedLongLong((unsigned long long )*counter);
}
I use python 2.7 and I compile the code like this

Code: Select all

gcc -I/usr/include/python2.7 -lpython2.7 -c freeRunningCounter.c
gcc -shared freeRunningCounter.o -o freeRunningCounter.so
To check it just try

Code: Select all

pi@raspberrypi ~ $ sudo python
Python 2.7.3 (default, Mar 18 2014, 05:13:23) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import freeRunningCounter
>>> print freeRunningCounter.getCounter()
17054570755
>>> print freeRunningCounter.getCounter()
17055602152
>>> quit()
pi@raspberrypi ~ $ 
And now if we return to the stepper I just create a small program to show you how to implement it.
I use print and you shouldn't since printing will be very slow.

PrecisePulse.py

Code: Select all

#!/usr/bin/env python
import freeRunningCounter

stepTiming = input('enter time step in micro-second ? ')

Target = freeRunningCounter.getCounter() + stepTiming;


while True:

  Current = freeRunningCounter.getCounter()
  print Current, 
  if Current > Target:
     Target= Target + stepTiming
     print "Move stepper"
  else:
     print "\r",

pi@raspberrypi ~ $ 
P.S. you need to run "sudo" to get access to the free running counter

N.B. the timing between step is not exact but the overall pulse count will be precise. (crystal resolution). If each step of your stepper is not noticeable on the image capture, that will work fine.

Code: Select all

sudo python PrecisePulse.py

Re: Startracker

Posted: Sun Jan 25, 2015 11:43 pm
by danjperron
I modified PrecisePulse.py to display the error in micro-second between each step

PrecisePulse.py

Code: Select all

#!/usr/bin/env python
import freeRunningCounter

stepTiming = input('enter time step in micro-second ? ')
Target = freeRunningCounter.getCounter();
# round it to 1 sec
Remainder = Target % 1000000L
Target = Target - Remainder + 1000000L
while True:
  Current = freeRunningCounter.getCounter()
  if Current > Target:
     print Current, 
     print "delta:",
     print Current-Target,
     print " Move stepper"
     Target= Target + stepTiming
and got a big 2.3 ms lag one time.

Code: Select all

pi@raspberrypi ~ $ sudo python PrecisePulse.py
enter time step in micro-second ? 1000000
26748000008 delta: 8  Move stepper
26749000004 delta: 4  Move stepper
26750000004 delta: 4  Move stepper
26751000001 delta: 1  Move stepper
26752000007 delta: 7  Move stepper
26753000008 delta: 8  Move stepper
26754000003 delta: 3  Move stepper
26755000009 delta: 9  Move stepper
26756000004 delta: 4  Move stepper
26757000011 delta: 11  Move stepper
26758000051 delta: 51  Move stepper
26759000004 delta: 4  Move stepper
26760000369 delta: 369  Move stepper
26761002317 delta: 2317  Move stepper
26762000007 delta: 7  Move stepper
26763000005 delta: 5  Move stepper

Re: Startracker

Posted: Mon Jan 26, 2015 2:13 pm
by slawekw79
hi
Big thank you for your help. I'll try this later, first of all i need finish mechanical part of my project. Thank you once again.

Re: Startracker

Posted: Tue Jan 27, 2015 2:56 am
by danjperron
I found that stepper drivers for 3D printer are ideal if you want to use it with the Raspberry PI.

Some are 3.3V compatible and they do micro-stepping with a current adjustment.

On this example I use a chinese version of the polulu A4988

https://www.pololu.com/product/1182
http://www.dx.com/p/3d-printer-a4988-ar ... ver-265980

I connect the A4988 like this

GND: ENABLE,DIR,and GND
3.3V: VDD,MS1,MS2,MS3
15V: VMOT
RESET with SLEEP (use the reset pull up)
and GPIO7 GPIO17 on STEP

Image

With all MSn to 3.3v the driver is set to 16 microstep

My stepper motor is 200 step/revolution.
I want 1 revolution per minute.

1 revolution = 60 sec
1 revolution = 200 step * 16 microstep/step

time between step = 60 / (200 * 16) = 18750 ┬Ásec

This is the code

Code: Select all

#!/usr/bin/env python
import sys
import RPi.GPIO as GPIO
import freeRunningCounter

GPIO.setmode(GPIO.BCM)
GPIO.setup(17,GPIO.OUT)
GPIO.output(17,0)


wantedStep = input('Enter time step in micro-second ? ')

stepTiming =  int(wantedStep)
stepFraction =  wantedStep - stepTiming

FractionAccumulator=0

Target = freeRunningCounter.getCounter();


try: 

 while True:

  Current = freeRunningCounter.getCounter()
  if Current > Target:
     GPIO.output(17,True)
     FractionAccumulator = FractionAccumulator + stepFraction
     if FractionAccumulator >= 1:
       Target = Target + stepTiming + 1
       FractionAccumulator = FractionAccumulator - 1
     else:
       Target= Target + stepTiming
     GPIO.output(17,False)
except:
    quit()

Re: Startracker

Posted: Tue Jan 27, 2015 2:14 pm
by slawekw79
danjperron wrote: GND: ENABLE,DIR,and GND
3.3V: VDD,MS1,MS2,MS3
15V: VMOT
RESET with SLEEP (use the reset pull up)
and GPIO7 on STEP
You meant GPIO17?
I already have L298n and EasyDriver 4.4 drivers so i don't want buy another one :). In other day I've tried them together https://www.youtube.com/watch?v=JUOUox4iw_g
but with varying degree of success. L298n goes so hot I'm burnt my fingers :) . What you think, if I connect again them together with your setup RPi-EasyDriver ( A4988 and EasyDriver are almost equal) it will work?. My stepper is a 1.7A so A4988 will solve my problem, so maybe i should consider purchase.

Re: Startracker

Posted: Tue Jan 27, 2015 2:37 pm
by Ravenous
If your only problem is that the L298N is getting hot, can you just bolt a heatsink to it? I haven't used the 298, but I think it's designed to have a heatsink bolted right on to prevent overheating...

By heatsink, I mean any scrap of metal that can have a mounting hole drilled in, to conduct heat away from the driver chip, you don't have to go out and buy an expensive finned heatsink just to test the system...

Re: Startracker

Posted: Tue Jan 27, 2015 2:55 pm
by danjperron
Oops Typo it is GPIO17.


At the cost and simplicity buy a A4988 module.

Do you really need to run your stepper at full load?
What is the needed rotation time to sync the telescope with the star?
For better accuracy you should have a gear multiplicator anyway.


The Stepper motor I used for the demo is a PK264A2A-SG10. I took off the gear box.

It is rated 2A and it is working fine since you could control the current.

Daniel

Re: Startracker

Posted: Tue Jan 27, 2015 3:55 pm
by danjperron
If the current load is not too much the easy driver should work!

Please maximize the micro-step.

Don't go below 2500 micro second. More micro-step and less vibration you will have.

Re: Startracker

Posted: Tue Jan 27, 2015 4:24 pm
by slawekw79
Sorry I forget mention i have worm gear 100:1 and i will attach timing belt pulleys 80 teeth on worm gear shaft and 20 teeth on stepper shaft, so at all this give me ratio 400. Timing of my camera is exactly like Earth speed 23:56:04 hours what is 86164 sec. After gear give me 215.41 sec per one motor revolution. As i observed my camera made good pictures in time lest than 0.5 sec. So this give minimum 450 steps or microsteps per rev. That why i need so precision timing

Re: Startracker

Posted: Tue Jan 27, 2015 4:41 pm
by danjperron
Assuming that you have a 50 step/revolution stepper and you set the eazy driver to 8 micro-step.

215.41 sec / ( 50 step * 8 microstep/step) = 0.538525 sec/ microstep



Then it is 538525 micro-second to set for PrecisePulse.py


If it is a 200 step/revolution stepper, the value is 134631.25

N.B. My latest python code will take care of the fraction.
Daniel

Re: Startracker

Posted: Tue Jan 27, 2015 5:36 pm
by slawekw79
Hi
I've tried to compile your C code freeRunningCounter.c and i received error:

Code: Select all

pi@raspberrypi ~ $ gcc -I/usr/include/python2.7 -lpython2.7 -c freeRunningCounter.c
freeRunningCounter.c:7:20: fatal error: Python.h: No such file or directory
compilation terminated.
pi@raspberrypi ~ $ 
Do you know what i'm doing wrong?

Re: Startracker

Posted: Tue Jan 27, 2015 5:50 pm
by danjperron
Install python-dev

Code: Select all

sudo apt-get install python-dev

Re: Startracker

Posted: Tue Jan 27, 2015 6:45 pm
by slawekw79
Yes it's working!!
I connect my EasyDriver to my 42SHD0404-22 stepper motor and i use your PrecisePulse.py and working realy nice. When i finish my mechanic part i will run my setup in 24 hours test and i will you know a results. Thanks again for your indescribable help.

Re: Startracker

Posted: Tue Jan 27, 2015 7:28 pm
by danjperron
Good stuff!


When everything will be together don't forget to adjust the current .

Too little and you could miss step. Too much and it will heat for nothing and won't be smooth.

Maybe adding a pulsing led to confirm that is running could be a good idea.

Daniel

Re: Startracker

Posted: Tue Feb 10, 2015 5:42 pm
by slawekw79
Hi
I've just tested my setup, it's ran for 6 hours and it made nice 90 degrees. If in next weekend will be good weather i will go make some fotos. Next step add the HDMIPI screen and find adequate software for controlling my DLSR with Life View.