periph: periph.io/x/periph/conn/physic Index | Examples | Files

package physic

import "periph.io/x/periph/conn/physic"

Package physic declares types for physical input, outputs and measurement units.

This includes temperature, humidity, pressure, tension, current, etc.

SI units

The supported S.I. units is a subset of the official ones.

T  	tera 	10¹²  	1000000000000
G  	giga 	10⁹   	1000000000
M  	mega 	10⁶   	1000000
k  	kilo 	10³   	1000
m  	milli	10⁻³  	0.001
µ,u	micro	10⁻⁶  	0.000001
n  	nano 	10⁻⁹  	0.000000001
p  	pico 	10⁻¹² 	0.000000000001

Index

Examples

Package Files

doc.go physic.go units.go

type Angle Uses

type Angle int64

Angle is the measurement of the difference in orientation between two vectors stored as an int64 nano radian.

A negative angle is valid.

The highest representable value is a bit over 9.223GRad or 500,000,000,000°.

const (
    NanoRadian  Angle = 1
    MicroRadian Angle = 1000 * NanoRadian
    MilliRadian Angle = 1000 * MicroRadian
    Radian      Angle = 1000 * MilliRadian

    // Theta is 2π. This is equivalent to 360°.
    Theta  Angle = 6283185307 * NanoRadian
    Pi     Angle = 3141592653 * NanoRadian
    Degree Angle = 17453293 * NanoRadian
)

Well known Angle constants.

Code:

fmt.Println(physic.Degree)
fmt.Println(physic.Pi)
fmt.Println(physic.Theta)

Output:

1.000°
180.0°
360.0°

Code:

var a physic.Angle

flag.Var(&a, "angle", "angle to set the servo to")
flag.Parse()

Code:

// A 45° angle. The +2 here is to help integer based rounding.
v := (physic.Pi + 2) / 4

// Convert to float64 as degree.
fd := float64(v) / float64(physic.Degree)

// Convert to float64 as radian.
fr := float64(v) / float64(physic.Radian)

fmt.Println(v)
fmt.Printf("%.1fdeg\n", fd)
fmt.Printf("%frad\n", fr)

Output:

45.00°
45.0deg
0.785398rad

func (*Angle) Set Uses

func (a *Angle) Set(s string) error

Set sets the Angle to the value represented by s. Units are to be provided in "rad", "deg" or "°" with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".

Code:

var a physic.Angle

if err := a.Set("2°"); err != nil {
    log.Fatal(a)
}
fmt.Println(a)

if err := a.Set("90deg"); err != nil {
    log.Fatal(a)
}
fmt.Println(a)

if err := a.Set("1rad"); err != nil {
    log.Fatal(a)
}
fmt.Println(a)

Output:

2.000°
90.00°
57.296°

func (Angle) String Uses

func (a Angle) String() string

String returns the angle formatted as a string in degree.

type Distance Uses

type Distance int64

Distance is a measurement of length stored as an int64 nano metre.

This is one of the base unit in the International System of Units.

The highest representable value is 9.2Gm.

const (
    NanoMetre  Distance = 1
    MicroMetre Distance = 1000 * NanoMetre
    MilliMetre Distance = 1000 * MicroMetre
    Metre      Distance = 1000 * MilliMetre
    KiloMetre  Distance = 1000 * Metre
    MegaMetre  Distance = 1000 * KiloMetre
    GigaMetre  Distance = 1000 * MegaMetre

    // Conversion between Metre and imperial units.
    Thou Distance = 25400 * NanoMetre
    Inch Distance = 1000 * Thou
    Foot Distance = 12 * Inch
    Yard Distance = 3 * Foot
    Mile Distance = 1760 * Yard
)

Well known Distance constants.

Code:

fmt.Println(physic.Inch)
fmt.Println(physic.Foot)
fmt.Println(physic.Mile)

Output:

25.400mm
304.800mm
1.609km

Code:

var d physic.Distance

flag.Var(&d, "distance", "x axis travel length")
flag.Parse()

Code:

// Distance between the Earth and the Moon.
v := 384400 * physic.KiloMetre

// Convert to float64 as meter.
fm := float64(v) / float64(physic.Metre)

// Convert to float64 as inches.
fi := float64(v) / float64(physic.Inch)

fmt.Println(v)
fmt.Printf("%.0fm\n", fm)
fmt.Printf("%.0fin\n", fi)

Output:

384.400Mm
384400000m
15133858268in

func (*Distance) Set Uses

func (d *Distance) Set(s string) error

Set sets the Distance to the value represented by s. Units are to be provided in "m", "Mile", "Yard", "in", or "ft" with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".

Code:

var d physic.Distance

if err := d.Set("1ft"); err != nil {
    log.Fatal(err)
}
fmt.Println(d)

if err := d.Set("1m"); err != nil {
    log.Fatal(err)
}
fmt.Println(d)

if err := d.Set("9Mile"); err != nil {
    log.Fatal(err)
}
fmt.Println(d)

Output:

304.800mm
1m
14.484km

func (Distance) String Uses

func (d Distance) String() string

String returns the distance formatted as a string in metre.

type ElectricCurrent Uses

