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-1648] Use rtkgps as a model #1742

Conversation

randhid
Copy link
Member

@randhid randhid commented Jan 6, 2023

Mea culpa, when refactoring I used the wrong creator function for a model. Fixed.

Also added read locks for cameramono.

@viambot viambot added the safe to test This pull request is marked safe to test from a trusted zone label Jan 6, 2023
@@ -133,7 +133,7 @@ func init() {
cfg config.Component,
logger golog.Logger,
) (interface{}, error) {
return newRTKStation(ctx, deps, cfg, logger)
return newRTKMovementSensor(ctx, deps, cfg, logger)
Copy link
Contributor

Choose a reason for hiding this comment

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

nice. We should add a test that catches this if it were still broken.

Copy link
Member Author

@randhid randhid Jan 6, 2023

Choose a reason for hiding this comment

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

I've added a guard, not totally convinced the pattern in base_test.go helps here, we're missing the concept of a movementsensor.Local. There is a way to rewrite it so that the creation function is based on a switch case depending on the resource name, but I'm not sure that's a good way to do it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ugh you're right and I'm sorry I led you astray there. The test should be more like https://github.com/viamrobotics/rdk/blob/main/robot/session_test.go#L156 where we create a config, refer to the model, construct the robot, and get the component out of it. That's fairly end to end. You'll want to move this test into a new file with package gpsrtk_test so that you can import robot. Sorry!

Copy link
Member

@penguinland penguinland left a comment

Choose a reason for hiding this comment

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

Lock the mutex when you write to your internal state, demonstrate that this works with actual GPS hardware, and things LGTM!

The real proof that this is right is when you can build a robot that uses this component, and have it give back your (at least approximate) location. Does that work?

I tried to do it during the bughunt, and got bogged down getting correction data. If it would help, I've got an account with the NYS Spatial Reference Network and would be happy to share my credentials (or you can make your own: it's free). but I never figured out how to plumb those credentials into the rest of the GPS (possibly because I was missing the current PR! 🙃 ), so don't have great advice on that.

@@ -118,6 +118,7 @@ type cameramono struct {
logger golog.Logger
result result
stream gostream.VideoStream
mu sync.RWMutex
Copy link
Member

Choose a reason for hiding this comment

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

We lock the mutex whenever we read our state, but not when we write it. which means the mutex isn't yet preventing race conditions. Lock it in a few more places, too.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added, let me know if I've missed any.

Copy link
Member

Choose a reason for hiding this comment

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

tl/dr: see the very last line.

Part of me says "any time anyone reads or writes any state within the struct (including result, stream, motion, and lastErr), they should lock the mutex." but that's overly broad:

  • stream is only used in the constructor (no chance of race conditions), the background goroutine, and Close(). and if Close() causes trouble with the background thread, that's okay because we're closing things and it'll never be used again. So, it's probably fine that this isn't protected by the mutex.
  • motion is only used by the background goroutine and extractMovementFromOdometer(), which is only called from the background goroutine. So, no chance of race conditions here, either. Part of me wonders if it can be made a local variable in backgroundWorker() instead of a field in the struct, but that's probably out of scope for the current PR.
  • There will only be race conditions around lastErr if copying and assigning it are not atomic. I don't know enough about Go's data model to know if that's a good assumption. For peace of mind, I'd protect lastErr with the mutex just in case.
  • Every piece of result except result.dt is protected by the mutex. However, result.dt appears to be written in one place and never read, so not protecting it seems fine. Maybe it can be removed entirely? but that's probably out of scope for the current PR.

So, I'd lock the mutex whenever reading or writing lastErr (and maybe even that is unnecessary), and the rest LGTM!

Copy link
Member Author

Choose a reason for hiding this comment

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

results.dt is in the struct in case we ever need to mutate time for the algorithm long term, will lock if ever. And yes other things can be locked/unlocked without a defer but I feel unnecessary.

Will change lastErr mutex.

@penguinland
Copy link
Member

penguinland commented Jan 6, 2023

...I wonder if the mutex added to the cameramono component was intended for a different PR: it seems unrelated to the rtkgps model, and only half-implemented. OTOH, maybe it goes in here: both changes (mutex in cameramono, constructor in rtkgps) seem like small self-contained tweaks to pre-existing components.

edit: no, you clarified this in the PR description, and I need to read better! 😅

@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 Jan 6, 2023
@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 Jan 6, 2023
@github-actions

This comment was marked as resolved.

@randhid
Copy link
Member Author

randhid commented Jan 6, 2023

Lock the mutex when you write to your internal state, demonstrate that this works with actual GPS hardware, and things LGTM!

The real proof that this is right is when you can build a robot that uses this component, and have it give back your (at least approximate) location. Does that work?

I tried to do it during the bughunt, and got bogged down getting correction data. If it would help, I've got an account with the NYS Spatial Reference Network and would be happy to share my credentials (or you can make your own: it's free). but I never figured out how to plumb those credentials into the rest of the GPS (possibly because I was missing the current PR! 🙃 ), so don't have great advice on that.

