Browse Source

various fixes

master
Tim Schuster 2 years ago
parent
commit
8c460f5b8c
Signed by: Tim Schuster <mail@timschuster.info> GPG Key ID: F9E27097EFB77F61
2 changed files with 11 additions and 3 deletions
  1. 5
    2
      example/main.go
  2. 6
    1
      servo.go

+ 5
- 2
example/main.go View File

@@ -30,7 +30,7 @@ func main() {
30 30
 	}
31 31
 	fmt.Printf("Current Angle: %d °\n", servo.CurrentAngle())
32 32
 	var angle int16
33
-	for angle, err = parseKeyboardInput(stepsize, servo); err != nil; {
33
+	for angle, err = parseKeyboardInput(stepsize, servo); err == nil; angle, err = parseKeyboardInput(stepsize, servo) {
34 34
 		err = servo.TurnTo(angle)
35 35
 		if err != nil {
36 36
 			log.Fatal(err)
@@ -38,6 +38,9 @@ func main() {
38 38
 		}
39 39
 		fmt.Printf("Current Angle: %d °\n", servo.CurrentAngle())
40 40
 	}
41
+	if err != nil {
42
+		fmt.Printf("Error: %s", err)
43
+	}
41 44
 }
42 45
 
43 46
 func parseKeyboardInput(step int16, s *adafruitPCA9685.Servo) (int16, error) {
@@ -64,7 +67,7 @@ func parseKeyboardInput(step int16, s *adafruitPCA9685.Servo) (int16, error) {
64 67
 	}
65 68
 	if strings.HasPrefix(command, "goto ") {
66 69
 		command = strings.TrimPrefix(command, "goto ")
67
-		angleI, err := strconv.Atoi(command)
70
+		angleI, err := strconv.Atoi(strings.TrimSpace(command))
68 71
 		if err != nil {
69 72
 			fmt.Printf("Error reading command: %s\n", err)
70 73
 			return s.CurrentAngle(), nil

+ 6
- 1
servo.go View File

@@ -49,7 +49,12 @@ func (s *Servo) TurnTo(angle int16) error {
49 49
 	if angle > s.maxAngle {
50 50
 		return errHiAngle
51 51
 	}
52
-	return s.controller.SetPWM(s.channel, 0, angle)
52
+	err := s.controller.SetPWM(s.channel, 0, angle)
53
+	if err != nil {
54
+		return err
55
+	}
56
+	s.currentAngle = angle
57
+	return nil
53 58
 }
54 59
 
55 60
 func (s *Servo) DisableSignal() error {

Loading…
Cancel
Save