def main(): #inits AMBF client and connects to it. Declared globally so other functions can access it. global _client _client = Client() _client.connect() rospy.Subscriber("joy", Joy, JoyInput) Body = Movement("RoverBody") Body.ABSmove([0, 0, 0]) while not rospy.is_shutdown(): if Xb1.axes[1] != 0: Body.move(Xb1.axes[1]) rospy.loginfo("Trying to move") if Xb1.buttons[0] == 1: Body.reset( ) #resets to (0,0,0) and RPY (0,0,0) activated with A button rospy.logwarn("Resetting Position to (0,0)") if Xb1.buttons[1] == 1: Body.ABSmove( [float(input("X coord: ")), float(input("Y coord: ")), 0] ) #for debugging allows movement to custom coordinate activated with B button if Xb1.axes[3] != 0: Body.rotate( Xb1.axes[3]) #no need to normalize since it already is +- 1 _client.clean_up() rate.sleep()
def test_ambf_psm(): c = Client() c.connect() time.sleep(2.0) print(c.get_obj_names()) b = c.get_obj_handle('baselink') time.sleep(1.0) # The following are the names of the controllable joints. # 'baselink-yawlink', 0 # 'yawlink-pitchbacklink', 1 # 'pitchendlink-maininsertionlink', 2 # 'maininsertionlink-toolrolllink', 3 # 'toolrolllink-toolpitchlink', 4 # 'toolpitchlink-toolgripper1link', 5a # 'toolpitchlink-toolgripper2link', 5b test_q = [-0.3, 0.2, 0.1, -0.9, 0.0, -1.2, 0.0] T_7_0 = compute_FK(test_q) computed_q = compute_IK(convert_mat_to_frame(T_7_0)) print('SETTING JOINTS: ') print(computed_q) b.set_joint_pos('baselink-yawlink', computed_q[0]) b.set_joint_pos('yawlink-pitchbacklink', computed_q[1]) b.set_joint_pos('pitchendlink-maininsertionlink', computed_q[2]) b.set_joint_pos('maininsertionlink-toolrolllink', computed_q[3]) b.set_joint_pos('toolrolllink-toolpitchlink', computed_q[4]) b.set_joint_pos('toolpitchlink-toolgripper1link', computed_q[5]) b.set_joint_pos('toolpitchlink-toolgripper2link', -computed_q[5]) time.sleep(3.0)
def __init__(self, mass, height): """ :param name_space: name space of the the body """ super(AMBF, self).__init__(mass, height) _client = Client() _client.connect() time.sleep(2) handle = _client.get_obj_handle('Hip')
def main(): _client = Client() _client.connect() wheel = Joint("/ambf/env/2WheelSpringRight",0) #wheel.setTorque() print("got here") while not rospy.is_shutdown(): print "into loop" rospy.loginfo(axes) if abs(axes) >= 1000: wheel.move(500) else: wheel.move(-500) _client.clean_up() rate.sleep()
def controller(): # Create a instance of the client _client = Client() _client.connect() # You can print the names of objects found print(_client.get_obj_names()) tool = _client.get_obj_handle('ecm/remotecenterlink') body = _client.get_obj_handle('ecm/baselink') body.set_joint_pos(0, 0) print body.get_children_names() pos = tool.get_pos() y = pos.y while 1: pos = tool.get_pos() x = pos.x print pos tool.set_pos(x + .1, 0.0, 0) time.sleep(0.1)
def main(): # Begin Argument Parser Code parser = ArgumentParser() parser.add_argument('-d', action='store', dest='spacenav_name', help='Specify ros base name of spacenav', default='/spacenav/') parser.add_argument('-o', action='store', dest='obj_name', help='Specify AMBF Obj Name', default='BoxTool') parser.add_argument('-c', action='store', dest='camera_name', help='Specify AMBF Camera Name', default='default_camera') parser.add_argument('-a', action='store', dest='client_name', help='Specify AMBF Client Name', default='client_name') parser.add_argument('-p', action='store', dest='print_obj_names', help='Print Object Names', default=False) parsed_args = parser.parse_args() print('Specified Arguments') print parsed_args _spacenav_one_name = parsed_args.spacenav_name _obj_one_name = parsed_args.obj_name _default_camera_name = parsed_args.camera_name _ambf_client_name = parsed_args.client_name _print_obj_names = parsed_args.print_obj_names client = Client(_ambf_client_name) client.connect() if _print_obj_names: print('Printing Found AMBF Object Names: ') print client.get_obj_names() exit() _pair_one_specified = True _device_pairs = [] default_camera = client.get_obj_handle(_default_camera_name) # The publish frequency _pub_freq = 500 spacenav_one = SpaceNavDevice(_spacenav_one_name, client, _obj_one_name, default_camera) _device_pairs.append(spacenav_one) rate = rospy.Rate(_pub_freq) msg_index = 0 _start_time = rospy.get_time() while not rospy.is_shutdown(): for dev in _device_pairs: dev.process_commands() rate.sleep() msg_index = msg_index + 1 if msg_index % _pub_freq * 3 == 0: # Print every 3 seconds as a flag to show that this code is alive print('SpaceNav AMBF Node Alive...', round(rospy.get_time() - _start_time, 3), 'secs') if msg_index >= _pub_freq * 10: # After ten seconds, reset, no need to keep increasing this msg_index = 0
from sensor_msgs.msg import PointCloud import numpy as np from ambf_client import Client import rospy from geometry_msgs.msg import Point32 client = Client() client.connect() sensor = client.get_obj_handle("/ambf/env/Prox") rospy.sleep(3) pub = rospy.Publisher('cloud', PointCloud, queue_size=10) rate = rospy.Rate(1000) theta = np.linspace(0, 2 * np.pi, 10) while 1: ranges = sensor.get_all_measurements() range = sensor.get_range(0) print(range) parent = sensor.get_parent() print(parent) pose = sensor.get_pose() print(pose) msg = PointCloud() msg.header.stamp = rospy.Time.now() msg.header.frame_id = "/stair" #print(range)
class AmbfEnvHERDDPG(gym.GoalEnv): def __init__(self, action_space_limit, joints_to_control, goal_position_range, position_error_threshold, goal_error_margin, joint_limits, workspace_limits, enable_step_throttling): super(AmbfEnvHERDDPG, self).__init__() self.obj_handle = Object self.world_handle = World self.ambf_client = Client() self.ambf_client.connect() time.sleep(5) self.ambf_client.create_objs_from_rostopics() self.seed() self.n_skip_steps = 5 self.enable_step_throttling = enable_step_throttling self.action = [] self.obs = Observation() self.initial_pos = copy.deepcopy(self.obs.cur_observation()[0]) self.cmd_joint_pos = np.zeros(7) self.goal_error_margin = goal_error_margin self.joint_limits = joint_limits self.workspace_limits = workspace_limits self.n_actions = 7 self.action_lims_low = -action_space_limit * np.ones(self.n_actions) self.action_lims_high = action_space_limit * np.ones(self.n_actions) self.action_space = spaces.Box(low=-action_space_limit, high=action_space_limit, shape=(self.n_actions, ), dtype="float32") self.observation_space = spaces.Dict( dict( desired_goal=spaces.Box( -np.inf, np.inf, shape=self.initial_pos['achieved_goal'].shape, dtype='float32'), achieved_goal=spaces.Box( -np.inf, np.inf, shape=self.initial_pos['achieved_goal'].shape, dtype='float32'), observation=spaces.Box( -np.inf, np.inf, shape=self.initial_pos['observation'].shape, dtype='float32'), )) self.joints_to_control = joints_to_control self.goal_position_range = goal_position_range self.goal = np.array([0.0, 0.0, -0.1, 0.0, 0.0, 0.0]) self.prev_sim_step = 0 self.pos_error_threshold = position_error_threshold self.count_for_print = 0 def skip_sim_steps(self, num): # Function to define the number of steps that can be skipped if Step Throttling is enabled self.n_skip_steps = num self.world_handle.set_num_step_skips(num) def set_throttling_enable(self, check): # Function to set the Step Throttling Boolean self.enable_step_throttling = check self.world_handle.enable_throttling(check) def make(self, a_name): # Function to create object handle for the robot and world in AMBF self.obj_handle = self.ambf_client.get_obj_handle(a_name) # self.base_handle = self.ambf_client.get_obj_handle('SampleObj') self.world_handle = self.ambf_client.get_world_handle() self.world_handle.enable_throttling(self.enable_step_throttling) self.world_handle.set_num_step_skips(self.n_skip_steps) time.sleep(2) if self.obj_handle is None or self.world_handle is None: raise Exception def seed(self, seed=None): # Random seed self.np_random, seed = seeding.np_random(seed) return [seed] def reset(self): # Function to reset the model # Sets the initial position of PSM self.set_initial_pos_func() # Type 1 Reset : Uses the previous reached state as the initial state for next iteration # action = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] # observation, _, _, _ = self.step(action) # Type 2 Reset : Sets the robot to a predefined initial state for each iteration initial_joint_pos, initial_state_vel = self.get_joint_pos_vel_func() end_effector_frame = compute_FK(initial_joint_pos) # Updates the observation to the initialized position observation, _, _, _ = self._update_observation( end_effector_frame, initial_joint_pos, initial_state_vel) # Samples a goal self.goal = self._sample_goal(observation) return observation def set_initial_pos_func(self): # Function to set the initial joint positions of the robot for joint_idx, jt_name in enumerate(self.joints_to_control): # Prismatic joint is set to different value to ensure at least some part of robot tip goes past the cannula if joint_idx == 2: self.obj_handle.set_joint_pos( jt_name, self.joint_limits['lower_limit'][2]) else: self.obj_handle.set_joint_pos(jt_name, 0) time.sleep(0.5) def get_joint_pos_vel_func(self): # Function to compute Joint Position and Velocity of the robot joint_position = np.zeros(7) joint_velocity = np.zeros(7) for joint_idx, jt_name in enumerate(self.joints_to_control): joint_position[joint_idx] = self.obj_handle.get_joint_pos(jt_name) joint_velocity[joint_idx] = self.obj_handle.get_joint_vel(jt_name) return joint_position, joint_velocity def step(self, action): assert len(action) == 7 # Counter for printing position, action and reward self.count_for_print += 1 # Initialization of variables new_state_joint_pos = np.zeros(7) # Clipping the action value between the desired range action = np.clip(action, self.action_lims_low, self.action_lims_high) current_joint_pos, state_vel = self.get_joint_pos_vel_func() # Take the action from current state to reach the new state for joint_idx, jt_name in enumerate(self.joints_to_control): new_state_joint_pos[joint_idx] = np.add( current_joint_pos[joint_idx], action[joint_idx]) # Ensure the new state is within valid joint positions, if invalid then stay at the joint limit position desired_joint_pos = self.limit_joint_pos(new_state_joint_pos) # Ensures that PSM joints reach the desired joint positions self.set_commanded_joint_pos(desired_joint_pos) current_end_effector_frame = compute_FK(desired_joint_pos) # Update state, reward, done flag and world values in the code updated_state, rewards, done, info = self._update_observation( current_end_effector_frame, desired_joint_pos, state_vel) self.world_handle.update() # Print function for viewing the output intermittently if self.count_for_print % 10000 == 0: print("count ", self.count_for_print, "Action is ", action, " new pos after action ", updated_state) print("Reward is ", rewards) return updated_state, rewards, done, info def set_commanded_joint_pos(self, commanded_joint_pos): # Function to ensure the robot tip reaches the desired goal position before moving on to next iteration # Counter to avoid getting stuck in while loop because of very small errors in positions count_for_joint_pos = 0 while True: reached_joint_pos = np.zeros(7) for joint_idx, jt_name in enumerate(self.joints_to_control): self.obj_handle.set_joint_pos(jt_name, commanded_joint_pos[joint_idx]) reached_joint_pos[joint_idx] = self.obj_handle.get_joint_pos( jt_name) error_in_pos = np.around(np.subtract(commanded_joint_pos, reached_joint_pos), decimals=3) # Since Prismatic joint limits are smaller compared to the limits of other joints error_in_pos_joint2 = np.around(np.subtract( commanded_joint_pos[2], reached_joint_pos[2]), decimals=4) count_for_joint_pos += 1 if (np.all(np.abs(error_in_pos) <= self.pos_error_threshold) and np.abs(error_in_pos_joint2) <= 0.5*self.pos_error_threshold) \ or count_for_joint_pos > 75: break def limit_cartesian_pos(self, cart_pos): # Limits the robot tip X, Y, Z positions limit_cartesian_pos_values = np.zeros(3) cartesian_pos_lower_limit = self.workspace_limits['lower_limit'] cartesian_pos_upper_limit = self.workspace_limits['upper_limit'] for axis in range(3): limit_cartesian_pos_values[axis] = np.clip( cart_pos[axis], cartesian_pos_lower_limit[axis], cartesian_pos_upper_limit[axis]) return limit_cartesian_pos_values def limit_joint_pos(self, joint_pos): # Limits the joint position values of the robot # dvrk_limits_low = np.array([-1.605, -0.93556, -0.002444, -3.0456, -3.0414, -3.0481, -3.0498]) # dvrk_limits_high = np.array([1.5994, 0.94249, 0.24001, 3.0485, 3.0528, 3.0376, 3.0399]) # Note: Joint 5 and 6, joint pos = 0, 0 is closed jaw and 0.5, 0.5 is open limit_joint_values = np.zeros(7) joint_lower_limit = self.joint_limits['lower_limit'] joint_upper_limit = self.joint_limits['upper_limit'] for joint_idx in range(len(joint_pos)): limit_joint_values[joint_idx] = np.clip( joint_pos[joint_idx], joint_lower_limit[joint_idx], joint_upper_limit[joint_idx]) return limit_joint_values def render(self, mode): # Not being utilized since AMBF runs in parallel for visualization print(' I am {} Ironman'.format(mode)) def _update_observation(self, end_effector_frame, joint_pos, state_vel): # Function to update all the values # Function implementing Step Throttling algorithm if self.enable_step_throttling: step_jump = 0 while step_jump < self.n_skip_steps: step_jump = self.obj_handle.get_sim_step() - self.prev_sim_step time.sleep(0.00001) self.prev_sim_step = self.obj_handle.get_sim_step() if step_jump > self.n_skip_steps: print('WARN: Jumped {} steps, Default skip limit {} Steps'. format(step_jump, self.n_skip_steps)) else: cur_sim_step = self.obj_handle.get_sim_step() step_jump = cur_sim_step - self.prev_sim_step self.prev_sim_step = cur_sim_step # Find the tip position and rotation by computing forward kinematics (not being used currently) cartesian_pos = self.limit_cartesian_pos( end_effector_frame[0:3, 3]).reshape((3, 1)) achieved_rot = np.array( euler_from_matrix(end_effector_frame[0:3, 0:3], axes='szyx')).reshape((3, 1)) # Combine tip position and rotation to a single numpy array as achieved goal achieved_goal = np.asarray( np.concatenate((cartesian_pos.copy(), achieved_rot.copy()), axis=0)).reshape(-1) obs = np.asarray( np.concatenate((cartesian_pos.copy(), achieved_rot.copy(), joint_pos.reshape((7, 1))), axis=0)).reshape(-1) # Combine the current state positions and velocity into a single array as observation observation = np.concatenate((obs, state_vel), axis=None) # Update the observation dictionary self.obs.state.update(observation=observation.copy(), achieved_goal=achieved_goal.copy(), desired_goal=self.goal.copy()) # Update info self.obs.info = self._update_info() # Compute the reward self.obs.reward = self.compute_reward(self.obs.state['achieved_goal'], self.goal, self.obs.info) self.obs.is_done = self._check_if_done() # Return the values to step function return self.obs.state, self.obs.reward, self.obs.is_done, self.obs.info def compute_reward(self, achieved_goal, goal, info): # Function to compute reward received by the agent # Find the distance between goal and achieved goal cur_dist = LA.norm(np.subtract(goal[0:3], achieved_goal[0:3])) # Continuous reward # reward = round(1 - float(abs(cur_dist)/0.3)*0.5, 5) # Sparse reward if abs(cur_dist) < 0.01: reward = 1 else: reward = -1 self.obs.dist = cur_dist return reward def _sample_goal(self, observation): # Function to samples new goal positions and ensures its within the workspace of PSM rand_val_pos = np.around(np.add( observation['achieved_goal'][0:3], self.np_random.uniform(-self.goal_position_range, self.goal_position_range, size=3)), decimals=4) rand_val_pos[0] = np.around(np.clip( rand_val_pos[0], self.workspace_limits['lower_limit'][0], self.workspace_limits['upper_limit'][0]), decimals=4) rand_val_pos[1] = np.around(np.clip( rand_val_pos[1], self.workspace_limits['lower_limit'][1], self.workspace_limits['upper_limit'][1]), decimals=4) rand_val_pos[2] = np.around(np.clip( rand_val_pos[2], self.workspace_limits['lower_limit'][2], self.workspace_limits['upper_limit'][2]), decimals=4) # Uncomment below lines if individual limits need to be set for generating desired goal state ''' rand_val_pos[0] = np.around(np.clip(rand_val_pos[0], -0.1388, 0.1319), decimals=4) rand_val_pos[1] = np.around(np.clip(rand_val_pos[1], -0.1319, 0.1388), decimals=4) rand_val_pos[2] = np.around(np.clip(rand_val_pos[2], -0.1935, -0.0477), decimals=4) rand_val_angle[0] = np.clip(rand_val_angle[0], -0.15, 0.15) rand_val_angle[1] = np.clip(rand_val_angle[1], -0.15, 0.15) rand_val_angle[2] = np.clip(rand_val_angle[2], -0.15, 0.15) ''' # Provide the range for generating the desired orientation at the terminal state rand_val_angle = self.np_random.uniform(-1.5, 1.5, size=3) goal = np.concatenate((rand_val_pos, rand_val_angle), axis=None) return goal.copy() def _check_if_done(self): # Function to check if the episode was successful if abs(self.obs.dist) < self.goal_error_margin: return True else: return False def _update_info(self): # Can be used to Provide information for debugging purpose info = {'is_success': self._is_success()} return info def _is_success(self): # Function to check if the robot reached the desired goal within a predefined error margin if abs(self.obs.dist) < self.goal_error_margin: return True else: return False
def main(): # Begin Argument Parser Code parser = ArgumentParser() parser.add_argument('-d', action='store', dest='joystick_name', help='Specify ros base name of joystick', default='/') parser.add_argument('-o', action='store', dest='obj_name', help='Specify AMBF Obj Name', default='Chassis') parser.add_argument('-a', action='store', dest='client_name', help='Specify AMBF Client Name', default='client_name') parser.add_argument('-p', action='store', dest='print_obj_names', help='Print Object Names', default=False) parsed_args = parser.parse_args() print('Specified Arguments') print parsed_args client = Client(parsed_args.client_name) client.connect() if parsed_args.print_obj_names: print('Printing Found AMBF Object Names: ') print client.get_obj_names() exit() control_units = [] num_rovers = 2 num_actuators = 4 # Since we have only one JS for now, joystick = JoyStickDevice(parsed_args.joystick_name) for i in range(num_rovers): rover_prefix = 'rover' + str(i + 1) + '/' rover = client.get_obj_handle(rover_prefix + 'Rover') arm = client.get_obj_handle(rover_prefix + 'arm_link1') sensor = client.get_obj_handle(rover_prefix + 'Proximity0') actuators = [] for j in range(num_actuators): actuator = client.get_obj_handle(rover_prefix + 'Constraint' + str(j)) actuators.append(actuator) # If you have multiple Joysticks, you can pass in different names control_unit = ControlUnit(joystick, rover, arm, sensor, actuators) control_units.append(control_unit) # The publish frequency pub_freq = 60 rate = rospy.Rate(pub_freq) msg_index = 0 while not rospy.is_shutdown(): # Control the active Control Unit control_units[active_rover].process_commands() rate.sleep() msg_index = msg_index + 1 if msg_index % pub_freq * 50 == 0: # Print every 3 seconds as a flag to show that this code is alive # print('Running JoyStick Controller Node...', round(rospy.get_time() - _start_time, 3), 'secs') pass if msg_index >= pub_freq * 10: # After ten seconds, reset, no need to keep increasing this msg_index = 0
def set_point_control(req): _client = Client() _client.connect() base = _client.get_obj_handle('/ambf/env/neuro_robot/base_link') base.set_joint_pos(0, 0) base.set_joint_pos(1, 0) base.set_joint_pos(2, 0) base.set_joint_pos(3, 0) base.set_joint_pos(4, 0) base.set_joint_pos(5, 0) base.set_joint_pos(6, 0) time.sleep(1) print("Recieved joint positions") print(req.pose) joint_pos = np.array(req.pose, dtype=np.float32) cfm = MRI_collide_detection(joint_pos) cfh = Head_collide_detection(joint_pos) wm = [cfm[0]] wh = [cfh[0]] t = [0] if cfm[0] == 1 or cfh[0] == 1: wm = cfm[1] wh = cfh[1] print("Destination is not reachable without collision") return {'wm': wm, 'wh': wh, 't': t} else: print("Destination is reachable without collision") # current joint pose c_pos = np.array([ .00000000, .00000000, .00000000, .0000000, .00000000, .00000000, .0000000 ]) # ending joint pose e_pos = joint_pos e_pos[0] = e_pos[0] / 1000 e_pos[1] = e_pos[1] / 1000 e_pos[2] = e_pos[2] e_pos[3] = e_pos[3] e_pos[4] = -e_pos[4] / 1000 e_pos[6] = -e_pos[6] / 1000 # step joint pose s_pos = e_pos / 100 print(s_pos) print("starting") # we will rotate the robot while slowly pull out the needle first if joint_pos[2] != 0 or joint_pos[3] != 0: dinsert = -40 / 100000 i = 0 while i <= 100: # first check the next point is safe n_pos = c_pos n_pos[2] = n_pos[2] + s_pos[2] n_pos[3] = n_pos[3] + s_pos[3] n_pos[4] = n_pos[4] + dinsert n_pos[5] = n_pos[5] + s_pos[5] cfmr = MRI_collide_detection(n_pos) cfhr = Head_collide_detection(n_pos) if cfmr[0] != 1 and cfhr[0] != 1: base.set_joint_pos(3, n_pos[2]) base.set_joint_pos(2, n_pos[3]) base.set_joint_pos(4, n_pos[4]) base.set_joint_pos(5, n_pos[5]) c_pos[2] = base.get_joint_pos(2) c_pos[3] = base.get_joint_pos(3) c_pos[4] = base.get_joint_pos(4) c_pos[5] = base.get_joint_pos(5) elif cfmr[0] == 1 and cfhr[0] != 1: base.set_joint_pos(2, n_pos[2]) base.set_joint_pos(3, n_pos[3]) base.set_joint_pos(4, c_pos[4] - dinsert) base.set_joint_pos(5, n_pos[5]) c_pos[2] = base.get_joint_pos(2) c_pos[3] = base.get_joint_pos(3) c_pos[4] = base.get_joint_pos(4) c_pos[5] = base.get_joint_pos(5) else: print("Trajectory error") sys.exit(1) wm.append(cfmr[0]) wh.append(cfhr[0]) t.append(t[-1] + 0.1) i = i + 1 time.sleep(0.1) dinsert = (e_pos[4] - c_pos[4]) / 100 i = 0 while i <= 100: # first check the next point is safe n_pos = c_pos n_pos[0] = n_pos[0] + s_pos[0] n_pos[1] = n_pos[1] + s_pos[1] n_pos[4] = n_pos[4] + dinsert n_pos[6] = n_pos[6] + s_pos[6] cfmr = MRI_collide_detection(n_pos) cfhr = Head_collide_detection(n_pos) if cfmr[0] != 1 or cfhr[0] != 1: base.set_joint_pos(0, n_pos[0]) base.set_joint_pos(1, n_pos[1]) base.set_joint_pos(4, n_pos[4]) base.set_joint_pos(6, n_pos[6]) c_pos[0] = base.get_joint_pos(0) c_pos[1] = base.get_joint_pos(1) c_pos[4] = base.get_joint_pos(4) c_pos[6] = base.get_joint_pos(6) wm.append(cfmr[0]) wh.append(cfhr[0]) t.append(t[-1] + 0.1) i = i + 1 time.sleep(0.1) else: print("Trajectory error") sys.exit(1) wm = np.array(wm, dtype=np.float32) wh = np.array(wh, dtype=np.float32) t = np.array(t, dtype=np.float32) return {'wm': wm, 'wh': wh, 't': t}
class AMBF_controller: """Main class for ambf controller """ chain = None solver = None ambf_client = None robot_handle = None def __init__(self, yaml_file): """Initializer for the AMBF class Arguments: yaml_file {string} -- Path of the YAML file to load for kinematic chain """ data = None with open(yaml_file, 'rb') as f: data = yaml.load(f) if 'solver' in data: solver_to_use = data['solver'] else: # Exiting if no solver is defined sys.exit("No Solver is defined. Exiting..") # =============================================================================== Load data, solver # Generating Chain from yaml self.chain = Chain(data) # Get Solver Collection solverCollection = SolverCollection() log.debug("Kinematics Solvers detected :") log.debug(solverCollection.getAllSolvers()) # Get an instance of the solver to be used self.solver = solverCollection.getSolver( solver_to_use, self.chain, strict=True) log.debug("Loaded Solver >> " + self.solver.name) rospy.Subscriber("/ambf/validate", Pose, self.set_pose_callback) # =============================================================================== Connect to AMBF # Create a instance of the client self.ambf_client = Client() # Connect the client which in turn creates callable objects from ROS topics # and initiates a shared pool of threads for bi-directional communication self.ambf_client.connect() # Get handle to set joint positions to self.robot_handle = self.ambf_client.get_obj_handle( self.chain.getBaseBody().name) self.robot_tip_handle = self.ambf_client.get_obj_handle( self.chain.getTipBody().name) # =============================================================================== Start ROS comm rospy.Subscriber("/ambf/setPose", Pose, self.set_pose_callback) rospy.Subscriber("/ambf/setJointParams", Float64MultiArray, self.set_joint_params_callback) rate = rospy.Rate(10) while not rospy.is_shutdown(): # print(self.robot_tip_handle.get_pos()) rate.sleep() # rospy.spin() def set_pose_callback(self, msg): # Build a Pose pose = SolverPose(msg.position.x, msg.position.y, msg.position.z, msg.orientation.x, msg.orientation.y, msg.orientation.z, msg.orientation.w) self.solver.set_current_jointstates(self.robot_handle.get_all_joint_pos()) joint_states = self.solver.solve_for_ik_pos(pose) log.info("Joint Params calculated:") log.info(joint_states) log.info(pose) i = 0 for joint_value in joint_states: self.robot_handle.set_joint_pos(i, joint_value) i += 1 def set_joint_params_callback(self, msg): pose = self.solver.solve_for_fk_pos(msg.data) log.info("Pose calculated:") log.info(pose) for i in range(len(msg.data)): self.robot_handle.set_joint_pos(i, msg.data[i])
class TestPSMIK: def __init__(self, run_psm_one, run_psm_two, run_psm_three, OG1=None, OG2=None, PG3=None): self.c = Client() self.c.connect() self.psm1_scale = 10.0 self.psm2_scale = 10.0 self.psm3_scale = 10.0 self.run_psm_one = run_psm_one self.run_psm_two = run_psm_two self.run_psm_three = run_psm_three time.sleep(1.0) if self.run_psm_one is True: print('PREPARING TO LOAD IK FOR PSM1') self.PSM1 = PSM(self.c, 'psm1', OG1, self.psm1_scale) if self.run_psm_two is True: print('PREPARING TO LOAD IK FOR PSM2') self.PSM2 = PSM(self.c, 'psm2', OG2, self.psm2_scale) if self.run_psm_three is True: print('PREPARING TO LOAD IK FOR PSM3') self.PSM3 = PSM(self.c, 'psm3', OG3, self.psm3_scale) if not run_psm_one and not run_psm_two and not run_psm_three: print( 'YOU HAVE TO RUN ATLEAST ONE PSMS IK FOR THIS SCRIPT TO DO ANYTHING' ) # The following are the names of the controllable joints. # 'baselink-yawlink', 0 # 'yawlink-pitchbacklink', 1 # 'pitchendlink-maininsertionlink', 2 # 'maininsertionlink-toolrolllink', 3 # 'toolrolllink-toolpitchlink', 4 # 'toolpitchlink-toolyawlink', 5 # 'toolyawlink-toolgripper1link', 6a # 'toolyawlink-toolgripper2link', 6b def runOneArm(self, arm): arm.obj_gui.App.update() # Move the Target Position Based on the GUI x = arm.obj_gui.x y = arm.obj_gui.y z = arm.obj_gui.z ro = arm.obj_gui.ro pi = arm.obj_gui.pi ya = arm.obj_gui.ya gr = arm.obj_gui.gr arm.target.set_pos(x, y, z) arm.target.set_rpy(ro, pi, ya) p = arm.base.get_pos() q = arm.base.get_rot() P_b_w = Vector(p.x, p.y, p.z) R_b_w = Rotation.Quaternion(q.x, q.y, q.z, q.w) T_b_w = Frame(R_b_w, P_b_w) p = arm.target.get_pos() q = arm.target.get_rot() P_t_w = Vector(p.x, p.y, p.z) R_t_w = Rotation.Quaternion(q.x, q.y, q.z, q.w) T_t_w = Frame(R_t_w, P_t_w) T_t_b = T_b_w.Inverse() * T_t_w # P_t_b = T_t_b.p # P_t_b_scaled = P_t_b / self.psm1_scale # T_t_b.p = P_t_b_scaled computed_q = compute_IK(T_t_b) # print('SETTING JOINTS: ') # print(computed_q) arm.base.set_joint_pos('baselink-yawlink', computed_q[0]) arm.base.set_joint_pos('yawlink-pitchbacklink', computed_q[1]) arm.base.set_joint_pos('pitchendlink-maininsertionlink', computed_q[2]) arm.base.set_joint_pos('maininsertionlink-toolrolllink', computed_q[3]) arm.base.set_joint_pos('toolrolllink-toolpitchlink', computed_q[4]) arm.base.set_joint_pos('toolpitchlink-toolyawlink', -computed_q[5]) arm.base.set_joint_pos('toolyawlink-toolgripper1link', gr) arm.base.set_joint_pos('toolyawlink-toolgripper2link', gr) for i in range(3): if arm.sensor.is_triggered(i) and gr <= 0.2: sensed_obj = arm.sensor.get_sensed_object(i) if sensed_obj == 'Needle': if not arm.grasped[i]: arm.actuators[i].actuate(sensed_obj) arm.grasped[i] = True print('Grasping Sensed Object Names', sensed_obj) else: if gr > 0.2: arm.actuators[i].deactuate() arm.grasped[i] = False # print('Releasing Actuator ', i) def run(self): while not rospy.is_shutdown(): if self.run_psm_one is True: self.runOneArm(self.PSM1) if self.run_psm_two is True: self.runOneArm(self.PSM2) if self.run_psm_two is True: self.runOneArm(self.PSM3) time.sleep(0.005)
def set_point_control(req): _client = Client() _client.connect() base = _client.get_obj_handle('/ambf/env/neuro_robot/base_link') base.set_joint_pos(0, 0) base.set_joint_pos(1, 0) base.set_joint_pos(2, 0) base.set_joint_pos(3, 0) base.set_joint_pos(4, 0) base.set_joint_pos(5, 0) base.set_joint_pos(6, 0) joint_pos = np.array(req.pose, dtype='float64') cfm = MRI_collide_detection(joint_pos) cfh = Head_collide_detection(joint_pos) if cfm[0] == 1 or cfh[0] == 1: sys.exit(1) wm = np.array([cfm[0]], dtype='float64') wh = np.array([cfh[0]], dtype='float64') t = np.array([0], dtype='float64') # current joint pose c_pos = np.array([0, 0, 0, 0, 0, 0, 0]) # ending joint pose e_pos = joint_pos / 1000 # step joint pose s_pos = joint_pos / 100000 # we will rotate the robot while slowly pull out the needle first if not (joint_pos[2] == 0 and joint_pos[3] == 0 and joint_pos[4] == 0 and joint_pos[5] == 0): dinsert = 40 / 100000 while np.mean(abs(e_pos[2:4] - c_pos[2:4])) > 0.0002: # first check the next point is safe n_pos = c_pos n_pos[2] = n_pos[2] + s_pos[2] n_pos[3] = n_pos[3] + s_pos[3] n_pos[4] = n_pos[4] + dinsert n_pos[5] = n_pos[5] + s_pos[5] cfmr = MRI_collide_detection(n_pos) cfhr = Head_collide_detection(n_pos) if not (cfmr[0] == 1 or cfhr[0] == 1): base.set_joint_pos(2, c_pos[2] + s_pos[2]) base.set_joint_pos(3, c_pos[3] + s_pos[3]) base.set_joint_pos(4, c_pos[4] + dinsert) base.set_joint_pos(5, c_pos[5] + s_pos[5]) c_pos[2] = base.get_joint_pos(2) c_pos[3] = base.get_joint_pos(3) c_pos[4] = base.get_joint_pos(4) c_pos[5] = base.get_joint_pos(5) elif cfmr[0] == 1 and cfhr[0] != 1: base.set_joint_pos(2, c_pos[2] + s_pos[2]) base.set_joint_pos(3, c_pos[3] + s_pos[3]) base.set_joint_pos(4, c_pos[4] - dinsert) base.set_joint_pos(5, c_pos[5] + s_pos[5]) c_pos[2] = base.get_joint_pos(2) c_pos[3] = base.get_joint_pos(3) c_pos[4] = base.get_joint_pos(4) c_pos[5] = base.get_joint_pos(5) np.append(wm, cfmr[0]) np.append(wh, cfhr[0]) np.append(t, t[-1] + 0.05) time.sleep(0.048) dinsert = e_pos[4] - c_pos[4] while np.mean(abs(e_pos - c_pos)) > 0.0002: # first check the next point is safe n_pos = c_pos n_pos[0] = n_pos[0] + s_pos[0] n_pos[1] = n_pos[1] + s_pos[1] n_pos[4] = n_pos[4] + dinsert n_pos[6] = n_pos[6] + s_pos[6] cfmr = MRI_collide_detection(n_pos) cfhr = Head_collide_detection(n_pos) if not (cfmr[0] == 1 or cfhr[0] == 1): base.set_joint_pos(0, c_pos[0] + s_pos[0]) base.set_joint_pos(1, c_pos[1] + s_pos[1]) base.set_joint_pos(4, c_pos[4] + dinsert) base.set_joint_pos(6, c_pos[6] + s_pos[6]) c_pos[0] = base.get_joint_pos(0) c_pos[1] = base.get_joint_pos(1) c_pos[4] = base.get_joint_pos(4) c_pos[6] = base.get_joint_pos(6) np.append(wm, cfmr[0]) np.append(wh, cfhr[0]) np.append(t, t[-1] + 0.05) time.sleep(0.048) else: print("Trajectory error") sys.exit(1) return {'wm': wm, 'wh': wh, 't': t}
def temp_controller(): # Create a instance of the client _client = Client() # Connect the client which in turn creates callable objects from ROS topics # and initiates a shared pool of threads for bi-directional communication _client.connect() print('\n\n----') raw_input( "We can see what objects the client has found. Press Enter to continue..." ) # You can print the names of objects found. We should see all the links found print(_client.get_obj_names()) # Lets get a handle to PSM and ECM, as we can see in the printed # object names, 'ecm/baselink' and 'psm/baselink' should exist ecm_handle = _client.get_obj_handle('ecm/toollink') # Let's sleep for a very brief moment to give the internal callbacks # to sync up new data from the running simulator time.sleep(0.2) print('\n\n----') raw_input("Let's Get Some Pose Info. Press Enter to continue...") # Not we can print the pos and rotation of object in the World Frame print('ECM Base Pos:') print(ecm_handle.get_pos()) print('\n\n----') raw_input("Let's get Joints and Children Info. Press Enter to continue...") # We can get the number of children and joints connected to each object as ecm_num_joints = ecm_handle.get_num_joints( ) # Get the number of joints of this object print('Number of Joints in ECM:') print(ecm_num_joints) print(' ') print('Name of PSM\'s children:') print('\n\n----') raw_input("Control ECMs joint positions. Press Enter to continue...") # Now let's control some joints # The 1st joint, which the ECM Yaw ecm_handle.set_joint_pos(0, 0) # The 2nd joint, which is the ECM Pitch print('\n\n----') raw_input( "Mixed Pos and Effort control of PSM\'s joints. Press Enter to continue..." ) print('\n\n----') raw_input( "Set force on MTM's Wrist Yaw link for 5 secs. Press Enter to continue..." ) # Let's directly control the forces and torques on the mtmWristYaw Link # Notice that these are in the world frame. Another important thing to notice # is that unlike position control, forces control requires a continuous update # to meet a watchdog timing condition otherwise the forces are reset Null. This # is purely for safety reasons to prevent unchecked forces in case of malfunctioning # python client code print('\n\n----') raw_input("Let's clean up. Press Enter to continue...") # Lastly to cleanup _client.clean_up()
q4_res = round(-math.pi + math.acos((a**2 + c**2 - b**2)/(2*a*c)),3) A6 = np.array([math.cos(math.pi/2 + q4_res), -math.sin(math.pi/2 + q4_res)*math.cos(0), math.sin(math.pi/2 + q4_res)*math.sin(0), a*math.cos(math.pi/2 + q4_res), math.sin(math.pi/2 + q4_res), math.cos(math.pi/2 + q4_res)*math.cos(0), -math.cos(math.pi/2 + q4_res)*math.sin(0), a*math.sin(math.pi/2 + q4_res), 0, math.sin(0), math.cos(0), 0, 0, 0, 0, 1]).reshape((4,4)) T_2_5 = np.matmul(np.matmul(np.linalg.inv(shoulder_pose),end_effector_pose),np.linalg.inv(A6)) q2_res = round(math.asin(-T_2_5[2,1]),3) q3_res = round(math.asin(T_2_5[2,2]/math.cos(q2_res)),3) q1_res = round(math.asin(T_2_5[1,1]/math.cos(q2_res)),3) return [q1_res,q2_res,q3_res,q4_res] # Initialize client c = Client() # Connect client and simulation c.connect() time.sleep(2) b = c.get_obj_handle('base') elbow = c.get_obj_handle('elbow') tip = c.get_obj_handle('wrist') # Joints that can be actuated joint_list = [0,1,2,5] base_joint_names = [] for item in joint_list: base_joint_names.append(b.get_joint_names()[item]) print(base_joint_names) # Set all joints to 0 for item in joint_list: b.set_joint_pos(item,0) time.sleep(0.01) time.sleep(3)
def test_ambf_ecm(): c = Client('ecm_ik_test') c.connect() time.sleep(2.0) print(c.get_obj_names()) b = c.get_obj_handle('ecm/baselink') target_ik = c.get_obj_handle('ecm/target_ik') target_fk = c.get_obj_handle('ecm/target_fk') time.sleep(1.0) # The following are the names of the controllable joints. # 'baselink-yawlink', 0 # 'yawlink-pitchbacklink', 1 # 'pitchendlink-maininsertionlink', 2 # 'maininsertionlink-toollink', 3 num_joints = 4 joint_lims = np.zeros((num_joints, 2)) joint_lims[0] = [np.deg2rad(-91.96), np.deg2rad(91.96)] joint_lims[1] = [np.deg2rad(-60), np.deg2rad(60)] joint_lims[2] = [0.0, 0.24] joint_lims[3] = [np.deg2rad(-175), np.deg2rad(175)] js_traj = JointSpaceTrajectory(num_joints=num_joints, num_traj_points=50, joint_limits=joint_lims) num_points = js_traj.get_num_traj_points() for i in range(num_points): test_q = js_traj.get_traj_at_point(i) T_4_0 = compute_FK(test_q) if target_ik is not None: P_0_w = Vector(b.get_pos().x, b.get_pos().y, b.get_pos().z) R_0_w = Rotation.RPY(b.get_rpy()[0], b.get_rpy()[1], b.get_rpy()[2]) T_0_w = Frame(R_0_w, P_0_w) T_7_w = T_0_w * convert_mat_to_frame(T_4_0) target_ik.set_pos(T_7_w.p[0], T_7_w.p[1], T_7_w.p[2]) target_ik.set_rpy(T_7_w.M.GetRPY()[0], T_7_w.M.GetRPY()[1], T_7_w.M.GetRPY()[2]) computed_q = compute_IK(convert_mat_to_frame(T_4_0)) # computed_q = enforce_limits(computed_q, joint_lims) if target_fk is not None: P_0_w = Vector(b.get_pos().x, b.get_pos().y, b.get_pos().z) R_0_w = Rotation.RPY(b.get_rpy()[0], b.get_rpy()[1], b.get_rpy()[2]) T_0_w = Frame(R_0_w, P_0_w) T_4_0_fk = compute_FK(computed_q) T_4_w = T_0_w * convert_mat_to_frame(T_4_0_fk) target_fk.set_pos(T_4_w.p[0], T_4_w.p[1], T_4_w.p[2]) target_fk.set_rpy(T_4_w.M.GetRPY()[0], T_4_w.M.GetRPY()[1], T_4_w.M.GetRPY()[2]) # print('SETTING JOINTS: ') # print(computed_q) b.set_joint_pos('baselink-yawlink', computed_q[0]) b.set_joint_pos('yawlink-pitchbacklink', computed_q[1]) b.set_joint_pos('pitchendlink-maininsertionlink', computed_q[2]) b.set_joint_pos('maininsertionlink-toollink', computed_q[3]) test_q = round_vec(test_q) T_4_0 = round_mat(T_4_0, 4, 4, 3) errors = [0] * num_joints for j in range(num_joints): errors[j] = test_q[j] - computed_q[j] print('--------------------------------------') print('**************************************') print('Test Number:', i) print('Joint Positions used to T_EE_B (EndEffector in Base)') print test_q print('Requested Transform for T_EE_B (EndEffector in Base)') print(T_4_0) print('Joint Errors from IK Solver') print["{0:0.2f}".format(k) for k in errors] time.sleep(1.0) time.sleep(3.0)
#! /usr/bin/env python import time # Import the Client from ambf_client package from ambf_client import Client # Create a instance of the client _client = Client() # Connect the client which in turn creates callable objects from ROS topics # and initiates a shared pool of threads for bi-directional communication _client.connect() print('\n\n----') print("List of Objects") print(_client.get_obj_names()) _handle = _client.get_obj_handle('base') time.sleep(0.2) print('\n\n----') # print(' Base Pos:') # print(_handle.get_pos()) # print(' Base Rotation as Quaternion:') # print(_handle.get_rot()) print('\n\n----')
def test_ambf_mtm(): c = Client('mtm_ik_test') c.connect() time.sleep(2.0) print(c.get_obj_names()) b = c.get_obj_handle('mtm/TopPanel') target_ik = c.get_obj_handle('mtm/target_ik') target_fk = c.get_obj_handle('mtm/target_fk') time.sleep(1.0) # The following are the names of the controllable joints. # 'TopPanel-OutPitchShoulder', 0 # 'OutPitchShoulder-ArmParallel', 1 # 'ArmParallel-BottomArm', 2 # 'BottomArm-WristPlatform', 3 # 'WristPlatform-WristPitch', 4 # 'WristPitch-WristYaw', 5 # 'WristYaw-WristRoll', 6 num_joints = 7 joint_lims = np.zeros((num_joints, 2)) joint_lims[0] = [np.deg2rad(-75), np.deg2rad(44.98)] joint_lims[1] = [np.deg2rad(-44.98), np.deg2rad(44.98)] joint_lims[2] = [np.deg2rad(-44.98), np.deg2rad(44.98)] joint_lims[3] = [np.deg2rad(-59), np.deg2rad(245)] joint_lims[4] = [np.deg2rad(-90), np.deg2rad(180)] joint_lims[5] = [np.deg2rad(-44.98), np.deg2rad(44.98)] joint_lims[6] = [np.deg2rad(-175), np.deg2rad(175)] js_traj = JointSpaceTrajectory(num_joints=7, num_traj_points=50, joint_limits=joint_lims) num_points = js_traj.get_num_traj_points() num_joints = 7 for i in range(num_points): test_q = js_traj.get_traj_at_point(i) T_7_0 = compute_FK(test_q) if target_ik is not None: P_0_w = Vector(b.get_pos().x, b.get_pos().y, b.get_pos().z) R_0_w = Rotation.RPY(b.get_rpy()[0], b.get_rpy()[1], b.get_rpy()[2]) T_0_w = Frame(R_0_w, P_0_w) T_7_w = T_0_w * convert_mat_to_frame(T_7_0) target_ik.set_pos(T_7_w.p[0], T_7_w.p[1], T_7_w.p[2]) target_ik.set_rpy(T_7_w.M.GetRPY()[0], T_7_w.M.GetRPY()[1], T_7_w.M.GetRPY()[2]) computed_q = compute_IK(convert_mat_to_frame(T_7_0)) # computed_q = enforce_limits(computed_q, joint_lims) if target_fk is not None: P_0_w = Vector(b.get_pos().x, b.get_pos().y, b.get_pos().z) R_0_w = Rotation.RPY(b.get_rpy()[0], b.get_rpy()[1], b.get_rpy()[2]) T_0_w = Frame(R_0_w, P_0_w) T_7_0_fk = compute_FK(computed_q) T_7_w = T_0_w * convert_mat_to_frame(T_7_0_fk) target_fk.set_pos(T_7_w.p[0], T_7_w.p[1], T_7_w.p[2]) target_fk.set_rpy(T_7_w.M.GetRPY()[0], T_7_w.M.GetRPY()[1], T_7_w.M.GetRPY()[2]) # print('SETTING JOINTS: ') # print(computed_q) b.set_joint_pos('TopPanel-OutPitchShoulder', computed_q[0]) b.set_joint_pos('OutPitchShoulder-ArmParallel', computed_q[1]) b.set_joint_pos('ArmParallel-BottomArm', computed_q[2]) b.set_joint_pos('BottomArm-WristPlatform', computed_q[3]) b.set_joint_pos('WristPlatform-WristPitch', computed_q[4]) b.set_joint_pos('WristPitch-WristYaw', computed_q[5]) b.set_joint_pos('WristYaw-WristRoll', computed_q[6]) test_q = round_vec(test_q) T_7_0 = round_mat(T_7_0, 4, 4, 3) errors = [0]*num_joints print ('--------------------------------------') print ('**************************************') print ('Test Number:', i) print ('Joint Positions used to T_EE_B (EndEffector in Base)') print test_q print ('Requested Transform for T_EE_B (EndEffector in Base)') print (T_7_0) print ('Joint Errors from IK Solver') print ["{0:0.2f}".format(k) for k in errors] time.sleep(1.0) time.sleep(3.0)