type ElectricCurrent int64

ElectricCurrent is a measurement of a flow of electric charge stored as an int64 nano Ampere.

This is one of the base unit in the International System of Units.

The highest representable value is 9.2GA.

const (
    NanoAmpere  ElectricCurrent = 1
    MicroAmpere ElectricCurrent = 1000 * NanoAmpere
    MilliAmpere ElectricCurrent = 1000 * MicroAmpere
    Ampere      ElectricCurrent = 1000 * MilliAmpere
    KiloAmpere  ElectricCurrent = 1000 * Ampere
    MegaAmpere  ElectricCurrent = 1000 * KiloAmpere
    GigaAmpere  ElectricCurrent = 1000 * MegaAmpere
)

Well known ElectricCurrent constants.

Code:

fmt.Println(10010 * physic.MilliAmpere)
fmt.Println(10 * physic.Ampere)
fmt.Println(-10 * physic.MilliAmpere)

Output:

10.010A
10A
-10mA

Code:

var m physic.ElectricCurrent

flag.Var(&m, "motor", "rated motor current")
flag.Parse()

Code:

// The maximum current that can be drawn from all Raspberry Pi GPIOs combined.
v := 51 * physic.MilliAmpere

// Convert to float64 as ampere.
f := float64(v) / float64(physic.Ampere)

fmt.Println(v)
fmt.Printf("%.3fA\n", f)

Output:

51mA
0.051A

func (*ElectricCurrent) Set Uses

func (c *ElectricCurrent) Set(s string) error

Set sets the ElectricCurrent to the value represented by s. Units are to be provided in "A" with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".

Code:

var e physic.ElectricCurrent

if err := e.Set("12.5mA"); err != nil {
    log.Fatal(err)
}
fmt.Println(e)

if err := e.Set("2.4kA"); err != nil {
    log.Fatal(err)
}
fmt.Println(e)

if err := e.Set("2A"); err != nil {
    log.Fatal(err)
}
fmt.Println(e)

Output:

12.500mA
2.400kA
2A

func (ElectricCurrent) String Uses

func (c ElectricCurrent) String() string

String returns the current formatted as a string in Ampere.

type ElectricPotential Uses

type ElectricPotential int64

ElectricPotential is a measurement of electric potential stored as an int64 nano Volt.

The highest representable value is 9.2GV.

const (
    // Volt is W/A, kg⋅m²/s³/A.
    NanoVolt  ElectricPotential = 1
    MicroVolt ElectricPotential = 1000 * NanoVolt
    MilliVolt ElectricPotential = 1000 * MicroVolt
    Volt      ElectricPotential = 1000 * MilliVolt
    KiloVolt  ElectricPotential = 1000 * Volt
    MegaVolt  ElectricPotential = 1000 * KiloVolt
    GigaVolt  ElectricPotential = 1000 * MegaVolt
)

Well known ElectricPotential constants.

Code:

fmt.Println(10010 * physic.MilliVolt)
fmt.Println(10 * physic.Volt)
fmt.Println(-10 * physic.MilliVolt)

Output:

10.010V
10V
-10mV

Code:

var v physic.ElectricPotential
flag.Var(&v, "cutout", "battery full charge voltage")
flag.Parse()

Code:

// The level of Raspberry Pi GPIO when high.
v := 3300 * physic.MilliVolt

// Convert to float64 as volt.
f := float64(v) / float64(physic.Volt)

fmt.Println(v)
fmt.Printf("%.1fV\n", f)

Output:

3.300V
3.3V

func (*ElectricPotential) Set Uses

func (p *ElectricPotential) Set(s string) error

Set sets the ElectricPotential to the value represented by s. Units are to be provided in "V" with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".

Code:

var v physic.ElectricPotential
if err := v.Set("250uV"); err != nil {
    log.Fatal(err)
}
fmt.Println(v)

if err := v.Set("100kV"); err != nil {
    log.Fatal(err)
}
fmt.Println(v)

if err := v.Set("12V"); err != nil {
    log.Fatal(err)
}
fmt.Println(v)

Output:

250µV
100kV
12V

func (ElectricPotential) String Uses

func (p ElectricPotential) String() string

String returns the tension formatted as a string in Volt.

type ElectricResistance Uses

type ElectricResistance int64

ElectricResistance is a measurement of the difficulty to pass an electric current through a conductor stored as an int64 nano Ohm.

The highest representable value is 9.2GΩ.

const (
    // Ohm is V/A, kg⋅m²/s³/A².
    NanoOhm  ElectricResistance = 1
    MicroOhm ElectricResistance = 1000 * NanoOhm
    MilliOhm ElectricResistance = 1000 * MicroOhm
    Ohm      ElectricResistance = 1000 * MilliOhm
    KiloOhm  ElectricResistance = 1000 * Ohm
    MegaOhm  ElectricResistance = 1000 * KiloOhm
    GigaOhm  ElectricResistance = 1000 * MegaOhm
)

Well known ElectricResistance constants.

Code:

fmt.Println(10010 * physic.MilliOhm)
fmt.Println(10 * physic.Ohm)
fmt.Println(24 * physic.MegaOhm)

Output:

