CNCzone.com-The Largest Machinist Community on the net!



Home Page Mark Forums Read Today's Posts My Replies Classifieds Reviews Photo Gallery Web Links Share Files Advertise With Us Ad List
Go Back   CNCzone.com-The Largest Machinist Community on the net! > Machine Controllers Software and Solutions > Visual Basic


Visual Basic Discuss Visual Basic programing.


Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Ban this user!
Old 01-11-2006, 01:08 PM
 
Join Date: Jan 2005
Location: USA
Posts: 7
109jb is on a distinguished road
Timing for a new controller program

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
Tweet this Post!Share on Facebook
Reply With Quote

  #2   Ban this user!
Old 01-12-2006, 10:01 AM
 
Join Date: May 2004
Location: United States
Posts: 455
JRoque is on a distinguished road

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.

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
Tweet this Post!Share on Facebook
Reply With Quote

  #3   Ban this user!
Old 01-12-2006, 12:05 PM
 
Join Date: Jan 2005
Location: USA
Posts: 7
109jb is on a distinguished road

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
Tweet this Post!Share on Facebook
Reply With Quote

  #4   Ban this user!
Old 01-12-2006, 05:31 PM
CLaNZeR's Avatar  
Join Date: Jul 2005
Location: UK
Posts: 101
CLaNZeR is on a distinguished road

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

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

objTimer.Wait 1000


Hope this helps

Regards

Sean.
__________________
********************
http://www.cncdudez.co.uk
Tweet this Post!Share on Facebook
Reply With Quote

  #5   Ban this user!
Old 01-13-2006, 04:46 PM
 
Join Date: May 2004
Location: United States
Posts: 455
JRoque is on a distinguished road

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
Tweet this Post!Share on Facebook
Reply With Quote

Sponsored Links
  #6   Ban this user!
Old 01-14-2006, 11:15 AM
 
Join Date: Jan 2005
Location: USA
Posts: 7
109jb is on a distinguished road

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.
Tweet this Post!Share on Facebook
Reply With Quote

  #7  
Old 01-14-2006, 04:25 PM
ger21's Avatar
Community Moderator
 
Join Date: Mar 2003
Location: Shelby Twp, MI....USA
Posts: 19,570
ger21 is on a distinguished road
Buy me a Beer?

Originally Posted by 109jb

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??
__________________
Gerry

Mach3 2010 Screenset
http://home.comcast.net/~cncwoodworker/2010.html

(Note: The opinions expressed in this post are my own and are not necessarily those of CNCzone and its management)
Tweet this Post!Share on Facebook
Reply With Quote

  #8   Ban this user!
Old 01-14-2006, 08:12 PM
 
Join Date: Sep 2003
Location: Sweden
Posts: 16
strombom is on a distinguished road

a microcontroller connected to the serial port could be used as a buffer to get perfect timing
Tweet this Post!Share on Facebook
Reply With Quote

  #9   Ban this user!
Old 01-14-2006, 11:38 PM
 
Join Date: Jan 2005
Location: USA
Posts: 7
109jb is on a distinguished road

Originally Posted by ger21
Got a link??
http://msdn.microsoft.com/vstudio/express/default.aspx
Tweet this Post!Share on Facebook
Reply With Quote

  #10   Ban this user!
Old 01-15-2006, 01:17 AM
 
Join Date: May 2004
Location: United States
Posts: 455
JRoque is on a distinguished road

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
Tweet this Post!Share on Facebook
Reply With Quote

Sponsored Links
  #11   Ban this user!
Old 07-05-2006, 10:56 PM
 
Join Date: Apr 2005
Location: US
Posts: 14
Goat is on a distinguished road

Originally Posted by 109jb
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
Tweet this Post!Share on Facebook
Reply With Quote

Reply




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On





All times are GMT -5. The time now is 06:31 AM.





Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Content Relevant URLs by vBSEO
Template-Modifications by TMS

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353