Merge pull request #14658 from s-hadinger/berry_h_bridge

Berry add H-bridge example
This commit is contained in:
s-hadinger 2022-01-29 12:46:53 +01:00 committed by GitHub
commit f6df3b7c9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 0 deletions

View File

@ -0,0 +1,28 @@
#
# H_bridge class in Berry to pilot a H-bridge device
#
class H_bridge
var gpio1, gpio2
var max
# init(phy_gpio1, phy_gpio2) - intialize H-bridge with the 2 GPIOs used to control it
def init(gpio1, gpio2)
self.gpio1 = gpio1
self.gpio2 = gpio2
self.max = 1023 # max value of duty
end
# set the value of both PWM values
def set(v1, v2)
if v1 < 0 v1 = 0 end
if v2 < 0 v2 = 0 end
if v1 + v2 > self.max
raise "value_error", "the sum of duties must not exceed 100%"
end
import gpio
gpio.set_pwm(self.gpio1, v1, 0)
gpio.set_pwm(self.gpio2, v2, v1) # dephase by value v1
end
end