10.010Ω
10Ω
24MΩ

Code:

var r physic.ElectricResistance

flag.Var(&r, "shunt", "shunt resistor value")
flag.Parse()

Code:

// A common resistor value.
v := 4700 * physic.Ohm

// Convert to float64 as ohm.
f := float64(v) / float64(physic.Ohm)

fmt.Println(v)
fmt.Printf("%.0fOhm\n", f)

Output:

4.700kΩ
4700Ohm

func (*ElectricResistance) Set Uses

func (r *ElectricResistance) Set(s string) error

Set sets the ElectricResistance to the value represented by s. Units are to be provided in "Ohm", or "Ω" with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".

Code:

var r physic.ElectricResistance

if err := r.Set("33.3kOhm"); err != nil {
    log.Fatal(err)
}
fmt.Println(r)

if err := r.Set("1Ohm"); err != nil {
    log.Fatal(err)
}
fmt.Println(r)

if err := r.Set("5MOhm"); err != nil {
    log.Fatal(err)
}
fmt.Println(r)

Output:

33.300kΩ
1Ω
5MΩ

func (ElectricResistance) String Uses

func (r ElectricResistance) String() string

String returns the resistance formatted as a string in Ohm.

type ElectricalCapacitance Uses

type ElectricalCapacitance int64

ElectricalCapacitance is a measurement of capacitance stored as a pico farad.

The highest representable value is 9.2MF.

const (
    // Farad is a unit of capacitance. kg⁻¹⋅m⁻²⋅s⁴A²
    PicoFarad  ElectricalCapacitance = 1
    NanoFarad  ElectricalCapacitance = 1000 * PicoFarad
    MicroFarad ElectricalCapacitance = 1000 * NanoFarad
    MilliFarad ElectricalCapacitance = 1000 * MicroFarad
    Farad      ElectricalCapacitance = 1000 * MilliFarad
    KiloFarad  ElectricalCapacitance = 1000 * Farad
    MegaFarad  ElectricalCapacitance = 1000 * KiloFarad
)

Well known ElectricalCapacitance constants.

Code:

fmt.Println(1 * physic.Farad)
fmt.Println(22 * physic.PicoFarad)

Output:

1F
22pF

Code:

var c physic.ElectricalCapacitance

flag.Var(&c, "mintouch", "minimum touch sensitivity")
flag.Parse()

Code:

// A typical condensator.
v := 4700 * physic.NanoFarad

// Convert to float64 as microfarad.
f := float64(v) / float64(physic.MicroFarad)

fmt.Println(v)
fmt.Printf("%.1fµF\n", f)

Output:

4.700µF
4.7µF

func (*ElectricalCapacitance) Set Uses

func (c *ElectricalCapacitance) Set(s string) error

Set sets the ElectricalCapacitance to the value represented by s. Units are to be provided in "F" with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".

Code:

var c physic.ElectricalCapacitance

if err := c.Set("1F"); err != nil {
    log.Fatal(err)
}
fmt.Println(c)

if err := c.Set("22pF"); err != nil {
    log.Fatal(err)
}
fmt.Println(c)

Output:

1F
22pF

func (ElectricalCapacitance) String Uses

func (c ElectricalCapacitance) String() string

String returns the energy formatted as a string in Farad.

type Energy Uses

type Energy int64

Energy is a measurement of work stored as a nano joules.

The highest representable value is 9.2GJ.

const (
    // Joule is a unit of work. kg⋅m²⋅s⁻²
    NanoJoule  Energy = 1
    MicroJoule Energy = 1000 * NanoJoule
    MilliJoule Energy = 1000 * MicroJoule
    Joule      Energy = 1000 * MilliJoule
    KiloJoule  Energy = 1000 * Joule
    MegaJoule  Energy = 1000 * KiloJoule
    GigaJoule  Energy = 1000 * MegaJoule

    // BTU (British thermal unit) is the heat required to raise the temperature
    // of one pound of water by one degree Fahrenheit. This is the ISO value.
    BTU Energy = 1055060 * MilliJoule

    WattSecond   Energy = Joule
    WattHour     Energy = 3600 * Joule
    KiloWattHour Energy = 3600 * KiloJoule
)

Well known Energy constants.

Code:

fmt.Println(1 * physic.Joule)
fmt.Println(1 * physic.WattSecond)
fmt.Println(1 * physic.KiloWattHour)

Output:

1J
1J
3.600MJ

Code:

var e physic.Energy

flag.Var(&e, "capacity", "capacity of battery")
flag.Parse()

Code:

// BTU is used in barbecue rating. It is a measure of the thermal energy
// content of the of fuel consumed by the barbecue at its maximum rate. A
// 35000 BTU barbecue is an average power.
v := 35000 * physic.BTU

// Convert to float64 as joule.
f := float64(v) / float64(physic.Joule)

fmt.Println(v)
fmt.Printf("%.0f\n", f)

Output:

36.927MJ
36927100

func (*Energy) Set Uses

func (e *Energy) Set(s string) error

Set sets the Energy to the value represented by s. Units are to be provided in "J" with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".

Code:

var e physic.Energy

