Sidebar

Home



PlanetCNC SDK


TNG API

Python

  About
  Installation

  Module 'planetcnc'
   print
   expr

   App status
    is_running
    is_initialized
    starttime

   Version & Profile Info
    get_ver
    get_version
    get_profile_name
    get_profile_path
    get_description

   Paths
    get_path
    get_path_exe
    get_path_profile
    get_path_temp
    get_path_working

   Parameters
    param_get
    param_set
    param_remove
    param_persistent

   Machine Commands
    estop
    stop
    pause
    start

    open
    close

    output
    output_pwm
    output_rc

    cmd_list
    cmd_count

    cmd_get_id
    cmd_get_name
    cmd_get_displayname

    cmd_is_enabled
    cmd_is_checked

    cmd_exec

   Machine Command status
    is_estop_enabled
    is_stop_enabled
    is_pause_enabled
    is_start_enabled
    is_output_enabled

    is_open_enabled
    is_close_enabled

    is_code_enabled
    is_codeex_enabled

   Helpers
    open_file
    open_code

    start_file
    start_code

    test_file
    test_code

   UI
    is_visible
    show
    hide
    set_progress
    set_status

   Message
    msg
    msgerr

    msg_show

    msg_count
    msg_is_active

    msg_click_ok
    msg_click_cancel

    bubble

   Dialog
    dlg_file_open
    dlg_file_save

    dlg_new
    dlg_init
    dlg_update
    dlg_clear

    dlg_show
    dlg_run
    dlg_abort

    dlg_is_valid
    dlg_keep_open
    dlg_callback

    dlg_set_modal
    dlg_set_resizable
    dlg_set_pos
    dlg_set_size
    dlg_set_btn
    dlg_set_name
    dlg_set_color

    dlg_add_option
    dlg_add_separator
    dlg_add_image
    dlg_add_label
    dlg_add_checkbox
    dlg_add_num_input
    dlg_add_txt_input
    dlg_add_dropbox
    dlg_add_button

    dlg_comp_pos
    dlg_comp_size
    dlg_comp_color
    dlg_comp_font
    dlg_comp_name
    dlg_comp_value
    dlg_comp_callback

    dlg_draw_line
    dlg_draw_arrow
    dlg_draw_circle
    dlg_draw_rect

    dlg_click_apply
    dlg_click_ok
    dlg_click_close

    dlg_has_btn_apply
    dlg_has_btn_ok
    dlg_has_btn_close

   SW State
    is_license_valid
    is_ui_ready
    is_program_loaded
    is_controller_connected
    is_controller_ready
    is_controller_running

   HW State
    hw_isinit
    hw_serial
    hw_version
    sw_version
    hw_version_valid
    hw_sim

    hw_buffavail
    hw_buffutil

    hw_idle
    hw_estop
    hw_stop
    hw_pause

    hw_isprog
    hw_iscmd
    hw_isjog

    hw_motor
    hw_work

    hw_speed
    hw_accel

    hw_spindle
    hw_spindle_dir
    hw_spindle_idx
    hw_spindle_enc
    hw_spindle_ui

    hw_mist
    hw_flood

    hw_output
    hw_output_freq
    hw_output_duty

    hw_input
    hw_limit
    hw_jog
    hw_jogpot
    hw_ctrl
    hw_aux

   Points
    points_clear
    points_count
    points_add
    points_get
    points_set
    points_delete
    points_load
    points_save

   Image
    image_open
    image_open_data
    image_close
    image_save
    image_size
    image_get_pixel
    image_get_pixel_gray
    image_to_cam
    image_from_cam

   G-Code
    gcode_is_ready
    gcode_is_opening
    gcode_is_running
    gcode_close
    gcode_open
    gcode_load
    gcode_line_add_allowed
    gcode_line_add

   Misc
    sleep

    ready
    terminate
    send_message
    read_message

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'.

If PIP is already installed and you just need to upgrade it use command:

python -m pip install --upgrade pip



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:

#! /usr/bin/env python
 
import cv2
import planetcnc
 
def OpenCV():
	print(f"OpenCV version: {cv2.__version__}")
	backends = cv2.videoio_registry.getBackends()
 
	print("Available OpenCV VideoCapture Backends:")
	for backend_id in backends:
		backend_name = cv2.videoio_registry.getBackendName(backend_id)  
		print(f"- {backend_name}")
 
 
	cap = cv2.VideoCapture(0, cv2.CAP_DSHOW) #Windows 
	#cap = cv2.VideoCapture(0, cv2.CAP_V4L2) #Linux 
 
	if not cap.isOpened():
		print("Error: Could not open camera.")
		return
 
	# Get the backend name
	backend = cap.getBackendName()  
	print(f"Using backend: {backend}")
 
	while(1): 
		# Capture image
		ret, image = cap.read() 
		if not ret:
			print("Error: Could not read frame from camera.")
			break
 
		# Convert image to grayscale
		img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
 
		# Find canny edges
		edges = cv2.Canny(img_gray, 30, 200)
 
		# Find contours
		contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
 
		# Draw all contours (-1 means drawing all contours)
		cv2.drawContours(image, contours, -1, (0, 255, 0), 1)
 
		# Show 
		cv2.imshow('Contours', image)
 
		# Wait for a key press (1ms delay)
		key = cv2.waitKey(1)
 
		if cv2.getWindowProperty('Contours', cv2.WND_PROP_VISIBLE) < 1:
			break
 
		if key & 0xFF == 27:  # Allow quitting with 'ESC'
			print("Window closed by pressing 'ESC'.")
			break
 
	cap.release() 
	cv2.destroyAllWindows()
 
 
# Run function
OpenCV()



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: by 127.0.0.1

Page Tools