Na tej stronie
Produkty użyte w tym poradniku
W tym poradniku zobaczysz, jak podłączyć najpopularniejsze typy silników do SBC. Pokażemy Ci również, jak nimi sterować na konkretnych przykładach. Najpopularniejsze rodzaje silników to:
- Silniki DC są tanie i łatwe w sterowaniu – wymagają tylko jednego wyjścia zasilania, które można aktywować sygnałem PWM.
- Serwomechanizmy są podobne do silników DC, ale posiadają wbudowaną elektronikę oraz wewnętrzny potencjometr tworzące zamkniętą pętlę sprzężenia zwrotnego, co pozwala na precyzyjną kontrolę kąta obrotu. Sterowanie również odbywa się za pomocą PWM, jednak w tym przypadku sygnał sterujący małą moc, a stopień mocy jest zintegrowany w samym serwomechanizmie.
- Silniki krokowe mają kilka uzwojeń i wymagają odpowiedniej sekwencji sterowania, ale w zamian oferują dużą precyzję, ponieważ przemieszczają się krok po kroku za każdym razem, gdy polaryzacja ich uzwojeń zostaje odwrócona.
1. Silniki DC
Aby sterować silnikiem DC, podłączymy go do SBC zgodnie z poniższym schematem:


Musimy znać pewne parametry naszego silnika, w szczególności relację między przyłożonym napięciem a prędkością obrotową (stałą Kv).

Klasa Dc_Motor reprezentuje silnik DC sterowany sygnałem PWM o częstotliwości 50 Hz. Jak widać, należy podać pewne parametry, takie jak napięcie zasilania i stała Kv, a także wykorzystywany pin. W zamian otrzymujemy dwie funkcje: set_voltage oraz set_speed.
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 ) |
Przykładowe użycie:
>>> 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 ) |
A oto przykładowe użycie:
>>> 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() |
Na koniec przykład użycia:
>>> 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 )
Nadal masz problemy?
Skontaktuj się z nami, aby uzyskać wsparcie od prawdziwego inżyniera – a nie sztucznej inteligencji – lub poproś o brakujący poradnik
i