if err := e.Set("2.6kJ"); err != nil {
    log.Fatal(err)
}
fmt.Println(e)

if err := e.Set("45mJ"); err != nil {
    log.Fatal(err)
}
fmt.Println(e)

Output:

2.600kJ
45mJ

func (Energy) String Uses

func (e Energy) String() string

String returns the energy formatted as a string in Joules.

type Env Uses

type Env struct {
    Temperature Temperature
    Pressure    Pressure
    Humidity    RelativeHumidity
}

Env represents measurements from an environmental sensor.

type Force Uses

type Force int64

Force is a measurement of interaction that will change the motion of an object stored as an int64 nano Newton.

A measurement of Force is a vector and has a direction but this unit only represents the magnitude. The orientation needs to be stored as a Quaternion independently.

The highest representable value is 9.2TN.

const (
    // Newton is kg⋅m/s².
    NanoNewton  Force = 1
    MicroNewton Force = 1000 * NanoNewton
    MilliNewton Force = 1000 * MicroNewton
    Newton      Force = 1000 * MilliNewton
    KiloNewton  Force = 1000 * Newton
    MegaNewton  Force = 1000 * KiloNewton
    GigaNewton  Force = 1000 * MegaNewton

    EarthGravity Force = 9806650 * MicroNewton

    // Conversion between Newton and imperial units.
    // Pound is both a unit of mass and weight (force). The suffix Force is added
    // to disambiguate the measurement it represents.
    PoundForce Force = 4448221615 * NanoNewton
)

Well known Force constants.

Code:

fmt.Println(10 * physic.MilliNewton)
fmt.Println(physic.EarthGravity)
fmt.Println(physic.PoundForce)

Output:

10mN
9.807N
4.448N

Code:

var f physic.Force

flag.Var(&f, "force", "load cell wakeup force")
flag.Parse()

Code:

// The gravity of Earth.
v := physic.EarthGravity

// Convert to float64 as newton.
fn := float64(v) / float64(physic.Newton)

// Convert to float64 as lbf.
fl := float64(v) / float64(physic.PoundForce)

fmt.Println(v)
fmt.Printf("%fN\n", fn)
fmt.Printf("%flbf\n", fl)

Output:

9.807N
9.806650N
2.204623lbf

func (*Force) Set Uses

func (f *Force) Set(s string) error

Set sets the Force to the value represented by s. Units are to be provided in "N", or "lbf" (Pound force) with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".

Code:

var f physic.Force

if err := f.Set("9.8N"); err != nil {
    log.Fatal(f)
}
fmt.Println(f)

if err := f.Set("20lbf"); err != nil {
    log.Fatal(f)
}
fmt.Println(f)

Output:

9.800N
88.964N

func (Force) String Uses

func (f Force) String() string

String returns the force formatted as a string in Newton.

type Frequency Uses

type Frequency int64

Frequency is a measurement of cycle per second, stored as an int64 micro Hertz.

The highest representable value is 9.2THz.

const (
    // Hertz is 1/s.
    MicroHertz Frequency = 1
    MilliHertz Frequency = 1000 * MicroHertz
    Hertz      Frequency = 1000 * MilliHertz
    KiloHertz  Frequency = 1000 * Hertz
    MegaHertz  Frequency = 1000 * KiloHertz
    GigaHertz  Frequency = 1000 * MegaHertz
    TeraHertz  Frequency = 1000 * GigaHertz

    // RPM is revolutions per minute. It is used to quantify angular frequency.
    RPM Frequency = 16667 * MicroHertz
)

Well known Frequency constants.

Code:

fmt.Println(10 * physic.MilliHertz)
fmt.Println(101010 * physic.MilliHertz)
fmt.Println(10 * physic.MegaHertz)
fmt.Println(60 * physic.RPM)

Output:

10mHz
101.010Hz
10MHz
1Hz

Code:

var pwm physic.Frequency

flag.Var(&pwm, "pwm", "pwm frequency")
flag.Parse()

Code:

// NTSC color subcarrier.
v := (315*physic.MegaHertz + 44) / 88

// Convert to float64 as hertz.
f := float64(v) / float64(physic.Hertz)

fmt.Println(v)
fmt.Printf("%fHz\n", f)

Output:

3.580MHz
3579545.454545Hz

func PeriodToFrequency Uses

func PeriodToFrequency(p time.Duration) Frequency

PeriodToFrequency returns the frequency for a period of this interval.

A 0s period returns a 0Hz frequency.

Code:

fmt.Println(physic.PeriodToFrequency(time.Microsecond))
fmt.Println(physic.PeriodToFrequency(time.Minute))

Output:

1MHz
16.667mHz

func (Frequency) Duration Uses

func (f Frequency) Duration() time.Duration

Duration returns the duration of one cycle at this frequency.

Deprecated: This method is removed in v4.0.0. Use Period() instead.

func (Frequency) Period Uses

func (f Frequency) Period() time.Duration

Period returns the duration of one cycle at this frequency.

Frequency above GigaHertz cannot be represented as Duration.

A 0Hz frequency returns a 0s period.

Code:

fmt.Println(physic.MilliHertz.Period())
fmt.Println(physic.MegaHertz.Period())

Output:

16m40s
1µs

func (*Frequency) Set Uses

func (f *Frequency) Set(s string) error

Set sets the Frequency to the value represented by s. Units are to be provided in "Hz" or "rpm" with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".

Unlike most Set() functions, "Hz" is assumed by default.

Code:

var f physic.Frequency

if err := f.Set("10MHz"); err != nil {
    log.Fatal(err)
}
fmt.Println(f)

if err := f.Set("10mHz"); err != nil {
    log.Fatal(err)
}
fmt.Println(f)

if err := f.Set("1kHz"); err != nil {
    log.Fatal(err)
}
fmt.Println(f)

Output:

10MHz
10mHz
1kHz

func (Frequency) String Uses

func (f Frequency) String() string

String returns the frequency formatted as a string in Hertz.

type LuminousFlux Uses

type LuminousFlux int64

LuminousFlux is a measurement of total quantity of visible light energy emitted with wavelength power weighted by a luminosity function which represents a model of the human eye's response to different wavelengths. The CIE 1931 luminosity function is the standard for lumens.

LuminousFlux is stored as nano lumens.

The highest representable value is 9.2Glm.

const (
    // Lumen is a unit of luminous flux. cd⋅sr
    NanoLumen  LuminousFlux = 1
    MicroLumen LuminousFlux = 1000 * NanoLumen
    MilliLumen LuminousFlux = 1000 * MicroLumen
    Lumen      LuminousFlux = 1000 * MilliLumen
    KiloLumen  LuminousFlux = 1000 * Lumen
    MegaLumen  LuminousFlux = 1000 * KiloLumen
    GigaLumen  LuminousFlux = 1000 * MegaLumen
)

Well known LuminousFlux constants.

Code:

fmt.Println(18282 * physic.Lumen)

Output:

18.282klm

Code:

var l physic.LuminousFlux

flag.Var(&l, "low", "mood light level")
flag.Parse()

Code:

// Typical output of a 7W LED.
v := 450 * physic.Lumen

// Convert to float64 as lumen.
f := float64(v) / float64(physic.Lumen)

fmt.Println(v)
fmt.Printf("%.0f\n", f)

Output:

450lm
450

func (*LuminousFlux) Set Uses

func (f *LuminousFlux) Set(s string) error

Set sets the LuminousFlux to the value represented by s. Units are to be provided in "lm" with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".

Code:

var l physic.LuminousFlux

if err := l.Set("25mlm"); err != nil {
    log.Fatal(err)
}
fmt.Println(l)

if err := l.Set("2.5Mlm"); err != nil {
    log.Fatal(err)
}
fmt.Println(l)

Output:

25mlm
2.500Mlm

func (LuminousFlux) String Uses

func (f LuminousFlux) String() string

String returns the energy formatted as a string in Lumens.

type LuminousIntensity Uses

type LuminousIntensity int64

LuminousIntensity is a measurement of the quantity of visible light energy emitted per unit solid angle with wavelength power weighted by a luminosity function which represents the human eye's response to different wavelengths. The CIE 1931 luminosity function is the SI standard for candela.

LuminousIntensity is stored as nano candela.

This is one of the base unit in the International System of Units.

The highest representable value is 9.2Gcd.

const (
    // Candela is a unit of luminous intensity. cd
    NanoCandela  LuminousIntensity = 1
    MicroCandela LuminousIntensity = 1000 * NanoCandela
    MilliCandela LuminousIntensity = 1000 * MicroCandela
    Candela      LuminousIntensity = 1000 * MilliCandela
    KiloCandela  LuminousIntensity = 1000 * Candela
    MegaCandela  LuminousIntensity = 1000 * KiloCandela
    GigaCandela  LuminousIntensity = 1000 * MegaCandela
)

Well known LuminousIntensity constants.

Code:

fmt.Println(12 * physic.Candela)

Output:

12cd

Code:

var l physic.LuminousIntensity

flag.Var(&l, "dusk", "light level to turn on light")
flag.Parse()

Code:

// A 7W LED generating 450lm in all directions (4π steradian) will have an
// intensity of 450lm/4π = ~35.8cd.
v := 35800 * physic.MilliCandela

// Convert to float64 as candela.
f := float64(v) / float64(physic.Candela)

fmt.Println(v)
fmt.Printf("%.1f\n", f)

Output:

35.800cd
35.8

func (*LuminousIntensity) Set Uses

func (i *LuminousIntensity) Set(s string) error

Set sets the LuminousIntensity to the value represented by s. Units are to be provided in "cd" with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".

Code:

var l physic.LuminousIntensity

if err := l.Set("16cd"); err != nil {
    log.Fatal(err)
}
fmt.Println(l)

Output:

16cd

func (LuminousIntensity) String Uses

func (i LuminousIntensity) String() string

String returns the energy formatted as a string in Candela.

type Mass Uses

type Mass int64

Mass is a measurement of mass stored as an int64 nano gram.

This is one of the base unit in the International System of Units.

The highest representable value is 9.2Gg.

