Blog

Sep
24
2014

Run Golang On The Intel Edison With Gobot

The Gobot team was in attendance at the recent Intel Developer Forum in San Francisco, and we were able to get one of the new Intel Edison single board computers. Our mission: get Golang running on the Edison, so we can use it with Gobot for robotics or connected devices.

The Edison is a very small System-on-Chip (SoC) single board Linux computer. Its dual-core Atom processor, 1GB of RAM, and built-in WiFi/Bluetooth LE all make it a very powerful machine in a very tiny package. The base Edison itself requires that at least one of the Edison's breakout boards, known as "blocks", be connected via the 70-pin Hirose connector. This is rather different from most other single-board computers. This extreme modularity is a very interesting design approach, and Sparkfun has an entire line of blocks that will be coming out in the coming weeks.

We had previously tried running Golang code on the Intel Galileo when we first got our Gen1 board a few months ago, but were unable to run anything compiled in Golang on it at all, due to the Galileo not supporting the MMX instruction set.

Once we got ahold of the Edison, we immediately retired to a corner of the conference, and set to work putting the new board through its paces. The results were successful! The Edison is able to run Go code.

Of course, for us running Golang was just the beginning. We wanted to use the Edison's many bult-in GPIO & i2c pins, so we can communicate with many devices.

So we rolled up our sleeves, and got to work. In many cases, we made good progress from reading the code helpfully provided by the Intel IoT team's MRAA library. In others, we were simply blazing a trail of code.

With that, we now present to you gobot-intel-iot, our support in Gobot for the Edison board's wonderful capabilities.

Here is a short video using Gobot to control the Edison's GPIO for reading analog input, and then using PWM output to turn on the LED:

Here is the code we used to run the above sample:

import (
  "fmt"

  "github.com/hybridgroup/gobot"
  "github.com/hybridgroup/gobot/platforms/gpio"
  "github.com/hybridgroup/gobot/platforms/intel-iot/edison"
)

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

  edisonAdaptor := edison.NewEdisonAdaptor("edison")
  sensor := gpio.NewAnalogSensorDriver(edisonAdaptor, "sensor", "0")
  led := gpio.NewLedDriver(edisonAdaptor, "led", "3")

  work := func() {
    gobot.On(sensor.Event("data"), func(data interface{}) {
      brightness := uint8(
              gobot.ToScale(gobot.FromScale(float64(data.(int)), 0, 4096), 0, 255),
      )
      fmt.Println("sensor", data)
      fmt.Println("brightness", brightness)
      led.Brightness(brightness)
    })
  }

  robot := gobot.NewRobot("sensorBot",
    []gobot.Connection{edisonAdaptor},
    []gobot.Device{sensor, led},
    work,
  )

  gbot.AddRobot(robot)

  gbot.Start()
}

We also have implemented i2c support. In the following video, we are showing an Edison, running Gobot, reading analog input, and then communicating via i2c with a BlinkM.

Here is the code:

package main

import (
  "fmt"

  "github.com/hybridgroup/gobot"
  "github.com/hybridgroup/gobot/platforms/intel-iot/edison"
  "github.com/hybridgroup/gobot/platforms/gpio"
  "github.com/hybridgroup/gobot/platforms/i2c"
)

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

  edisonAdaptor := edison.NewEdisonAdaptor("edison")
  blinkm := i2c.NewBlinkMDriver(edisonAdaptor, "blinkm")
  sensor := gpio.NewAnalogSensorDriver(edisonAdaptor, "sensor", "0")

  work := func() {
    gobot.On(sensor.Event("data"), func(data interface{}) {
      brightness := uint8(
        gobot.ToScale(gobot.FromScale(float64(data.(int)), 0, 4096), 0, 255),
      )
      fmt.Println("sensor", data)
      fmt.Println("brightness", brightness)
      blinkm.Rgb(0, brightness, 0)
    })
  }

  robot := gobot.NewRobot("sensorBot",
    []gobot.Connection{edisonAdaptor},
    []gobot.Device{blinkm, sensor},
    work,
  )

  gbot.AddRobot(robot)

  gbot.Start()
}

Here is how we got it "go"-ing, if you will. You must first configure your local Go environment for 386 linux cross compiling:

$ cd $GOROOT/src
$ GOOS=linux GOARCH=386 ./make.bash --no-clean

Then compile your Go program, like this:

$ GOARCH=386 GOOS=linux go build edison_blink.go

Then you can simply upload your program over the network from your host computer to the Edison

$ scp edison_blink root@192.168.1.xxx:/home/root/

and execute it on your Edison itself, by SSHing in, and running it

$ ./edison_blink

We are very excited about the possibilities of this new board from Intel, now that we can run Golang and Gobot on it!

Please follow us on Twitter at @gobotio to keep up to date on the latest news and information about Golang powered robotics using Gobot.