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

add a wrapper around motion/builtin DoCommand utilities #4384

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion services/motion/builtin/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
44 changes: 44 additions & 0 deletions services/motion/builtin/experimental/do_wrappers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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 (
"context"
"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"
)

// 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 {
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 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
}
Loading