gorplidar

package module
v0.0.0-...-e1ab39e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 11, 2024 License: MIT Imports: 7 Imported by: 0

README

Go RPLidar

GoDoc TravisCI

Package gorplidar provides a library to control the Slamtec RPLidar.

Protocol: https://www.robotshop.com/media/files/pdf2/rpk-02-communication-protocol.pdf

This package aims to satisfy the communication protocol specified in the document linked above.

Currently, not all protocols are supported. Configurability could also be improved, as many options are set by default on instantiation of the RPLidar structure.

Usage

package main

import (
    "fmt"
    "log"

    "github.com/gorplidar"
)

func main() {
    lidar := gorplidar.NewRPLidar("/dev/ttyUSB0", 115200)
    lidar.Connect()
    status, errcode, err := lidar.Health()
    if err != nil {
        log.Fatal(err)
    } else if status == "Warning" {
        log.Printf("Lidar status: %v Error Code: %v\n", status, errcode)
    } else if status == "Error" {
        log.Fatalf("Lidar status: %v Error Code: %v\n", status, errcode)
    }
    lidar.StartMotor()
    scanResults, err := lidar.StartScan(3)
    if err != nil {
        log.Fatal(err)
    }
    for _, p := range scanResults {
		fmt.Printf("Quality: %v\tAngle: %.2f\tDistance: %.2f\n", p.Quality, p.Angle, p.Distance)
	}
    lidar.StopMotor()
    lidar.Disconnect()
}

Documentation

https://godoc.org/github.com/Dolphindalt/gorplidar

License

MIT License

Copyright (c) 2018 Dalton Caron

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Files from the go-serial library are licensed under the Apache License, Version 2.

Copyright 2014 Michael Phan-Ba

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Files from the libserialport library are licensed under the GNU Lesset General Public License.

Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
Copyright (C) 2010-2012 Uwe Hermann <uwe@hermann-uwe.de>
Copyright (C) 2013-2014 Martin Ling <martin-libserialport@earth.li>
Copyright (C) 2013 Matthias Heidbrink <m-sigrok@heidbrink.biz>
Copyright (C) 2014 Aurelien Jacobs <aurel@gnuage.org>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

Documentation

Overview

Package gorplidar provides a library to control the Slamtec RPLidar.

Protocol: https://www.robotshop.com/media/files/pdf2/rpk-02-communication-protocol.pdf

This package aims to satisfy the communication protocol specified in the document linked above.

Currently, not all protocols are supported. Configurability could also be improved, as many options are set by default on instantiation of the RPLidar structure.

Example usage:

package main

import (
	"fmt"
	"log"

	"github.com/gorplidar"
)

func main() {
	lidar := gorplidar.NewRPLidar("/dev/ttyUSB0", 115200)
	lidar.Connect()
	status, errcode, err := lidar.Health()
	if err != nil {
		log.Fatal(err)
	} else if status == "Warning" {
		log.Printf("Lidar status: %v Error Code: %v\n", status, errcode)
	} else if status == "Error" {
		log.Fatalf("Lidar status: %v Error Code: %v\n", status, errcode)
	}
	lidar.StartMotor()
	scanResults, err := lidar.StartScan(3)
	if err != nil {
		log.Fatal(err)
	}
	for _, p := range scanResults {
		fmt.Printf("Quality: %v\tAngle: %.2f\tDistance: %.2f\n", p.Quality, p.Angle, p.Distance)
	}
	lidar.StopMotor()
	lidar.Disconnect()
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RPLidar

type RPLidar struct {
	MotorActive bool

	Connected bool
	Scanning  bool
	// contains filtered or unexported fields
}

RPLidar holds information used to communicate through serial to the lidar.

func NewRPLidar

func NewRPLidar(portName string, baudrate int) *RPLidar

NewRPLidar creates an instance of RPLidar. The portName refers to the name of the serial port. Baudrate is the rate at which data is transfered through the serial.

func (*RPLidar) Connect

func (rpl *RPLidar) Connect() error

Connect establishes a serial communication channel with the lidar using the information provided form RPLidar.

func (*RPLidar) DeviceInfo

func (rpl *RPLidar) DeviceInfo() (*RPLidarInfo, error)

DeviceInfo returns a struct containing information about the lidar.

func (*RPLidar) Disconnect

func (rpl *RPLidar) Disconnect() error

Disconnect closes serial communication.

func (*RPLidar) ExpressScan

func (rpl *RPLidar) ExpressScan(scanCycles int) ([]*RPLidarPoint, error)

ExpressScan performs a scan as fast as possible (A2 4khz). The A1 device should just use the StartScan function because of the same sampling rate (2khz).

func (*RPLidar) Health

func (rpl *RPLidar) Health() (string, int, error)

Health returns a string representing the status and an error code representing the health of the lidar.

func (*RPLidar) PWM

func (rpl *RPLidar) PWM(pwm uint16) error

PWM stands for Pulse Width Modulation. pwm can be zero or less than or equal to 1023.

func (*RPLidar) Reset

func (rpl *RPLidar) Reset()

Reset forces the lidar to reset into a state similar to after powering up.

func (*RPLidar) SampleRate

func (rpl *RPLidar) SampleRate() (int, int, error)

SampleRate returns a single measurement duration for standard and express scanning modes. This function seems to only be supported by the A2 model. The time is measured in micro seconds. The first int returned is the standard, the second is the express.

func (*RPLidar) StartMotor

func (rpl *RPLidar) StartMotor() error

StartMotor toggles the motor into an active spinning state.

func (*RPLidar) StartScan

func (rpl *RPLidar) StartScan(scanCycles int) ([]*RPLidarPoint, error)

StartScan begins a lidar scan for the amount of scan cycles desired. The scans parameter refers to how many scan cycles will occur.

func (*RPLidar) StopMotor

func (rpl *RPLidar) StopMotor() error

StopMotor brings the motor to zero velocity.

func (*RPLidar) StopScan

func (rpl *RPLidar) StopScan()

StopScan forces the lidar to exit the current scan.

type RPLidarInfo

type RPLidarInfo struct {
	Model        int
	Firmware     [2]int
	Hardware     int
	SerialNumber string
}

RPLidarInfo is returned by GetDeviceInfo.

type RPLidarPoint

type RPLidarPoint struct {
	Quality  int
	Angle    float32
	Distance float32
	X        float32
	Y        float32
}

RPLidarPoint represents a single point of data from a lidar scan.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL