1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-05-02 22:17:12 +08:00
hybridgroup.gobot/platforms/gpio/motor_driver.go

170 lines
3.0 KiB
Go
Raw Normal View History

2014-04-27 19:34:16 -07:00
package gpio
import (
"github.com/hybridgroup/gobot"
)
2014-04-27 19:34:16 -07:00
type MotorDriver struct {
gobot.Driver
2014-04-27 19:34:16 -07:00
Adaptor PwmDigitalWriter
SpeedPin string
SwitchPin string
DirectionPin string
ForwardPin string
BackwardPin string
CurrentState byte
CurrentSpeed byte
CurrentMode string
CurrentDirection string
}
2014-05-22 21:29:37 -07:00
func NewMotorDriver(a PwmDigitalWriter, name string, pin string) *MotorDriver {
2014-04-27 19:34:16 -07:00
return &MotorDriver{
Driver: gobot.Driver{
2014-05-22 21:29:37 -07:00
Name: name,
Pin: pin,
2014-04-27 19:34:16 -07:00
Commands: []string{
"OffC",
"OnC",
"IsOnC",
"IsOffC",
"ToggleC",
"SpeedC",
"MinC",
"MaxC",
"ForwardC",
"BackwardC",
"CurrentSpeedC",
},
},
CurrentState: 0,
CurrentSpeed: 0,
CurrentMode: "digital",
CurrentDirection: "forward",
Adaptor: a,
}
}
2014-04-27 19:34:16 -07:00
func (m *MotorDriver) Start() bool { return true }
func (m *MotorDriver) Halt() bool { return true }
func (m *MotorDriver) Init() bool { return true }
2014-04-27 19:34:16 -07:00
func (m *MotorDriver) Off() {
if m.isDigital() {
m.changeState(0)
} else {
m.Speed(0)
}
}
2014-04-27 19:34:16 -07:00
func (m *MotorDriver) On() {
if m.isDigital() {
m.changeState(1)
} else {
if m.CurrentSpeed == 0 {
m.CurrentSpeed = 255
}
m.Speed(m.CurrentSpeed)
}
}
2014-04-27 19:34:16 -07:00
func (m *MotorDriver) Min() {
m.Off()
}
2014-04-27 19:34:16 -07:00
func (m *MotorDriver) Max() {
m.Speed(255)
}
2014-04-27 19:34:16 -07:00
func (m *MotorDriver) IsOn() bool {
if m.isDigital() {
return m.CurrentState == 1
} else {
return m.CurrentSpeed > 0
}
}
2014-04-27 19:34:16 -07:00
func (m *MotorDriver) IsOff() bool {
return !m.IsOn()
}
2014-04-27 19:34:16 -07:00
func (m *MotorDriver) Toggle() {
if m.IsOn() {
m.Off()
} else {
m.On()
}
}
2014-04-27 19:34:16 -07:00
func (m *MotorDriver) Speed(value byte) {
m.CurrentMode = "analog"
m.CurrentSpeed = value
m.Adaptor.PwmWrite(m.SpeedPin, value)
}
2014-04-27 19:34:16 -07:00
func (m *MotorDriver) Forward(speed byte) {
m.Direction("forward")
m.Speed(speed)
}
2014-04-27 19:34:16 -07:00
func (m *MotorDriver) Backward(speed byte) {
m.Direction("backward")
m.Speed(speed)
}
2014-04-27 19:34:16 -07:00
func (m *MotorDriver) Direction(direction string) {
m.CurrentDirection = direction
if m.DirectionPin != "" {
var level byte
if direction == "forward" {
level = 1
} else {
level = 0
}
m.Adaptor.DigitalWrite(m.DirectionPin, level)
} else {
var forwardLevel, backwardLevel byte
switch direction {
case "forward":
forwardLevel = 1
backwardLevel = 0
case "backward":
forwardLevel = 0
backwardLevel = 1
case "none":
forwardLevel = 0
backwardLevel = 0
}
m.Adaptor.DigitalWrite(m.ForwardPin, forwardLevel)
m.Adaptor.DigitalWrite(m.BackwardPin, backwardLevel)
}
}
2014-04-27 19:34:16 -07:00
func (m *MotorDriver) isDigital() bool {
if m.CurrentMode == "digital" {
return true
}
return false
}
2014-04-27 19:34:16 -07:00
func (m *MotorDriver) changeState(state byte) {
m.CurrentState = state
if state == 1 {
m.CurrentSpeed = 0
} else {
m.CurrentSpeed = 255
}
if m.ForwardPin != "" {
if state == 0 {
m.Direction(m.CurrentDirection)
if m.SpeedPin != "" {
m.Speed(m.CurrentSpeed)
}
} else {
m.Direction("none")
}
} else {
m.Adaptor.DigitalWrite(m.SpeedPin, state)
}
}