@@ -274,25 +274,16 @@ def _setup_mink(self):
274274 self .damping = 1e-3
275275 self .rate_limiter = RateLimiter (frequency = 1000 , warn = False )
276276
277- def solve_ik (self ,
278- left_target_position = None ,
279- left_target_orientation = None ,
280- right_target_position = None ,
281- right_target_orientation = None ,
282- limit_velocity = False
283- ):
284- """
285- Solve IK for specified arm(s) and return final joint positions
277+ def compute_simple_ik (self , start_joint , target_pose , arm_id = "left_arm" ):
278+ """Compute inverse kinematics using Mink.
286279
287280 Args:
288- left_target_position: Target position for left arm [x, y, z]
289- left_target_orientation: Target orientation for left arm as quaternion [x, y, z, w]
290- right_target_position: Target position for right arm [x, y, z]
291- right_target_orientation: Target orientation for right arm as quaternion [x, y, z, w]
292- limit_velocity: Whether to apply velocity limits
281+ start_joint: Initial joint configuration (not used in current implementation)
282+ target_pose: Target pose [x, y, z, qx, qy, qz, qw] in world coordinates
283+ arm_id: The ID of the arm, either "left_arm" or "right_arm"
293284
294285 Returns:
295- Dictionary containing final joint positions for each module
286+ Target joint configuration for the specified arm
296287 """
297288 # Set target for chassis
298289 chassis_target = mink .SE3 .from_rotation_and_translation (
@@ -313,26 +304,27 @@ def solve_ik(self,
313304 # Set target for posture
314305 self .tasks ["posture" ].set_target_from_configuration (self .mink_config )
315306
316- # Set targets for left and right arm
317- if left_target_position is not None and left_target_orientation is not None :
307+ # Extract position and orientation from target pose
308+ target_position = target_pose [:3 ]
309+ target_orientation = target_pose [3 :7 ] # [qx, qy, qz, qw]
310+
311+ # Set target for the specified arm
312+ if arm_id == "left_arm" :
318313 target = mink .SE3 .from_rotation_and_translation (
319- rotation = mink .SO3 (wxyz = xyzw_to_wxyz (left_target_orientation )),
320- translation = left_target_position
314+ rotation = mink .SO3 (wxyz = xyzw_to_wxyz (target_orientation )),
315+ translation = target_position
321316 )
322317 self .tasks ["left_arm" ].set_target (target )
323- if right_target_position is not None and right_target_orientation is not None :
318+ tasks = [self .tasks ["torso" ], self .tasks ["posture" ], self .tasks ["chassis" ], self .tasks ["left_arm" ]]
319+ elif arm_id == "right_arm" :
324320 target = mink .SE3 .from_rotation_and_translation (
325- rotation = mink .SO3 (wxyz = xyzw_to_wxyz (right_target_orientation )),
326- translation = right_target_position
321+ rotation = mink .SO3 (wxyz = xyzw_to_wxyz (target_orientation )),
322+ translation = target_position
327323 )
328324 self .tasks ["right_arm" ].set_target (target )
329-
330- # Collect all tasks
331- tasks = [self .tasks ["torso" ], self .tasks ["posture" ], self .tasks ["chassis" ]]
332- if left_target_position is not None and left_target_orientation is not None :
333- tasks .append (self .tasks ["left_arm" ])
334- if right_target_position is not None and right_target_orientation is not None :
335- tasks .append (self .tasks ["right_arm" ])
325+ tasks = [self .tasks ["torso" ], self .tasks ["posture" ], self .tasks ["chassis" ], self .tasks ["right_arm" ]]
326+ else :
327+ raise ValueError (f"Invalid arm_id: { arm_id } " )
336328
337329 # Iterative IK solving to get final positions
338330 dt = 1e-3
@@ -348,47 +340,30 @@ def solve_ik(self,
348340 dt ,
349341 self .solver ,
350342 self .damping ,
351- limits = [self .velocity_limit ] if limit_velocity else None
343+ limits = [self .velocity_limit ] if False else None
352344 )
353345
354346 # Integrate to update configuration
355347 self .mink_config .integrate_inplace (vel , dt )
356348
357- # Check convergence for left arm
358- if left_target_position is not None and left_target_orientation is not None :
359- error = self .tasks ["left_arm" ].compute_error (self .mink_config )
360- pos_error = np .linalg .norm (error [:3 ])
361- ori_error = np .linalg .norm (error [3 :])
362- if pos_error < position_tolerance and ori_error < orientation_tolerance :
363- break
364-
365- # Check convergence for right arm
366- if right_target_position is not None and right_target_orientation is not None :
367- error = self .tasks ["right_arm" ].compute_error (self .mink_config )
368- pos_error = np .linalg .norm (error [:3 ])
369- ori_error = np .linalg .norm (error [3 :])
370- if pos_error < position_tolerance and ori_error < orientation_tolerance :
371- break
349+ # Check convergence for the specified arm
350+ error = self .tasks [arm_id ].compute_error (self .mink_config )
351+ pos_error = np .linalg .norm (error [:3 ])
352+ ori_error = np .linalg .norm (error [3 :])
353+ if pos_error < position_tolerance and ori_error < orientation_tolerance :
354+ break
372355
373356 # Get final joint positions
374357 joint_positions = self .mink_config .q
375358
376- # Extract joint positions for each module
377- left_arm_joint_indexes = self .interface .left_arm .joint_indexes
378- left_arm_joint_positions = joint_positions [left_arm_joint_indexes ]
379- right_arm_joint_indexes = self .interface .right_arm .joint_indexes
380- right_arm_joint_positions = joint_positions [right_arm_joint_indexes ]
381- head_joint_indexes = self .interface .head .joint_indexes
382- head_joint_positions = joint_positions [head_joint_indexes ]
383- leg_joint_indexes = self .interface .leg .joint_indexes
384- leg_joint_positions = joint_positions [leg_joint_indexes ]
385-
386- return {
387- "left_arm" : left_arm_joint_positions ,
388- "right_arm" : right_arm_joint_positions ,
389- "head" : head_joint_positions ,
390- "leg" : leg_joint_positions
391- }
359+ # Extract joint positions for the specified arm
360+ if arm_id == "left_arm" :
361+ arm_joint_indexes = self .interface .left_arm .joint_indexes
362+ else : # right_arm
363+ arm_joint_indexes = self .interface .right_arm .joint_indexes
364+
365+ arm_joint_positions = joint_positions [arm_joint_indexes ]
366+ return arm_joint_positions
392367
393368 def _init_pose (self ):
394369 # Initialize robot pose
@@ -458,21 +433,25 @@ def pick_and_place_callback(self):
458433 def init_state ():
459434 """Move to initial pose"""
460435 if not self .motion_in_progress :
461- joint_positions = self .solve_ik (
462- left_target_position = np .array ([0.5 , 0.3 , 0.7 ]),
463- left_target_orientation = np .array ([0 , 0.7071 , 0 , 0.7071 ]),
464- right_target_position = np .array ([0.5 , - 0.3 , 0.7 ]),
465- right_target_orientation = np .array ([0 , 0.7071 , 0 , 0.7071 ])
466- )
436+ # Get current joint positions as start configuration
437+ current_joints = self .mink_config .q
438+
439+ # Solve IK for left arm
440+ left_target_pose = np .array ([0.5 , 0.3 , 0.7 , 0 , 0.7071 , 0 , 0.7071 ])
441+ left_arm_joints = self .compute_simple_ik (current_joints , left_target_pose , "left_arm" )
442+
443+ # Solve IK for right arm
444+ right_target_pose = np .array ([0.5 , - 0.3 , 0.7 , 0 , 0.7071 , 0 , 0.7071 ])
445+ right_arm_joints = self .compute_simple_ik (current_joints , right_target_pose , "right_arm" )
467446
468447 # Start motion for arms only
469- self ._move_joints_to_target (self .interface .left_arm , joint_positions [ "left_arm" ] )
470- self ._move_joints_to_target (self .interface .right_arm , joint_positions [ "right_arm" ] )
448+ self ._move_joints_to_target (self .interface .left_arm , left_arm_joints )
449+ self ._move_joints_to_target (self .interface .right_arm , right_arm_joints )
471450
472451 # Store target positions for completion check
473452 self .target_joint_positions = {
474- "left_arm" : joint_positions [ "left_arm" ] ,
475- "right_arm" : joint_positions [ "right_arm" ]
453+ "left_arm" : left_arm_joints ,
454+ "right_arm" : right_arm_joints
476455 }
477456 self .motion_in_progress = True
478457
@@ -490,19 +469,22 @@ def move_to_pre_pick_state():
490469 self .cube_position = cube_state ["position" ].copy ()
491470 self .state_first_entry = False
492471
493- joint_positions = self .solve_ik (
494- left_target_position = self .cube_position + np .array ([0 , 0 , 0.15 ]),
495- left_target_orientation = np .array ([0 , 0.7071 , 0 , 0.7071 ]),
496- right_target_position = np .array ([0.5 , - 0.3 , 0.7 ]),
497- right_target_orientation = np .array ([0 , 0.7071 , 0 , 0.7071 ])
498- )
472+ current_joints = self .mink_config .q
499473
500- self ._move_joints_to_target (self .interface .left_arm , joint_positions ["left_arm" ])
501- self ._move_joints_to_target (self .interface .right_arm , joint_positions ["right_arm" ])
474+ # Solve IK for left arm
475+ left_target_pose = np .concatenate ([self .cube_position + np .array ([0 , 0 , 0.15 ]), np .array ([0 , 0.7071 , 0 , 0.7071 ])])
476+ left_arm_joints = self .compute_simple_ik (current_joints , left_target_pose , "left_arm" )
477+
478+ # Solve IK for right arm
479+ right_target_pose = np .array ([0.5 , - 0.3 , 0.7 , 0 , 0.7071 , 0 , 0.7071 ])
480+ right_arm_joints = self .compute_simple_ik (current_joints , right_target_pose , "right_arm" )
481+
482+ self ._move_joints_to_target (self .interface .left_arm , left_arm_joints )
483+ self ._move_joints_to_target (self .interface .right_arm , right_arm_joints )
502484
503485 self .target_joint_positions = {
504- "left_arm" : joint_positions [ "left_arm" ] ,
505- "right_arm" : joint_positions [ "right_arm" ]
486+ "left_arm" : left_arm_joints ,
487+ "right_arm" : right_arm_joints
506488 }
507489 self .motion_in_progress = True
508490
@@ -514,19 +496,22 @@ def move_to_pre_pick_state():
514496 def move_to_pick_state ():
515497 """Move to pick position"""
516498 if not self .motion_in_progress :
517- joint_positions = self .solve_ik (
518- left_target_position = self .cube_position + np .array ([0 , 0 , 0.03 ]),
519- left_target_orientation = np .array ([0 , 0.7071 , 0 , 0.7071 ]),
520- right_target_position = np .array ([0.5 , - 0.3 , 0.7 ]),
521- right_target_orientation = np .array ([0 , 0.7071 , 0 , 0.7071 ])
522- )
499+ current_joints = self .mink_config .q
500+
501+ # Solve IK for left arm
502+ left_target_pose = np .concatenate ([self .cube_position + np .array ([0 , 0 , 0.03 ]), np .array ([0 , 0.7071 , 0 , 0.7071 ])])
503+ left_arm_joints = self .compute_simple_ik (current_joints , left_target_pose , "left_arm" )
504+
505+ # Solve IK for right arm
506+ right_target_pose = np .array ([0.5 , - 0.3 , 0.7 , 0 , 0.7071 , 0 , 0.7071 ])
507+ right_arm_joints = self .compute_simple_ik (current_joints , right_target_pose , "right_arm" )
523508
524- self ._move_joints_to_target (self .interface .left_arm , joint_positions [ "left_arm" ] )
525- self ._move_joints_to_target (self .interface .right_arm , joint_positions [ "right_arm" ] )
509+ self ._move_joints_to_target (self .interface .left_arm , left_arm_joints )
510+ self ._move_joints_to_target (self .interface .right_arm , right_arm_joints )
526511
527512 self .target_joint_positions = {
528- "left_arm" : joint_positions [ "left_arm" ] ,
529- "right_arm" : joint_positions [ "right_arm" ]
513+ "left_arm" : left_arm_joints ,
514+ "right_arm" : right_arm_joints
530515 }
531516 self .motion_in_progress = True
532517
@@ -550,19 +535,22 @@ def grasp_state():
550535 def move_to_pre_place_state ():
551536 """Move to pre-place position"""
552537 if not self .motion_in_progress :
553- joint_positions = self .solve_ik (
554- left_target_position = self .cube_position + np .array ([- 0.1 , 0 , 0.4 ]),
555- left_target_orientation = np .array ([0 , 0.7071 , 0 , 0.7071 ]),
556- right_target_position = np .array ([0.5 , - 0.3 , 0.7 ]),
557- right_target_orientation = np .array ([0 , 0.7071 , 0 , 0.7071 ])
558- )
538+ current_joints = self .mink_config .q
559539
560- self ._move_joints_to_target (self .interface .left_arm , joint_positions ["left_arm" ])
561- self ._move_joints_to_target (self .interface .right_arm , joint_positions ["right_arm" ])
540+ # Solve IK for left arm
541+ left_target_pose = np .concatenate ([self .cube_position + np .array ([- 0.1 , 0 , 0.4 ]), np .array ([0 , 0.7071 , 0 , 0.7071 ])])
542+ left_arm_joints = self .compute_simple_ik (current_joints , left_target_pose , "left_arm" )
543+
544+ # Solve IK for right arm
545+ right_target_pose = np .array ([0.5 , - 0.3 , 0.7 , 0 , 0.7071 , 0 , 0.7071 ])
546+ right_arm_joints = self .compute_simple_ik (current_joints , right_target_pose , "right_arm" )
547+
548+ self ._move_joints_to_target (self .interface .left_arm , left_arm_joints )
549+ self ._move_joints_to_target (self .interface .right_arm , right_arm_joints )
562550
563551 self .target_joint_positions = {
564- "left_arm" : joint_positions [ "left_arm" ] ,
565- "right_arm" : joint_positions [ "right_arm" ]
552+ "left_arm" : left_arm_joints ,
553+ "right_arm" : right_arm_joints
566554 }
567555 self .motion_in_progress = True
568556
@@ -579,19 +567,22 @@ def move_to_place_state():
579567 self .bucket_position = bucket_state ["position" ].copy ()
580568 self .state_first_entry = False
581569
582- joint_positions = self .solve_ik (
583- left_target_position = self .bucket_position + np .array ([0 , 0 , 0.3 ]),
584- left_target_orientation = np .array ([0 , 0.7071 , 0 , 0.7071 ]),
585- right_target_position = np .array ([0.5 , - 0.3 , 0.7 ]),
586- right_target_orientation = np .array ([0 , 0.7071 , 0 , 0.7071 ])
587- )
570+ current_joints = self .mink_config .q
571+
572+ # Solve IK for left arm
573+ left_target_pose = np .concatenate ([self .bucket_position + np .array ([0 , 0 , 0.3 ]), np .array ([0 , 0.7071 , 0 , 0.7071 ])])
574+ left_arm_joints = self .compute_simple_ik (current_joints , left_target_pose , "left_arm" )
575+
576+ # Solve IK for right arm
577+ right_target_pose = np .array ([0.5 , - 0.3 , 0.7 , 0 , 0.7071 , 0 , 0.7071 ])
578+ right_arm_joints = self .compute_simple_ik (current_joints , right_target_pose , "right_arm" )
588579
589- self ._move_joints_to_target (self .interface .left_arm , joint_positions [ "left_arm" ] )
590- self ._move_joints_to_target (self .interface .right_arm , joint_positions [ "right_arm" ] )
580+ self ._move_joints_to_target (self .interface .left_arm , left_arm_joints )
581+ self ._move_joints_to_target (self .interface .right_arm , right_arm_joints )
591582
592583 self .target_joint_positions = {
593- "left_arm" : joint_positions [ "left_arm" ] ,
594- "right_arm" : joint_positions [ "right_arm" ]
584+ "left_arm" : left_arm_joints ,
585+ "right_arm" : right_arm_joints
595586 }
596587 self .motion_in_progress = True
597588
0 commit comments