Page 1 of 1

10...20 KHz interrupt needed

Posted: Mon Jul 12, 2010 9:06 pm
by marc_256
Hi to all,

I need a fixed (to control the steppers and DC motors) 10 to 20 kHz interrupt
(10.000 ... 20.000 Hz)
With the ElapsedMillisecond() command I only have msec. = 1.000 Hz
Is there a other command for a fixed interrupt,
or a work around ?

Thanks,
Marc

Re: 10...20 KHz interrupt needed

Posted: Mon Jul 12, 2010 10:47 pm
by netmaestro
This isn't 100% accurate, but it's pretty close (less than half of one percent error margin). The window receives a #INTERRUPT message approx. 10k times per second:

Code: Select all

#INTERRUPT = #WM_APP + 1
#NEWSECOND = #WM_APP + 2

Procedure kHz_Interrupt(kHz) 
  Protected t.q, lastt.q
  QueryPerformanceFrequency_(@maxfreq.q) 
  maxfreq/(1000*kHz)
  Repeat
    QueryPerformanceCounter_(@t)
    t/maxfreq 
    If t<> lastt
      lastt=t
      PostMessage_(WindowID(0), #INTERRUPT, 0, 0)
    EndIf
  ForEver
EndProcedure

Procedure Timer(uID, uMsg, dwUser, dw1, dw2)
  PostMessage_(WindowID(0), #NEWSECOND, 0, 0)
EndProcedure

OpenWindow(0,0,0,320,200,"Interrupt",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
TextGadget(0, 10,70, 90,20, "Total Interrupts:")
StringGadget(1,100,70,180,20,"", #PB_String_ReadOnly)
TextGadget(2, 10,100, 90,20, "Total Seconds:")
StringGadget(3,100,100,180,20,"", #PB_String_ReadOnly)

CreateThread(@kHz_Interrupt(), 10) ; 10 kHz interrupt, pass 20 for 20kHz

tID = timeSetEvent_(1000, 0, @Timer(), 0, #TIME_PERIODIC)

seconds=0

Repeat
  EventID = WaitWindowEvent()
  Select EventID
    Case #INTERRUPT
      icount+1
    Case #NEWSECOND
      seconds+1
      SetGadgetText(1, Str(icount))
      SetGadgetText(3, Str(seconds))
  EndSelect
Until EventID = #PB_Event_CloseWindow

timeKillEvent_(tID)

Re: 10...20 KHz interrupt needed

Posted: Tue Jul 13, 2010 8:49 am
by marc_256
Thanks for the help,

Marc

Re: 10...20 KHz interrupt needed

Posted: Tue Jul 13, 2010 9:15 am
by helpy
netmaestro wrote:This isn't 100% accurate, but it's pretty close (less than half of one percent error margin). The window receives a #INTERRUPT message approx. 10k times per second:
BUT the thread needs excessive CPU time!

Re: 10...20 KHz interrupt needed

Posted: Tue Jul 13, 2010 9:28 am
by netmaestro
Yes, the thread is going to use one whole core of the cpu. Do you know a way that uses less? An asm instruction that could go in the loop that will relinquish a tiny slice of cpu? I tried Sleep_(0) without success.

Re: 10...20 KHz interrupt needed

Posted: Tue Jul 13, 2010 9:39 am
by Josh
which os are you using?

it's not a good idea to control a stepper-motor from windows? how should your motor run, when your program has no cpu time? i think, this will never run correctly, windows is not a realtime os.

maybe it's possible to run under linunx.

Re: 10...20 KHz interrupt needed

Posted: Tue Jul 13, 2010 10:00 am
by helpy
Hier einige Infos:

==> http://www.eggheadcafe.com/software/asp ... timer.aspx

Problem: 10kHz (20kHz) entsprechen einer Periodendauer von 0,1ms (0,05ms) ... das sind: 100µs bzw. 50µs

Da Windows kein Echtzeit-Betriebssystem ist, wird es nicht möglichsein hier einen Task laufen zu lassen, der das einhält! Bedingt durch das Betriebssystemkonzept von Windows kann es da zu Abweichungen von mehreren ms kommen!

Du kannst Dir da sicher vorstellen, was mit einem Schrittmotor passiert, wenn die Impulse mit einer Ungenauigkeit von mehreren ms daherkommen.

cu, helpy

Re: 10...20 KHz interrupt needed

Posted: Tue Jul 13, 2010 1:02 pm
by eriansa
Another approach is perhaps to use the clock of p.e. a soundcard. I am thinking about ASIO and the new Vista/Win7 audio subsystem.

Re: 10...20 KHz interrupt needed

Posted: Tue Jul 13, 2010 1:24 pm
by helpy
UUUUUPs ... I wrote in german ... sorry ;-)

Here the translation

A link with some informations:

==> http://www.eggheadcafe.com/software/asp ... timer.aspx (german link)

Problem: 10kHz (20kHz) correspond to a periodoc time of 0,1ms (0,05ms) ...
in [µs] ==> 100µs or 50µs

Windows is not an realtime operating system. Therfore it is not possible to run a task, which will keep an accuaracy of 100µs or 50µs. Due to the concept of windows itself, a tolerance of several x ms are possible.

You can imagine what will happen, if 100µs pulses with an accuaracy of ±10ms (or more) will be applied to an stepper motor.

cu, helpy

Re: 10...20 KHz interrupt needed

Posted: Tue Jul 13, 2010 8:19 pm
by RE-A
Why not using a USB microcontroller to control the stepper.
You then can control speed and direction just by USB commands :wink:

Re: 10...20 KHz interrupt needed

Posted: Wed Jul 14, 2010 4:33 pm
by marc_256
Thanks all,

@Josh
I use Win XP SP3 - AMP +3200 XP cpu at 2.2 GHz - and 1.5 GB ram

@All
I just realize that the only solution to have a stable interrupt time,
is to use a Hardware Interrupt Generator (HIG)
That is not so complicated for me to build.
But how to activate PureBasic stepper subroutine is the problem.

Via Centronics port (5 VDC) ?, RS232 port (+-12 VDC) ?, Other

@RE-A
is USB not to complicated to control (communication protocol) ?
I used in a previous project an 68000 and 68HC11 both written in Assembler.
Works fine for 2 steppers.
For my new robot I need to control 8 steppers in real time,so ...
- 2 steppers for motion control
- 4 steppers for robot arm

Some information here (sorry written in dutch)
http://www.marc-systems.be/new_2010/hob ... _deel3.htm


Is PureBasic a stand alone program after compilation or is it depending Windows ?


Marc

Re: 10...20 KHz interrupt needed

Posted: Wed Jul 14, 2010 4:50 pm
by Thorium
marc_256 wrote: Is PureBasic a stand alone program after compilation or is it depending Windows ?
It's depending on the OS you compile for, Windows, Linux or MacOS X.

For realtime control you realy should use hardware or a realmode OS like FreeDOS.

Re: 10...20 KHz interrupt needed

Posted: Wed Jul 14, 2010 4:55 pm
by Josh
look here
just for information, it's not really cheap and it's intended for milling machines. but this part can handle 4 stepping motors

Re: 10...20 KHz interrupt needed

Posted: Wed Jul 14, 2010 6:41 pm
by RE-A
Marc,

You are right, USB isn't easy but lots of pc's and specially laptops doesn't have the printer or RS232 port anymore and RS232 converters are very slow in bit-bang mode.

I created a USB PIC programmer with PB for Windows and Linux using the 18F2550 microcontroller and designed my own communication protocol. You can do the same for your commands like direction, acceleration, speed, etc….

I wouldn’t recommend assembler to create the USB firmware, it’s a little to complicated for that :wink: