2014-07-19 04:18:01 +08:00
|
|
|
// Copyright 2014 The Gobot Authors, HybridGroup. All rights reserved.
|
2014-04-27 00:13:33 +08:00
|
|
|
|
|
|
|
/*
|
2014-10-29 05:52:59 +08:00
|
|
|
Package gobot provides a framework for robotics, physical computing and the internet of things.
|
2014-11-14 03:06:57 +08:00
|
|
|
It is the main point of entry for your Gobot application. A Gobot program
|
2014-07-19 04:18:01 +08:00
|
|
|
is typically composed of one or more robots that makes up a project.
|
|
|
|
|
|
|
|
Basic Setup
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hybridgroup/gobot"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
robot := gobot.NewRobot("Eve", func() {
|
|
|
|
gobot.Every(500*time.Millisecond, func() {
|
|
|
|
fmt.Println("Greeting Human")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-10-19 03:57:14 +08:00
|
|
|
robot.Start()
|
2014-07-19 04:18:01 +08:00
|
|
|
}
|
|
|
|
|
2014-11-14 03:06:57 +08:00
|
|
|
Blinking an LED (Hello Eve!)
|
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
|
|
|
|
|
|
|
"github.com/hybridgroup/gobot"
|
2016-10-19 03:57:14 +08:00
|
|
|
"github.com/hybridgroup/gobot/drivers/gpio"
|
2014-11-14 03:06:57 +08:00
|
|
|
"github.com/hybridgroup/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
|
|
|
|
2014-11-14 03:06:57 +08:00
|
|
|
robot := gobot.NewRobot("Eve",
|
|
|
|
[]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
|
|
|
}
|
|
|
|
|
2014-11-14 03:06:57 +08:00
|
|
|
Web Enabled? You bet! Gobot can be configured to expose a restful HTTP interface
|
|
|
|
using the api package. You can define custom commands on your robots, in addition
|
|
|
|
to the built-in device driver commands, and interact with your application as a
|
|
|
|
web service.
|
|
|
|
|
2014-07-19 04:18:01 +08:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-11-14 03:06:57 +08:00
|
|
|
"fmt"
|
2014-07-19 04:18:01 +08:00
|
|
|
|
|
|
|
"github.com/hybridgroup/gobot"
|
2014-11-14 03:06:57 +08:00
|
|
|
"github.com/hybridgroup/gobot/api"
|
2014-07-19 04:18:01 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2016-10-19 03:57:14 +08:00
|
|
|
master := gobot.NewMaster()
|
2014-07-19 04:18:01 +08:00
|
|
|
|
2014-11-14 03:06:57 +08:00
|
|
|
// Starts the API server on default port 3000
|
2016-10-19 03:57:14 +08:00
|
|
|
api.NewAPI(master).Start()
|
2014-07-19 04:18:01 +08:00
|
|
|
|
2014-11-14 03:06:57 +08:00
|
|
|
// Accessible via http://localhost:3000/api/commands/say_hello
|
2016-10-19 03:57:14 +08:00
|
|
|
master.AddCommand("say_hello", func(params map[string]interface{}) interface{} {
|
2014-11-14 03:06:57 +08:00
|
|
|
return "Master says hello!"
|
|
|
|
})
|
2014-07-19 04:18:01 +08:00
|
|
|
|
2016-10-19 03:57:14 +08:00
|
|
|
hello := master.AddRobot(gobot.NewRobot("Eve"))
|
2014-07-19 04:18:01 +08:00
|
|
|
|
2014-11-14 03:06:57 +08:00
|
|
|
// Accessible via http://localhost:3000/robots/Eve/commands/say_hello
|
|
|
|
hello.AddCommand("say_hello", func(params map[string]interface{}) interface{} {
|
|
|
|
return fmt.Sprintf("%v says hello!", hello.Name)
|
|
|
|
})
|
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
|
|
|
|
2014-04-27 00:13:33 +08:00
|
|
|
*/
|
2014-07-19 04:18:01 +08:00
|
|
|
package gobot
|