545,750 active members*
2,285 visitors online*
Register for free
Login
Results 1 to 20 of 20
  1. #1

    Join Date
    Apr 2021
    Posts
    13

    Post IronPython Scripting for Kmotion Library

    Hi,

    I'm working on setting up an IronPython script to control a KMotion 6 axis machine. I have successfully executed individual axis control functions but am having difficulty executing CoordMotion functions such as StraightFeed and ArcFeed

    Here is my reference code, I've been operating on trial and error as there is not much documentation to go off for scripting in Python,

    import wpf,sys,clr,System,os.path
    from System.Windows import Application, Window


    from os.path import dirname

    sys.path.append(dirname(dirname(dirname(dirname(os .path.abspath(__file__))))) + r"\KMotion\Release")
    # clr.AddReference("KMotion_dotNet")

    clr.AddReferenceToFileAndPath('C:\\KMotion433\\Kmo tion\\Release\\Kmotion_dotnet')

    from KMotion_dotNet import KM_Controller
    from KMotion_dotNet import KM_CoordMotion
    from KMotion_dotNet import KM_Interpreter

    KM = KM_Controller()
    CM = KM_CoordMotion(KM)
    MP = CM.MotionParams

    MP.BreakAngle = 30
    MP.MaxAccelX = 1
    MP.MaxAccelY = 1
    MP.MaxAccelZ = 1
    MP.MaxAccelA = 1
    MP.MaxAccelB = 1
    MP.MaxAccelC = 1
    MP.MaxVelX = 1
    MP.MaxVelY = 1
    MP.MaxVelZ = 1
    MP.MaxVelA = 1
    MP.MaxVelB = 1
    MP.MaxVelC = 1
    MP.CountsPerInchX = 100
    MP.CountsPerInchY = 100
    MP.CountsPerInchZ = 100
    MP.CountsPerInchA = 100
    MP.CountsPerInchB = 100
    MP.CountsPerInchC = 100

    MP.DegreesA = MP.DegreesB = MP.DegreesC = False
    MP.ArcsToSegs = True

    CM.SetTPParams()
    SPEED = 0.5

    CM.SetAxisDefinitions(0,1,2,3,4,5)

    CM.ClearAbort()

    CM.DoKMotionBufCmd('SetBitBuf0')
    CM.StartTraverse(0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, True, True)
    CM.DoKMotionBufCmd('ClearBitBuf0')

    # CM.ArcFeed(SPEED, CANON_PLANE_XY, 0.000, 0.500, 0.000, 0.250, DIR_CCW, 0.000, 0.000, 0.000, 0.000, 0, 0)
    # CM.ArcFeed(SPEED, CANON_PLANE_XY, 0.000, 0.000, 0.000, 0.250, DIR_CCW, 0.000, 0.000, 0.000, 0.000, 0, 0)


    CM.StraightFeed(SPEED, 0, 0.5, 0, 0, 0, 0, 0, 0)
    CM.StraightFeed(SPEED, 0.5, 0.5, 0, 0, 0, 0, 1, 1)
    CM.StraightFeed(SPEED, 0.5, 0, 0, 0, 0, 0, 2, 2)
    CM.StraightFeed(SPEED, 0, 0, 0, 0, 0, 0, 3, 3)

    CM.FlushSegments()

    print(CM.BufferedSegments)
    print(CM.TotalTimeInBuffer)


    I would love some feedback or help on how to execute simple CoordMotion commands in Python!

    Thank you

  2. #2
    Junior Member
    Join Date
    May 2006
    Posts
    3832

    Re: IronPython Scripting for Kmotion Library

    Hi,

    What happens when you try it?

    Attached is a Python GCode Example that works in V4.35f for me.
    TK
    http://dynomotion.com

  3. #3

    Join Date
    Apr 2021
    Posts
    13

    Re: IronPython Scripting for Kmotion Library

    Hi Tom,

    I've returned to this project and have tested your script which I'm able to run. I'm wondering how can I use IronPython to directly access and create CoordMotion commands? I don't want to be executing GCode, rather I am looking to use the CoordMotion methods such as StraightFeed, ArcFeed and StartTraverse. Do you have any sample python code for this?

    Thanks

  4. #4
    Junior Member
    Join Date
    May 2006
    Posts
    3832

    Re: IronPython Scripting for Kmotion Library

    Hi,

    No we don't have a Python Example for that. You might look at the KMotion_dotNet Console C# Example. In Program.cs the RunCoordinatedMotionExample() it should be relatively straightforward to port each line from C# to Python.

    HTH
    TK
    http://dynomotion.com

  5. #5

    Join Date
    Apr 2021
    Posts
    13

    Re: IronPython Scripting for Kmotion Library

    Hi Tom,

    I found the C# example you were referring to and have ported each and every line to Python. However the program runs but nothing appears to happen on the machine. Any ideas or thoughts? I've pasted the code below.

    Thanks

    KM = KM Controller()

    CoordMotion = KM_CoordMotion (KM)

    KM.CoordMotion.Abort()
    KM.CoordMotion.ClearAbort()

    KM.WriteLine("DefineCS 0 1 2 -1 -1 -1')
    KM.WriteLine("EnableAxis0')
    KM.WriteLine('EnableAxis1')
    KM.WriteLine('EnableAxis2')

    KM.CoordMotion.MotionParams.BreakAngle = 30
    KM.CoordMotion.MotionParams.RadiusA= 5
    KM.CoordMotion.MotionParams.RadiusB = 5
    KM.CoordMotion.MotionParams.RadiusC=5
    KM.CoordMotion.MotionParams.MaxAccelX = 30000
    KM.CoordMotion.MotionParams.MaxAccelY = 3000
    KM.CoordMotion.MotionParams.MaxAccelZ = 3000
    KM.CoordMotion.MotionParams.MaxAccelA = 30
    KM.CoordMotion.MotionParams.MaxAccelB = 30
    KM.CoordMotion.MotionParams.MaxAccelC= 30
    KM.CoordMotion.MotionParams.MaxVelX = 3000
    KM.CoordMotion.MotionParams.MaxVelY = 30
    KM.CoordMotion.MotionParams.MaxVelZ = 30
    KM.CoordMotion.MotionParams.MaxVelA = 30
    KM.CoordMotion.MotionParams.MaxVelB = 30
    KM.CoordMotion.MotionParams.MaxVelC= 30
    KM.CoordMotion.MotionParams.CountsPerInchX = 300
    KM.CoordMotion.MotionParams.CountsPerInchY = 300
    KM.CoordMotion.MotionParams.CountsPerInchZ = 300
    KM.CoordMotion.MotionParams.CountsPerInchA = 38
    KM.CoordMotion.MotionParams.CountsPerInchB = 30
    KM.CoordMotion.MotionParams.CountsPerInchC = 30
    KM.CoordMotion.MotionParams.DegreesA= False
    KM.CoordMotion.MotionParams.DegreesB= False
    KM.CoordMotion.MotionParams.DegreesC= False

    SPEED = 2

    KM.CoordMotion.ReadAndSyncCurPositions (0, 0, 0, 0, 0, 0)
    KM.CoordMotion.Straight Traverse (6.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, False)

    KM.CoordMotion.StraightFeed(SPEED, 0, 10, 0, 0, 0, 0, 0, 0)

    KM.CoordMotion.Dwell(2,0)
    KM.CoordMotion.FlushSegments()

  6. #6
    Junior Member
    Join Date
    May 2006
    Posts
    3832

    Re: IronPython Scripting for Kmotion Library

    Hi,

    You are mixing quotes with apostrophes here:
    Code:
    KM.WriteLine("DefineCS 0 1 2 -1 -1 -1')
    KM.WriteLine("EnableAxis0')
    does that execute?

    You might run KMotion.exe and look at the Axis screen to see if the axes become enabled.

    Also use the Console Screen to enter DefineCS to see if the Coordinate System is properly configured.

    Also use the Console Screen to enter MoveRel0=1000 to see if KFLOP is configured and wired correctly to move your motors.
    TK
    http://dynomotion.com

  7. #7

    Join Date
    Apr 2021
    Posts
    13

    Re: IronPython Scripting for Kmotion Library

    Hi Tom,

    Thanks for the suggestions. The apostrophes are actually due to the fact that I used Google Lens to copy and paste the code from an image haha.
    I have had success running individual axes commands using the KM Controller Axis class. Am still debugging the CoordMotion and will update when I have tried your suggestions.

    Thanks

  8. #8

    Join Date
    Apr 2021
    Posts
    13

    Re: IronPython Scripting for Kmotion Library

    Hi Tom,

    WIth the above code, I can verify that the axis 0, 1 and 2 are being set through the Axis screen on KMotion.exe. Does the KMotion.exe need to be running in order for IronPython scripts to be able to control the KFLOP controller? Now when I run the script I get an exception "General Exception thrown: Caller - [Kmotion_dotNet.KM_CoordMotion]:: Member - [StraightTraverse]"

    Any suggestions on how I can debug these traceback errors?

    Thanks!

  9. #9
    Junior Member
    Join Date
    May 2006
    Posts
    3832

    Re: IronPython Scripting for Kmotion Library

    Hi,

    WIth the above code, I can verify that the axis 0, 1 and 2 are being set through the Axis screen on KMotion.exe.
    What exactly did you check?


    Does the KMotion.exe need to be running in order for IronPython scripts to be able to control the KFLOP controller?
    No. But it may needed to configure KFLOP beforehand if your App isn't. It is also good to have running to observe things and see any Console messages.


    Now when I run the script I get an exception "General Exception thrown: Caller - [Kmotion_dotNet.KM_CoordMotion]:: Member - [StraightTraverse]"
    Isn't there a more details option?


    Any suggestions on how I can debug these traceback errors?
    I'm not familiar with Python. Are you running from Visual Studio? I thought there was a claim to be able to step through Python and into Native C++ code (our libraries) but I haven't been able to get it work. Otherwise you might be able to attach the Visual Studio Debugger to our GCodeInterpreter.dll (after it is loaded by your App) and set a breakpoint in StraightTraverse to see where it goes wrong.
    TK
    http://dynomotion.com

  10. #10

    Join Date
    Apr 2021
    Posts
    13

    Re: IronPython Scripting for Kmotion Library

    Quote Originally Posted by TomKerekes View Post
    What exactly did you check?
    I checked under KMotion -> Axis
    and saw the axes 0, 1 and 2 have been checked. When I uncheck them, running my script will reenable them.

    Quote Originally Posted by TomKerekes View Post
    No. But it may needed to configure KFLOP beforehand if your App isn't. It is also good to have running to observe things and see any Console messages.
    I see, running my script doesn't yield any console messages in Kmotion Console... Should I be witnessing console outputs?


    Quote Originally Posted by TomKerekes View Post
    Isn't there a more details option?
    I'm not sure, how would one enable a verbose exception debug command.

    Quote Originally Posted by TomKerekes View Post
    I'm not familiar with Python. Are you running from Visual Studio? I thought there was a claim to be able to step through Python and into Native C++ code (our libraries) but I haven't been able to get it work. Otherwise you might be able to attach the Visual Studio Debugger to our GCodeInterpreter.dll (after it is loaded by your App) and set a breakpoint in StraightTraverse to see where it goes wrong.
    I'm running it in PyCharm. Debug mode won't let me step through the loaded dlls.

  11. #11

    Join Date
    Apr 2021
    Posts
    13

    Re: IronPython Scripting for Kmotion Library

    Also forgot to mention that "MoveRel0 = 1000" works in the KMotion console as well as through my script app. I can use the KM.GetAxis() method to enable and command individual axis control through IronPython. The only thing that is not working for me are the CoordMotion examples through IronPython.

  12. #12
    Junior Member
    Join Date
    May 2006
    Posts
    3832

    Re: IronPython Scripting for Kmotion Library

    Hi,

    Please also check if the DefineCS is correct as I asked.

    One thing I now see is you ported:

    Code:
                // Sync Coordinated Motion Library with Current Position of Machine
                double x=0, y=0, z=0, a=0, b=0, c=0;
                _Controller.CoordMotion.ReadAndSyncCurPositions(ref x, ref y, ref z, ref a, ref b, ref c);
    as

    Code:
    KM.CoordMotion.ReadAndSyncCurPositions (0, 0, 0, 0, 0, 0)
    which is completely different. In the first case xyzabc are reference variables where the Current Position read from KFLOP is returned to the caller. In the second case they are constants.

    Seems strange that Python would allow this, Not Sync'ing to the current position might cause problems. You might use KMotion.exe to move the axis to some non-zero positions then in Python verify after the Sync call that the xyzabc variables are actually updated and are the correct positions. Note you will need to convert KFLOP raw counts to User units to do the comparison.
    TK
    http://dynomotion.com

  13. #13

    Join Date
    Apr 2021
    Posts
    13

    Re: IronPython Scripting for Kmotion Library

    Hi Tom,

    So I've updated to code to now read

    Code:
    x = y = z= a = b = c = 0
    x,y,z,a,b,c = KM.CoordMotion.ReadAndSyncCurPositions (x, y, z, a, b, c)
    print(x,y,z,a,b,c)
    This updates the x,y,z variables and will print out KFLOP raw counts. I'm unsure how to convert the units, but it seems to be in line with when I move the axis positions around.

    I've also entered "DefineCS" into the console and it prints "0 1 2 -1 -1 -1" So I think it is being set properly.

    However, it still doesn't like when I call the CoordMotion members and is throwing an error exception.

  14. #14
    Junior Member
    Join Date
    May 2006
    Posts
    3832

    Re: IronPython Scripting for Kmotion Library

    Hi,

    That doesn't look quite right to me (but probably isn't the problem), The function returns an error code not xyzabc.

    Can you send a screen shot of the Exception Message Box? Usually there is a option for more details.

    Have you been able to build our DLLs in debug mode as describe here.

    If so, you should be able to do as described here and set a break point within StraightTraverse, step through it and see where it throws an exception. You would probably need to place a pause in your Python program to allow you to Attach the Visual Studio debugger to your process (and our libraries) and set the break point (in our library) before continuing.

    Another approach would be to modify our libraries to add message boxes to say "I got here" and so forth.

    Sorry I don't know anything about Python.

    I see in your code there is a space between Straight and Traverse. Is this another case where you are posting code that is different from what you are testing?
    TK
    http://dynomotion.com

  15. #15

    Join Date
    Apr 2021
    Posts
    13

    Re: IronPython Scripting for Kmotion Library

    Hi,

    I'm having trouble attaching a screenshot but there's no Exception Message Box that appears. Rather it is an exception thrown during the execution of the Python Code. An error messages appears in the Python console that says "General Exception thrown: Caller - [Kmotion_dotNet.KM_CoordMotion]:: Member - [StraightTraverse]"

    I'll try your methods for debugging but it is difficult as the machine I'm debugging on is offline with no network access.

    I almost feel like there is a simple configuration error that I can't seem to figure out. Everything is enabled and individual axes commands work through Python, so I'm still not sure why CoordMotions aren't working.

    Thanks

  16. #16

    Join Date
    Apr 2021
    Posts
    13

    Re: IronPython Scripting for Kmotion Library

    I see in your code there is a space between Straight and Traverse. Is this another case where you are posting code that is different from what you are testing?
    I'm curious, are you able to execute my code on your setup? And I forgot to add that yes there is no space present in my code.

  17. #17

    Join Date
    Apr 2021
    Posts
    13

    Re: IronPython Scripting for Kmotion Library

    Hi Tom,

    Some development updates, I've switched to developing on a different machine with KMotion434 and have reflashed the KFLOP controller with KMotion434. I noticed earlier you were running a version number 435f. I wasn't able to find that package on the KMotion downloads page.
    I've also retried your GCode script on Kmotion434 and it doesn't appear to be working.

    Are there any significant updates/differences between 434 and 435f?

    Thanks

  18. #18
    Junior Member
    Join Date
    May 2006
    Posts
    3832

    Re: IronPython Scripting for Kmotion Library

    Hi,

    I'm curious, are you able to execute my code on your setup?
    No I don't have that Python.


    Newer Versions are listed on the wiki and the forum. See here. There are many differences but I would expect there to be no motion at all. I would highly recommend using the latest code.
    TK
    http://dynomotion.com

  19. #19

    Join Date
    Apr 2021
    Posts
    13

    Re: IronPython Scripting for Kmotion Library

    Hey Tom,

    Another python question, do you know if KMotion_dotNet.dll can be loaded using a normal python interpreter (as opposed to IronPython). I have tried loading in the DLL using ctypes but it can't read any of the methods in the same way as IronPython. Just curious if you have any additional experience using Python to interface with KMotion.

    Thanks!

  20. #20
    Junior Member
    Join Date
    May 2006
    Posts
    3832

    Re: IronPython Scripting for Kmotion Library

    No sorry I don't have any experience. Googling gives lots of hits.
    TK
    http://dynomotion.com

Similar Threads

  1. Scripting help
    By yar20 in forum PlanetCNC
    Replies: 18
    Last Post: 11-18-2019, 02:34 AM
  2. Replies: 32
    Last Post: 02-07-2017, 09:10 PM
  3. Scripting
    By Koblenzer in forum BobCad-Cam
    Replies: 1
    Last Post: 12-07-2011, 03:01 PM
  4. VB Scripting 101
    By UKRobotics in forum Screen Layouts, Post Processors & Misc
    Replies: 12
    Last Post: 05-18-2009, 12:04 PM
  5. Scripting
    By Klox in forum BobCad-Cam
    Replies: 29
    Last Post: 10-03-2003, 04:11 AM

Tags for this Thread

Posting Permissions

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