2018-02-14 15:24:39 +08:00
// Copyright 2014-2018 The Hybrid Group. All rights reserved.
2014-04-27 00:13:33 +08:00
/ *
2016-12-20 01:10:34 +08:00
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 .
2014-07-19 04:18:01 +08:00
2016-12-20 01:10:34 +08:00
It provides a simple , yet powerful way to create solutions that incorporate multiple , different hardware devices at the same time .
2014-07-19 04:18:01 +08:00
2016-12-20 01:10:34 +08:00
Classic Gobot
2014-07-19 04:18:01 +08:00
2016-12-20 01:10:34 +08:00
Here is a "Classic Gobot" program that blinks an LED using an Arduino :
2014-07-19 04:18:01 +08:00
package main
import (
2014-11-14 03:06:57 +08:00
"time"
2014-07-19 04:18:01 +08:00
2016-12-08 20:24:03 +08:00
"gobot.io/x/gobot"
2016-12-20 01:10:34 +08:00
"gobot.io/x/gobot/drivers/gpio"
2016-12-08 20:24:03 +08:00
"gobot.io/x/gobot/platforms/firmata"
2014-07-19 04:18:01 +08:00
)
2014-04-27 00:13:33 +08:00
2014-07-19 04:18:01 +08:00
func main ( ) {
2016-10-19 03:57:14 +08:00
firmataAdaptor := firmata . NewAdaptor ( "/dev/ttyACM0" )
led := gpio . NewLedDriver ( firmataAdaptor , "13" )
2014-04-27 00:13:33 +08:00
2014-11-14 03:06:57 +08:00
work := func ( ) {
gobot . Every ( 1 * time . Second , func ( ) {
led . Toggle ( )
} )
}
2014-04-27 00:13:33 +08:00
2016-12-20 01:10:34 +08:00
robot := gobot . NewRobot ( "bot" ,
2014-11-14 03:06:57 +08:00
[ ] gobot . Connection { firmataAdaptor } ,
[ ] gobot . Device { led } ,
work ,
)
2014-07-19 04:18:01 +08:00
2016-10-19 03:57:14 +08:00
robot . Start ( )
2014-07-19 04:18:01 +08:00
}
2016-12-20 01:10:34 +08:00
Metal Gobot
2014-11-14 03:06:57 +08:00
2016-12-20 01:10:34 +08:00
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/drivers/gpio"
"gobot.io/x/gobot/platforms/intel-iot/edison"
"time"
)
func main ( ) {
e := edison . NewAdaptor ( )
e . Connect ( )
led := gpio . NewLedDriver ( e , "13" )
led . Start ( )
for {
led . Toggle ( )
time . Sleep ( 1000 * time . Millisecond )
}
}
Master Gobot
Finally , you can use Master Gobot to add the complete Gobot API or control swarms of Robots :
2014-07-19 04:18:01 +08:00
package main
import (
2014-11-14 03:06:57 +08:00
"fmt"
2016-12-20 01:10:34 +08:00
"time"
2014-07-19 04:18:01 +08:00
2016-12-08 20:24:03 +08:00
"gobot.io/x/gobot"
"gobot.io/x/gobot/api"
2016-12-20 01:10:34 +08:00
"gobot.io/x/gobot/platforms/sphero"
2014-07-19 04:18:01 +08:00
)
2016-12-20 01:10:34 +08:00
func NewSwarmBot ( port string ) * gobot . Robot {
spheroAdaptor := sphero . NewAdaptor ( port )
spheroDriver := sphero . NewSpheroDriver ( spheroAdaptor )
spheroDriver . SetName ( "Sphero" + port )
work := func ( ) {
spheroDriver . Stop ( )
spheroDriver . On ( sphero . Collision , 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
}
2014-07-19 04:18:01 +08:00
func main ( ) {
2016-10-19 03:57:14 +08:00
master := gobot . NewMaster ( )
api . NewAPI ( master ) . Start ( )
2014-07-19 04:18:01 +08:00
2016-12-20 01:10:34 +08:00
spheros := [ ] string {
"/dev/rfcomm0" ,
"/dev/rfcomm1" ,
"/dev/rfcomm2" ,
"/dev/rfcomm3" ,
}
2014-07-19 04:18:01 +08:00
2016-12-20 01:10:34 +08:00
for _ , port := range spheros {
master . AddRobot ( NewSwarmBot ( port ) )
}
2014-07-19 04:18:01 +08:00
2016-10-19 03:57:14 +08:00
master . Start ( )
2014-07-19 04:18:01 +08:00
}
2014-11-14 03:06:57 +08:00
2018-02-14 15:24:39 +08:00
Copyright ( c ) 2013 - 2018 The Hybrid Group . Licensed under the Apache 2.0 license .
2014-04-27 00:13:33 +08:00
* /
2016-12-08 20:24:03 +08:00
package gobot // import "gobot.io/x/gobot"