-
Notifications
You must be signed in to change notification settings - Fork 1
/
cctvbot.go
79 lines (68 loc) · 1.56 KB
/
cctvbot.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
package main
import (
"fmt"
"time"
"gobot.io/x/gobot"
"gobot.io/x/gobot/drivers/gpio"
"gobot.io/x/gobot/platforms/raspi"
)
func tiltServoMove(servo *gpio.ServoDriver, angle uint8) {
if angle < 11 {
angle = 11
}
if angle > 24 {
angle = 24
}
servo.Move(angle)
}
func panServoMove(servo *gpio.ServoDriver, angle uint8) {
if angle < 11 {
angle = 11
}
if angle > 42 {
angle = 42
}
servo.Move(angle)
}
func autoPan(panServo, tiltServo *gpio.ServoDriver) {
tiltServo.Move(20)
angle := 10
angleDiff := 1
gobot.Every(200*time.Millisecond, func() {
if angle == 42 {
angleDiff = -1
} else if angle == 10 {
angleDiff = 1
}
angle += angleDiff
fmt.Println("Angle: ", angle)
panServo.Move(uint8(angle))
})
}
func NewCCTVBot() *gobot.Robot {
adapter := raspi.NewAdaptor()
panServo := gpio.NewServoDriver(adapter, "11")
tiltServo := gpio.NewServoDriver(adapter, "12")
panServoMove(panServo, 0)
tiltServoMove(tiltServo, 0)
robot := gobot.NewRobot("CCTVBot",
[]gobot.Connection{adapter},
[]gobot.Device{panServo, tiltServo},
func() {},
// func() { autoPan(panServo, tiltServo) },
)
robot.AddCommand("pan", func(params map[string]interface{}) interface{} {
switch params["direction"] {
case "left":
panServoMove(panServo, panServo.CurrentAngle+1)
case "right":
panServoMove(panServo, panServo.CurrentAngle-1)
case "up":
tiltServoMove(tiltServo, tiltServo.CurrentAngle-1)
case "down":
tiltServoMove(tiltServo, tiltServo.CurrentAngle+1)
}
return fmt.Sprintf("This command pans the cctv %+v", params)
})
return robot
}