|
| 1 | +// Package arm contains a gRPC based arm client. |
| 2 | +package arm |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + "sync" |
| 7 | + |
| 8 | + commonpb "go.viam.com/api/common/v1" |
| 9 | + pb "go.viam.com/api/component/arm/v1" |
| 10 | + "go.viam.com/utils/protoutils" |
| 11 | + "go.viam.com/utils/rpc" |
| 12 | + |
| 13 | + "go.viam.com/rdk/logging" |
| 14 | + rprotoutils "go.viam.com/rdk/protoutils" |
| 15 | + "go.viam.com/rdk/referenceframe" |
| 16 | + "go.viam.com/rdk/resource" |
| 17 | + "go.viam.com/rdk/spatialmath" |
| 18 | +) |
| 19 | + |
| 20 | +// client implements ArmServiceClient. |
| 21 | +type client struct { |
| 22 | + resource.Named |
| 23 | + resource.TriviallyReconfigurable |
| 24 | + resource.TriviallyCloseable |
| 25 | + name string |
| 26 | + client pb.ArmServiceClient |
| 27 | + logger logging.Logger |
| 28 | + |
| 29 | + mu sync.Mutex |
| 30 | + model referenceframe.Model |
| 31 | +} |
| 32 | + |
| 33 | +// NewClientFromConn constructs a new Client from connection passed in. |
| 34 | +func NewClientFromConn( |
| 35 | + ctx context.Context, |
| 36 | + conn rpc.ClientConn, |
| 37 | + remoteName string, |
| 38 | + name resource.Name, |
| 39 | + logger logging.Logger, |
| 40 | +) (Arm, error) { |
| 41 | + pbClient := pb.NewArmServiceClient(conn) |
| 42 | + return &client{ |
| 43 | + Named: name.PrependRemote(remoteName).AsNamed(), |
| 44 | + name: name.ShortName(), |
| 45 | + client: pbClient, |
| 46 | + logger: logger, |
| 47 | + }, nil |
| 48 | +} |
| 49 | + |
| 50 | +func (c *client) EndPosition(ctx context.Context, extra map[string]interface{}) (spatialmath.Pose, error) { |
| 51 | + ext, err := protoutils.StructToStructPb(extra) |
| 52 | + if err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + resp, err := c.client.GetEndPosition(ctx, &pb.GetEndPositionRequest{ |
| 56 | + Name: c.name, |
| 57 | + Extra: ext, |
| 58 | + }) |
| 59 | + if err != nil { |
| 60 | + return nil, err |
| 61 | + } |
| 62 | + return spatialmath.NewPoseFromProtobuf(resp.Pose), nil |
| 63 | +} |
| 64 | + |
| 65 | +func (c *client) MoveToPosition(ctx context.Context, pose spatialmath.Pose, extra map[string]interface{}) error { |
| 66 | + ext, err := protoutils.StructToStructPb(extra) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + if pose == nil { |
| 71 | + c.logger.Warnf("%s MoveToPosition: pose parameter is nil", c.name) |
| 72 | + } |
| 73 | + _, err = c.client.MoveToPosition(ctx, &pb.MoveToPositionRequest{ |
| 74 | + Name: c.name, |
| 75 | + To: spatialmath.PoseToProtobuf(pose), |
| 76 | + Extra: ext, |
| 77 | + }) |
| 78 | + return err |
| 79 | +} |
| 80 | + |
| 81 | +func (c *client) MoveToJointPositions(ctx context.Context, positions []referenceframe.Input, extra map[string]interface{}) error { |
| 82 | + ext, err := protoutils.StructToStructPb(extra) |
| 83 | + if err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + m, err := c.Kinematics(ctx) |
| 87 | + if err != nil { |
| 88 | + warnKinematicsUnsafe(ctx, c.logger, err) |
| 89 | + } |
| 90 | + jp, err := referenceframe.JointPositionsFromInputs(m, positions) |
| 91 | + if err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + _, err = c.client.MoveToJointPositions(ctx, &pb.MoveToJointPositionsRequest{ |
| 95 | + Name: c.name, |
| 96 | + Positions: jp, |
| 97 | + Extra: ext, |
| 98 | + }) |
| 99 | + return err |
| 100 | +} |
| 101 | + |
| 102 | +func (c *client) MoveThroughJointPositions( |
| 103 | + ctx context.Context, |
| 104 | + positions [][]referenceframe.Input, |
| 105 | + options *MoveOptions, |
| 106 | + extra map[string]interface{}, |
| 107 | +) error { |
| 108 | + ext, err := protoutils.StructToStructPb(extra) |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + if positions == nil { |
| 113 | + c.logger.Warnf("%s MoveThroughJointPositions: position argument is nil", c.name) |
| 114 | + } |
| 115 | + allJPs := make([]*pb.JointPositions, 0, len(positions)) |
| 116 | + m, err := c.Kinematics(ctx) |
| 117 | + if err != nil { |
| 118 | + warnKinematicsUnsafe(ctx, c.logger, err) |
| 119 | + } |
| 120 | + for _, position := range positions { |
| 121 | + jp, err := referenceframe.JointPositionsFromInputs(m, position) |
| 122 | + if err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + allJPs = append(allJPs, jp) |
| 126 | + } |
| 127 | + req := &pb.MoveThroughJointPositionsRequest{ |
| 128 | + Name: c.name, |
| 129 | + Positions: allJPs, |
| 130 | + Extra: ext, |
| 131 | + } |
| 132 | + if options != nil { |
| 133 | + req.Options = options.toProtobuf() |
| 134 | + } |
| 135 | + _, err = c.client.MoveThroughJointPositions(ctx, req) |
| 136 | + return err |
| 137 | +} |
| 138 | + |
| 139 | +func (c *client) JointPositions(ctx context.Context, extra map[string]interface{}) ([]referenceframe.Input, error) { |
| 140 | + ext, err := protoutils.StructToStructPb(extra) |
| 141 | + if err != nil { |
| 142 | + return nil, err |
| 143 | + } |
| 144 | + resp, err := c.client.GetJointPositions(ctx, &pb.GetJointPositionsRequest{ |
| 145 | + Name: c.name, |
| 146 | + Extra: ext, |
| 147 | + }) |
| 148 | + if err != nil { |
| 149 | + return nil, err |
| 150 | + } |
| 151 | + m, err := c.Kinematics(ctx) |
| 152 | + if err != nil { |
| 153 | + warnKinematicsUnsafe(ctx, c.logger, err) |
| 154 | + } |
| 155 | + return referenceframe.InputsFromJointPositions(m, resp.Positions) |
| 156 | +} |
| 157 | + |
| 158 | +func (c *client) Stop(ctx context.Context, extra map[string]interface{}) error { |
| 159 | + ext, err := protoutils.StructToStructPb(extra) |
| 160 | + if err != nil { |
| 161 | + return err |
| 162 | + } |
| 163 | + _, err = c.client.Stop(ctx, &pb.StopRequest{ |
| 164 | + Name: c.name, |
| 165 | + Extra: ext, |
| 166 | + }) |
| 167 | + return err |
| 168 | +} |
| 169 | + |
| 170 | +func (c *client) Kinematics(ctx context.Context) (referenceframe.Model, error) { |
| 171 | + c.mu.Lock() |
| 172 | + defer c.mu.Unlock() |
| 173 | + |
| 174 | + // for performance we cache the model after building it once, and can quickly return if its already been created. |
| 175 | + if c.model == nil { |
| 176 | + resp, err := c.client.GetKinematics(ctx, &commonpb.GetKinematicsRequest{Name: c.name}) |
| 177 | + if err != nil { |
| 178 | + return nil, err |
| 179 | + } |
| 180 | + model, err := referenceframe.KinematicModelFromProtobuf(c.name, resp) |
| 181 | + if err != nil { |
| 182 | + return nil, err |
| 183 | + } |
| 184 | + c.model = model |
| 185 | + } |
| 186 | + return c.model, nil |
| 187 | +} |
| 188 | + |
| 189 | +func (c *client) CurrentInputs(ctx context.Context) ([]referenceframe.Input, error) { |
| 190 | + return c.JointPositions(ctx, nil) |
| 191 | +} |
| 192 | + |
| 193 | +func (c *client) GoToInputs(ctx context.Context, inputSteps ...[]referenceframe.Input) error { |
| 194 | + return c.MoveThroughJointPositions(ctx, inputSteps, nil, nil) |
| 195 | +} |
| 196 | + |
| 197 | +func (c *client) DoCommand(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error) { |
| 198 | + return rprotoutils.DoFromResourceClient(ctx, c.client, c.name, cmd) |
| 199 | +} |
| 200 | + |
| 201 | +func (c *client) IsMoving(ctx context.Context) (bool, error) { |
| 202 | + resp, err := c.client.IsMoving(ctx, &pb.IsMovingRequest{Name: c.name}) |
| 203 | + if err != nil { |
| 204 | + return false, err |
| 205 | + } |
| 206 | + return resp.IsMoving, nil |
| 207 | +} |
| 208 | + |
| 209 | +func (c *client) Geometries(ctx context.Context, extra map[string]interface{}) ([]spatialmath.Geometry, error) { |
| 210 | + ext, err := protoutils.StructToStructPb(extra) |
| 211 | + if err != nil { |
| 212 | + return nil, err |
| 213 | + } |
| 214 | + resp, err := c.client.GetGeometries(ctx, &commonpb.GetGeometriesRequest{ |
| 215 | + Name: c.name, |
| 216 | + Extra: ext, |
| 217 | + }) |
| 218 | + if err != nil { |
| 219 | + return nil, err |
| 220 | + } |
| 221 | + return spatialmath.NewGeometriesFromProto(resp.GetGeometries()) |
| 222 | +} |
| 223 | + |
| 224 | +// warnKinematicsUnsafe is a helper function to warn the user that no kinematics have been supplied for the conversion between |
| 225 | +// joints space and Inputs. The assumption we are making here is safe for any arm that has only revolute joints (true for most |
| 226 | +// commercially available arms) and will only come into play if the kinematics for the arm have not been cached successfully yet. |
| 227 | +// The other assumption being made here is that it will be annoying for new users implementing an arm module to not be able to move their |
| 228 | +// arm until the kinematics have been supplied. This log message will be very noisy as it will be logged whenever kinematics are not found |
| 229 | +// so we are hoping that they will want to do things the correct way and supply kinematics to quiet it. |
| 230 | +func warnKinematicsUnsafe(ctx context.Context, logger logging.Logger, err error) { |
| 231 | + logger.CWarnw( |
| 232 | + ctx, |
| 233 | + "error getting model for arm; making the assumption that joints are revolute and that their positions are specified in degrees", |
| 234 | + "err", |
| 235 | + err, |
| 236 | + ) |
| 237 | +} |
0 commit comments