LoginSignup
15
15

More than 5 years have passed since last update.

SpheroをGo言語から制御して遊んでみた

Posted at

最近Golangを勉強しています。

前に、こちらの記事(Node.jsで操作できるロボティックボールSpheroでHello World)でCyloy.jsを使ってみました。

Cylon.jsはIoT向けのNode.jsフレームワーク(サイトにはフレームワークって書いてありますがライブラリでいいと思う)です。その姉妹的なプロジェクトでGo言語向けのGobotがあります。

最近Go勉強しているのでGobotも使ってみました。

Gobot

GobotはCylon.jsの姉妹プロジェクト

サイトはこちら。http://gobot.io/

$ go get github.com/hybridgroup/gobot

とりあえずこれで使えるようになります。

Spheroも操作できます。

サンプルを試してみる

まずはこちらの記事(Node.jsで操作できるロボティックボールSpheroでHello World)を参考にポート情報を確認しましょう。

私のspheroポートは/dev/tty.Sphero-RRY-AMP-SPPでした。

$ go get github.com/hybridgroup/gobot/platforms/sphero

サンプルコードを元に実行してみましょう。
サイト上だとfmtパッケージも読み込んでますが、不要なので記述から削除してあります。

sphero.go
package main

import (
    "time"

    "github.com/hybridgroup/gobot"
    "github.com/hybridgroup/gobot/platforms/sphero"
)

func main() {
    gbot := gobot.NewGobot()

    adaptor := sphero.NewSpheroAdaptor("sphero", "/dev/tty.Sphero-RRY-AMP-SPP") // ポートを自分の環境にあわせる
    driver := sphero.NewSpheroDriver(adaptor, "sphero")

    work := func() {
        gobot.Every(3*time.Second, func() {
            driver.Roll(30, uint16(gobot.Rand(360)))
        })
    }

    robot := gobot.NewRobot("sphero",
        []gobot.Connection{adaptor},
        []gobot.Device{driver},
        work,
    )

    gbot.AddRobot(robot)

    gbot.Start()
}

実行します。

$ go run sphero.go
2015/06/23 04:18:44 Initializing Robot sphero ...
2015/06/23 04:18:44 Initializing connections...
2015/06/23 04:18:44 Initializing connection sphero ...
2015/06/23 04:18:44 Initializing devices...
2015/06/23 04:18:44 Initializing device sphero ...
2015/06/23 04:18:44 Starting Robot sphero ...
2015/06/23 04:18:44 Starting connections...
2015/06/23 04:18:44 Starting connection sphero on port /dev/tty.Sphero-RRY-AMP-SPP...
2015/06/23 04:18:47 Starting devices...
2015/06/23 04:18:47 Starting device sphero...
2015/06/23 04:18:47 Starting work...

こんな感じで表示されて、spheroがコロコロ動き出すと思います。

LEDの色変えたりも

sphero.go

sphero.go
/*省略*/
  work := func() {
    gobot.Every(3*time.Second, func() {
      r := uint8(gobot.Rand(255))
      g := uint8(gobot.Rand(255))
      b := uint8(gobot.Rand(255))
      driver.SetRGB(r,g,b)
      fmt.Println(r,g,b)
    })
  }
/*省略*/

まとめ

もっと色々やりたかったけど、眠くなったからやめておきます。

Cylon.jsよりもGobotの方がspheroに関しては実装されてるメソッドが多いかもなぁという印象です。

また色々と触ってみよう。

15
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
15