tinygo-org / drivers

TinyGo drivers for sensors, displays, wireless adaptors, and other devices that use I2C, SPI, GPIO, ADC, and UART interfaces.
https://tinygo.org
BSD 3-Clause "New" or "Revised" License
599 stars 188 forks source link

bts7960: Add new device package #447

Open kostyay opened 2 years ago

kostyay commented 2 years ago

Added support for bts 7960 motor driver. It is based on the C++ code from this library: https://github.com/luisllamasbinaburo/Arduino-BTS7960

kostyay commented 2 years ago

Fixed ur comments

deadprogram commented 2 years ago

First of all, thanks for working on this.

SO if you look at both the L293x and the BTS7960, it can be noted that they are both H-bridges and as a result have essentially the same sort of way that you connect to them and use them.

As the Wikipedia page says:

These circuits are often used in robotics and other applications to allow DC motors to run forwards or backwards.

The only difference is that the BTS7960 has a connection to enable separately the forward direction from the reverse direction.

I would suggest basically copy the structure/format of the L293x driver. Of course rename the pins, and then implement the same API in the manner that the BTS7960 requires. I propose that we define Motoring interface:

type Motoring interface {
    Forward()
    ForwardWithSpeed(speed uint32)
    Backward()
    BackwardWithSpeed(speed uint32)
    Stop()
}

In the case of the Forward() and Backward() methods, a device with PWM would use full speed.

In the case of the ForwardWithSpeed() and BackwardWithSpeed() methods, a device without PWM would ignore the params and use full speed.

This will make it possible to use either the BTS7960 or the L293X with a minimal amount of code changes.

Last comment, the L293X driver does not try to configure the PWM for the user: https://github.com/tinygo-org/drivers/blob/release/l293x/l293x.go#L88-L89

This is part because not every board has the same PWM implementation, even though they expose the same interface. Just seems like it simplifies things for users.

What do you think? If it seems like a good idea, I can go back to the L293x and make sure it is doing the same thing.

kostyay commented 2 years ago

Hey Sounds good, I will do it :)