Skip to content
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
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ The following 81 devices are supported.
| [4x4 Membrane Keypad](https://cdn.sparkfun.com/assets/f/f/a/5/0/DS-16038.pdf) | GPIO |
| [L293x motor driver](https://www.ti.com/lit/ds/symlink/l293d.pdf) | GPIO/PWM |
| [L9110x motor driver](https://www.elecrow.com/download/datasheet-l9110.pdf) | GPIO/PWM |
| [BTS7960 motor driver](https://www.handsontec.com/dataspecs/module/BTS7960%20Motor%20Driver.pdf) | GPIO/PWM |
| [LIS2MDL magnetometer](https://www.st.com/resource/en/datasheet/lis2mdl.pdf) | I2C |
| [LIS3DH accelerometer](https://www.st.com/resource/en/datasheet/lis3dh.pdf) | I2C |
| [LPS22HB MEMS nano pressure sensor](https://www.st.com/resource/en/datasheet/dm00140895.pdf) | I2C |
Expand Down
77 changes: 77 additions & 0 deletions bts7960/bts7960.go
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 {
Copy link
Member

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:

type PWM interface {
    Channel(pin machine.Pin) (channel uint8, err error)
    Set(channel uint8, value uint32)
    Top() uint32
}
Suggested change
func New(lEn, rEn, lPwm, rPwm machine.Pin, pwm machine.PWM) *Device {
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) {
Copy link
Member

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).

Copy link
Author

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
image

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)
}