const (
    NanoGram  Mass = 1
    MicroGram Mass = 1000 * NanoGram
    MilliGram Mass = 1000 * MicroGram
    Gram      Mass = 1000 * MilliGram
    KiloGram  Mass = 1000 * Gram
    MegaGram  Mass = 1000 * KiloGram
    GigaGram  Mass = 1000 * MegaGram
    Tonne     Mass = MegaGram

    // Conversion between Gram and imperial units.
    // Ounce is both a unit of mass, weight (force) or volume depending on
    // context. The suffix Mass is added to disambiguate the measurement it
    // represents.
    OunceMass Mass = 28349523125 * NanoGram
    // Pound is both a unit of mass and weight (force). The suffix Mass is added
    // to disambiguate the measurement it represents.
    PoundMass Mass = 16 * OunceMass

    Slug Mass = 14593903 * MilliGram
)

Well known Mass constants.

Code:

fmt.Println(10 * physic.MilliGram)
fmt.Println(physic.OunceMass)
fmt.Println(physic.PoundMass)
fmt.Println(physic.Slug)

Output:

10mg
28.350g
453.592g
14.594kg

Code:

var m physic.Mass

flag.Var(&m, "weight", "amount of cat food to dispense")
flag.Parse()

Code:

// Weight of a Loonie (Canadian metal piece).
v := 6270 * physic.MilliGram

// Convert to float64 as gram.
f := float64(v) / float64(physic.Gram)

fmt.Println(v)
fmt.Printf("%.2fg\n", f)

Output:

6.270g
6.27g

func (*Mass) Set Uses

func (m *Mass) Set(s string) error

Set sets the Mass to the value represented by s. Units are to be provided in "g", "lb" or "oz" with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".

Code:

var m physic.Mass

if err := m.Set("10mg"); err != nil {
    log.Fatal(err)
}
fmt.Println(m)

if err := m.Set("16.5kg"); err != nil {
    log.Fatal(err)
}
fmt.Println(m)

if err := m.Set("2.2oz"); err != nil {
    log.Fatal(err)
}
fmt.Println(m)

if err := m.Set("16lb"); err != nil {
    log.Fatal(err)
}
fmt.Println(m)

Output:

10mg
16.500kg
62.369g
7.257kg

func (Mass) String Uses

func (m Mass) String() string

String returns the mass formatted as a string in gram.

type Power Uses

type Power int64

Power is a measurement of power stored as a nano watts.

The highest representable value is 9.2GW.

const (
    // Watt is unit of power J/s, kg⋅m²⋅s⁻³
    NanoWatt  Power = 1
    MicroWatt Power = 1000 * NanoWatt
    MilliWatt Power = 1000 * MicroWatt
    Watt      Power = 1000 * MilliWatt
    KiloWatt  Power = 1000 * Watt
    MegaWatt  Power = 1000 * KiloWatt
    GigaWatt  Power = 1000 * MegaWatt
)

Well known Power constants.

Code:

fmt.Println(1 * physic.Watt)
fmt.Println(16 * physic.MilliWatt)
fmt.Println(1210 * physic.MegaWatt)

Output:

1W
16mW
1.210GW

Code:

var p physic.Power

flag.Var(&p, "power", "heater maximum power")
flag.Parse()

Code:

// Maximum emitted power by Bluetooth class 2 device.
v := 2500 * physic.MicroWatt

// Convert to float64 as watt.
f := float64(v) / float64(physic.Watt)

fmt.Println(v)
fmt.Printf("%.4f\n", f)

Output:

2.500mW
0.0025

func (*Power) Set Uses

func (p *Power) Set(s string) error

Set sets the Power to the value represented by s. Units are to be provided in "W" with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".

Code:

var p physic.Power

if err := p.Set("25mW"); err != nil {
    log.Fatal(err)
}
fmt.Println(p)

if err := p.Set("1W"); err != nil {
    log.Fatal(err)
}
fmt.Println(p)

if err := p.Set("1.21GW"); err != nil {
    log.Fatal(err)
}
fmt.Println(p)

Output:

25mW
1W
1.210GW

func (Power) String Uses

func (p Power) String() string

String returns the power formatted as a string in watts.

type Pressure Uses

type Pressure int64

Pressure is a measurement of force applied to a surface per unit area (stress) stored as an int64 nano Pascal.

The highest representable value is 9.2GPa.

const (
    // Pascal is N/m², kg/m/s².
    NanoPascal  Pressure = 1
    MicroPascal Pressure = 1000 * NanoPascal
    MilliPascal Pressure = 1000 * MicroPascal
    Pascal      Pressure = 1000 * MilliPascal
    KiloPascal  Pressure = 1000 * Pascal
    MegaPascal  Pressure = 1000 * KiloPascal
    GigaPascal  Pressure = 1000 * MegaPascal
)

Well known Pressure constants.

Code:

fmt.Println(101010 * physic.Pascal)
fmt.Println(101 * physic.KiloPascal)

Output:

101.010kPa
101kPa

Code:

var p physic.Pressure

flag.Var(&p, "setpoint", "pressure for pump to maintain")
flag.Parse()

Code:

// A typical tire pressure in North America (33psi).
v := 227526990 * physic.MicroPascal

// Convert to float64 as pascal.
f := float64(v) / float64(physic.Pascal)

fmt.Println(v)
fmt.Printf("%f\n", f)

Output:

227.527Pa
227.526990

func (*Pressure) Set Uses

func (p *Pressure) Set(s string) error

Set sets the Pressure to the value represented by s. Units are to be provided in "Pa" with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".

Code:

var p physic.Pressure

if err := p.Set("300kPa"); err != nil {
    log.Fatal(err)
}
fmt.Println(p)

if err := p.Set("16MPa"); err != nil {
    log.Fatal(err)
}
fmt.Println(p)

Output:

300kPa
16MPa

func (Pressure) String Uses

func (p Pressure) String() string

String returns the pressure formatted as a string in Pascal.

type RelativeHumidity Uses

type RelativeHumidity int32

RelativeHumidity is a humidity level measurement stored as an int32 fixed point integer at a precision of 0.00001%rH.

Valid values are between 0% and 100%.

const (
    TenthMicroRH RelativeHumidity = 1                 // 0.00001%rH
    MicroRH      RelativeHumidity = 10 * TenthMicroRH // 0.0001%rH
    MilliRH      RelativeHumidity = 1000 * MicroRH    // 0.1%rH
    PercentRH    RelativeHumidity = 10 * MilliRH      // 1%rH

)

Well known RelativeHumidity constants.

Code:

fmt.Println(506 * physic.MilliRH)
fmt.Println(20 * physic.PercentRH)

Output:

50.6%rH
20%rH

Code:

var h physic.RelativeHumidity

flag.Var(&h, "humidity", "green house humidity high alarm level")
flag.Parse()

Code:

// Yearly humidity average of Nadi, Fiji.
v := 800 * physic.MilliRH

// Convert to float64 as %.
f := float64(v) / float64(physic.PercentRH)

fmt.Println(v)
fmt.Printf("%.0f\n", f)

Output:

80%rH
80

func (*RelativeHumidity) Set Uses

func (r *RelativeHumidity) Set(s string) error

Set sets the RelativeHumidity to the value represented by s. Units are to be provided in "%rH" or "%" with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".

Code:

var r physic.RelativeHumidity

if err := r.Set("50.6%rH"); err != nil {
    log.Fatal(err)
}
fmt.Println(r)

if err := r.Set("20%"); err != nil {
    log.Fatal(err)
}
fmt.Println(r)

Output:

50.6%rH
20%rH

func (RelativeHumidity) String Uses

func (r RelativeHumidity) String() string

String returns the humidity formatted as a string.

type SenseEnv Uses

type SenseEnv interface {
    conn.Resource

    // Sense returns the value read from the sensor. Unsupported metrics are not
    // modified.
    Sense(env *Env) error
    // SenseContinuous initiates a continuous sensing at the specified interval.
    //
    // It is important to call Halt() once done with the sensing, which will turn
    // the device off and will close the channel.
    SenseContinuous(interval time.Duration) (<-chan Env, error)
    // Precision returns this sensor's precision.
    //
    // The env values are set to the number of bits that are significant for each
    // items that this sensor can measure.
    //
    // Precision is not accuracy. The sensor may have absolute and relative
    // errors in its measurement, that are likely well above the reported
    // precision. Accuracy may be improved on some sensor by using oversampling,
    // or doing oversampling in software. Refer to its datasheet if available.
    Precision(env *Env)
}

SenseEnv represents an environmental sensor.

type Speed Uses

type Speed int64

Speed is a measurement of magnitude of velocity stored as an int64 nano Metre per Second.

The highest representable value is 9.2Gm/s.

const (
    // MetrePerSecond is m/s.
    NanoMetrePerSecond  Speed = 1
    MicroMetrePerSecond Speed = 1000 * NanoMetrePerSecond
    MilliMetrePerSecond Speed = 1000 * MicroMetrePerSecond
    MetrePerSecond      Speed = 1000 * MilliMetrePerSecond
    KiloMetrePerSecond  Speed = 1000 * MetrePerSecond
    MegaMetrePerSecond  Speed = 1000 * KiloMetrePerSecond
    GigaMetrePerSecond  Speed = 1000 * MegaMetrePerSecond

    LightSpeed Speed = 299792458 * MetrePerSecond

    KilometrePerHour Speed = 277777778 * NanoMetrePerSecond
    MilePerHour      Speed = 447040 * MicroMetrePerSecond
    FootPerSecond    Speed = 304800 * MicroMetrePerSecond
)

Well known Speed constants.

Code:

fmt.Println(10 * physic.MilliMetrePerSecond)
fmt.Println(physic.LightSpeed)
fmt.Println(physic.KilometrePerHour)
fmt.Println(physic.MilePerHour)
fmt.Println(physic.FootPerSecond)

Output:

10mm/s
299.792Mm/s
277.778mm/s
447.040mm/s
304.800mm/s

Code:

var s physic.Speed

flag.Var(&s, "speed", "window shutter closing speed")
flag.Parse()

Code:

// Speed of light in vacuum.
v := physic.LightSpeed

// Convert to float64 as m/s.
fms := float64(v) / float64(physic.MetrePerSecond)

// Convert to float64 as km/h.
fkh := float64(v) / float64(physic.KilometrePerHour)

