Sidebar

Home



PlanetCNC SDK


TNG API

Python

  About
  Installation

  Module 'planetcnc'

    print
    msg
    msgerr
    dlgFileOpen
    dlgFileSave

    result

   Paths
    getPath
    getPathExe
    getPathProfile
    getPathTemp
    getPathWorking

   Run status
    getVer
    isRunning
    isInitialized

   Version & Profile Info
    getVersionString
    getProfileNameString
    getProfilePathString
    getDescriptionString

   Screen
    isVisible
    show
    hide
    setProgress
    setStatus

   Parameters & Eval
    getParam
    setParam
    evaluate

   State
    isLicenseValid
    isUIReady
    isControllerReady
    isControllerRunning
    isProgramLoaded
    isIdle
    isEStop
    isStop
    isPause
    isProg
    isCmd
    isJog

   Machine Commands
    getCmdId
    getCmdIdFromMdi
    getCmdName
    getCmdDisplayName

    cmdExec
    cmdExecStr
    cmdExecVal
    cmdExecIntVal

    estop
    estopToggle
    stop
    pause
    pauseToggle
    start
    open
    close

   Machine Command status
    isCmdEnabled
    isCmdChecked

    isEStopEnabled
    isStopEnabled
    isPauseEnabled
    isStartEnabled
    isOutputEnabled
    isCodeEnabled
    isCodeExEnabled
    isOpenEnabled
    isCloseEnabled

   Helpers
    openFn
    openCode

    startFn
    startCode

    testFn
    testCode

    cmd
    mdi

   Info
    infoIsInitialized
    infoSerial
    infoHWVersion
    infoSWVersion
    infoIsVersionValid
    infoBufferAvailable
    infoBufferUtilization

    infoWorkPos
    infoWorkPosList
    infoMotorPos
    infoMotorPosList
    infoSpeed
    infoAcceleration

    infoSpindle
    infoSpindleIdx
    infoSpindleEnc
    infoSpindleSet
    infoOutput
    infoOutputFreq
    infoOutputDuty

    infoInput
    infoJog
    infoJogPot
    infoLimit
    infoAux

   Dlg
    dlgNew
    dlgShow
    dlgDelete
    dlgAddLabel
    dlgAddImage
    dlgAddCheckbox
    dlgAddInput

    dlgRawNew
    dlgRawShow
    dlgRawClose
    dlgRawAddLabel
    dlgRawAddImage
    dlgRawAddCheckbox
    dlgRawAddInput
    dlgRawGetValue
    dlgRawGetValues

   Points
    points_clear
    points_count
    points_add

   Image
    imageOpen
    imageOpenData
    imageClose
    imageSave
    imageToCam
    imageFromCam
    imageSize
    imageGetPixel
    imageGetPixelGray

   G-Code
    gcodeIsReady
    gcodeIsOpening
    gcodeIsRunning
    gcodeClose
    gcodeOpen
    gcodeLoad
    gcodeLineAddAllowed
    gcodeLineAdd

  Module 'gcode'
   G-Code
    isReady
    isOpening
    isRunning
    close
    open
    load
    lineAddAllowed
    lineAdd

Named Pipes

sdk:python:installation

How to install and configure Python

Embedded Python installation

Download Windows embeddable package .zip file from Python download page.
If you use 64-bit TNG then you need 64-bit Python version.
If you use 32-bit TNG then you need 32-bit Python version.



Unzip .zip file to folder where your embeddable Python will be. In my case this is 'C:\Python\python-3.11.2-embed-win32'.



Open folder and find main library .dll file. In my case it is 'python311.dll'. If you use different Python version it will be different but similar.
Full path is therefore 'C:\Python\python-3.11.2-embed-win32\python311.dll'.



Open TNG, go to settings and set this path as 'Python Library'.



Check if installation was successful and that Python is available to TNG by clicking 'Check' button. You should see dialog showing your Python version.



If Python is available then you can use it with TNG. It is recommended that you restart TNG after changing 'Python Library' setting.


Adding modules to embedded Python

Python can use various 3rd party modules. To use them, you need to install them to your embedded Python.



This is done with command prompt. Open new command prompt using 'Run' and then typing 'cmd.



Change directory to your embedded Python installation with 'cd' command.
In my case it is:

cd "C:\Python\python-3.11.2-embed-win32\"




Check that Python is working by using command:

python --version


You should see your Python version.



Then you need to install PIP. PIP is package manager used by Python to install modules.
To do this you need to download file named 'get-pip.py' from https://bootstrap.pypa.io/get-pip.py.
You can use 'curl' command which is included with Windows 10. You can also use other way to get this file.

curl -sSL https://bootstrap.pypa.io/get-pip.py -o get-pip.py




Make sure that get_pip.py file was successfully downloaded.



Install PIP using command:

python get-pip.py




Make sure installation was successful.

If it was then pip.exe is located in subfolder named 'Scripts'.



Open 'python311._pth' file with text editor.
Add two path lines lines:
 .\Lib
 .\Lib\site-packages
Uncomment (remove # character) from line 'import site':
It should look like this:

python311.zip
.
.\Lib
.\Lib\site-packages

# Uncomment to run site.main() automatically
import site



Now we can use PIP to install Python modules.
I will install 'NumPy' which offers comprehensive mathematical functions, random number generators, linear algebra routines, Fourier transforms, and more.

.\Scripts\pip.exe install numpy




Make sure there were no errors:



I will also install 'OpnenCV' which is a real-time optimized computer vision library.

.\Scripts\pip.exe install opencv_python




Make sure there were no errors:



You have successfully added two modules you your embedded Python installation.

You can create Python script to check if 'OpenCV' runs correctly. You need webcam for this test to work.
Create test file in your Python folder named 'cvtest.py'.
Add this content:

import cv2
 
cap = cv2.VideoCapture(0)
while(1): 
	# Capture image
	ret, image = cap.read() 
	#cv2.imshow('Camera',image) 
 
	# Convert image to grayscale
	img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
	#cv2.imshow('Gray',img_gray) 
 
	# Find canny edges
	edges = cv2.Canny(img_gray, 30, 200)
 
	# Find contours
	contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
	#cv2.imshow('Edges',edges)
 
	# Draw all contours (-1 means drawing all contours)
	cv2.drawContours(image, contours, -1, (0, 255, 0), 1)
	cv2.imshow('Contours', image)
 
	if ((cv2.waitKey(1) & 0xFF) == ord('q')):
		break
 
cv2.destroyAllWindows()
cap.release() 



Save file and run it with with:

python cvtest.py




After few seconds window with 'OpenCV' video stream will show.

Enjoy!

sdk/python/installation.txt · Last modified: 2023/03/23 09:09 by 127.0.0.1

Page Tools