slawekw79
Posts: 31
Joined: Tue Dec 30, 2014 3:36 pm

Startracker

Wed Jan 07, 2015 8:58 pm

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?

danjperron
Posts: 3405
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Startracker

Wed Jan 07, 2015 9:56 pm

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

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: Startracker

Wed Jan 07, 2015 10:05 pm

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
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

User avatar
mad-hatter
Posts: 419
Joined: Sun Feb 26, 2012 2:58 pm
Location: By the UK seaside

Re: Startracker

Thu Jan 08, 2015 12:45 am

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

slawekw79
Posts: 31
Joined: Tue Dec 30, 2014 3:36 pm

Re: Startracker

Thu Jan 08, 2015 9:25 pm

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?

danjperron
Posts: 3405
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Startracker

Fri Jan 09, 2015 12:10 am

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

slawekw79
Posts: 31
Joined: Tue Dec 30, 2014 3:36 pm

Re: Startracker

Fri Jan 09, 2015 7:51 pm

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.

slawekw79
Posts: 31
Joined: Tue Dec 30, 2014 3:36 pm

Re: Startracker

Sat Jan 24, 2015 3:33 pm

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?

User avatar
DougieLawson
Posts: 36320
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Startracker

Sat Jan 24, 2015 5:47 pm

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.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

danjperron
Posts: 3405
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Startracker

Sat Jan 24, 2015 6:33 pm

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

danjperron
Posts: 3405
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Startracker

Sun Jan 25, 2015 9:06 pm

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

[email protected] ~ $ 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()
[email protected] ~ $ 
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",

[email protected] ~ $ 
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
Last edited by danjperron on Mon Jan 26, 2015 3:04 pm, edited 1 time in total.

danjperron
Posts: 3405
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Startracker

Sun Jan 25, 2015 11:43 pm

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

[email protected] ~ $ 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

slawekw79
Posts: 31
Joined: Tue Dec 30, 2014 3:36 pm

Re: Startracker

Mon Jan 26, 2015 2:13 pm

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.

danjperron
Posts: 3405
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Startracker

Tue Jan 27, 2015 2:56 am

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()
Last edited by danjperron on Tue Jan 27, 2015 2:57 pm, edited 1 time in total.

slawekw79
Posts: 31
Joined: Tue Dec 30, 2014 3:36 pm

Re: Startracker

Tue Jan 27, 2015 2:14 pm

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.

Ravenous
Posts: 1956
Joined: Fri Feb 24, 2012 1:01 pm
Location: UK

Re: Startracker

Tue Jan 27, 2015 2:37 pm

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...

danjperron
Posts: 3405
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Startracker

Tue Jan 27, 2015 2:55 pm

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

danjperron
Posts: 3405
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Startracker

Tue Jan 27, 2015 3:55 pm

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.

slawekw79
Posts: 31
Joined: Tue Dec 30, 2014 3:36 pm

Re: Startracker

Tue Jan 27, 2015 4:24 pm

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

danjperron
Posts: 3405
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Startracker

Tue Jan 27, 2015 4:41 pm

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

slawekw79
Posts: 31
Joined: Tue Dec 30, 2014 3:36 pm

Re: Startracker

Tue Jan 27, 2015 5:36 pm

Hi
I've tried to compile your C code freeRunningCounter.c and i received error:

Code: Select all

[email protected] ~ $ 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.
[email protected] ~ $ 
Do you know what i'm doing wrong?

danjperron
Posts: 3405
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Startracker

Tue Jan 27, 2015 5:50 pm

Install python-dev

Code: Select all

sudo apt-get install python-dev

slawekw79
Posts: 31
Joined: Tue Dec 30, 2014 3:36 pm

Re: Startracker

Tue Jan 27, 2015 6:45 pm

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.

danjperron
Posts: 3405
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Startracker

Tue Jan 27, 2015 7:28 pm

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

slawekw79
Posts: 31
Joined: Tue Dec 30, 2014 3:36 pm

Re: Startracker

Tue Feb 10, 2015 5:42 pm

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.

Return to “Automation, sensing and robotics”