godaq

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

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

Go to latest
Published: Oct 15, 2020 License: Apache-2.0 Imports: 13 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Golang bindings for OpenDAQ

OpenDAQ is an open source data acquisition device.

The hardware uses a VCP (Virtual COM Port) USB interface. The communication protocol is described here.

Note: This project is in an early stage. Only a small subset of the commands is supported.

Installation

Install godaq and its dependencies to your src directory:

go get -v github.com/opendaq/godaq

Updating:

go get -u -v github.com/opendaq/godaq

Usage example

package main

import (
	"fmt"
	"log"
	"time"

	"github.com/opendaq/godaq"
)

func checkErr(err error) {
	if err != nil {
		log.Fatal(err)
	}
}

func main() {
	daq, err := godaq.New("/dev/ttyUSB0")
	checkErr(err)
	defer daq.Close()

	model, version, serial, err := daq.GetInfo()
	checkErr(err)
	fmt.Println("model:", model, "version:", version, "serial:", serial)

	checkErr(daq.SetLED(1, godaq.RED))

	// Set the output voltage to 2 V
	checkErr(daq.SetAnalog(1, 2.0))

	// Configure the ADC: read from input 1 with gainID=1,
	// average 10 samples each time
	checkErr(daq.ConfigureADC(1, 0, 1, 10))

	// Read 20 samples
	for i := 0; i < 20; i++ {
		val, err := daq.ReadAnalog()
		checkErr(err)
		fmt.Println(val)
		time.Sleep(100*time.Millisecond)
	}
}

Documentation

Index

Constants

View Source
const (
	AIN         = 1
	AIN_CFG     = 2
	PIO         = 3
	AIN_ALL     = 4
	PIO_DIR     = 5
	PORT        = 7
	PORT_DIR    = 9
	SET_DAC     = 13
	LED_W       = 18
	SET_ANALOG  = 24
	GET_CALIB   = 36
	ID_CONFIG   = 39
	GET_AIN_CFG = 40
)
View Source
const ModelMId = 1
View Source
const ModelNId = 3
View Source
const ModelSId = 2

Variables

View Source
var (
	ErrUnknownModel    = errors.New("Unknown device model number")
	ErrInvalidLed      = errors.New("Invalid LED number")
	ErrInvalidInput    = errors.New("Invalid input number")
	ErrInvalidOutput   = errors.New("Invalid output number")
	ErrInvalidPIO      = errors.New("Invalid PIO number")
	ErrInvalidGainID   = errors.New("Invalid gain ID")
	ErrInvalidID       = errors.New("ID out of range")
	ErrInvalidPIOValue = errors.New("Invalid PIO value")
)
View Source
var (
	ErrChecksum      = errors.New("Checksum error")
	ErrInvalidLength = errors.New("Invalid message length")
	ErrNakReceived   = errors.New("NAK response received")
)

Functions

func ListPorts

func ListPorts() ([]string, error)

List all available USB-serial ports (Linux only)

Types

type ADC

type ADC struct {
	Bits       uint
	Signed     bool
	Invert     bool
	VMin, VMax float32
	Gains      []float32
}

Analog-to-digital converter

func (*ADC) ToVolts

func (adc *ADC) ToVolts(raw int, gainId uint, cal1, cal2 Calib) float32

Convert an ADC value to volts cal1: pre-PGA calibration values cal2: post-PGA calibration values

type Calib

type Calib struct {
	Gain   float32 // Gain calibration (-1 to 1)
	Offset float32 // Offset calibraton in ADUs
}

type Color

type Color uint8
const (
	OFF Color = iota
	GREEN
	RED
	YELLOW
)

type CommandNumber

type CommandNumber uint8

type DAC

type DAC struct {
	Bits       uint
	Signed     bool
	Invert     bool
	VMin, VMax float32
}

Digital-to-analog converter

func (*DAC) FromVolts

func (dac *DAC) FromVolts(v float32, cal Calib) int

Convert a voltage to a DAC value

type DevicePort

type DevicePort struct {
	Model uint8
	Port  string
}

func ListDevicePorts

func ListDevicePorts() ([]DevicePort, error)

type HwFeatures

type HwFeatures struct {
	Name                              string
	NPIOs, NLeds                      uint
	NInputs, NOutputs, NHiddenOutputs uint
	NCalibRegs                        uint
	Dac                               DAC
	Adc                               ADC
}

type HwModel

type HwModel interface {
	GetFeatures() HwFeatures
	GetCalibIndex(isOutput, diffMode, secondStage bool, n, gainId uint) (uint, error)
	CheckValidInputs(pos, neg uint) error
}

