-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
doc.go
149 lines (115 loc) · 3.49 KB
/
doc.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2014-2018 The Hybrid Group. All rights reserved.
/*
Package gobot is the primary entrypoint for Gobot (http://gobot.io), a framework for robotics, physical computing, and
the Internet of Things written using the Go programming language .
It provides a simple, yet powerful way to create solutions that incorporate multiple, different hardware devices at the
same time.
# Classic Gobot
Here is a "Classic Gobot" program that blinks an LED using an Arduino:
package main
import (
"time"
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/gpio"
"gobot.io/x/gobot/v2/platforms/firmata"
)
func main() {
firmataAdaptor := firmata.NewAdaptor("/dev/ttyACM0")
led := gpio.NewLedDriver(firmataAdaptor, "13")
work := func() {
gobot.Every(1*time.Second, func() {
if err := led.Toggle(); err != nil {
fmt.Println(err)
}
})
}
robot := gobot.NewRobot("bot",
[]gobot.Connection{firmataAdaptor},
[]gobot.Device{led},
work,
)
if err := robot.Start(); err != nil {
panic(err)
}
}
# Metal Gobot
You can also use Metal Gobot and pick and choose from the various Gobot packages to control hardware with nothing but
pure idiomatic Golang code. For example:
package main
import (
"gobot.io/x/gobot/v2/drivers/gpio"
"gobot.io/x/gobot/v2/platforms/intel-iot/edison"
"time"
)
func main() {
e := edison.NewAdaptor()
if err := e.Connect(); err != nil {
fmt.Println(err)
}
led := gpio.NewLedDriver(e, "13")
if err := led.Start(); err != nil {
fmt.Println(err)
}
for {
if err := led.Toggle(); err != nil {
fmt.Println(err)
}
time.Sleep(1000 * time.Millisecond)
}
}
# Manager Gobot
Finally, you can use Manager Gobot to add the complete Gobot API or control swarms of Robots:
package main
import (
"fmt"
"time"
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/api"
"gobot.io/x/gobot/v2/drivers/common/spherocommon"
"gobot.io/x/gobot/v2/drivers/serial"
"gobot.io/x/gobot/v2/platforms/serialport"
)
func NewSwarmBot(port string) *gobot.Robot {
spheroAdaptor := serialport.NewAdaptor(port)
spheroDriver := sphero.NewSpheroDriver(spheroAdaptor, serial.WithName("Sphero" + port))
work := func() {
spheroDriver.Stop()
_ = spheroDriver.On(sphero.CollisionEvent, func(data interface{}) {
fmt.Println("Collision Detected!")
})
gobot.Every(1*time.Second, func() {
spheroDriver.Roll(100, uint16(gobot.Rand(360)))
})
gobot.Every(3*time.Second, func() {
spheroDriver.SetRGB(uint8(gobot.Rand(255)),
uint8(gobot.Rand(255)),
uint8(gobot.Rand(255)),
)
})
}
robot := gobot.NewRobot("sphero",
[]gobot.Connection{spheroAdaptor},
[]gobot.Device{spheroDriver},
work,
)
return robot
}
func main() {
manager := gobot.NewManager()
api.NewAPI(manager).Start()
spheros := []string{
"/dev/rfcomm0",
"/dev/rfcomm1",
"/dev/rfcomm2",
"/dev/rfcomm3",
}
for _, port := range spheros {
manager.AddRobot(NewSwarmBot(port))
}
if err := manager.Start(); err != nil {
panic(err)
}
}
Copyright (c) 2013-2018 The Hybrid Group. Licensed under the Apache 2.0 license.
*/
package gobot // import "gobot.io/x/gobot/v2"