diff options
Diffstat (limited to 'servo/servo_highlevel.go')
-rw-r--r-- | servo/servo_highlevel.go | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/servo/servo_highlevel.go b/servo/servo_highlevel.go new file mode 100644 index 0000000..66ec261 --- /dev/null +++ b/servo/servo_highlevel.go @@ -0,0 +1,58 @@ +package servo + +// High-level interface. (Most of this should be removed, or moved to a separate +// type which embeds or interacts with the servo type.) + +func (s *Servo) posToAngle(p int) float64 { + return (positionToAngle * float64(p)) +} + +func (s *Servo) angleToPos(angle float64) int { + return int((angle) * angleToPosition) +} + +/* +// Returns the current position of the servo, relative to the zero angle. +func (s *Servo) Angle() (float64, error) { + p, err := s.Position() + + if err != nil { + return 0, err + } + + return s.posToAngle(p), nil +} + +// MoveTo sets the goal position of the servo by angle (in degrees), where zero +// is the midpoint, 150 deg is max left (clockwise), and -150 deg is max right +// (counter-clockwise). This is generally preferable to calling SetGoalPosition, +// which uses the internal uint16 representation. +func (s *Servo) MoveTo(angle float64) error { + p := s.angleToPos(normalizeAngle(angle)) + return s.SetGoalPosition(p) +} + +// Voltage returns the current voltage supplied. Unlike the underlying register, +// this is the actual voltage, not multiplied by ten. +func (s *Servo) Voltage() (float64, error) { + val, err := s.PresentVoltage() + if err != nil { + return 0.0, err + } + + // Convert the return value into actual volts. + return (float64(val) / 10), nil +} +*/ +// +func normalizeAngle(d float64) float64 { + if d > 180 { + return normalizeAngle(d - 360) + + } else if d < -180 { + return normalizeAngle(d + 360) + + } else { + return d + } +} |