type Message

type Message struct {
	Number CommandNumber
	Body   []byte
}

func (*Message) Marshal

func (m *Message) Marshal() ([]byte, error)

type ModelM

type ModelM struct {
	HwFeatures
}

func NewModelM

func NewModelM() *ModelM

func (*ModelM) CheckValidInputs

func (m *ModelM) CheckValidInputs(pos, neg uint) error

func (*ModelM) GetCalibIndex

func (m *ModelM) GetCalibIndex(isOutput, diffMode, secondStage bool, n, gainId uint) (uint, error)

Get the index of a calibration register. Each register contains a pair of calibration values: a gain and and offset.

isOutput: Obtain the calibration values of an output diffMode: Some models have different calibration values depending on the input mode (single-ended or differential) secondStage: The inputs with a PGA need two calibration registers. One is applied before the PGA and the other is applied after the PGA (second stage)

func (*ModelM) GetFeatures

func (m *ModelM) GetFeatures() HwFeatures

type ModelN

type ModelN struct {
	HwFeatures
}

func NewModelN

func NewModelN() *ModelN

func (*ModelN) CheckValidInputs

func (m *ModelN) CheckValidInputs(pos, neg uint) error

func (*ModelN) GetCalibIndex

func (m *ModelN) GetCalibIndex(isOutput, diffMode, secondStage bool, n, gainId uint) (uint, error)

func (*ModelN) GetFeatures

func (m *ModelN) GetFeatures() HwFeatures

type ModelS

type ModelS struct {
	HwFeatures
}

func NewModelS

func NewModelS() *ModelS

func (*ModelS) CheckValidInputs

func (m *ModelS) CheckValidInputs(pos, neg uint) error

func (*ModelS) GetCalibIndex

func (m *ModelS) GetCalibIndex(isOutput, diffMode, secondStage bool, n, gainId uint) (uint, error)

func (*ModelS) GetFeatures

func (m *ModelS) GetFeatures() HwFeatures

type OpenDAQ

type OpenDAQ struct {
	HwFeatures

	sync.Mutex
	// contains filtered or unexported fields
}

func New

func New(port string) (*OpenDAQ, error)

func (*OpenDAQ) Close

func (daq *OpenDAQ) Close() error

func (*OpenDAQ) ConfigureADC

func (daq *OpenDAQ) ConfigureADC(posInput, negInput, gainId uint, nSamples uint8) error

func (*OpenDAQ) GetCalib

func (daq *OpenDAQ) GetCalib(isOutput, diffMode, secondStage bool, n, gainId uint) Calib

Return the calibration values for a given input or output. The gain ID and the input mode (single-ended or differential) are needed. Different device models use different calibration schemas.

func (*OpenDAQ) GetInfo

func (daq *OpenDAQ) GetInfo() (model, version uint8, serial string, err error)

func (*OpenDAQ) ReadADC

func (daq *OpenDAQ) ReadADC() (int16, error)

Read a raw value from the ADC

func (*OpenDAQ) ReadAnalog

func (daq *OpenDAQ) ReadAnalog() (float32, error)

Read a value in volts from the ADC

func (*OpenDAQ) ReadPIO

func (daq *OpenDAQ) ReadPIO(n uint) (uint8, error)

func (*OpenDAQ) ReadPort

func (daq *OpenDAQ) ReadPort() (uint8, error)

ead all PIO values.

func (*OpenDAQ) SetAnalog

func (daq *OpenDAQ) SetAnalog(n uint, val float32) error

Set the voltage at output n

func (*OpenDAQ) SetDAC

func (daq *OpenDAQ) SetDAC(n uint, val int) error

Set the raw value of the DAC at output n

func (*OpenDAQ) SetId

func (daq *OpenDAQ) SetId(id uint32) (uint16, error)

func (*OpenDAQ) SetLED

func (daq *OpenDAQ) SetLED(n uint, c Color) error

func (*OpenDAQ) SetPIO

func (daq *OpenDAQ) SetPIO(n uint, value bool) error

func (*OpenDAQ) SetPIODir

func (daq *OpenDAQ) SetPIODir(n uint, out bool) error

func (*OpenDAQ) SetPort

func (daq *OpenDAQ) SetPort(value_port uint8) error

Write all PIO values.

func (*OpenDAQ) SetPortDir

func (daq *OpenDAQ) SetPortDir(dir_port uint8) error

Configure all PIO direction.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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