// Convert to float64 as mph.
fmh := float64(v) / float64(physic.MilePerHour)

fmt.Println(v)
fmt.Printf("%.0fm/s\n", fms)
fmt.Printf("%.1fkm/h\n", fkh)
fmt.Printf("%.1fmph\n", fmh)

Output:

299.792Mm/s
299792458m/s
1079252847.9km/h
670616629.4mph

func (*Speed) Set Uses

func (sp *Speed) Set(s string) error

Set sets the Speed to the value represented by s. Units are to be provided in "mps"(meters per second), "m/s", "kph", "fps", or "mph" with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".

Code:

var s physic.Speed

if err := s.Set("10m/s"); err != nil {
    log.Fatal(err)
}
fmt.Println(s)

if err := s.Set("100kph"); err != nil {
    log.Fatal(err)
}
fmt.Println(s)

if err := s.Set("2067fps"); err != nil {
    log.Fatal(err)
}
fmt.Println(s)

if err := s.Set("55mph"); err != nil {
    log.Fatal(err)
}
fmt.Println(s)

Output:

10m/s
27.778m/s
630.022m/s
24.587m/s

func (Speed) String Uses

func (sp Speed) String() string

String returns the speed formatted as a string in m/s.

type Temperature Uses

type Temperature int64

Temperature is a measurement of hotness stored as a nano kelvin.

Negative values are invalid.

The highest representable value is 9.2GK.

const (
    NanoKelvin  Temperature = 1
    MicroKelvin Temperature = 1000 * NanoKelvin
    MilliKelvin Temperature = 1000 * MicroKelvin
    Kelvin      Temperature = 1000 * MilliKelvin
    KiloKelvin  Temperature = 1000 * Kelvin
    MegaKelvin  Temperature = 1000 * KiloKelvin
    GigaKelvin  Temperature = 1000 * MegaKelvin

    // Conversion between Kelvin and Celsius.
    ZeroCelsius  Temperature = 273150 * MilliKelvin
    MilliCelsius Temperature = MilliKelvin
    Celsius      Temperature = Kelvin

    // Conversion between Kelvin and Fahrenheit.
    ZeroFahrenheit  Temperature = 255372222222 * NanoKelvin
    MilliFahrenheit Temperature = 555555 * NanoKelvin
    Fahrenheit      Temperature = 555555555 * NanoKelvin
)

Well known Temperature constants.

Code:

fmt.Println(0 * physic.Kelvin)
fmt.Println(23010*physic.MilliCelsius + physic.ZeroCelsius)
fmt.Println(80*physic.Fahrenheit + physic.ZeroFahrenheit)

Output:

-273.150°C
23.010°C
26.667°C

Code:

var t physic.Temperature

flag.Var(&t, "temp", "thermostat setpoint")
flag.Parse()

Code:

// Normal average human body temperature.
v := 37*physic.Celsius + physic.ZeroCelsius

// Convert to float64 as Kelvin.
f := float64(v) / float64(physic.Kelvin)

fmt.Println(v)
fmt.Printf("%.1fK\n", f)

Output:

37°C
310.1K

func (Temperature) Celsius Uses

func (t Temperature) Celsius() float64

Celsius returns the temperature as a floating number of °Celsius.

Code:

// Normal average human body temperature.
v := 37*physic.Celsius + physic.ZeroCelsius

// Convert to float64 as Celsius.
f := v.Celsius()

fmt.Println(v)
fmt.Printf("%.1f°C\n", f)

Output:

37°C
37.0°C

func (Temperature) Fahrenheit Uses

func (t Temperature) Fahrenheit() float64

Fahrenheit returns the temperature as a floating number of °Fahrenheit.

Code:

// Normal average human body temperature.
v := 37*physic.Celsius + physic.ZeroCelsius

// Convert to float64 as Fahrenheit.
f := v.Fahrenheit()

fmt.Println(v)
fmt.Printf("%.1f°F\n", f)

Output:

37°C
98.6°F

func (*Temperature) Set Uses

func (t *Temperature) Set(s string) error

Set sets the Temperature to the value represented by s. Units are to be provided in "C", "°C", "F", "°F" or "K" with an optional SI prefix: "p", "n", "u", "µ", "m", "k", "M", "G" or "T".

Code:

var t physic.Temperature

if err := t.Set("0°C"); err != nil {
    log.Fatal(err)
}
fmt.Println(t)

if err := t.Set("1C"); err != nil {
    log.Fatal(err)
}
fmt.Println(t)

if err := t.Set("5MK"); err != nil {
    log.Fatal(err)
}
fmt.Println(t)

if err := t.Set("0°F"); err != nil {
    log.Fatal(err)
}
fmt.Println(t)

if err := t.Set("32F"); err != nil {
    log.Fatal(err)
}
fmt.Println(t)

Output:

0°C
1°C
5M°C
-17.778°C
0°C

func (Temperature) String Uses

func (t Temperature) String() string

String returns the temperature formatted as a string in °Celsius.

Package physic imports 6 packages (graph) and is imported by 93 packages. Updated 2019-08-27. Refresh now. Tools for package owners.