Yes. The other model only cares about correction data, this model is a GPS floating around with the correction data resulting in coordinates with sub 2cm accuracy. The constructor function was copied wrong when I moved models around.

@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 Jan 6, 2023
@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 Jan 7, 2023
@randhid randhid requested a review from edaniels January 9, 2023 15:19
test.That(t, err, test.ShouldBeNil)

_, err = movementsensor.FromRobot(r, "station")
test.That(t, err.Error(), test.ShouldContainSubstring, gpsrtk.ErrStationValidation.Error())
Copy link
Member Author

Choose a reason for hiding this comment

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

Checked that this breaks with the wrong creator type. Sufficient or should I spend more time on this and figure out how to mock the GPS, ntrip client, serial port, i2c port, and have this test consume them?

Copy link
Contributor

Choose a reason for hiding this comment

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

good!

Copy link
Contributor

@edaniels edaniels left a comment

Choose a reason for hiding this comment

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

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 Jan 10, 2023
@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 Jan 10, 2023
@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 Jan 10, 2023
@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 Jan 10, 2023
@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 Jan 10, 2023
@github-actions
Copy link
Contributor

Code Coverage

Package Line Rate Delta Health
go.viam.com/rdk/components/arm 56% 0.00%
go.viam.com/rdk/components/arm/universalrobots 41% 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.34%
go.viam.com/rdk/components/base 63% 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/align 63% 0.00%
go.viam.com/rdk/components/camera/fake 67% 0.00%
go.viam.com/rdk/components/camera/ffmpeg 76% -1.43%
go.viam.com/rdk/components/camera/transformpipeline 78% -0.97%
go.viam.com/rdk/components/camera/videosource 49% -0.63%
go.viam.com/rdk/components/encoder 4% 0.00%
go.viam.com/rdk/components/encoder/fake 76% 0.00%
go.viam.com/rdk/components/gantry 60% 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 68% 0.00%
go.viam.com/rdk/components/input 87% 0.00%
go.viam.com/rdk/components/input/gpio 84% 0.00%
go.viam.com/rdk/components/motor 77% 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 64% 0.00%
go.viam.com/rdk/components/motor/gpiostepper 57% +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 46% +5.90%
go.viam.com/rdk/components/movementsensor/gpsnmea 36% 0.00%
go.viam.com/rdk/components/movementsensor/gpsrtk 28% +0.33%
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 32% 0.00%
go.viam.com/rdk/components/servo 68% 0.00%
go.viam.com/rdk/components/servo/gpio 71% 0.00%
go.viam.com/rdk/config 76% 0.00%
go.viam.com/rdk/control 57% 0.00%
go.viam.com/rdk/data 79% 0.00%
go.viam.com/rdk/examples/customresources/demos/complexmodule 0% 0.00%
go.viam.com/rdk/examples/customresources/demos/remoteserver 0% 0.00%
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/module 63% 0.00%
go.viam.com/rdk/module/modmanager 79% 0.00%
go.viam.com/rdk/motionplan 59% -0.10%
go.viam.com/rdk/octree 98% 0.00%
go.viam.com/rdk/operation 82% 0.00%
go.viam.com/rdk/pointcloud 72% 0.00%
go.viam.com/rdk/protoutils 59% 0.00%
go.viam.com/rdk/referenceframe 71% 0.00%
go.viam.com/rdk/registry 89% 0.00%
go.viam.com/rdk/resource 84% 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 85% 0.00%
go.viam.com/rdk/robot/client 81% 0.00%
go.viam.com/rdk/robot/framesystem 65% 0.00%
go.viam.com/rdk/robot/impl 80% 0.00%
go.viam.com/rdk/robot/server 55% 0.00%
go.viam.com/rdk/robot/web 62% 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 52% 0.00%
go.viam.com/rdk/services/baseremotecontrol 71% 0.00%
go.viam.com/rdk/services/baseremotecontrol/builtin 79% 0.00%
go.viam.com/rdk/services/datamanager 79% 0.00%
go.viam.com/rdk/services/datamanager/builtin 78% -0.91%
go.viam.com/rdk/services/datamanager/datacapture 70% 0.00%
go.viam.com/rdk/services/datamanager/datasync 0% 0.00%
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 53% 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 73% 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/session 97% 0.00%
go.viam.com/rdk/spatialmath 86% 0.00%
go.viam.com/rdk/subtype 92% 0.00%
go.viam.com/rdk/utils 72% 0.00%
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 25% 0.00%
Summary 65% (20910 / 32235) 0.00%

@randhid randhid merged commit dc50a18 into viamrobotics:main Jan 10, 2023
@randhid randhid deleted the RSDK-1648-get-the-gps-rtk-sensor-to-be-used-as-a-component branch January 10, 2023 21:22
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.

4 participants