524,059 active members*
2,124 visitors online*
Register for free
Login
Results 1 to 1 of 1
  1. #1
    Member
    Join Date
    May 2020
    Posts
    3

    G code interpreter with Visual Basic

    I have two motion controllers (Newmark and Suruga-Seiki) that I need to control. I need to use data from G code to send signals to the motion controllers to perform specific tasks. So far I've written a code that is able to send signals to the motion controllers for straight lines. But for arcs and circle I am completely stumped. I am not sure if this is still a viable path or I should just purchase new equipment for this project. I am sort of shooting in the dark with this project.

    For context, this project is for laser processing where the linear translation stage is controlled by both the Newmark and Suruga-Seiki motion controllers to move a sample across the laser beam. I will have a DXF file for the required shape/dimensions for the end result. I converted the DXF file to G code but now the controllers are not compatible with G code so I used Visual Basic to pass signals to the motion controllers according to the data from the G code.

    I am not sure if this is worth pursuing. Any advice is appreciated.

  2. #2
    Community Moderator Jim Dawson's Avatar
    Join Date
    Dec 2013
    Posts
    4315

    Re: G code interpreter with Visual Basic

    I know nothing about your motion controllers, but if the motion controllers have native arc commands available then all you have to do is pass the proper command parameters to the controller.

    If there are no native arc commands available, then the Atan2() math function is your friend. You can almost directly plug in the I and J from the G code into the Atan2() function and create an array of tiny X/Y moves to generate the arc, it's just simple trig. This is my prefered method, but be careful of rounding errors creeping in, you need to write code to correct the rounding errors as you are generating the array. This is because you are working in floating point math, but the controller only understands integer values for the encoder pulses.
    Jim Dawson
    Sandy, Oregon, USA

  3. #3
    Member
    Join Date
    May 2020
    Posts
    3

    Re: G code interpreter with Visual Basic

    I find it really difficult to convert the G2 or G3 commands to the native arc commands. The arc commands require the radius and angle. The radius can be found relatively simple but the angle is rather difficult.

  4. #4
    Community Moderator Jim Dawson's Avatar
    Join Date
    Dec 2013
    Posts
    4315

    Re: G code interpreter with Visual Basic

    Quote Originally Posted by njkleong View Post
    I find it really difficult to convert the G2 or G3 commands to the native arc commands. The arc commands require the radius and angle. The radius can be found relatively simple but the angle is rather difficult.
    Normally the angle is figured as the start angle - the end angle = sweep angle. The current tool position is the start angle, the tool end position is the value in the G2 or G3 line.

    So let's assume the start angle is 20° and the end angle is 120° then the sweep angle would be 100°

    Many CAM softwares will output the format G2 Xnn.nnn Ynn.nnn Rnn.nnn but you still have to calculate the sweep, computers work in radians, but the controllers may require degrees.

    The coordinate system is always as below



    Here is a code snippet that should help get you started.

    Public Function G2CalcArc(ByVal startX As Single, ByVal startY As Single, ByVal startZ As Single, ByVal endX As Single, ByVal endY As Single, ByVal endZ As Single, ByVal I As Single, ByVal J As Single, ByVal K As Single, ByVal WP As String, Optional ByRef Radius As Single = 0, Optional ByRef aStart As Single = 0, Optional ByRef aSweep As Single = 0)




    If InvertSign = 1 Then
    vStartI = -I
    vStartJ = -J
    vStartK = -K
    Else
    vStartI = I
    vStartJ = J
    vStartK = K
    End If
    vStartX = startX
    vStartY = startY
    vStartZ = startZ
    vEndX = endX
    vEndY = endY
    vEndZ = endZ


    vDeltaX = vEndX - vStartX
    vDeltaY = vEndY - vStartY
    vDeltaZ = vEndZ - vStartZ


    vEndI = vStartI + vDeltaX ' - vStartI '- Math.Abs(vStartI) '- vDeltaX
    vEndJ = vStartJ + vDeltaY '- Math.Abs(vDeltaY)
    vEndK = vStartK + vDeltaZ


    If Math.Abs(vEndI) < 0.0001 Then vEndI = 0
    If Math.Abs(vEndJ) < 0.0001 Then vEndJ = 0
    If Math.Abs(vEndK) < 0.0001 Then vEndK = 0
    absX = vStartX + I
    absY = vStartY + J
    absZ = vStartZ + K
    WorkPlane = WP
    Direction = "G2"
    'Calc()
    Select Case WorkPlane
    Case "XY"
    G2XY()
    Case "XZ"
    G2XZ()
    Case "YZ"
    G2YZ()
    End Select



    vRadius = System.Math.Sqrt(vStartI ^ 2 + vStartJ ^ 2)
    RadiansStart = Math.Atan2(vStartJ, vStartI)
    RadiansEnd = Math.Atan2(vEndJ, vEndI)


    Radius = vRadius
    aStart = vStart
    aSweep = vSweep






    End Function
    Jim Dawson
    Sandy, Oregon, USA

  5. #5
    Member
    Join Date
    May 2020
    Posts
    3

    Re: G code interpreter with Visual Basic

    Thanks! This was really helpful

  6. #6
    Community Moderator Jim Dawson's Avatar
    Join Date
    Dec 2013
    Posts
    4315

    Re: G code interpreter with Visual Basic

    Quote Originally Posted by njkleong View Post
    Thanks! This was really helpful
    My pleasure
    Jim Dawson
    Sandy, Oregon, USA

Posting Permissions

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