APIドキュメント
python-step-series へようこそ!このページでは、インストールについてや、STEPシリーズを構成・制御するための最初のアプリケーションを開発する方法を説明しています。
注釈
始める前に、サイドバーにあるSTEP400/STEP800のチュートリアルを必ずお読みください。
概要
python-step-series はSTEP400やSTEP800を使ってモータを制御するための使いやすいプログラムのインタフェイスを提供するために構築されたライブラリです。STEP400/STEP800 のチュートリアルで述べたように、他のサードパーティアプリケーションを使ってこのライブラリと同じ動作を行うことも可能です。
このチュートリアルを通して、ライブラリをインストールし、その正しい使い方を学ぶことができます。
インストール
注釈
このセクションは、このライブラリのエンドユーザーに向けて書かれています。ライブラリへコントリビュートしたい方は、本ライブラリのGithubにあるCONTRIBUTINGガイドをご覧ください。
おすすめのインストール方は、Pythonの virtualenv 内で pip を使用する方法です。
pip install virtualenv
virtualenv -v my-venv
source my-venv/bin/activate
# Windows users: .\my-venv\Scripts\activate
pip install python-step-series
以上です!ライブラリをインポートするだけでインストールを確認することができます。
import stepseries
注釈
新しいターミナル画面では、virtualenv がアクティブ状態になっていません。上記の activate コマンドを再度実行する必要があります。
virtualenvを終了するには、以下のコマンドを実行してください。
deactivate
最初のステップ
前述したように、 python-step-series はできるだけ簡単に使えるように努めています。例えば、どちらのデバイスともわずか4行のコードで通信を確立できます。
# Import the commands module. Commands are essentially templates
# that need to be filled with data. This ensures you only have to
# worry about WHAT to send, not how.
from stepseries import commands
# Import the interface class. This class is what is used to send and
# receive data with the physical device.
from stepseries.step400 import STEP400 # Replace "400" with "800" if you have a STEP800
# Set-up and configure the library for communication with the device
device = STEP400(1, "10.0.0.101")
# Establish communication with the device
device.set(commands.SetDestIP())
デバイスとの通信を確立するには、必ず commands.SetDestIP() を送信する必要があります。
CommandsとResponsesを理解する
Commands はデバイスを制御するために使用し、 Responses はデバイスから送信されます。このライブラリは、データクラスオブジェクトとして両方を提供し、値を入れるテンプレートや構造化された応答を提供します。
Commandsはライブラリの commands モジュールにあるので、上ではそれをインポートしました。同様に、Responsesは responses モジュールに存在します。
コールバックを使う
python-step-series はコールバックを用いたプログラミングをサポートします。単一または複数のコールバックを単一のレスポンスにバインドして、デバイスからレスポンスが送信された際に呼び出すことができます。
例えば:
from stepseries import commands, responses, step400
def version_handler(message: responses.Version) -> None:
print("Firmware:")
print(" - Name:", message.firmware_name)
print(" - Version:", message.firmware_version)
print(" - Compiled:", message.compile_date)
device = step400.STEP400(1, "10.0.0.101")
device.set(commands.SetDestIP())
# Call 'version_handler' when a 'Version' response is sent
device.on(responses.Version, version_handler)
# Get the current version of the firmware
# Notice that the code in 'version_handler' will be printed to your
# console
# Also note that the response is returned by the function call while
# being sent to the handler
version = device.get(commands.GetVersion())
あるいは、すべてのレスポンスを単一のコールバックにバインドすることもできます。
from stepseries import commands, responses, step400
def default_handler(message: OSCResponse) -> None:
print("Message received:", message)
device = step400.STEP400(1, "10.0.0.101")
device.set(commands.SetDestIP())
# Call 'default_handler' when any response is sent by the device
device.on(None, default_handler) # None means send everything
device.get(commands.GetVersion())
device.get(commands.GetStatus(motor_id=1))
python-step-series のコールバックAPIは必要に応じてシンプルにも複雑にもすることができます。可能性はほぼ無限大です。
この短いチュートリアルで、ライブラリが提供するものの概要をご理解いただけたと思います。どのようなコマンドやレスポンスが利用できるかは、モジュールのページで確認されることをお勧めします。