Documentation
¶
Overview ¶
Example ¶
package main import ( "fmt" "log" "github.com/cgxeiji/servo" ) func main() { // Use servo.Close() to close the connection of all servos and pi-blaster. defer servo.Close() // If you want to move the servos, make sure that pi-blaster is running. // For example, start pi-blaster as: // $ sudo pi-blaster --gpio 14 --pcm // Create a new servo connected to gpio 14. myServo := servo.New(14) // (optional) Initialize the servo with your preferred values. // myServo.Flags = servo.Normalized | servo.Centered myServo.MinPulse = 0.05 // Set the minimum pwm pulse width (default: 0.05). myServo.MaxPulse = 0.25 // Set the maximum pwm pulse width (default: 0.25). myServo.SetPosition(90) // Set the initial position to 90 degrees. myServo.SetSpeed(0.2) // Set the speed to 20% (default: 1.0). // NOTE: The maximum speed of the servo is 0.19s/60degrees. // (optional) Set a verbose name. myServo.Name = "My Servo" // Print the information of the servo. fmt.Println(myServo) // Connect the servo to the daemon. err := myServo.Connect() if err != nil { log.Fatal(err) } // (optional) Use myServo.Close() to close the connection to a specific // servo. You still need to close the connection to pi-blaster with // `servo.Close()`. defer myServo.Close() myServo.SetSpeed(0.5) // Set the speed to half. This is concurrent-safe. myServo.MoveTo(180) // This is a non-blocking call. /* do some work */ myServo.Wait() // Call Wait() to sync with the servo. // MoveTo() returns a Waiter interface that can be used to move and wait on // the same line. myServo.MoveTo(0).Wait() // This is a blocking call. }
Output: servo "My Servo" connected to gpio(14) [flags: ( NONE )]
Index ¶
- Constants
- func Close()
- func Rate(r time.Duration)
- type Servo
- func (s *Servo) Close()
- func (s *Servo) Connect() error
- func (s *Servo) MoveTo(target float64) (wait Waiter)
- func (s *Servo) Position() float64
- func (s *Servo) SetPosition(position float64)
- func (s *Servo) SetSpeed(percentage float64)
- func (s *Servo) Stop()
- func (s *Servo) String() string
- func (s *Servo) Wait()
- type Waiter
Examples ¶
Constants ¶
const ( // Centered sets the range of the servo from -90 to 90 degrees. // Together with Normalized, the range of the servo is set to -1 to 1. Centered flag = (1 << iota) // Normalized sets the range of the servo from 0 to 2. // Together with Centered, the range of the servo is set to -1 to 1. Normalized )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Servo ¶
type Servo struct { // Name is an optional value to assign a meaningful name to the servo. Name string // Flags is a bit flag that sets various configuration parameters. // // servo.Centered sets the range of the servo from -90 to 90 degrees. // // servo.Normalized sets the range of the servo from 0 to 2. // Together with servo.Centered, the range of the servo is set to -1 to 1. Flags flag // MinPulse is the minimum pwm pulse of the servo. (default 0.05 s) // MaxPulse is the maximum pwm pulse of the servo. (default 0.25 s) // These calibration variables should be immutables once the servo is // connected.. MinPulse, MaxPulse float64 // contains filtered or unexported fields }
Servo is a struct that holds all the information necessary to control a servo motor. Use the function servo.New(gpio) for correct initialization. Servo is designed to be concurrent-safe.
func New ¶
New creates a new Servo struct with default values, connected at a GPIO pin of the Raspberry Pi. You should check that the pin is controllable with pi-blaster.
CAUTION: Incorrect pin assignment might cause damage to your Raspberry Pi.
func (*Servo) Close ¶
func (s *Servo) Close()
Close cleans up the state of the servo and deactivates the corresponding GPIO pin.
func (*Servo) MoveTo ¶
MoveTo sets a target angle for the servo to move. The magnitude of the target depends on the servo's Flags. The target is automatically clamped to the set range. If called concurrently, the target position is overridden by the last goroutine (usually non-deterministic).
func (*Servo) SetPosition ¶
SetPosition immediately sets the angle the servo.
func (*Servo) SetSpeed ¶
SetSpeed changes the speed of the servo from (still) 0.0 to 1.0 (max speed). Setting a speed of 0.0 effectively sets the target position to the current position and the servo will not move.
func (*Servo) Stop ¶
func (s *Servo) Stop()
Stop stops moving the servo. This effectively sets the target position to the stopped position of the servo.
func (*Servo) String ¶
String implements the Stringer interface. It returns a string in the following format:
servo "NAME" connected to gpio(GPIO_PIN) [flags: ( FLAGS_SET )]
where NAME is the verbose name (default: fmt.Sprintf("Servo%d", GPIO)), GPIO_PIN is the connection pin of the servo, and FLAGS_SET is the list of flags set (default: NONE).