PDA

View Full Version : Timing for a new controller program



109jb
01-11-2006, 02:08 PM
I am in the process of writing a CNC controller in visual basic. I am doing this mainly to learn more about programming in VB and because I am a glutton for punishment. I plan to run this on a Windows 98 machine which will avoid the problems associated with direct access of the printer port.

My question concerns timing for the step pulses to the printer port. The Timer control in VB is obviously not up to the task. I had a couple of thoughts on this as follows:

1. First, I wrote some code that just uses a loop to put a pause in before issuing the next step pulse. It seems to work OK, but I was wondering if there was a better way.

2. My next thought was to use the timeGetTime function to read the system clock. I would first read the system time and then enter a loop. Inside the loop a comparison would be made of the start time, interval, and current time to see if the loop should be exited. (ie: if start_time + interval >= current_time....)

Here are the problems I have with these two options.
Option 1 will be machine specific requiring some kind of constant to adjust timing between different machines. This option can also be affected by other programs running in windows or windows processes themselves running.
Option 2 is only accurate to 1 millisecond and therefore would limit maximum feedrate based on the number of steps per revolution. Take my first project for example. A mill with 1000 steps/inch would yield a maximum feedrate of 60 inches per minute. Probably OK for my small mill, but not for a router, and not if I use micro-stepping on the motors. Both of which I would like to do someday.

Does anyone have any ideas for other options or ways to make these options better??

Thanks,

John Brannen

JRoque
01-12-2006, 11:01 AM
Hey John,

Assuming you're talking about VB6, IMHO its terrible for timing tasks and overall very slow. Try a Do..Loop cycle flipping a parallel port bit on/off and measure the speed with a logic analyzer or scope... not very fast or stable.

You can get handles on the printer port under WinXP by using a DLL interface. (http://www.logix4u.net/inpout32.htm)

A possible workaround is to use external hardware to handle the timing and use VB to send the commands down. I don't mean to discourage you - even if slow, it can be useful, especially if you have a proprietary solution/package.

JR

109jb
01-12-2006, 01:05 PM
JR,

Thanks for the reply. When you say that VB is terrible for timing tasks, do you mean in Windows 95 or 98 too? I had thought that some of the timing problems were related to Win 2K and XP not allowing direct access to the printer port.

I already wrote a little code that uses a Do..Loop to run a single stepper, and it seems to work OK, but I don't have any kind of scope or logic analyzer to check out how the timing on the pulses really look. All I know is that the motor seems to run smooth. At least it does now that I put accel and decel ramps in by modifying the Do.Loop each time through.

My plan is to use an old computer that I have as a dedicated controller with the OS stripped of everything not necessary and only running the controller software. Since windows 3.1 has virtually no overhead by today's standards, would a program written for windows 3.1 be more stable wrt timing? Will a program written in VB6 even run on win 3.1? My plan had been to use win 98.

Just trying to learn.

Thanks again

CLaNZeR
01-12-2006, 06:31 PM
Hi John

I have tried various ways in VB over the years to get a decent pause using timers and other routines that are about the web.

In the end I have used the following:

create a file called clsWaitableTimer.cls in notepad.

paste in the following:

'**************************************
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "clsWaitableTimer"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

'**************************************
' Name: clsWaitableTimer
'
' Description: This class encapsulate the WaitableTimer API functions to
' put the thread of your application to Sleep for a period of time.
' The benefit of a Waitable timer to the Sleep API is that your
' application will still be responsive to events, where Sleep
' will freeze your application for the set interval.
'
' Example: 'This is an example for idling your application
' Private mobjWaitTimer As clsWaitableTimer
' Private Sub RunProcess()
' Set mobjWaitTimer = New clsWaitableTimer
' Do
' If mbWorkToDo Then
' Call ProcessWork()
' Else
' mobjWaitTimer.Wait(5000) 'Wait for 5 seconds
' End If
' Loop Until Not mbStop
' Set mobjWaitTimer = nothing
' End Sub
'
' Revision History:

Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type

Private Const WAIT_ABANDONED& = &H80&
Private Const WAIT_ABANDONED_0& = &H80&
Private Const WAIT_FAILED& = -1&
Private Const WAIT_IO_COMPLETION& = &HC0&
Private Const WAIT_OBJECT_0& = 0
Private Const WAIT_OBJECT_1& = 1
Private Const WAIT_TIMEOUT& = &H102&
Private Const INFINITE = &HFFFF
Private Const ERROR_ALREADY_EXISTS = 183&
Private Const QS_HOTKEY& = &H80
Private Const QS_KEY& = &H1
Private Const QS_MOUSEBUTTON& = &H4
Private Const QS_MOUSEMOVE& = &H2
Private Const QS_PAINT& = &H20
Private Const QS_POSTMESSAGE& = &H8
Private Const QS_SENDMESSAGE& = &H40
Private Const QS_TIMER& = &H10
Private Const QS_MOUSE& = (QS_MOUSEMOVE Or QS_MOUSEBUTTON)
Private Const QS_INPUT& = (QS_MOUSE Or QS_KEY)
Private Const QS_ALLEVENTS& = (QS_INPUT Or QS_POSTMESSAGE Or QS_TIMER Or QS_PAINT Or QS_HOTKEY)
Private Const QS_ALLINPUT& = (QS_SENDMESSAGE Or QS_PAINT Or QS_TIMER Or QS_POSTMESSAGE Or QS_MOUSEBUTTON Or QS_MOUSEMOVE Or QS_HOTKEY Or QS_KEY)

Private Const UNITS = 4294967296#
Private Const MAX_LONG = -2147483648#

Private Declare Function CreateWaitableTimer Lib "kernel32" Alias "CreateWaitableTimerA" (ByVal lpSemaphoreAttributes As Long, ByVal bManualReset As Long, ByVal lpName As String) As Long
Private Declare Function OpenWaitableTimer Lib "kernel32" Alias "OpenWaitableTimerA" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal lpName As String) As Long
Private Declare Function SetWaitableTimer Lib "kernel32" (ByVal hTimer As Long, lpDueTime As FILETIME, ByVal lPeriod As Long, ByVal pfnCompletionRoutine As Long, ByVal lpArgToCompletionRoutine As Long, ByVal fResume As Long) As Long
Private Declare Function CancelWaitableTimer Lib "kernel32" (ByVal hTimer As Long)
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function MsgWaitForMultipleObjects Lib "user32" (ByVal nCount As Long, pHandles As Long, ByVal fWaitAll As Long, ByVal dwMilliseconds As Long, ByVal dwWakeMask As Long) As Long

