-
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
Open
kostyay
wants to merge
3
commits into
tinygo-org:dev
Choose a base branch
from
kostyay:support_bts7960
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package bts7960 | ||
|
||
import ( | ||
"time" | ||
|
||
"machine" | ||
) | ||
|
||
// PWM is the interface necessary for controlling the bts 7960 motor. | ||
type PWM interface { | ||
Channel(pin machine.Pin) (channel uint8, err error) | ||
Top() uint32 | ||
Set(channel uint8, value uint32) | ||
} | ||
|
||
// Device is the bts7960 device | ||
type Device struct { | ||
lEn machine.Pin | ||
rEn machine.Pin | ||
lPwm machine.Pin | ||
rPwm machine.Pin | ||
rPwmCh, lPwmCh uint8 | ||
pwm PWM | ||
} | ||
|
||
// New returns a new motor driver. | ||
func New(lEn, rEn, lPwm, rPwm machine.Pin, pwm 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) { | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package main | ||
|
||
import ( | ||
"machine" | ||
"time" | ||
|
||
"tinygo.org/x/drivers/bts7960" | ||
) | ||
|
||
// Configuration for the Arduino Uno. | ||
// Please change the PWM and pin if you want to try this example on a different | ||
// board. | ||
var ( | ||
pwm = machine.Timer0 | ||
|
||
rEn = machine.D2 | ||
lEn = machine.D3 | ||
|
||
rPwm = machine.PD5 | ||
lPwm = machine.PD6 | ||
) | ||
|
||
func main() { | ||
if err := pwm.Configure(machine.PWMConfig{}); err != nil { | ||
println(err.Error()) | ||
return | ||
} | ||
|
||
bts := bts7960.New(lEn, rEn, lPwm, rPwm, pwm) | ||
err := bts.Configure() | ||
if err != nil { | ||
println("cannot configure bts: " + err.Error()) | ||
return | ||
} | ||
|
||
println("rotating left") | ||
bts.Left(50) | ||
|
||
println("rotating left") | ||
bts.Right(50) | ||
|
||
for { | ||
time.Sleep(time.Second) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Hmm, what about
Clockwise
/Counterclockwise
? No strong opinion from me, but it might be easier to understand. (I don't know this motor though so I don't know which terminology makes sense).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.
The controller has a L and and R pins so I prefer to keep it this way
