-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservo.go
102 lines (87 loc) · 3.1 KB
/
servo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
Copyright (c) 2020
Author: Pavlo Lytvynoff
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package pca9685
import (
"fmt"
)
const (
// The specified pulse width range of a servo has historically been 1000-2000us,
// for a 90 degree range of motion. But nearly all modern servos have a 170-180
// degree range, and the pulse widths can go well out of the range to achieve this
// extended motion. The default values here of `750` and `2250` typically give
// 135 degrees of motion. You can set `Range` to correspond to the
// actual range of motion you observe with your given `MinPulse` and `MaxPulse` values.
ServoRangeDef int = 135
ServoMinPulseDef float32 = 750.0
ServoMaxPulseDef float32 = 2250.0
)
// Servo structure
type Servo struct {
pca *PCA9685
channel int
options *ServOptions
}
// ServOptions for servo
type ServOptions struct {
AcRange int // actuation range
MinPulse float32
MaxPulse float32
}
// ServoNew creates a new servo driver
func (pca *PCA9685) ServoNew(chn int, o *ServOptions) *Servo {
s := &Servo{
pca: pca,
channel: chn,
options: &ServOptions{
AcRange: ServoRangeDef,
MinPulse: ServoMinPulseDef,
MaxPulse: ServoMaxPulseDef,
},
}
if o != nil {
s.options = o
}
return s
}
// Angle in degrees. Must be in the range `0` to `Range`.
func (s *Servo) Angle(a int) (err error) {
if a < 0 || a > s.options.AcRange {
return fmt.Errorf("Angle out of range")
}
return s.Fraction(float32(a) / float32(s.options.AcRange))
}
// Fraction as pulse width expressed between 0.0 `MinPulse` and 1.0 `MaxPulse`.
// For conventional servos, corresponds to the servo position as a fraction
// of the actuation range.
func (s *Servo) Fraction(f float32) (err error) {
if f < 0.0 || f > 1.0 {
return fmt.Errorf("Must be 0.0 to 1.0")
}
freq := s.pca.GetFreq()
minDuty := s.options.MinPulse * freq / 1000000 * 0xFFFF
maxDuty := s.options.MaxPulse * freq / 1000000 * 0xFFFF
dutyRange := maxDuty - minDuty
dutyCycle := (int(minDuty+f*dutyRange) + 1) >> 4
return s.pca.SetChannel(int(s.channel), 0, dutyCycle)
}
// Reset channel
func (s *Servo) Reset() (err error) {
return s.pca.SetChannel(s.channel, 0, 0)
}