Private mlTimer As Long

Private Sub Class_Terminate()
On Error Resume Next
If mlTimer <> 0 Then CloseHandle mlTimer
End Sub

Public Sub Wait(MilliSeconds As Long)
On Error GoTo ErrHandler
Dim ft As FILETIME
Dim lBusy As Long
Dim lRet As Long
Dim dblDelay As Double
Dim dblDelayLow As Double

mlTimer = CreateWaitableTimer(0, True, App.EXEName & "Timer" & Format$(Now(), "NNSS"))

If Err.LastDllError <> ERROR_ALREADY_EXISTS Then
ft.dwLowDateTime = -1
ft.dwHighDateTime = -1
lRet = SetWaitableTimer(mlTimer, ft, 0, 0, 0, 0)
End If

' Convert the Units to nanoseconds.
dblDelay = CDbl(MilliSeconds) * 10000#

' By setting the high/low time to a negative number, it tells
' the Wait (in SetWaitableTimer) to use an offset time as
' opposed to a hardcoded time. If it were positive, it would
' try to convert the value to GMT.
ft.dwHighDateTime = -CLng(dblDelay / UNITS) - 1
dblDelayLow = -UNITS * (dblDelay / UNITS - Fix(CStr(dblDelay / UNITS)))

If dblDelayLow < MAX_LONG Then dblDelayLow = UNITS + dblDelayLow

ft.dwLowDateTime = CLng(dblDelayLow)
lRet = SetWaitableTimer(mlTimer, ft, 0, 0, 0, False)

Do
' QS_ALLINPUT means that MsgWaitForMultipleObjects will
' return every time the thread in which it is running gets
' a message. If you wanted to handle messages in here you could,
' but by calling Doevents you are letting DefWindowProc
' do its normal windows message handling---Like DDE, etc.
lBusy = MsgWaitForMultipleObjects(1, mlTimer, False, INFINITE, QS_ALLINPUT&)
DoEvents
Loop Until lBusy = WAIT_OBJECT_0

' Close the handles when you are done with them.
CloseHandle mlTimer
mlTimer = 0
Exit Sub

ErrHandler:
Err.Raise Err.Number, Err.Source, "[clsWaitableTimer.Wait]" & Err.Description
End Sub
'**************************************

Save file.

Add this to your project as a class module.

Then in your form add the following at the top:

Dim objTimer As clsWaitableTimer


