Update sphero package and examples
This commit is contained in:
parent
f81aa43336
commit
d877ffd251
|
@ -3,27 +3,26 @@ package main
|
|||
import (
|
||||
"fmt"
|
||||
"github.com/hybridgroup/gobot"
|
||||
"github.com/hybridgroup/gobot/sphero"
|
||||
"github.com/hybridgroup/gobot/platforms/sphero"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
adaptor := sphero.NewAdaptor()
|
||||
adaptor.Name = "Sphero"
|
||||
adaptor.Port = "/dev/rfcomm0"
|
||||
gbot := gobot.NewGobot()
|
||||
|
||||
spheroDriver := sphero.NewSpheroDriver(adaptor)
|
||||
spheroDriver.Name = "sphero"
|
||||
adaptor := sphero.NewSpheroAdaptor("Sphero", "/dev/rfcomm0")
|
||||
spheroDriver := sphero.NewSpheroDriver(adaptor, "sphero")
|
||||
|
||||
work := func() {
|
||||
gobot.On(spheroDriver.Events["Collision"], func(data interface{}) {
|
||||
fmt.Println("Collision Detected!")
|
||||
})
|
||||
|
||||
gobot.Every("3s", func() {
|
||||
gobot.Every(3*time.Second, func() {
|
||||
spheroDriver.Roll(30, uint16(gobot.Rand(360)))
|
||||
})
|
||||
|
||||
gobot.Every("1s", func() {
|
||||
gobot.Every(1*time.Second, func() {
|
||||
r := uint8(gobot.Rand(255))
|
||||
g := uint8(gobot.Rand(255))
|
||||
b := uint8(gobot.Rand(255))
|
||||
|
@ -31,11 +30,8 @@ func main() {
|
|||
})
|
||||
}
|
||||
|
||||
robot := gobot.Robot{
|
||||
Connections: []gobot.Connection{adaptor},
|
||||
Devices: []gobot.Device{spheroDriver},
|
||||
Work: work,
|
||||
}
|
||||
gbot.Robots = append(gbot.Robots,
|
||||
gobot.NewRobot("sphero", []gobot.Connection{adaptor}, []gobot.Device{spheroDriver}, work))
|
||||
|
||||
robot.Start()
|
||||
gbot.Start()
|
||||
}
|
||||
|
|
|
@ -2,10 +2,11 @@ package main
|
|||
|
||||
import (
|
||||
"github.com/hybridgroup/gobot"
|
||||
"github.com/hybridgroup/gobot/sphero"
|
||||
"github.com/hybridgroup/gobot/api"
|
||||
"github.com/hybridgroup/gobot/platforms/sphero"
|
||||
)
|
||||
|
||||
var Master *gobot.Master = gobot.NewMaster()
|
||||
Master := gobot.NewGobot()
|
||||
|
||||
func TurnBlue(params map[string]interface{}) bool {
|
||||
spheroDriver := Master.FindRobotDevice(params["robotname"].(string), "sphero")
|
||||
|
@ -14,32 +15,25 @@ func TurnBlue(params map[string]interface{}) bool {
|
|||
}
|
||||
|
||||
func main() {
|
||||
gobot.Api(Master)
|
||||
api.Api(Master).Start()
|
||||
|
||||
spheros := map[string]string{
|
||||
"Sphero-BPO": "/dev/rfcomm0",
|
||||
}
|
||||
|
||||
for name, port := range spheros {
|
||||
spheroAdaptor := sphero.NewSpheroAdaptor()
|
||||
spheroAdaptor.Name = "sphero"
|
||||
spheroAdaptor.Port = port
|
||||
spheroAdaptor := sphero.NewSpheroAdaptor("sphero", port)
|
||||
|
||||
spheroDriver := sphero.NewSpheroDriver(spheroAdaptor)
|
||||
spheroDriver.Name = "sphero"
|
||||
spheroDriver.Interval = "0.5s"
|
||||
spheroDriver := sphero.NewSpheroDriver(spheroAdaptor, "sphero")
|
||||
|
||||
work := func() {
|
||||
spheroDriver.SetRGB(uint8(255), uint8(0), uint8(0))
|
||||
}
|
||||
|
||||
Master.Robots = append(Master.Robots, &gobot.Robot{
|
||||
Name: name,
|
||||
Connections: []gobot.Connection{spheroAdaptor},
|
||||
Devices: []gobot.Device{spheroDriver},
|
||||
Work: work,
|
||||
Commands: map[string]interface{}{"TurnBlue": TurnBlue},
|
||||
})
|
||||
robot := gobot.NewRobot(name, []gobot.Connection{spheroAdaptor}, []gobot.Device{spheroDriver}, work)
|
||||
robot.Commands = map[string]interface{}{"TurnBlue": TurnBlue}
|
||||
|
||||
Master.Robots = append(Master.Robots, robot)
|
||||
}
|
||||
|
||||
Master.Start()
|
||||
|
|
|
@ -3,7 +3,8 @@ package main
|
|||
import (
|
||||
"fmt"
|
||||
"github.com/hybridgroup/gobot"
|
||||
"github.com/hybridgroup/gobot/sphero"
|
||||
"github.com/hybridgroup/gobot/platforms/sphero"
|
||||
"time"
|
||||
)
|
||||
|
||||
type conway struct {
|
||||
|
@ -14,7 +15,7 @@ type conway struct {
|
|||
}
|
||||
|
||||
func main() {
|
||||
master := gobot.NewMaster()
|
||||
master := gobot.NewGobot()
|
||||
|
||||
spheros := []string{
|
||||
"/dev/rfcomm0",
|
||||
|
@ -23,12 +24,9 @@ func main() {
|
|||
}
|
||||
|
||||
for s := range spheros {
|
||||
spheroAdaptor := sphero.NewSpheroAdaptor()
|
||||
spheroAdaptor.Name = "Sphero"
|
||||
spheroAdaptor.Port = spheros[s]
|
||||
spheroAdaptor := sphero.NewSpheroAdaptor("Sphero", spheros[s])
|
||||
|
||||
cell := sphero.NewSpheroDriver(spheroAdaptor)
|
||||
cell.Name = "Sphero" + spheros[s]
|
||||
cell := sphero.NewSpheroDriver(spheroAdaptor, "Sphero"+spheros[s])
|
||||
|
||||
work := func() {
|
||||
|
||||
|
@ -41,24 +39,21 @@ func main() {
|
|||
conway.contact()
|
||||
})
|
||||
|
||||
gobot.Every("3s", func() {
|
||||
gobot.Every(3*time.Second, func() {
|
||||
if conway.alive == true {
|
||||
conway.movement()
|
||||
}
|
||||
})
|
||||
|
||||
gobot.Every("10s", func() {
|
||||
gobot.Every(10*time.Second, func() {
|
||||
if conway.alive == true {
|
||||
conway.birthday()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
master.Robots = append(master.Robots, &gobot.Robot{
|
||||
Connections: []gobot.Connection{spheroAdaptor},
|
||||
Devices: []gobot.Device{cell},
|
||||
Work: work,
|
||||
})
|
||||
master.Robots = append(master.Robots,
|
||||
gobot.NewRobot("conway", []gobot.Connection{spheroAdaptor}, []gobot.Device{cell}, work))
|
||||
}
|
||||
|
||||
master.Start()
|
||||
|
|
|
@ -2,44 +2,40 @@ package main
|
|||
|
||||
import (
|
||||
"github.com/hybridgroup/gobot"
|
||||
"github.com/hybridgroup/gobot/sphero"
|
||||
"github.com/hybridgroup/gobot/platforms/sphero"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
master := gobot.GobotMaster()
|
||||
master := gobot.NewGobot()
|
||||
|
||||
spheros := map[string]string{
|
||||
"Sphero-BPO": "/dev/rfcomm0",
|
||||
}
|
||||
|
||||
for name, port := range spheros {
|
||||
spheroAdaptor := new(sphero.Adaptor)
|
||||
spheroAdaptor.Name = "sphero"
|
||||
spheroAdaptor.Port = port
|
||||
spheroAdaptor := sphero.NewSpheroAdaptor("sphero", port)
|
||||
|
||||
spheroDriver := sphero.NewSpheroDriver(spheroAdaptor)
|
||||
spheroDriver.Name = "sphero"
|
||||
spheroDriver.Interval = "0.5s"
|
||||
spheroDriver := sphero.NewSpheroDriver(spheroAdaptor, "sphero")
|
||||
|
||||
work := func() {
|
||||
spheroDriver.SetRGB(uint8(255), uint8(0), uint8(0))
|
||||
}
|
||||
|
||||
master.Robots = append(master.Robots, &gobot.Robot{
|
||||
Name: name,
|
||||
Connections: []gobot.Connection{spheroAdaptor},
|
||||
Devices: []gobot.Device{spheroDriver},
|
||||
Work: work,
|
||||
})
|
||||
master.Robots = append(master.Robots,
|
||||
gobot.NewRobot(name, []gobot.Connection{spheroAdaptor}, []gobot.Device{spheroDriver}, work))
|
||||
}
|
||||
|
||||
master.Robots = append(master.Robots, &gobot.Robot{
|
||||
Work: func() {
|
||||
gobot.Every("1s", func() {
|
||||
master.Robots = append(master.Robots, gobot.NewRobot(
|
||||
""
|
||||
nil,
|
||||
nil,
|
||||
func() {
|
||||
gobot.Every(1*time.Second, func() {
|
||||
gobot.Call(master.FindRobot("Sphero-BPO").GetDevice("spheroDriver").Driver, "SetRGB", uint8(gobot.Rand(255)), uint8(gobot.Rand(255)), uint8(gobot.Rand(255)))
|
||||
})
|
||||
},
|
||||
})
|
||||
))
|
||||
|
||||
master.Start()
|
||||
}
|
||||
|
|
|
@ -3,11 +3,12 @@ package main
|
|||
import (
|
||||
"fmt"
|
||||
"github.com/hybridgroup/gobot"
|
||||
"github.com/hybridgroup/gobot/sphero"
|
||||
"github.com/hybridgroup/gobot/platforms/sphero"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
master := gobot.NewMaster()
|
||||
master := gobot.NewGobot()
|
||||
|
||||
spheros := []string{
|
||||
"/dev/rfcomm0",
|
||||
|
@ -17,13 +18,9 @@ func main() {
|
|||
}
|
||||
|
||||
for s := range spheros {
|
||||
spheroAdaptor := sphero.NewSpheroAdaptor()
|
||||
spheroAdaptor.Name = "Sphero"
|
||||
spheroAdaptor.Port = spheros[s]
|
||||
spheroAdaptor := sphero.NewSpheroAdaptor("Sphero", spheros[s])
|
||||
|
||||
spheroDriver := sphero.NewSpheroDriver(spheroAdaptor)
|
||||
spheroDriver.Name = "Sphero" + spheros[s]
|
||||
spheroDriver.Interval = "0.5s"
|
||||
spheroDriver := sphero.NewSpheroDriver(spheroAdaptor, "Sphero"+spheros[s])
|
||||
|
||||
work := func() {
|
||||
spheroDriver.Stop()
|
||||
|
@ -32,19 +29,16 @@ func main() {
|
|||
fmt.Println("Collision Detected!")
|
||||
})
|
||||
|
||||
gobot.Every("1s", func() {
|
||||
gobot.Every(1*time.Second, func() {
|
||||
spheroDriver.Roll(100, uint16(gobot.Rand(360)))
|
||||
})
|
||||
gobot.Every("3s", func() {
|
||||
gobot.Every(3*time.Second, func() {
|
||||
spheroDriver.SetRGB(uint8(gobot.Rand(255)), uint8(gobot.Rand(255)), uint8(gobot.Rand(255)))
|
||||
})
|
||||
}
|
||||
|
||||
master.Robots = append(master.Robots, &gobot.Robot{
|
||||
Connections: []gobot.Connection{spheroAdaptor},
|
||||
Devices: []gobot.Device{spheroDriver},
|
||||
Work: work,
|
||||
})
|
||||
master.Robots = append(master.Robots,
|
||||
gobot.NewRobot("sphero", []gobot.Connection{spheroAdaptor}, []gobot.Device{spheroDriver}, work))
|
||||
}
|
||||
|
||||
master.Start()
|
||||
|
|
|
@ -12,8 +12,12 @@ type SpheroAdaptor struct {
|
|||
connect func(*SpheroAdaptor)
|
||||
}
|
||||
|
||||
func NewSpheroAdaptor() *SpheroAdaptor {
|
||||
func NewSpheroAdaptor(name string, port string) *SpheroAdaptor {
|
||||
return &SpheroAdaptor{
|
||||
Adaptor: gobot.Adaptor{
|
||||
Name: name,
|
||||
Port: port,
|
||||
},
|
||||
connect: func(a *SpheroAdaptor) {
|
||||
c := &serial.Config{Name: a.Port, Baud: 115200}
|
||||
s, err := serial.OpenPort(c)
|
||||
|
|
|
@ -11,7 +11,7 @@ var _ = Describe("SpheroAdaptor", func() {
|
|||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
a = NewSpheroAdaptor()
|
||||
a = NewSpheroAdaptor("bot", "/dev/null")
|
||||
a.sp = sp{}
|
||||
a.connect = func(a *SpheroAdaptor) {}
|
||||
})
|
||||
|
|
|
@ -22,9 +22,10 @@ type SpheroDriver struct {
|
|||
response_channel chan []uint8
|
||||
}
|
||||
|
||||
func NewSpheroDriver(a *SpheroAdaptor) *SpheroDriver {
|
||||
func NewSpheroDriver(a *SpheroAdaptor, name string) *SpheroDriver {
|
||||
return &SpheroDriver{
|
||||
Driver: gobot.Driver{
|
||||
Name: name,
|
||||
Events: make(map[string]chan interface{}),
|
||||
Commands: []string{
|
||||
"SetRGBC",
|
||||
|
|
|
@ -12,9 +12,9 @@ var _ = Describe("SpheroDriver", func() {
|
|||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
a = NewSpheroAdaptor()
|
||||
a = NewSpheroAdaptor("bot", "/dev/null")
|
||||
a.sp = sp{}
|
||||
s = NewSpheroDriver(a)
|
||||
s = NewSpheroDriver(a, "bot")
|
||||
})
|
||||
|
||||
It("Must be able to Start", func() {
|
||||
|
|
Loading…
Reference in New Issue