-
Notifications
You must be signed in to change notification settings - Fork 200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bts7960: Add new device package #447
base: dev
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package bts7960 | ||
|
||
import ( | ||
"machine" | ||
"time" | ||
) | ||
|
||
type Device struct { | ||
lEn machine.Pin | ||
rEn machine.Pin | ||
lPwm machine.Pin | ||
rPwm machine.Pin | ||
rPwmCh, lPwmCh uint8 | ||
pwm machine.PWM | ||
} | ||
|
||
// New returns a new motor driver. | ||
func New(lEn, rEn, lPwm, rPwm machine.Pin, pwm machine.PWM) *Device { | ||
return &Device{lEn: lEn, rEn: rEn, lPwm: lPwm, rPwm: rPwm, pwm: pwm} | ||
} | ||
|
||
// Configure configures the Device. | ||
func (d *Device) Configure() error { | ||
d.rPwm.Configure(machine.PinConfig{Mode: machine.PinOutput}) | ||
d.lPwm.Configure(machine.PinConfig{Mode: machine.PinOutput}) | ||
d.lEn.Configure(machine.PinConfig{Mode: machine.PinOutput}) | ||
d.rEn.Configure(machine.PinConfig{Mode: machine.PinOutput}) | ||
|
||
var err error | ||
d.lPwmCh, err = d.pwm.Channel(d.lPwm) | ||
if err != nil { | ||
println("failed to configure lpwn: " + err.Error()) | ||
return err | ||
} | ||
|
||
d.rPwmCh, err = d.pwm.Channel(d.rPwm) | ||
if err != nil { | ||
println("failed to configure rpwn: " + err.Error()) | ||
return err | ||
} | ||
|
||
d.Stop() | ||
|
||
return nil | ||
} | ||
|
||
// Enable enables the motor driver | ||
func (d *Device) Enable() { | ||
d.lEn.High() | ||
d.rEn.High() | ||
} | ||
|
||
// Disable disabled the motor driver | ||
func (d *Device) Disable() { | ||
d.lEn.Low() | ||
d.rEn.Low() | ||
} | ||
|
||
// Stop stops the motor | ||
func (d *Device) Stop() { | ||
d.pwm.Set(d.lPwmCh, 0) | ||
d.pwm.Set(d.rPwmCh, 0) | ||
} | ||
|
||
// Left turns motor left. | ||
func (d *Device) Left(speed uint32) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, what about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
d.pwm.Set(d.rPwmCh, 0) | ||
time.Sleep(time.Microsecond * 100) | ||
d.pwm.Set(d.lPwmCh, d.pwm.Top()*speed/100) | ||
} | ||
|
||
// Right turns motor right. | ||
func (d *Device) Right(speed uint32) { | ||
d.pwm.Set(d.lPwmCh, 0) | ||
time.Sleep(time.Microsecond * 100) | ||
d.pwm.Set(d.rPwmCh, d.pwm.Top()*speed/100) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you define and use a PWM interface instead of using
machine.PWM
? It is called differently on other chips.For example, with the following interface: