-
Notifications
You must be signed in to change notification settings - Fork 167
ina219 i2c driver: high side current and voltage sensor #292
Conversation
There was a problem hiding this 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.
conn/physic/example_test.go
Outdated
@@ -67,7 +67,7 @@ func ExampleForce() { | |||
fmt.Println(physic.PoundForce) | |||
// Output: | |||
// 10mN | |||
// 990.569kN | |||
// 990.570kN |
There was a problem hiding this comment.
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)
conn/physic/units.go
Outdated
} | ||
|
||
const ( | ||
//Watt is unit of power J/s, kg⋅m²⋅s⁻³ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Space between // and Watt.
conn/physic/units.go
Outdated
} | ||
|
||
const ( | ||
//Farad is a unit of capacitance. kg⁻¹⋅m⁻²⋅s⁴A² |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Space
conn/physic/units.go
Outdated
GigaJoule Energy = 1000 * MegaJoule | ||
) | ||
|
||
// Capacitance is a measurement of capacitance stored as a pico farad. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
conn/physic/units.go
Outdated
} | ||
|
||
const ( | ||
//Joule is a unit of work. kg⋅m2⋅s⁻² |
There was a problem hiding this comment.
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))) |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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 | ||
// |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove two trailing //
gohci |
Thank you for your review, I have split the PR, first time doing that :S |
There was a problem hiding this 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/doc.go
Outdated
// Datasheet | ||
// http://www.ti.com/lit/ds/symlink/ina219.pdf | ||
// | ||
// Slave Address: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
// 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lower case calibrate
gohci |
You'll have to rebase as the tests currently fail.
|
initial commit
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.
gohci |
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
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. |
gohci |
There was a problem hiding this 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 { |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
!! 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. |
Also, you may want to add yourself to AUTHORS and CONTRIBUTORS. |
adds initial experimental driver for Texas Instruments i2c INA219 high side current sensor IC.
depends on physic.Power in add more SI units #294