パルスモーターを1ステップずつ駆動しながらセンサーのアナログ出力を記録したい、という要望がありましたので、ラズパイPico2を使ってMicroPython + LabVIEWでテスト用のプログラムを作成してみました。

ステッピングモーターは17HS4401J(1.8°, 1.7A)と書かれているメーカー不明品で、モータードライバはMicrostep Driver(最小32マイクロステップ、9-40VDC)と書かれているメーカー不明品です。
接続表
Microstep Driver
ENA- >> GND (pico2)
ENA+ >> GP0(pico2)
DIR- >> GND(pico2)
DIR+ >> GP1(pico2)
PUL- >> GND(pico2)
PUL+ >> GP2(pico2)
B- >> 青(ステッピングモーター)
B+ >> 赤(ステッピングモーター)
A- >> 緑(ステッピングモーター)
A+ >> 黒(ステッピングモーター)
GND >> GND(電源)
VCC >> 10.5V / 3.3A(電源)
-----------------------------------------------------------
アナログセンサー(未接続) >> ADC0(pico2)
Pico2に保村するMicroPythonプログラム(ファイル名:step_measure.py)
LabVIEWなどから対話モード(インタラクティブモード)で利用します。
from step_measure import direction, step_m
で関数をインポートすれば、direction関数、step_m関数を使用することができます。
'''
Rotate the stepping motor one step and
measure the voltage with the analog input
a specified number of times.
2024.09.16 MaDA Lab. Koji Ohashi
'''
from machine import Pin, ADC
import time
ena = Pin( 0, Pin.OUT )
dir = Pin( 1, Pin.OUT )
pul = Pin( 2, Pin.OUT )
analog = ADC(0)
def direction(x):
if x <= 0:
dir.high()
else:
dir.low()
def unlock(x):
if x <= 0:
ena.low()
else:
ena.high()
def step_m(count, measure, wait_ms):
ena.low()
for i in range(count):
pul.high()
time.sleep_ms(1)
if measure > 0:
print(f'{analog.read_u16():04x}', end="")
print(',', end="")
pul.low()
time.sleep_ms(wait_ms)
print()
Thonnyでstep_measure.pyをPico2にコピーします。

LabVIEWプログラムはサブVI"sub_CommandResponse.vi"で対話を行います。とりあえずテスト的にMicroPythonと対話する手軽な方法だと思います。

メインプログラムでは空白を送って対話モードに入り、directionとstep_mをインポートします。その後whileループ内でCommandに
"step_m(ステップ数, アナログ入力あり(1) / なし(0), パルス後の待ち時間(ms)"命令を記入してWriteボタンを押します。回転方向を変更する場合は"direction(0または1)"を送ります。


400ステップなので400個のデータが送られてきます。Pico, Pico2は12ビットADCですが、読み値は16ビットデータとして出力されるので、係数3.3V/65536を掛けると電圧に換算できます。この例ではPico2の3.3V出力端子に接続していたのでffffが出力されています。
カンマ区切りのデータで最後に改行記号が入るので、LabVIEW側では1ブロックの出力として受け取ります。
MicroPythonではADC読み値を4桁の16進数(0パッディング)で改行無しでプリントするのは以下のコードのようです。
print(f'{analog.read_u16():04x}', end="")
MicroPython使いやすいと思いませんか?