10...20 KHz interrupt needed

Just starting out? Need help? Post your questions and find answers here.
marc_256
Addict
Addict
Posts: 835
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

10...20 KHz interrupt needed

Post 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
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: 10...20 KHz interrupt needed

Post 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)
BERESHEIT
marc_256
Addict
Addict
Posts: 835
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

Re: 10...20 KHz interrupt needed

Post by marc_256 »

Thanks for the help,

Marc
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: 10...20 KHz interrupt needed

Post 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!
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: 10...20 KHz interrupt needed

Post 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.
BERESHEIT
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: 10...20 KHz interrupt needed

Post 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.
sorry for my bad english
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: 10...20 KHz interrupt needed

Post 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
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
eriansa
Enthusiast
Enthusiast
Posts: 277
Joined: Wed Mar 17, 2004 12:31 am
Contact:

Re: 10...20 KHz interrupt needed

Post 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.
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: 10...20 KHz interrupt needed

Post 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
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
RE-A
User
User
Posts: 39
Joined: Thu Aug 21, 2008 4:19 pm
Location: Belgium
Contact:

Re: 10...20 KHz interrupt needed

Post 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:
marc_256
Addict
Addict
Posts: 835
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

Re: 10...20 KHz interrupt needed

Post 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
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
Thorium
Addict
Addict
Posts: 1305
Joined: Sat Aug 15, 2009 6:59 pm

Re: 10...20 KHz interrupt needed

Post 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.
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: 10...20 KHz interrupt needed

Post 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
sorry for my bad english
RE-A
User
User
Posts: 39
Joined: Thu Aug 21, 2008 4:19 pm
Location: Belgium
Contact:

Re: 10...20 KHz interrupt needed

Post 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:
Post Reply