Skip to content

Instantly share code, notes, and snippets.

@moyashi
Created February 6, 2021 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moyashi/172b1d53bb574c64f6fbfdc4435b9c5a to your computer and use it in GitHub Desktop.
Save moyashi/172b1d53bb574c64f6fbfdc4435b9c5a to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import os
import sys
# pip3 install pyperclip
import pyperclip
# これで実施したマニュアルメッシュベッドレベリングの補正用途: https://github.com/davidramiro/Marlin-Ai3M
# 設定部
OCTOPRINT_IP_ADDRESS = '192.168.1.42'
OCTOPRINT_API_KEY = 'Cxxxxxxxxxxxxxxxxxxxxxxxxxx'
FILE_NAME = 'level_shift.gcode'
# 準備:
# ・HomebrewなどでPython3をインストール。pip3でpyperclipをインストール
# ・OctoPrintが稼働しているマシンのIPアドレスを設定部のOCTOPRINT_IP_ADDRESSに記入
# ・OctoPrintの「設定>API」からAPI Keyを取得して設定部のOCTOPRINT_API_KEYに記入
# 使い方:
#
# OctoPrintのTerminalタブでM503を実行し、G29のメッシュテーブルを含む
# ログをクリップボードにコピー
# 使うのはG29の行だけで、余計な行が入ってしまっても処理から除外します
#
# こんなやつ↓
# Recv: echo:Mesh Bed Leveling:
# Recv: echo: M420 S1 Z0.00
# Recv: echo: G29 S3 X1 Y1 Z-1.17000
# Recv: echo: G29 S3 X2 Y1 Z-1.13000
# ...
# macOSのターミナルから以下のように実行
# python3 level_shifter.py -0.01
# 意味: ノズルを0.01mmベッドに近くメッシュベッドレベリングのテーブルをシフトする
# ノズルを0.01mmベッドから遠くする場合は正の数字を指定
# ./level_shifter.py 0.01
# するとデスクトップにlevel_shift.gcodeが生成され、OctoPrintにアップロードされる
# それを「Print」でプリンタに実行させるとメッシュベッドレベリングのテーブルが書き換えられ
# EEPROMに保存される(M500とM501まで実行される)
OUTPUT_FILE_PATH = os.path.join(os.environ['HOME'], "Desktop", FILE_NAME)
# 引数チェック
args = sys.argv
if (len(args) == 1):
print("Argument not specified.")
exit()
elif (len(args) > 2):
print("Too many Arguments specified.")
exit()
try:
diff = float(args[1])
except ValueError:
print("Invalid argument specified.")
exit()
# クリップボードから入力・整形
input = pyperclip.paste()
input = input.replace("Recv: echo: ", "")
# 主処理
with open(OUTPUT_FILE_PATH, mode='w') as f:
for i in input.split("\n"):
if (i[0:3] == "G29" and i[13:14] == "Z"):
try:
float(i[14:-1])
except ValueError:
print("Invalid line: " + i)
exit()
f.write(i[0:14] + '{:.2f}'.format(float(i[14:-1]) + diff) + '\n')
f.write('M500\n')
f.write('M501\n')
# OctoPrintへのアップロード
cmd1 = f'curl -k -H "X-Api-Key: {OCTOPRINT_API_KEY}" -F "file=@{OUTPUT_FILE_PATH}" -F "select=false" -F "print=false" "http://{OCTOPRINT_IP_ADDRESS}/api/files/local"'
cmd2 = f'open http://{OCTOPRINT_IP_ADDRESS}/'
print(cmd1)
os.system(cmd1)
print(cmd2)
os.system(cmd2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment