Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RSDK-1001] Add support for the ADXL345 accelerometer #1640

Merged
merged 21 commits into from
Dec 8, 2022

Conversation

penguinland
Copy link
Member

(and while I was looking at it, return any errors encountered when querying the MPU6050 accelerometer, too.)

This PR is adapted from https://github.com/viam-labs/adxl345-accelerometer/pull/1, and when this is merged we should probably delete that other repo.

Tried at my desk: results look plausible. I've got two accelerometers at my desk now, and although they both give okay results, the MPU6050 tends to think gravity is a pretty consistent 13.7% stronger than the ADXL345 thinks it is. Is this because I coded one of them up wrong, or because one of the chips is miscalibrated? Part of me suspects the former, but I don't actually know. 😬

but at the very least, they both give values that are the right order of magnitude, in the right direction.

Sorry the PR is so long; I would have preferred two PRs half the length, but didn't see an easy way to break it up. Maybe next time I can go for the skeleton code, and then the implementation itself?

I'm tagging Rand because she reviewed the MPU6050 code (#1611), and Nicolas because he reviewed https://github.com/viam-labs/adxl345-accelerometer/pull/1. I kinda feel like only one of you needs to review this, but I don't know which that should be.

@viambot viambot added the safe to test This pull request is marked safe to test from a trusted zone label Dec 5, 2022
@penguinland penguinland removed the request for review from randhid December 5, 2022 22:37
@penguinland
Copy link
Member Author

