Conversation
| x, y, z := accel.ReadAcceleration() | ||
| println(x, y, z) | ||
| time.Sleep(time.Millisecond * 100) | ||
| err := mpuDevice.Update() |
There was a problem hiding this comment.
I believe Update should have a parameter with the sensor values it should update?
There was a problem hiding this comment.
Yes, we should probably solve #345 before merging
There was a problem hiding this comment.
I'd leave this for last since I still don't have a good rebase methodology. As soon as everything else is reviewed and OK I'll try my hand at rebasing one more time
mpu6050/mpu6050.go
Outdated
| func DefaultConfig() IMUConfig { | ||
| return IMUConfig{ | ||
| AccRange: ACCEL_RANGE_16, | ||
| GyroRange: GYRO_RANGE_2000, | ||
| sampleRatio: 0, // TODO add const values. | ||
| clkSel: 0, | ||
| } |
There was a problem hiding this comment.
Any reason not to use the zero value for these options as the default value?
There was a problem hiding this comment.
I'm not sure I understand- I've added them here redundantly just so I can mark it with a TODO
|
|
||
| accel := mpu6050.New(machine.I2C0) | ||
| accel.Configure() | ||
| mpuDevice := mpu6050.New(machine.I2C0, mpu6050.DefaultAddress) |
There was a problem hiding this comment.
In other drivers, this has been solved by exporting Address and then just setting it before calling Configure. That way we do not have to break the existing API and all of the many projects that use this driver. I have several such projects myself 😺
mpuDevice := mpu6050.New(machine.I2C0)
mpuDevice.Address = 0x66 // overrides default
// now do everything elseThere was a problem hiding this comment.
With Go modules, I'm not too worried about breakage. Yes, it will break once you do a go get -u tinygo.org/x/drivers, but before that it will keep working just fine (and if you're doing a go get -u tinygo.org/x/drivers, you might as well add the default address).
I'm fine either way.
|
|
||
| accel := mpu6050.New(machine.I2C0) | ||
| accel.Configure() | ||
| mpuDevice := mpu6050.New(machine.I2C0, mpu6050.DefaultAddress) |
There was a problem hiding this comment.
With Go modules, I'm not too worried about breakage. Yes, it will break once you do a go get -u tinygo.org/x/drivers, but before that it will keep working just fine (and if you're doing a go get -u tinygo.org/x/drivers, you might as well add the default address).
I'm fine either way.
examples/mpu6050/main.go
Outdated
| err := mpuDevice.Configure(mpu6050.Config{ | ||
| AccRange: mpu6050.ACCEL_RANGE_16, | ||
| GyroRange: mpu6050.GYRO_RANGE_2000, | ||
| }) |
There was a problem hiding this comment.
...and here we have another breaking change, that I think is actually really useful.
| x, y, z := accel.ReadAcceleration() | ||
| println(x, y, z) | ||
| time.Sleep(time.Millisecond * 100) | ||
| err := mpuDevice.Update() |
mpu6050/mpu6050.go
Outdated
| func (p *Device) SetSampleRate(srDiv byte) (err error) { | ||
| // setSampleRate | ||
| var sr [1]byte | ||
| sr[0] = srDiv | ||
| if err = p.write8(_SMPRT_DIV, sr[0]); err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
| } |
There was a problem hiding this comment.
This is a new API.
Why not use time.Duration as an input? Or a sample rate in Hz, as an uint32? (Similar to machine.CPUFrequency() for example). The parameter that you're using here looks like a mpu6050-specific value.
mpu6050/mpu6050.go
Outdated
| // SetClockSource configures the source of the clock | ||
| // for the peripheral. | ||
| func (p *Device) SetClockSource(clkSel byte) (err error) { | ||
| // setClockSource | ||
| var pwrMgt [1]byte | ||
|
|
||
| if err = p.read(_PWR_MGMT_1, pwrMgt[:]); err != nil { | ||
| return err | ||
| } | ||
| pwrMgt[0] = (pwrMgt[0] & (^_CLK_SEL_MASK)) | clkSel // Escribo solo el campo de clk_sel | ||
| if err = p.write8(_PWR_MGMT_1, pwrMgt[0]); err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
| } |
There was a problem hiding this comment.
Is this actually useful? Can you give me an example where this would be used in practice?
If not, I suggest removing it to declutter the API.
(Also, it contains a Spanish comment?)
mpu6050/mpu6050.go
Outdated
| // SetRangeGyro configures the full scale range of the gyroscope. | ||
| // It has four possible values +- 250°/s, 500°/s, 1000°/s, 2000°/s. | ||
| // The function takes values of gyroRange from 0-3 where 0 means the | ||
| // lowest FSR (250°/s) and 3 is the highest FSR (2000°/s). | ||
| func (p *Device) SetRangeGyro(gyroRange byte) (err error) { | ||
| switch gyroRange { | ||
| case GYRO_RANGE_250: | ||
| p.gRange = 250 | ||
| case GYRO_RANGE_500: | ||
| p.gRange = 500 | ||
| case GYRO_RANGE_1000: | ||
| p.gRange = 1000 | ||
| case GYRO_RANGE_2000: | ||
| p.gRange = 2000 | ||
| default: | ||
| return errors.New("invalid gyroscope FSR input") | ||
| } | ||
| // setFullScaleGyroRange | ||
| var gConfig [1]byte | ||
|
|
||
| if err = p.read(_GYRO_CONFIG, gConfig[:]); err != nil { | ||
| return err | ||
| } | ||
| gConfig[0] = (gConfig[0] & (^_G_FS_SEL)) | (gyroRange << _G_FS_SHIFT) // Escribo solo el campo de FS_sel | ||
|
|
||
| if err = p.write8(_GYRO_CONFIG, gConfig[0]); err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
| } |
mpu6050/mpu6050.go
Outdated
| // It has four possible values +- 2g, 4g, 8g, 16g. | ||
| // The function takes values of accRange from 0-3 where 0 means the | ||
| // lowest FSR (2g) and 3 is the highest FSR (16g) | ||
| func (p *Device) SetRangeAccel(accRange byte) (err error) { |
There was a problem hiding this comment.
Same here. Please don't use magic constants (0, 1, 2, 3) and instead instruct users to use named constants.
mpu6050/mpu6050.go
Outdated
| func DefaultConfig() Config { | ||
| return Config{ | ||
| AccRange: ACCEL_RANGE_16, | ||
| GyroRange: GYRO_RANGE_2000, | ||
| sampleRatio: 0, // TODO add const values. | ||
| clkSel: 0, | ||
| } | ||
| } |
There was a problem hiding this comment.
Instead of DefaultConfig, have you considered making the zero value the default config?
That way, users can just do Configure(mpu6050.Config{GyroRange: ...}) and know that the acceleration range is also set to some reasonable default.
There was a problem hiding this comment.
I have pushed changes that make the zero value useful in latest commit!
|
Closing this PR in favor of #577 |
Resolves #544.
I have published the mpu6050 driver I've used independently for years now.
It has greatly increased functionality and is much faster and efficient than the existing implementation. I have broken API compatibility since the existing implementation was extremely limited. In doing so I've taken the liberty of implementing patterns from the RFC #310.
New Features: