Skip to content
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.

ina219 i2c driver: high side current and voltage sensor #292

Merged
merged 6 commits into from
Oct 14, 2018
Merged

ina219 i2c driver: high side current and voltage sensor #292

merged 6 commits into from
Oct 14, 2018

Conversation

NeuralSpaz
Copy link
Collaborator

@NeuralSpaz NeuralSpaz commented Oct 13, 2018

adds initial experimental driver for Texas Instruments i2c INA219 high side current sensor IC.

  • basic driver operation verified on raspberry pi zero w. ina219/cmd
    depends on physic.Power in add more SI units #294

Copy link
Contributor

@maruel maruel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! Thanks a lot for this extensive change! It's very appreciated that you fix TODOs!

I only nitpicked a few things in the driver itself, it's fine to commit WIP there so beside the copyright, the rest could be done as follow up. For the physic package I'm more stringent on the quality of the docs and coherency, hence my comments are focusing on this package.

I'd like to ask you a favor; would you mind to split this PR in 3?

  • One for the rounding improvement in physic, and only thing
  • One adding the new physic types
  • The rest

The reason is that if someone wants to figure out when rounding was changed, or when new types were added, it's simple if they are standalone comments.

@@ -67,7 +67,7 @@ func ExampleForce() {
fmt.Println(physic.PoundForce)
// Output:
// 10mN
// 990.569kN
// 990.570kN
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really wonder why I did the multiplication. I think you can replace line 66 with fmt.Println(physic.EarthGravity)

}

const (
//Watt is unit of power J/s, kg⋅m²⋅s⁻³
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space between // and Watt.

}

const (
//Farad is a unit of capacitance. kg⁻¹⋅m⁻²⋅s⁴A²
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space

GigaJoule Energy = 1000 * MegaJoule
)

// Capacitance is a measurement of capacitance stored as a pico farad.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used ElectricResistance because I felt that Resistance was a bit ambiguous. Resistance can be measured also as Drag, Hardness and Thermal resistance.

I used ElectricCurrent and ElectricPotential to be coherent; so I feel that Capacitance may be worth having the same treatment, even if it's not ambiguous. Maybe I made an error, I'm still undecided.

Just thinking out loud here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think having it as ElectricalCapacitance is more consistence with the current units even though there is no ambiguity and would cause less confusion than the other way; dropping the Electrical prefix.
Also thinking about it Flux probably should be LuminousFlux so as not to confuse lumens with webers or teslas in the future.

}