(removing Rand as a reviewer: she's busy, and Nicolas saw the original version of this.)

@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Dec 6, 2022
@penguinland
Copy link
Member Author

Thanks to @stevebriskin for pointing out that we have a naming convention for these things, and the field should be named i2c_bus!

@penguinland penguinland changed the title Add support for the ADXL345 accelerometer [RSDK-1001] Add support for the ADXL345 accelerometer Dec 6, 2022
}
localB, ok := b.(board.LocalBoard)
if !ok {
return nil, errors.Errorf("board %s is not local", cfg.BoardName)
Copy link
Member

Choose a reason for hiding this comment

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

prefer %q

}
bus, ok := localB.I2CByName(cfg.I2cBus)
if !ok {
return nil, errors.Errorf("can't find I2C bus '%s' for ADXL345 sensor", cfg.I2cBus)
Copy link
Member

Choose a reason for hiding this comment

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

same as above

// back the device ID (0xE5).
deviceID, err := sensor.readByte(ctx, 0)
if err != nil {
return nil, errors.Errorf("can't read from I2C address %d on bus %s of board %s: '%s'",
Copy link
Member

Choose a reason for hiding this comment

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

prefer errors.Wrap or errors.Wrapf

// To do this, we set the Power Control register (0x2D) to turn on the 8's bit.
err = sensor.writeByte(ctx, 0x2D, 0x08)
if err != nil {
return nil, errors.Errorf("unable to put ADXL345 into measurement mode: '%s'", err.Error())
Copy link
Member

Choose a reason for hiding this comment

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

see above

// Now, turn on the background goroutine that constantly reads from the chip and stores data in
// the object we created.
sensor.activeBackgroundWorkers.Add(1)
utils.PanicCapturingGo(sensor.pollData)
Copy link
Member

Choose a reason for hiding this comment

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

define the function here, we prefer to do it this way so no one else can call this function with later potential work on this driver

Copy link
Member Author

Choose a reason for hiding this comment

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

Subtle but wise choice! I should also do that in the MPU code; should that be in this same PR or a separate PR?

Copy link
Member Author

Choose a reason for hiding this comment

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

Nicolas and I discussed: it can go in this same PR. I've made similar changes re: helper functions in utils/math.go and reordering the select cases for the MPU6050 code, too.

}

// A helper function: takes 2 bytes and reinterprets them as a little-endian signed integer.
func toSignedValue(data []byte) int {
Copy link
Member

Choose a reason for hiding this comment

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

can you use one of the utils/math.go function instead? if it doesn't exists might make sense to add it

// The chip starts out in standby mode. Set it to measurement mode so we can get data from it.
// To do this, we set the Power Control register (0x2D) to turn on the 8's bit.
err = sensor.writeByte(ctx, 0x2D, 0x08)
if err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

nit: if err := sensor.writeByte(ctx, 0x2D, 0x08); err != nil {
....
}

rawConfig config.Component,
logger golog.Logger,
) (movementsensor.MovementSensor, error) {
cfg := rawConfig.ConvertedAttributes.(*AttrConfig)
Copy link
Member

Choose a reason for hiding this comment

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

cfg,ok := rawConfig.ConvertedAttributes.(*AttrConfig)


for {
select {
case <-adxl.backgroundContext.Done():
Copy link
Member

Choose a reason for hiding this comment

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

nit: prefer to have this case last

address = 0x53
}

backgroundContext, cancelFunc := context.WithCancel(ctx)
Copy link
Member

Choose a reason for hiding this comment

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

nit: cancelContext

Copy link
Member

@npmenard npmenard left a comment

Choose a reason for hiding this comment

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

Done with first pass, couple of comments but otherwise LGTM

@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Dec 8, 2022
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Dec 8, 2022
@penguinland
Copy link
Member Author

Excellent feedback! I think I've addressed it all.

@penguinland penguinland requested a review from npmenard December 8, 2022 20:30
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Dec 8, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Dec 8, 2022

Code Coverage

Package Line Rate Delta Health
go.viam.com/rdk/components/arm 61% 0.00%
go.viam.com/rdk/components/arm/universalrobots 13% 0.00%
go.viam.com/rdk/components/arm/xarm 2% 0.00%
go.viam.com/rdk/components/arm/yahboom 7% 0.00%
go.viam.com/rdk/components/audioinput 55% 0.00%
go.viam.com/rdk/components/base 67% 0.00%
go.viam.com/rdk/components/base/agilex 62% 0.00%
go.viam.com/rdk/components/base/boat 41% 0.00%
go.viam.com/rdk/components/base/wheeled 76% 0.00%
go.viam.com/rdk/components/board 68% 0.00%
go.viam.com/rdk/components/board/arduino 10% 0.00%
go.viam.com/rdk/components/board/commonsysfs 47% 0.00%
go.viam.com/rdk/components/board/fake 38% 0.00%
go.viam.com/rdk/components/board/numato 19% 0.00%
go.viam.com/rdk/components/board/pi 50% 0.00%
go.viam.com/rdk/components/camera 65% 0.00%
go.viam.com/rdk/components/camera/fake 67% 0.00%
go.viam.com/rdk/components/camera/ffmpeg 77% 0.00%
go.viam.com/rdk/components/camera/transformpipeline 78% +0.33%
go.viam.com/rdk/components/camera/videosource 55% -0.51%
go.viam.com/rdk/components/encoder/fake 77% 0.00%
go.viam.com/rdk/components/gantry 68% 0.00%
go.viam.com/rdk/components/gantry/multiaxis 84% 0.00%
go.viam.com/rdk/components/gantry/oneaxis 86% 0.00%
go.viam.com/rdk/components/generic 83% 0.00%
go.viam.com/rdk/components/gripper 80% 0.00%
go.viam.com/rdk/components/input 87% 0.00%
go.viam.com/rdk/components/input/gpio 83% 0.00%
go.viam.com/rdk/components/motor 81% 0.00%
go.viam.com/rdk/components/motor/dimensionengineering 63% 0.00%
go.viam.com/rdk/components/motor/dmc4000 69% 0.00%
go.viam.com/rdk/components/motor/fake 57% 0.00%
go.viam.com/rdk/components/motor/gpio 63% -0.43%
go.viam.com/rdk/components/motor/gpiostepper 56% -0.59%
go.viam.com/rdk/components/motor/tmcstepper 62% 0.00%
go.viam.com/rdk/components/movementsensor 75% 0.00%
go.viam.com/rdk/components/movementsensor/cameramono 40% 0.00%
go.viam.com/rdk/components/movementsensor/gpsnmea 37% 0.00%
go.viam.com/rdk/components/movementsensor/gpsrtk 28% 0.00%
go.viam.com/rdk/components/posetracker 86% 0.00%
go.viam.com/rdk/components/sensor 86% 0.00%
go.viam.com/rdk/components/sensor/ultrasonic 34% 0.00%
go.viam.com/rdk/components/servo 78% 0.00%
go.viam.com/rdk/components/servo/gpio 71% 0.00%
go.viam.com/rdk/config 76% -0.08%
go.viam.com/rdk/control 57% 0.00%
go.viam.com/rdk/data 77% -0.81%
go.viam.com/rdk/grpc 25% 0.00%
go.viam.com/rdk/ml 67% 0.00%
go.viam.com/rdk/ml/inference 70% 0.00%
go.viam.com/rdk/motionplan 68% +8.67%
go.viam.com/rdk/octree 93% 0.00%
go.viam.com/rdk/operation 82% 0.00%
go.viam.com/rdk/pointcloud 71% 0.00%
go.viam.com/rdk/protoutils 62% 0.00%
go.viam.com/rdk/referenceframe 69% 0.00%
go.viam.com/rdk/registry 88% 0.00%
go.viam.com/rdk/resource 83% 0.00%
go.viam.com/rdk/rimage 78% 0.00%
go.viam.com/rdk/rimage/depthadapter 94% 0.00%
go.viam.com/rdk/rimage/transform 73% 0.00%
go.viam.com/rdk/rimage/transform/cmd/extrinsic_calibration 67% 0.00%
go.viam.com/rdk/robot 93% 0.00%
go.viam.com/rdk/robot/client 79% 0.00%
go.viam.com/rdk/robot/framesystem 68% 0.00%
go.viam.com/rdk/robot/impl 81% 0.00%
go.viam.com/rdk/robot/server 58% 0.00%
go.viam.com/rdk/robot/web 60% 0.00%
go.viam.com/rdk/robot/web/stream 87% 0.00%
go.viam.com/rdk/services/armremotecontrol 71% 0.00%
go.viam.com/rdk/services/armremotecontrol/builtin 25% 0.00%
go.viam.com/rdk/services/baseremotecontrol 71% 0.00%
go.viam.com/rdk/services/baseremotecontrol/builtin 66% 0.00%
go.viam.com/rdk/services/datamanager 65% 0.00%
go.viam.com/rdk/services/datamanager/builtin 80% 0.00%
go.viam.com/rdk/services/datamanager/datacapture 21% 0.00%
go.viam.com/rdk/services/datamanager/datasync 72% -0.58%
go.viam.com/rdk/services/motion 63% 0.00%
go.viam.com/rdk/services/motion/builtin 88% 0.00%
go.viam.com/rdk/services/navigation 54% 0.00%
go.viam.com/rdk/services/sensors 77% 0.00%
go.viam.com/rdk/services/sensors/builtin 97% 0.00%
go.viam.com/rdk/services/shell 14% 0.00%
go.viam.com/rdk/services/slam 84% 0.00%
go.viam.com/rdk/services/slam/builtin 72% 0.00%
go.viam.com/rdk/services/vision 80% 0.00%
go.viam.com/rdk/services/vision/builtin 74% 0.00%
go.viam.com/rdk/spatialmath 85% 0.00%
go.viam.com/rdk/subtype 96% 0.00%
go.viam.com/rdk/utils 71% +0.11%
go.viam.com/rdk/vision 26% 0.00%
go.viam.com/rdk/vision/chess 80% 0.00%
go.viam.com/rdk/vision/delaunay 87% 0.00%
go.viam.com/rdk/vision/keypoints 92% 0.00%
go.viam.com/rdk/vision/objectdetection 82% 0.00%
go.viam.com/rdk/vision/odometry 60% 0.00%
go.viam.com/rdk/vision/odometry/cmd 0% 0.00%
go.viam.com/rdk/vision/segmentation 49% 0.00%
go.viam.com/rdk/web/server 26% 0.00%
Summary 66% (19745 / 29975) +0.54%

Copy link
Member

@npmenard npmenard left a comment

Choose a reason for hiding this comment

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

LGTM!

@penguinland
Copy link
Member Author

Tried one last time at my desk: both the ADXL and MPU chips still work fine!

@penguinland penguinland merged commit a0f7b4c into viamrobotics:main Dec 8, 2022
@penguinland penguinland deleted the adxl345 branch December 8, 2022 22:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
safe to test This pull request is marked safe to test from a trusted zone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants