このチュートリアルでは、最も一般的なタイプのモーターをSBCに接続する方法を見ていきます。また、いくつかの例を用いてそれらを制御する方法についても紹介します。最も一般的なタイプのモーターは次のとおりです。
- DCモーターは安価で制御が簡単です。必要な電源出力は1つだけで、これはPWM信号で有効化できます。
- サーボモーターはDCモーターに似ていますが、小型の電子回路と内部ポテンショメーターによる閉ループ制御を備えており、回転角度を制御できます。制御もPWMで行われますが、この場合の制御信号は低電力であり、電力段はサーボモーター自体に内蔵されています。
- ステッピングモーターには複数の巻線があり、制御にはシーケンスが必要ですが、その代わり、巻線の極性が反転するたびに1ステップずつ前進するため、高い精度が得られます。
1. DCモーター
DCモーターを制御するには、次の図のようにSBCに接続します。


モーターのいくつかのパラメータ、具体的には印加電圧とモーター速度の関係(Kv値)を知る必要があります。

Dc_MotorクラスはDCモーターを表し、50HzのPWMで制御されます。ご覧のとおり、電源電圧やKv定数、使用するピンなどのパラメータを指定する必要があります。その結果として、set_voltageとset_speedの2つの関数を取得できます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class Dc_Motor: FREQ_Hz = 50 def __init__( self, pin, vm, kv ): self.vm = vm self.kv = kv self.pin = pin self.dout = sbc.Dout() self.dout.set_pwm( self.pin, self.FREQ_Hz, 0 ) self.set_voltage( 0 ) def set_voltage( self, v ): pulse_percent_on = 100*v/self.vm self.dout.set_pwm( self.pin, self.FREQ_Hz, 100-pulse_percent_on ) def set_speed( self, rpm ): self.set_voltage( rpm/self.kv ) |
使用例は次のとおりです。
>>> import time
>>> import sbc_mtr
>>> motor = sbc_mtr.Dc_Motor( "DOUT3", vm=12, kv=0.5 )
>>> print( "Voltage control" )
Voltage control
>>> motor.set_voltage( 6 )
>>> time.sleep( 1 )
>>> motor.set_voltage( 0 )
>>> print( "Speed control" )
Speed control
>>> motor.set_speed( 100 )
>>> time.sleep( 1 )
>>> motor.set_speed( 0 )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class Servo_Motor: FREQ_Hz = 50 DUTY_MIN_ms = 0.5 DUTY_MAX_ms = 2.5 def __init__( self, pin ): self.pin = pin self.dout = sbc.Dout() self.dout.set_pwm( self.pin, self.FREQ_Hz, 0 ) def set_angle( self, angle_deg ): duty_ms = self.DUTY_MIN_ms + (self.DUTY_MAX_ms-self.DUTY_MIN_ms)*angle_deg/180 pulse_percent_on = self.FREQ_Hz*duty_ms/10 self.dout.set_pwm( self.pin, self.FREQ_Hz, 100-pulse_percent_on ) |
使用例:
>>> import time
>>> import sbc_mtr
>>> motor = sbc_mtr.Servo_Motor( "DOUT3" )
>>> print( "Set angle to 90 degrees" )
Set angle to 90 degrees
>>> motor.set_angle( 90 )
>>> time.sleep( 1 )
>>> print( "Set angle to 0 degrees" )
Set angle to 0 degrees
>>> motor.set_angle( 0 )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | class Stepper_Motor: def __init__( self, pins ): self.pins = pins self.dout = sbc.Dout() self.state = 0 self.steps = 0 self.dir = 1 self.timer = machine.Timer( -1 ) def step( self, direction ): if( self.state == 0 ): self.dout.write( self.pins[0], 1 ) self.dout.write( self.pins[1], 1 ) self.dout.write( self.pins[2], 0 ) self.dout.write( self.pins[3], 0 ) elif( self.state == 1 ): self.dout.write( self.pins[0], 0 ) self.dout.write( self.pins[1], 1 ) self.dout.write( self.pins[2], 1 ) self.dout.write( self.pins[3], 0 ) elif( self.state == 2 ): self.dout.write( self.pins[0], 0 ) self.dout.write( self.pins[1], 0 ) self.dout.write( self.pins[2], 1 ) self.dout.write( self.pins[3], 1 ) elif( self.state == 3 ): self.dout.write( self.pins[0], 1 ) self.dout.write( self.pins[1], 0 ) self.dout.write( self.pins[2], 0 ) self.dout.write( self.pins[3], 1 ) self.steps += direction self.state = (self.state + direction) & 0x03 def set_speed( self, steps_s, direction ): if( steps_s and direction ): self.timer.init( period=int(1000/steps_s), mode=machine.Timer.PERIODIC, callback=lambda tmr: self.step(direction) ) else: self.timer.deinit() |
最後に、使用例を示します:
>>> import time
>>> import sbc_mtr
>>> motor = sbc_mtr.Stepper_Motor( ["DOUT1", "DOUT2", "DOUT3", "DOUT4"] )
>>> print( "Step by step" )
Step by step
>>> for i in range( 100 ):
... motor.step( 1 )
... time.sleep_ms( 10 )
>>> print( "Continuous" )
Continuous
>>> motor.set_speed( 100, -1 )
>>> time.sleep( 1 )
>>> motor.set_speed( 0, 0 )
および




