Results 1 to 13 of 13

Thread: Please help with the application for my CNC

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Please help with the application for my CNC

    I cleaned up the code as much as I could.

    Before I get everyone confused, please see this thread first: http://www.vbforums.com/showthread.php?t=610022

    I am new to threading when it comes to VB.NET, one of the problems is about that.

    After the program runs (it sends and receives data to & from CNC), when I click the X (form close), it just hangs, and does not close. Every time I have to click the "Stop Debugging (Shift+F5)" button.

    Now let me explain how the CNC works.

    I hope you can keep up, because it's a long read, and a little complicated.

    The computer generates RAW (compressed) data that sends to the CNC (the microcontroller).
    The motors are stepper motors, there are 400 steps for one full rotation. The microcontroller (PIC18F4550 at 48MHz) sends impulses to the motor driver, and the motor driver gives current to the windings of the motor, therefore for each impulse the motor moves one "step", and each step is 0.9°. So 360° / 0.9 = 400 steps.

    The microcontroller has 4 buffers.
    2 buffers for incomming data (where data is compressed)
    2 buffers for output data / information with each step and direction for the motors to move.
    So when data comes in, it goes into one of the "incomming" buffers, when it's done, it calls a routine that will uncompress the data and then it goes to the "output data"
    At the beginning of the routine that does the uncompression, it gives a signal to the computer to start sending more data to fill the other buffer.
    So in short, as long as there is one empty buffer, the computer can send more data to fill it up.
    The microcontroller also sends a signal when one of the "output data" buffers is done processing.
    Also about 10 times per second, the microcontroller send the X & Y & Z positions (internal counters) to the computer.

    OK, now how does the microcontroller know what to do ?
    The data is basically a 2 bit number for each axis. So the 2 bits (therefore a 0 to 4 number),
    0 means - do nothing
    1 means - move left (also enables the motor / gives power)
    2 means - move right (enables the motor)
    3 means - do nothing - BUT keep the motor enabled

    So for example, to move the motor 3 steps to the left, small pause, then 3 steps to right it sends something like this:
    000033331113333333322233330000
    It enables the motor first, then sends 1 to move to left, keeps the motor enabled, then sends 2 to move to right, keeps the motor enabled for a while then powers down the motor.

    That was for one axis only, so for all 3 axes it uses 6 bits of data (for each step)

    Originally all coordinates are calculated as 3 Integers (32 bits) for X&Y&Z, then it substracts previous point with current point, therefore giving values of -1 or 0 or 1. Where -1 is 2 (move right), 0 remains 0 (do nothing), and 1 remains 1 (move left).

    Then it gets converted to 3 bytes (where each byte is 0 or 1 or 2), finally it gets "compressed" from 3 bytes (24 bits) to 6 bits, and then this data goes to the microcontroller.

    The code for the microcontroller I wrote it in mikroC PRO. I don't expect anyone to make improvements to it, but i'm posing it for reference if anyone is interested. See the CNC_C_Code.txt file attached.

    Now before I say what other problems I have with the code, I will let you "digg in" all this information, and see where we go from here...

    Now the problem for you is... How will you run the application without a microcontroller to "talk" to ? does anyone know how to create a virtual serial port ?
    Last edited by CVMichael; Apr 11th, 2010 at 11:13 PM.

  2. #2
    Lively Member
    Join Date
    Apr 2010
    Location
    York, Uk
    Posts
    103

    Re: Please help with the application for my CNC

    http://www.virtual-serial-port.com/

    there are several api's/controls to this effect google

  3. #3
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: Please help with the application for my CNC

    for the multithreading issue i think it is probably because the thread is still running that you get a hang, you could put a cancel thread routine in the close form event. Not looked at the code as not at my desk but having worked with cnc before i think i could fix this, what you have done so far seems like it would work fine will look back later when i'm home.
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Please help with the application for my CNC

    There are two links in my signatue that might interest you.

    Most people, and I know of one exception(besides me) on this forum, don't do serial port programming correctly in VB. There may be others.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Please help with the application for my CNC

    Quote Originally Posted by Joshwah! View Post
    http://www.virtual-serial-port.com/

    there are several api's/controls to this effect google
    That one looks really good, but the free trial is 15 days, or $129 to buy it.

    The virtual port is for you guys to test my CNC application on your computers...

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Please help with the application for my CNC

    Quote Originally Posted by dbasnett View Post
    There are two links in my signatue that might interest you.
    I looked at the "Serial Port" one, but I see way to much code to do something so simple.
    I don't know what is the second link you are talking about.

  7. #7
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Please help with the application for my CNC

    Hi CVMichael,

    One thing you may consider using are a mechanical limit on each of the three axis, they could be as simple as micro switches. Once tripped, power is isolated for example. You may already have 'software' limits, however the mechanical ones would provide an extra layer of protection for the CNC hardware as well as personal safety.

    These types of mechanical limits are handy when things run away for what ever reason, feedback loops open, software bugs, operator error etc.


    Great job BTW .

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Please help with the application for my CNC

    Thank you Bruce Fox,

    Right now there are no limits (not even software), but I plan to have optical switches, I already tested with one, and it worked beautifully.

    The switches that I plan to use look like this one:



    Honestly, at the moment I am not worried about that too much.
    Before I do anything I "simulate"... I have separate power supply to the motors and the microcontroller, therefore, if I don't plug the motors, and leave the microcontroller ON, then it executes all the commands from computer as usual without actually moving the motors.

    But other things I want to add:
    * Panic button
    * A wheel (or 2) to manually move on any axis
    * A small display to show the coordinates on the actual CNC (not just the computer)
    * Rotary encoders for each axis that are at least 1000 points per rotation (still trying to find good ones that I can plug into a microcontroller)
    * oh, and the most important: a good & powerfull spindle... (that I can control the speed through the microcontroller)

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Please help with the application for my CNC

    After some testing, I found that if I comment out the sub that displays the coordinates in the label SetTextPos_XYZ(), then my application does not hang when I close it.

    The weird thing is that the sub SetPicPreviewImage() works exactly the same way except it displays the bitmap (with the lines of the CNC does) instead of the coordinates in the label.

    VB.NET Code:
    1. Private Sub Read_PosXYZ(ByVal Buffer() As Byte)
    2.         ' Got X, Y, Z coordinates
    3.         Dim Pos_X As Integer = BitConverter.ToInt32(Buffer, 0)
    4.         Dim Pos_Y As Integer = BitConverter.ToInt32(Buffer, 4)
    5.         Dim Pos_Z As Integer = BitConverter.ToInt32(Buffer, 8)
    6.  
    7.         Me.CNC_Pos.SetValues(Pos_X, Pos_Y, Pos_Z)
    8.  
    9.         'SetTextPos_XYZ()
    10.     End Sub
    11.  
    12.     Private Sub SetTextPos_XYZ()
    13.         ' Display the coordinates on the interface
    14.         Try
    15.             If Me.lblPos.InvokeRequired() Then
    16.                 Dim d As New DelegateNoParams(AddressOf SetTextPos_XYZ)
    17.  
    18.                 Me.Invoke(d, New Object() {})
    19.             Else
    20.                 Me.lblPos.Text = "X: " & Me.CNC_Pos.X.ToString().PadLeft(6) & "(" & Format(Me.CNC_Pos.X / 6400.0, "00.00000") & """)" & _
    21.                     ", Y: " & Me.CNC_Pos.Y.ToString().PadLeft(6) & "(" & Format(Me.CNC_Pos.Y / 6400.0, "00.00000") & """)" & _
    22.                     ", Z: " & Me.CNC_Pos.Z.ToString().PadLeft(6) & "(" & Format(Me.CNC_Pos.Z / 6400.0, "00.00000") & """)"
    23.             End If
    24.         Catch ex As Exception
    25.             Debug.Print(ex.Message)
    26.         End Try
    27.     End Sub

    Can someone please help me with this problem ?

    Thanks

  10. #10
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: Please help with the application for my CNC

    hmm just looked quickly but is this right """)" at the end of the last 3 lines, this would appear as ") in the string. I would comment out the try catch block too for testing
    Last edited by Megalith; Apr 7th, 2010 at 07:43 PM.
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Please help with the application for my CNC

    Quote Originally Posted by Megalith View Post
    I would comment out the try catch block too for testing
    Before it was without the try catch, and I had the same problem. I just added it so that in case there is an error, it will show up in the Output window

  12. #12

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Please help with the application for my CNC

    I was expecting a little more help...

  13. #13
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Please help with the application for my CNC

    Quote Originally Posted by CVMichael View Post
    I looked at the "Serial Port" one, but I see way to much code to do something so simple.
    I don't know what is the second link you are talking about.
    It amazes me that people think SerialPorts are easy, they aren't, especially when done correctly. Also, I read carefully your original post, and you don't have a simple application.

    The link you looked at has an emulator embedded in it if you have a modem port. If not then you need a loopback for testing. Here is what mine looks like



    You can just see my USB to SerialAdapter.

    There is another poster here that knows serial ports well, Dick Grier sp?.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width