From cbb9a7c209ad606caabcd912a23bee327d609b74 Mon Sep 17 00:00:00 2001 From: Ray Bjorkman Date: Wed, 8 Jan 2025 15:32:38 -0500 Subject: [PATCH 1/2] add wrappers to a different package --- .../builtin/experimental/do_wrappers.go | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 services/motion/builtin/experimental/do_wrappers.go diff --git a/services/motion/builtin/experimental/do_wrappers.go b/services/motion/builtin/experimental/do_wrappers.go new file mode 100644 index 00000000000..e3e5043ba29 --- /dev/null +++ b/services/motion/builtin/experimental/do_wrappers.go @@ -0,0 +1,41 @@ +package experimental + +import ( + "context" + "errors" + + "github.com/go-viper/mapstructure/v2" + "go.viam.com/rdk/motionplan" + "go.viam.com/rdk/services/motion" + "go.viam.com/rdk/services/motion/builtin" + "go.viam.com/utils/web/protojson" +) + +// DoPlan is a helper function to wrap doPlan (a utility inside DoCommand) with types that are easier to work with. +func DoPlan(ctx context.Context, ms motion.Service, req motion.MoveReq) (motionplan.Trajectory, error) { + proto, err := req.ToProto(ms.Name().Name) + if err != nil { + return nil, err + } + bytes, err := protojson.Marshal(proto) + if err != nil { + return nil, err + } + resp, err := ms.DoCommand(ctx, map[string]interface{}{builtin.DoPlan: string(bytes)}) + if err != nil { + return nil, err + } + respMap, ok := resp[builtin.DoPlan] + if !ok { + return nil, errors.New("could not find Trajectory in DoCommand response") + } + var trajectory motionplan.Trajectory + err = mapstructure.Decode(respMap, &trajectory) + return trajectory, err +} + +// DoExecute is a helper function to wrap doExecute (a utility inside DoCommand) with types that are easier to work with. +func DoExecute(ctx context.Context, ms motion.Service, traj motionplan.Trajectory) error { + _, err := ms.DoCommand(ctx, map[string]interface{}{builtin.DoExecute: traj}) + return err +} From 73628e5648dd6181dc2bd3ee29c0c5b8dc73b5f2 Mon Sep 17 00:00:00 2001 From: Ray Bjorkman Date: Wed, 8 Jan 2025 17:00:57 -0500 Subject: [PATCH 2/2] fix typo --- services/motion/builtin/builtin_test.go | 2 +- services/motion/builtin/experimental/do_wrappers.go | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/services/motion/builtin/builtin_test.go b/services/motion/builtin/builtin_test.go index ac17b017612..95e2eb00b01 100644 --- a/services/motion/builtin/builtin_test.go +++ b/services/motion/builtin/builtin_test.go @@ -1303,7 +1303,7 @@ func TestDoCommand(t *testing.T) { test.That(t, len(trajectory), test.ShouldEqual, 2) }) - t.Run("DoExectute", func(t *testing.T) { + t.Run("DoExecute", func(t *testing.T) { ms, teardown := setupMotionServiceFromConfig(t, "../data/moving_arm.json") defer teardown() diff --git a/services/motion/builtin/experimental/do_wrappers.go b/services/motion/builtin/experimental/do_wrappers.go index e3e5043ba29..8b623ab9bf1 100644 --- a/services/motion/builtin/experimental/do_wrappers.go +++ b/services/motion/builtin/experimental/do_wrappers.go @@ -1,3 +1,5 @@ +// Package experimental contains functions which extend the motion/builtin resource with additional functionality +// the code contained within it comes with no stability guarantees and is likely to change often package experimental import ( @@ -5,13 +7,14 @@ import ( "errors" "github.com/go-viper/mapstructure/v2" + "google.golang.org/protobuf/encoding/protojson" + "go.viam.com/rdk/motionplan" "go.viam.com/rdk/services/motion" "go.viam.com/rdk/services/motion/builtin" - "go.viam.com/utils/web/protojson" ) -// DoPlan is a helper function to wrap doPlan (a utility inside DoCommand) with types that are easier to work with. +// DoPlan is a helper function to wrap DoPlan (a utility inside builtin/DoCommand) with types that are easier to work with. func DoPlan(ctx context.Context, ms motion.Service, req motion.MoveReq) (motionplan.Trajectory, error) { proto, err := req.ToProto(ms.Name().Name) if err != nil { @@ -34,7 +37,7 @@ func DoPlan(ctx context.Context, ms motion.Service, req motion.MoveReq) (motionp return trajectory, err } -// DoExecute is a helper function to wrap doExecute (a utility inside DoCommand) with types that are easier to work with. +// DoExecute is a helper function to wrap DoExecute (a utility inside builtin/DoCommand) with types that are easier to work with. func DoExecute(ctx context.Context, ms motion.Service, traj motionplan.Trajectory) error { _, err := ms.DoCommand(ctx, map[string]interface{}{builtin.DoExecute: traj}) return err