Then under your Private Sub Form_Load()

Set objTimer = New clsWaitableTimer


Now you can use in your code to pause the following:

objTimer.Wait <value>

So if you wanted to pause say 1 second you would use:

objTimer.Wait 1000


Hope this helps

Regards

Sean.

JRoque
01-13-2006, 05:46 PM
Hey John,

VB is generally not the best at timing apps mostly because they wrap it on another layer rather than compiling to native code. Execution time is rather slow compared to C++, for example. But, it does have it's place and it's actually my preferred language on Wintel. I haven't tried it yet but I hear VB .Net compiles natively. I've also heard that it's slower than VB6 in some cases. VB .Net also needs a huge runtime env that's not on most PCs.

You might be on to something by going with Win98 instead of XP or others. I believe that as long as you don't have heavy I/O or need too much resources (mem, disk, video) Win98 will do well. You have better control of threads in XP but that's not something easily done in VB.

I pause my VB apps by creating a module and pasting this in:

Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

I then call it with:

Call Sleep(X)

where X = milliseconds.

JR

109jb
01-14-2006, 12:15 PM
Thanks for the replies guys. The problem I have is that I need to be able to get a time reference with better than millisecond accuracy. Something that works like "TimeGetTime". The reason is because I may know that I need a certain amount of time to pass between step pulses, say 10 milliseconds, but I can't just have a 10 millisecond pause because the code that runs between each step pulse takes up some of that time. The amount of code run between each step can be different depending or if it's a straight line or arc, circle, etc., so the pause has to be different too. So as it is now, I have to be able to interrogate and calculate the elapsed time since the last step command. I currently have the code written to use TimeGetTime, and it seems to work well with that, but at only 1 ms resolution the best I can hope for is to have 60,000 steps per minute. With my stepper at 1000 steps per inch and using 1/4 step microstepping giving 4000 microsteps per inch yields only 15 inches per minute at best. I'd really like to be able to get about 100 IPM in rapid which would require stepping every 150 nanoseconds. Don't know if it's possible, especially in VB, but that's the way I have my code written right now.

I just found out that Microsoft has their Visual Studio 2005 Express development suite available for free for the next year. You can apparantly use it forever, but have to download within the nest year. They have Visual C++ Express available so I may see about switching my code over to C++. Only problem is that I only had 1 semester of C programming and that was 10 years ago. I guess this would force me to learn it though. Would be more efficient I'm sure.

Thanks again for the replies.

John B.

ger21
01-14-2006, 05:25 PM
I just found out that Microsoft has their Visual Studio 2005 Express development suite available for free for the next year. You can apparantly use it forever, but have to download within the nest year.

Got a link??

strombom
01-14-2006, 09:12 PM
a microcontroller connected to the serial port could be used as a buffer to get perfect timing

109jb
01-15-2006, 12:38 AM
Got a link??

http://msdn.microsoft.com/vstudio/express/default.aspx

JRoque
01-15-2006, 02:17 AM
Good deal John. I had forgotten about VS Express. In fact, I had installed the Beta 2 version and never even tried it. I'll be sure to upgrade it now that's GA. Thanks!

JR

Goat
07-05-2006, 11:56 PM
Thanks for the replies guys. The problem I have is that I need to be able to get a time reference with better than millisecond accuracy. Something that works like "TimeGetTime". The reason is because I may know that I need a certain amount of time to pass between step pulses, say 10 milliseconds, but I can't just have a 10 millisecond pause because the code that runs between each step pulse takes up some of that time. The amount of code run between each step can be different depending or if it's a straight line or arc, circle, etc., so the pause has to be different too. So as it is now, I have to be able to interrogate and calculate the elapsed time since the last step command. I currently have the code written to use TimeGetTime, and it seems to work well with that, but at only 1 ms resolution the best I can hope for is to have 60,000 steps per minute. With my stepper at 1000 steps per inch and using 1/4 step microstepping giving 4000 microsteps per inch yields only 15 inches per minute at best. I'd really like to be able to get about 100 IPM in rapid which would require stepping every 150 nanoseconds. Don't know if it's possible, especially in VB, but that's the way I have my code written right now.

John B.


There is a much higher resolution timer available in the windows kernel. However, the overhead is in the order of microseconds not nanoseconds like you want. It's an API function....I'll forward you the the MSDN page instead of trying to explain it. The functions that you are interested in are QueryPerformanceCounter & QueryPerformanceFrequency.

Link (http://support.microsoft.com/kb/q172338/)