1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-27 13:48:56 +08:00
hybridgroup.gobot/platforms/gpio/servo_driver.go

97 lines
2.2 KiB
Go
Raw Normal View History

2014-04-27 19:34:16 -07:00
package gpio
import (
"github.com/hybridgroup/gobot"
)
var _ gobot.DriverInterface = (*ServoDriver)(nil)
2014-09-27 11:34:13 -07:00
// Represents a Servo
2014-04-27 19:34:16 -07:00
type ServoDriver struct {
gobot.Driver
CurrentAngle byte
}
2014-09-27 11:34:13 -07:00
// NewSerovDriver return a new ServoDriver given a Servo, name and pin.
//
// Adds the following API Commands:
// "Move" - See ServoDriver.Move
// "Min" - See ServoDriver.Min
// "Center" - See ServoDriver.Center
// "Max" - See ServoDriver.Max
2014-05-22 21:29:37 -07:00
func NewServoDriver(a Servo, name string, pin string) *ServoDriver {
2014-06-11 17:41:04 -07:00
s := &ServoDriver{
Driver: *gobot.NewDriver(
name,
"ServoDriver",
a.(gobot.AdaptorInterface),
pin,
),
2014-04-27 19:34:16 -07:00
CurrentAngle: 0,
}
2014-06-11 17:41:04 -07:00
2014-07-09 18:32:27 -07:00
s.AddCommand("Move", func(params map[string]interface{}) interface{} {
2014-06-11 17:41:04 -07:00
angle := byte(params["angle"].(float64))
s.Move(angle)
return nil
})
2014-07-09 18:32:27 -07:00
s.AddCommand("Min", func(params map[string]interface{}) interface{} {
2014-06-11 17:41:04 -07:00
s.Min()
return nil
})
2014-07-09 18:32:27 -07:00
s.AddCommand("Center", func(params map[string]interface{}) interface{} {
2014-06-11 17:41:04 -07:00
s.Center()
return nil
})
2014-07-09 18:32:27 -07:00
s.AddCommand("Max", func(params map[string]interface{}) interface{} {
2014-06-11 17:41:04 -07:00
s.Max()
return nil
})
return s
2014-04-27 19:34:16 -07:00
}
func (s *ServoDriver) adaptor() Servo {
2014-07-09 18:32:27 -07:00
return s.Adaptor().(Servo)
}
2014-09-27 11:34:13 -07:00
// Start starts the ServoDriver. Returns true on successful start of the driver.
func (s *ServoDriver) Start() error { return nil }
2014-04-27 19:34:16 -07:00
2014-09-27 11:34:13 -07:00
// Halt halts the ServoDriver. Returns true on successful halt of the driver.
func (s *ServoDriver) Halt() error { return nil }
2014-09-27 11:34:13 -07:00
// InitServo initializes the ServoDriver on platforms which require an explicit initialization.
2014-04-27 19:34:16 -07:00
func (s *ServoDriver) InitServo() {
s.adaptor().InitServo()
2014-04-27 19:34:16 -07:00
}
2014-09-27 11:34:13 -07:00
// Move sets the servo to the specified angle
2014-04-27 19:34:16 -07:00
func (s *ServoDriver) Move(angle uint8) {
if !(angle >= 0 && angle <= 180) {
panic("Servo angle must be an integer between 0-180")
}
s.CurrentAngle = angle
s.adaptor().ServoWrite(s.Pin(), s.angleToSpan(angle))
2014-04-27 19:34:16 -07:00
}
2014-09-27 11:34:13 -07:00
// Min sets the servo to it's minimum position
2014-04-27 19:34:16 -07:00
func (s *ServoDriver) Min() {
s.Move(0)
}
2014-09-27 11:34:13 -07:00
// Center sets the servo to it's center position
2014-04-27 19:34:16 -07:00
func (s *ServoDriver) Center() {
s.Move(90)
}
2014-09-27 11:34:13 -07:00
// Max sets the servo to its maximum position
2014-04-27 19:34:16 -07:00
func (s *ServoDriver) Max() {
s.Move(180)
}
func (s *ServoDriver) angleToSpan(angle byte) byte {
return byte(angle * (255 / 180))
}