cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Connecting and reading Garmin LidarLite Sensor with Gobot Framework

Highlighted
Level 14

Connecting and reading Garmin LidarLite Sensor with Gobot Framework

GOBOT Framework

GOBOT is a framework written in Go programming language. Useful for connecting robotic components, variety of hardware & IoT devices.

 

Framework consists of

  • Robots -> Virtual entity representing rover, drones, sensors etc.
  • Adaptors -> Allows connectivity to the hardware e.g. connection to Arduino is done using Firmata Adaptor, defining how to talk to it
  • Drivers -> Defines specific functionality to support on specific hardware devices e.g. buttons, sensors, etc.
  • API -> Provides RESTful API to query Robot status

There are additional core features of the framework that I recommend having a look esp. Events, Commands allowing Subscribing / Publishing events to the device for more refer to the doc

There's already a long list of Platforms for which the drivers and adaptors are available. For this blog I will be working with Arduino + Garmin LidarLite v3. There are cheaper versions available for distance measurement, however if you are looking for high performance, high precision optical distance measurement sensor, then this is it.

Pre-requisite

How to connect

For our current setup I have Arduino connected to Ubuntu 16 over serial port, see here if you are looking for a different platform.

 

For ubuntu you just need following 3 commands to connect and upload the firmata as our Adaptor to prepare Arduino for connectivity

 

// Look for the connected serial devices
$ gort scan serial
// install avrdude to upload firmata to the Arduino
$ gort arduino install
// uploading the firmata to the serial port found via first scan command, mine was found at /dev/ttyACM0
$ gort arduino upload firmata /dev/ttyACM0

Reading Sensor data

Since there is a available driver for the LidarLite, I will be using it in the following Go code below in a file called main.go which connects and reads the sensor data.

 

For connecting and reading the sensor data we need the driver, connection object & the task / work that the robot is supposed to perform.

Adaptor

firmataAdaptor := firmata.NewAdaptor("/dev/ttyACM0") // this the port on which for me Arduino is connecting

Driver

As previously mentioned that Gobot provides several drivers on of the them is LidarLite we will be using this like so

 

d := i2c.NewLIDARLiteDriver(firmataAdaptor)

Work

Now that we have the adaptor & the driver setup lets assign the work this robot needs to do, which is to read the distance

	work := func() {
		gobot.Every(1*time.Second, func() {
			dist, err := d.Distance()

			if err != nil {
				log.Fatalln("failed to get dist")
			}
			fmt.Println("Fetching the dist", dist, "cms")
		})
	}

Notice the Every function provided by gobot to define that we want to perform certain action as the time lapses, here we are gathering the distance.

 

Note: The distance returned by the lidarLite sensor is in CMs & the max range for the sensor is 40m

Robot

Now we create the robot representing our entity which in this case is simple, its just the sensor itself

 

lidarRobot := gobot.NewRobot("lidarBot", []gobot.Connection{firmataAdaptor}, []gobot.Device{d}, work)
 

This defines the vitual representation of the entity and the driver + the work this robot needs to do.

Here's the complete code. Before running this pacakge make sure to build it as you likely will have to execute the runnable with sudo. To build simply navigate to the folder in the shell where the main.go exists and execute

 

$ go build
 

This will create runnable file with the package name execute the same with sudo if needed like so

 

$ sudo ./GarminLidarLite

 

And if everything done as required following ouput will appear with sensor readings printed out every second

2018/08/05 22:46:54 Initializing connections...
2018/08/05 22:46:54 Initializing connection Firmata-634725A2E59CBD50 ...
2018/08/05 22:46:54 Initializing devices...
2018/08/05 22:46:54 Initializing device LIDARLite-5D4F0034ECE4D0EB ...
2018/08/05 22:46:54 Robot lidarBot initialized.
2018/08/05 22:46:54 Starting Robot lidarBot ...
2018/08/05 22:46:54 Starting connections...
2018/08/05 22:46:54 Starting connection Firmata-634725A2E59CBD50 on port /dev/ttyACM0...
2018/08/05 22:46:58 Starting devices...
2018/08/05 22:46:58 Starting device LIDARLite-5D4F0034ECE4D0EB...
2018/08/05 22:46:58 Starting work...
Fetching the dist 166 cms
Fetching the dist 165 cms
Fetching the dist 165 cms

Here's complete code for reference

 

package main

import (
	"fmt"
	"log"
	"time"

	"gobot.io/x/gobot"
	"gobot.io/x/gobot/drivers/i2c"
	"gobot.io/x/gobot/platforms/firmata"
)

func main() {

	lidarLibTest()
}

// reading Garmin LidarLite data
func lidarLibTest() {

	firmataAdaptor := firmata.NewAdaptor("/dev/ttyACM0")

	d := i2c.NewLIDARLiteDriver(firmataAdaptor)

	work := func() {
		gobot.Every(1*time.Second, func() {
			dist, err := d.Distance()

			if err != nil {
				log.Fatalln("failed to get dist")
			}
			fmt.Println("Fetching the dist", dist, "cms")
		})
	}

	lidarRobot := gobot.NewRobot("lidarBot", []gobot.Connection{firmataAdaptor},
		[]gobot.Device{d}, work)

	lidarRobot.Start()

}