const (
//Joule is a unit of work. kg⋅m2⋅s⁻²
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space

return PowerMonitor{}, errReadShunt
}
var pm PowerMonitor
pm.Shunt = physic.ElectricPotential(int16(binary.BigEndian.Uint16(rx)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'd benefit from mmr here too if you want to use it.

@@ -0,0 +1,730 @@
// Copyright 2016 The Periph Authors. All rights reserved.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update year.

@@ -0,0 +1,18 @@
// Copyright 2016 The Periph Authors. All rights reserved.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update year. I'd recommend doc.go since this is what is generally used in other packages.

// Slave Address:
// Depending which pins the A1, A0 pins are connected to will change the slave
// address as bellow. Default configuration is address 0x40
//
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove two trailing //

@maruel
Copy link
Contributor

maruel commented Oct 13, 2018

gohci

@NeuralSpaz
Copy link
Collaborator Author

Thank you for your review, I have split the PR, first time doing that :S

@NeuralSpaz NeuralSpaz changed the title INA219 Driver TI i2c current and voltage sensor ina219 i2c driver: high side current and voltage sensor Oct 14, 2018
Copy link
Contributor

@maruel maruel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a few optional comments, I'll merge as soon as #294 is merged in, as these can be done in follow ups, I don't mind here as experimental is meant to improve drivers via iteration.

experimental/devices/ina219/cmd/cmd.go Outdated Show resolved Hide resolved
experimental/devices/ina219/cmd/cmd.go Outdated Show resolved Hide resolved
experimental/devices/ina219/cmd/cmd.go Outdated Show resolved Hide resolved
experimental/devices/ina219/doc.go Show resolved Hide resolved
// Datasheet
// http://www.ti.com/lit/ds/symlink/ina219.pdf
//
// Slave Address:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same. You can test locally with godoc -http=:6060 then visit http://localhost:6060/pkg/periph.io/x/periph/experimental/devices/ina219.

I think you'll have to remove the ':' for this to work.

That said, I think this belongs to Opts, not to package level documentation.

Copy link
Collaborator Author

@NeuralSpaz NeuralSpaz Oct 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have kept note about calibration in the package docs as I think it is important and easy to overlook with defaults.

experimental/devices/ina219/example_test.go Outdated Show resolved Hide resolved
experimental/devices/ina219/ina219.go Outdated Show resolved Hide resolved
experimental/devices/ina219/ina219.go Outdated Show resolved Hide resolved
experimental/devices/ina219/ina219.go Outdated Show resolved Hide resolved
experimental/devices/ina219/ina219.go Outdated Show resolved Hide resolved
// overflow int64 or loose resolution.
const calibratescale int64 = ((int64(physic.Ampere) * int64(physic.Ohm)) / 100000) << 12

// Calibrate sets the scaling factor of the current and power registers for the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lower case calibrate

@maruel
Copy link
Contributor

maruel commented Oct 14, 2018

gohci

@maruel
Copy link
Contributor

maruel commented Oct 14, 2018

You'll have to rebase as the tests currently fail.

git remote add periph https://github.com/google/periph
go fetch periph
git rebase periph/master
git push -f origin ina219

split of PR
Change Configuration to Opts.
Change default constants to public Opts variable.
Move cmd to ../cmd/ina219.
Fix comment formatting.
Fix godoc formatting.
Return error from smoketest.
Change calibrate to private since it is called in New and does not need to be called again the sensor has be calibrated.
@maruel
Copy link
Contributor

maruel commented Oct 14, 2018

gohci

@maruel
Copy link
Contributor

maruel commented Oct 14, 2018

experimental/devices/ina219/ina219smoketest/ina219smoketest.go:66:27: cannot use config (type ina219.Opts) as type *ina219.Opts in argument to ina219.New

I'm going outside, will merge when I'm back in a few hours and you pushed a fix. My only nit is the Calibrate doc that isn't correctly cased.

change Opts to pointer to *Opts in smoketest
@NeuralSpaz
Copy link
Collaborator Author

Thank you for your code review/s it has been extremely helpful and edifying. As I have about dozen or so more IC drivers I would like to contribute, it is all great to know.

@maruel
Copy link
Contributor

maruel commented Oct 14, 2018

gohci

Copy link
Contributor

@maruel maruel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's some comments for follow ups.

func New(bus i2c.Bus, opts *Opts) (*Dev, error) {

i2cAddress := DefaultOpts.Address
if opts.Address != 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine to force users to pass valid value and not 0, as there's DefaultOpts. Same for line 50, 58, which would simplify this code a bit.

d.mu.Lock()
defer d.mu.Unlock()

var pm PowerMonitor
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend to change line 139 with:

return PowerMonitor{
 Shunt: physic.ElectricPotential(shunt) * 10 * physic.MicroVolt,
  Voltage: physic.ElectricPotential(bus>>3) * 4 * physic.MilliVolt,
  Current: physic.ElectricCurrent(current) * d.currentLSB,
}, nil

That would make the function a bit more dense.

@maruel maruel merged commit afa1663 into google:master Oct 14, 2018
@maruel
Copy link
Contributor

maruel commented Oct 14, 2018

Thank you for your code review/s it has been extremely helpful and edifying. As I have about dozen or so more IC drivers I would like to contribute, it is all great to know.

!!

That's amazing.

Thanks for the thoughtful code, it was really easy to review. To migrate to non-experimental, I'll ask:

That's pretty much it, the code is pretty good already. Thanks again.

@maruel
Copy link
Contributor

maruel commented Oct 14, 2018

Also, you may want to add yourself to AUTHORS and CONTRIBUTORS.

@NeuralSpaz NeuralSpaz deleted the ina219 branch October 14, 2018 23:17
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants