def _record_demonstration(self): observations = [] while True: robot = self.environment.robot if robot._gripper: if robot._cuff.upper_button(): robot._gripper.open() elif robot._cuff.lower_button(): robot._gripper.close() data = { "time": self._time_stamp(), "robot": self.environment.get_robot_state(), "items": self.environment.get_item_state(), "triggered_constraints": self.environment.check_constraint_triggers() } observation = Observation(data) if self.publish_constraint_validity: valid_constraints = check_constraint_validity(self.environment, self.environment.constraints, observation)[1] pub = rospy.Publisher('cairo_lfd/valid_constraints', Int8MultiArray, queue_size=10) msg = Int8MultiArray(data=valid_constraints) pub.publish(msg) observations.append(observation) if self.command == "discard": rospy.loginfo("~~~DISCARDED~~~") self.interaction_publisher.send_position_mode_cmd() self._clear_command() return [] if self.command == "capture": rospy.loginfo("~~~CAPTURED~~~") self.interaction_publisher.send_position_mode_cmd() self._clear_command() rospy.loginfo("{} observations captured.".format(len(observations))) return observations self._rate.sleep()
def handle(self, constraint_type): # Get filepath to demo data main_filepath = rospy.get_param("MAIN_FILEPATH") config_filepath = main_filepath + "/config.json" pos_directory = main_filepath + "/positive/labeled" neg_directory = main_filepath + "/negative/labeled" # Set data directory by constraint value if constraint_type.constraint == 0: directory = neg_directory else: directory = pos_directory # Setup LfD parameters bandwidth = 0.025 number_of_samples = 5 rospy.loginfo("CC-LfD: %s" % str(constraint_type.constraint)) # Import the data importer = DataImporter() configs = import_configuration(config_filepath) labeled_demonstrations = importer.load_json_files(directory + "/*.json") # Convert imported data into Demonstrations and Observations demonstrations = [] for datum in labeled_demonstrations["data"]: observations = [] for entry in datum: observations.append(Observation(entry)) demonstrations.append(Demonstration(observations)) if len(demonstrations) == 0: rospy.logwarn("No demonstration data to model!!") return False """ Create the moveit_interface """ moveit_interface = SawyerMoveitInterface() moveit_interface.set_velocity_scaling(.35) moveit_interface.set_acceleration_scaling(.25) cclfd = CC_LFD(configs, moveit_interface) cclfd.build_environment() cclfd.build_keyframe_graph(demonstrations, bandwidth) cclfd.sample_keyframes(number_of_samples) cclfd.perform_skill() return True
def main(): arg_fmt = argparse.RawDescriptionHelpFormatter parser = argparse.ArgumentParser(formatter_class=arg_fmt, description=main.__doc__) required = parser.add_argument_group('required arguments') required.add_argument( '-c', '--config', dest='config', required=True, help='the file path of configuration config.json file ' ) required.add_argument( '-d', '--directory', dest='directory', required=True, help='the directory from which to input labeled demonstration .json files' ) parser.add_argument( '-b', '--bandwidth', type=float, default=.025, metavar='BANDWIDTH', help='gaussian kernel density bandwidth' ) parser.add_argument( '-t', '--threshold', type=int, metavar='THRESHOLD', help='log-liklihood threshold value' ) parser.add_argument( '-n', '--number_of_samples', type=int, default=50, metavar='NUMBEROFSAMPLES', help='the number of samples to validate for each keyframe' ) args = parser.parse_args(rospy.myargv()[1:]) rospy.init_node("graph_traverse") # Import the data importer = DataImporter() config_filepath = args.config configs = import_configuration(config_filepath) labeled_demonstrations = importer.load_json_files(args.directory + "/*.json") # Convert imported data into Demonstrations and Observations demonstrations = [] for datum in labeled_demonstrations["data"]: observations = [] for entry in datum: observations.append(Observation(entry)) demonstrations.append(Demonstration(labeled_observations=observations)) if len(demonstrations) == 0: rospy.logwarn("No demonstration data to model!!") return 0 """ Create the moveit_interface """ moveit_interface = SawyerMoveitInterface() moveit_interface.set_velocity_scaling(.35) moveit_interface.set_acceleration_scaling(.25) moveit_interface.set_planner(str(configs["settings"]["planner"])) lfd = LFD(configs, moveit_interface) lfd.build_environment() lfd.build_keyframe_graph(demonstrations, args.bandwidth) if args.threshold is not None: rospy.loginfo("Using user provided culling threshold of {}".format(args.threshold)) lfd.sample_keyframes(args.number_of_samples, automate_threshold=False, culling_threshold=args.threshold) else: lfd.sample_keyframes(args.number_of_samples, automate_threshold=True) lfd.perform_skill()
def main(): arg_fmt = argparse.RawDescriptionHelpFormatter parser = argparse.ArgumentParser(formatter_class=arg_fmt, description=main.__doc__) required = parser.add_argument_group('required arguments') required.add_argument('-c', '--config', dest='config', required=True, help='the file path of the demonstration ') required.add_argument( '-i', '--input_directory', dest='input_directory', required=False, help='the directory from which to input prior demonstration .json files' ) required.add_argument( '-o', '--output_directory', dest='output_directory', required=False, help='the directory to save the given subjects .json files data') required.add_argument('-t', '--task', dest='task', required=False, help='the name of the task being demonstrated') required.add_argument('-s', '--subject', dest='subject', required=False, help='the ID of the subject') args = parser.parse_args(rospy.myargv()[1:]) ############################ # ROS Node Initialization # ############################ print("Initializing node... ") rospy.init_node("ar4lfd") print("Getting robot state... ") robot_state = intera_interface.RobotEnable(CHECK_VERSION) print("Enabling robot... ") robot_state.enable() ######################## # Import Configuration # ######################## config_filepath = args.config configs = load_lfd_configuration(config_filepath) ################################# # Configure the LFD class model # ################################# model_settings = configs["settings"]["modeling_settings"] moveit_interface = SawyerMoveitInterface() moveit_interface.set_velocity_scaling(.35) moveit_interface.set_acceleration_scaling(.25) moveit_interface.set_planner(str(model_settings["planner"])) acclfd = ACC_LFD(configs, model_settings, moveit_interface) acclfd.build_environment() ##################################### # Raw Demonstration Data Processors # ##################################### # Build processors and process demonstrations to generate derivative data e.g. relative position. rk_processor = RelativeKinematicsProcessor( acclfd.environment.get_item_ids(), acclfd.environment.get_robot_id()) ic_processor = InContactProcessor(acclfd.environment.get_item_ids(), acclfd.environment.get_robot_id(), .06, .5) soi_processor = SphereOfInfluenceProcessor( acclfd.environment.get_item_ids(), acclfd.environment.get_robot_id()) rp_processor = RelativePositionProcessor(acclfd.environment.get_item_ids(), acclfd.environment.get_robot_id()) wp_processor = WithinPerimeterProcessor(acclfd.environment.get_item_ids(), acclfd.environment.get_robot_id()) processing_pipeline = DataProcessingPipeline([ rk_processor, ic_processor, soi_processor, rp_processor, wp_processor ]) ############################################### # Configure the Sawyer Demonstration Recorder # ############################################### rec_settings = configs["settings"]["recording_settings"] recorder = SawyerDemonstrationRecorder(rec_settings, acclfd.environment, processing_pipeline, publish_constraint_validity=True) rospy.on_shutdown(recorder.stop) ########################################################## # Configure the Sawyer Demonstration Aligner and Labeler # ########################################################## label_settings = configs["settings"]["labeling_settings"] # Demonstration vectorizor that converts observations into state vector in desired space for DTW alignment. demo_vectorizor = partial(vectorize_demonstration, vectorizors=[get_observation_joint_vector]) alignment = DemonstrationAlignment(demo_vectorizor) demo_labeler = SawyerDemonstrationLabeler(label_settings, alignment) ######################### # Import Initial Demos # ######################### if args.input_directory is not None: prior_poor_demonstrations = load_json_files(args.input_directory + "/*.json") # Convert imported data into Demonstrations and Observations demonstrations = [] for datum in prior_poor_demonstrations["data"]: observations = [] for entry in datum: observations.append(Observation(entry)) demonstrations.append(Demonstration(observations)) if len(demonstrations) == 0: rospy.logwarn( "No prior demonstration data to model!! You sure you're using the right experiment script?" ) return 0 labeled_initial_demos = demo_labeler.label(demonstrations) acclfd.build_keyframe_graph(labeled_initial_demos, model_settings.get("bandwidth", .025)) acclfd.generate_autoconstraints(labeled_initial_demos) acclfd.sample_keyframes(model_settings.get("number_of_samples", 50), automate_threshold=True) else: labeled_initial_demos = [] acclfd.build_keyframe_graph(labeled_initial_demos, model_settings.get("bandwidth", .025)) ####################################### # Set defaults for command line args # ####################################### if args.task is None: output_directory = "" else: output_directory = args.output_directory if args.task is None: task_name = "****" else: task_name = args.task if args.subject is None: subject = "####" else: subject = args.subject ######################### # Run Study Controller # ######################### study = ACCLfDController(acclfd, recorder, demo_labeler, labeled_initial_demos, output_directory, task_name, subject) study.run()
def main(): arg_fmt = argparse.RawDescriptionHelpFormatter parser = argparse.ArgumentParser(formatter_class=arg_fmt, description=main.__doc__) required = parser.add_argument_group('required arguments') # required.add_argument( # '-c', '--config', dest='config', required=True, # help='the file path of the demonstration ' # ) required.add_argument( '-i', '--input', dest='input_directory', required=True, help='the input directory to pull in raw demonstration .json files') # required.add_argument( # '-o', '--output', dest='output_directory', required=True, # help='the output directory for saving relative distance labeled demonstration .json files' # ) args = parser.parse_args(rospy.myargv()[1:]) print("Initializing node... ") # rospy.init_node("intermediate_trajectories") # print("Getting robot state... ") # rs = intera_interface.RobotEnable(CHECK_VERSION) # print("Enabling robot... ") # rs.enable() # config_filepath = args.config # configs = load_lfd_configuration(config_filepath) # items = ItemFactory(configs).generate_items() # constraints = ConstraintFactory(configs["constraints"]).generate_constraints() # # We only have just the one robot...for now....... # environment = Environment(items=items['items'], robot=items['robots'][0], constraints=constraints) # Import the data rospy.loginfo("Importing demonstration .json files...") raw_demonstrations = load_json_files(args.input_directory + "/*.json") # Convert imported data into Demonstrations and Observations demonstrations = [] for datum in raw_demonstrations["data"]: observations = [] for entry in datum: observations.append(Observation(entry)) demonstrations.append( Demonstration(None, labeled_observations=observations)) args = parser.parse_args(rospy.myargv()[1:]) inter_trajectories = IntermediateTrajectories().get_trajectories( demonstrations) it_data = { key: [[o.data for o in segment] for segment in group] for key, group in inter_trajectories.iteritems() } with file('./intermediate_trajectories.json', "w") as f: json.dump(it_data, f)
def main(): arg_fmt = argparse.RawDescriptionHelpFormatter parser = argparse.ArgumentParser(formatter_class=arg_fmt, description=main.__doc__) required = parser.add_argument_group('required arguments') required.add_argument( '-c', '--config', dest='config', required=True, help='the file path of configuration config.json file ') required.add_argument( '-d', '--directory', dest='directory', required=True, help= 'the directory from which to input labeled demonstration .json files') parser.add_argument('-b', '--bandwidth', type=float, default=.025, metavar='BANDWIDTH', help='gaussian kernel density bandwidth') parser.add_argument('-t', '--threshold', type=int, default=-1200, metavar='THRESHOLD', help='log-liklihood threshold value') parser.add_argument( '-n', '--number_of_samples', type=int, default=50, metavar='NUMBEROFSAMPLES', help='the number of samples to validate for each keyframe') args = parser.parse_args(rospy.myargv()[1:]) # Import the data importer = DataImporter() labeled_demonstrations = importer.load_json_files(args.directory + "/*.json") # Convert imported data into Demonstrations and Observations demonstrations = [] for datum in labeled_demonstrations["data"]: observations = [] for entry in datum: observations.append(Observation(entry)) demonstrations.append(Demonstration(observations)) if len(demonstrations) == 0: rospy.logwarn("No demonstration data to model!!") return 0 rospy.init_node("graph_traverse") """ Create the Cairo LfD environment """ config_filepath = args.config configs = import_configuration(config_filepath) items = ItemFactory(configs).generate_items() constraints = ConstraintFactory(configs).generate_constraints() # We only have just the one robot...for now....... environment = Environment(items=items['items'], robot=items['robots'][0], constraints=constraints, triggers=None) """ Create the moveit_interface """ moveit_interface = SawyerMoveitInterface() moveit_interface.set_velocity_scaling(.35) moveit_interface.set_acceleration_scaling(.25) """ Create KeyframeGraph object. """ graph = KeyframeGraph() cluster_generator = ObservationClusterer() """ Generate clusters using labeled observations, build the models, graphs, and atributes for each cluster in the KeyFrameGraph """ clusters = cluster_generator.generate_clusters(demonstrations) for cluster_id in clusters.keys(): graph.add_node(cluster_id) graph.nodes[cluster_id]["observations"] = clusters[cluster_id][ "observations"] graph.nodes[cluster_id]["keyframe_type"] = clusters[cluster_id][ "keyframe_type"] graph.nodes[cluster_id]["applied_constraints"] = clusters[cluster_id][ "applied_constraints"] graph.nodes[cluster_id]["meta_constraints"] = {} graph.nodes[cluster_id]["model"] = KDEModel(kernel='gaussian', bandwidth=args.bandwidth) graph.add_path(graph.nodes()) graph.fit_models(get_observation_joint_vector) graph._identify_primal_observations(get_observation_joint_vector) rospy.loginfo(graph.get_keyframe_sequence()) for node in graph.get_keyframe_sequence(): print("KEYFRAME: {}".format(node)) print(graph.nodes[node]["keyframe_type"]) print(graph.nodes[node]["applied_constraints"]) print # Create height segmentation and heuristic model position_vectorizor = partial(vectorize_demonstration, vectorizors=[vectorize_robot_position]) position_vectors = np.array(map(position_vectorizor, demonstrations)) # stack all observation vectors X = np.vstack(position_vectors) height_segment_model = BayesianGMMSegmentModel(X, n_components=10) height_heuristic = HeightHeuristicModel(height_segment_model) height_heuristic.fit() height_static_parameters = { "item_id": 1, "reference_height": 0, "direction": "positive" } height_metaconstraint_builder = HeightMetaconstraintBuilder( height_heuristic, height_static_parameters) metaconstraint_assigner = MetaconstraintAssigner( environment, graph, [height_metaconstraint_builder]) metaconstraint_assigner.assign_metaconstraints() """ Build a ConstraintAnalyzer and KeyframeGraphAnalyzer """ constraint_analyzer = ConstraintAnalyzer(environment) graph_analyzer = KeyframeGraphAnalyzer(graph, moveit_interface, get_observation_joint_vector) sample_to_obsv_converter = SawyerSampleConverter(moveit_interface) sampler = KeyframeSampler(constraint_analyzer, sample_to_obsv_converter) """ Generate raw_samples from graph for each keyframe """ for node in graph.get_keyframe_sequence(): n_samples = args.number_of_samples constraints = [ meta.constraints[4] for meta in graph.nodes[node]["metaconstraints"] ] for constraint in constraints: print constraint attempts, samples, matched_ids = sampler.generate_n_valid_samples( graph.nodes[node]["model"], graph.nodes[node]["primal_observation"], constraints, n=n_samples) rospy.loginfo("Keyframe %d: %s valid of %s attempts", node, len(samples), attempts) if len(samples) < n_samples: rospy.loginfo("Keyframe %d: only %s of %s waypoints provided", node, len(samples), n_samples) if len(samples) == 0: # TODO: DOWN SAMPLE METACONSTRAINTS AND KEEP TESTING rospy.loginfo("Keyframe %d has no valid sample observations", node) rospy.loginfo("Sampling with no meta constraints") attempts, samples, matched_ids = sampler.generate_n_valid_samples( graph.nodes[node]["model"], graph.nodes[node]["primal_observation"], [], n=n_samples) # Order sampled points based on their intra-model log-likelihood ranked_samples = sampler.rank_samples(graph.nodes[node]["model"], samples) # User converter object to convert raw sample vectors into LfD observations graph.nodes[node]["samples"] = [ sample_to_obsv_converter.convert(sample, run_fk=True) for sample in ranked_samples ] """ Clear occluded points (points in collision etc,.) """ for node in graph.get_keyframe_sequence(): samples = graph.nodes[node]["samples"] free_samples, trash = graph_analyzer.evaluate_keyframe_occlusion( samples) if free_samples == []: rospy.loginfo( "Keyframe {} has no free samples and will be culled.".format( node)) graph.cull_node(node) else: graph.nodes[node]["free_samples"] = free_samples """ Cull/remove keyframes/nodes that via change point estimation using log-likelihood """ graph_analyzer.cull_keyframes(threshold=args.threshold) # """ Order sampled points based on their intra-model log-likelihood """ # for node in graph.get_keyframe_sequence(): # graph.rank_waypoint_samples(node) output = [] """ Create a sequence of keyframe way points and execute motion plans to reconstruct skill """ joint_config_array = [] for node in graph.get_keyframe_sequence(): output.append((node, graph.nodes[node]["applied_constraints"])) sample = graph.nodes[node]["free_samples"][0] joints = sample.get_joint_angle() joint_config_array.append(joints) print output # moveit_interface.move_to_joint_targets(joint_config_array) return 0
def main(): arg_fmt = argparse.RawDescriptionHelpFormatter parser = argparse.ArgumentParser(formatter_class=arg_fmt, description=main.__doc__) required = parser.add_argument_group('required arguments') required.add_argument( '-c', '--config', dest='config', required=True, help='the file path of configuration config.json file ' ) required.add_argument( '-d', '--directory', dest='directory', required=True, help='the directory from which to input labeled demonstration .json files' ) parser.add_argument( '-b', '--bandwidth', type=float, default=.025, metavar='BANDWIDTH', help='gaussian kernel density bandwidth' ) parser.add_argument( '-t', '--threshold', type=int, default=, metavar='THRESHOLD', help='Kullbach-Leibler divergence threshold value - optional' ) parser.add_argument( '-n', '--number_of_samples', type=int, default=50, metavar='NUMBEROFSAMPLES', help='the number of samples to validate for each keyframe' ) args = parser.parse_args(rospy.myargv()[1:]) # Import the data importer = DataImporter() labeled_demonstrations = importer.load_json_files(args.directory + "/*.json") # Convert imported data into Demonstrations and Observations demonstrations = [] for datum in labeled_demonstrations["data"]: observations = [] for entry in datum: observations.append(Observation(entry)) demonstrations.append(Demonstration(observations)) if len(demonstrations) == 0: rospy.logwarn("No demonstration data to model!!") return 0 rospy.init_node("graph_traverse") """ Create the Cairo LfD environment """ config_filepath = args.config configs = import_configuration(config_filepath) items = ItemFactory(configs).generate_items() constraints = ConstraintFactory(configs).generate_constraints() # We only have just the one robot...for now....... environment = Environment(items=items['items'], robot=items['robots'][0], constraints=constraints, triggers=None) """ Create the moveit_interface """ moveit_interface = SawyerMoveitInterface() moveit_interface.set_velocity_scaling(.35) moveit_interface.set_acceleration_scaling(.25) """ Create KeyframeGraph object. """ graph = KeyframeGraph() cluster_generator = ObservationClusterer() """ Generate clusters using labeled observations, build the models, graphs, and atributes for each cluster in the KeyFrameGraph """ clusters = cluster_generator.generate_clusters(demonstrations) for cluster_id in clusters.keys(): graph.add_node(cluster_id) graph.nodes[cluster_id]["observations"] = clusters[cluster_id]["observations"] graph.nodes[cluster_id]["keyframe_type"] = clusters[cluster_id]["keyframe_type"] graph.nodes[cluster_id]["applied_constraints"] = clusters[cluster_id]["applied_constraints"] graph.nodes[cluster_id]["model"] = KDEModel(kernel='gaussian', bandwidth=args.bandwidth) graph.add_path(graph.nodes()) graph.fit_models(get_observation_joint_vector) graph._identify_primal_observations(get_observation_joint_vector) rospy.loginfo(graph.get_keyframe_sequence()) for node in graph.get_keyframe_sequence(): print("KEYFRAME: {}".format(node)) print(graph.nodes[node]["keyframe_type"]) print(graph.nodes[node]["applied_constraints"]) print """ Build a ConstraintAnalyzer and KeyframeGraphAnalyzer """ constraint_analyzer = ConstraintAnalyzer(environment) graph_analyzer = KeyframeGraphAnalyzer(graph, moveit_interface, get_observation_joint_vector) sample_to_obsv_converter = SawyerSampleConverter(moveit_interface) sampler = KeyframeSampler(constraint_analyzer, sample_to_obsv_converter) model_score_ranker = ModelScoreSampleRanker() configraution_ranker = ConfigurationSpaceSampleRanker() """ Generate raw_samples from graph for each keyframe """ prior_sample = None for node in graph.get_keyframe_sequence(): print "Keyframe {}".format(node) # Keep sampling if graph.nodes[node]["keyframe_type"] == "constraint_transition": rospy.loginfo("Sampling from a constraint transition keyframe.") constraints = [environment.get_constraint_by_id(constraint_id) for constraint_id in graph.nodes[node]["applied_constraints"]] attempts, samples, matched_ids = sampler.generate_n_valid_samples(graph.nodes[node]["model"], graph.nodes[node]["primal_observation"], constraints, n=n_samples) if len(samples) == 0: # Some constraints couldn't be sampled successfully, so using best available samples. diff = list(set(graph.nodes[node]["applied_constraints"]).difference(set(matched_ids))) if len(matched_ids) > 0: rospy.logwarn("Constraints {} couldn't be met so attempting to find valid samples with constraints {}.".format(diff, matched_ids)) constraints = [environment.get_constraint_by_id(constraint_id) for constraint_id in graph.nodes[node]["applied_constraints"]] attempts, samples, matched_ids = sampler.generate_n_valid_samples(graph.nodes[node]["model"], graph.nodes[node]["primal_observation"], constraints, n=n_samples) else: rospy.logwarn("Constraints {} couldn't be met so. Cannot meet any constraints.".format(diff)) else: n_samples = args.number_of_samples constraints = [environment.get_constraint_by_id(constraint_id) for constraint_id in graph.nodes[node]["applied_constraints"]] attempts, samples, matched_ids = sampler.generate_n_valid_samples(graph.nodes[node]["model"], graph.nodes[node]["primal_observation"], constraints, n=n_samples) rospy.loginfo("Keyframe %d: %s valid of %s attempts", node, len(samples), attempts) if len(samples) < n_samples: rospy.loginfo("Keyframe %d: only %s of %s waypoints provided", node, len(samples), n_samples) if len(samples) == 0: rospy.loginfo("Keyframe %d has no valid sample observations", node) graph.cull_node(node) else: # Order sampled points based on their intra-model log-likelihood if prior_sample is None: ranked_samples = model_score_ranker.rank(graph.nodes[node]["model"], samples) else: ranked_samples = configraution_ranker.rank(graph.nodes[node]["model"], samples, prior_sample) prior_sample = ranked_samples[0] # User converter object to convert raw sample vectors into LfD observations graph.nodes[node]["samples"] = [sample_to_obsv_converter.convert(sample, run_fk=True) for sample in ranked_samples] """ Clear occluded points (points in collision etc,.) """ for node in graph.get_keyframe_sequence(): samples = graph.nodes[node]["samples"] free_samples, trash = graph_analyzer.evaluate_keyframe_occlusion(samples) if free_samples == []: rospy.loginfo("Keyframe {} has no free samples and will be culled.".format(node)) graph.cull_node(node) else: graph.nodes[node]["free_samples"] = free_samples """ Cull/remove keyframes/nodes that via change point estimation using log-likelihood """ if "automated_culling_threshold" in config['settings']: automated_threshold = config['settings']['automated_culling_threshold'] else: automated_culling = True graph_analyzer.cull_keyframes(threshold=args.threshold, automated=automated_threshold) # """ Order sampled points based on their intra-model log-likelihood """ # for node in graph.get_keyframe_sequence(): # graph.rank_waypoint_samples(node) output = [] """ Create a sequence of keyframe way points and execute motion plans to reconstruct skill """ joint_config_array = [] for node in graph.get_keyframe_sequence(): output.append((node, graph.nodes[node]["applied_constraints"])) sample = graph.nodes[node]["free_samples"][0] joints = sample.get_joint_angle() joint_config_array.append(joints) print output moveit_interface.move_to_joint_targets(joint_config_array) return 0
def main(): """ Demonstration Recorder Record a series of demonstrations. """ arg_fmt = argparse.RawDescriptionHelpFormatter parser = argparse.ArgumentParser(formatter_class=arg_fmt, description=main.__doc__) required = parser.add_argument_group('required arguments') required.add_argument('-c', '--config', dest='config', required=True, help='the file path of the demonstration ') parser.add_argument('-r', '--record_rate', type=int, default=45, metavar='RECORDRATE', help='rate at which to record (default: 45)') args = parser.parse_args(rospy.myargv()[1:]) print("Initializing node... ") rospy.init_node("live_constraint") print("Getting robot state... ") robot_state = intera_interface.RobotEnable(CHECK_VERSION) print("Enabling robot... ") robot_state.enable() interaction_pub = InteractionPublisher() interaction_options = InteractionOptions() interaction_options.set_max_impedance([False]) interaction_options.set_rotations_for_constrained_zeroG(True) interaction_frame = Pose() interaction_frame.position.x = 0 interaction_frame.position.y = 0 interaction_frame.position.z = 0 interaction_frame.orientation.x = 0 interaction_frame.orientation.y = 0 interaction_frame.orientation.z = 0 interaction_frame.orientation.w = 1 interaction_options.set_K_impedance([0, 0, 0, 0, 0, 0]) interaction_options.set_K_nullspace([5, 5, 5, 5, 5, 5, 5]) interaction_options.set_interaction_frame(interaction_frame) rospy.loginfo(interaction_options.to_msg()) rospy.on_shutdown(interaction_pub.send_position_mode_cmd) interaction_pub.external_rate_send_command(interaction_options) config_filepath = args.config configs = import_configuration(config_filepath) items = ItemFactory(configs).generate_items() triggers = TriggerFactory(configs).generate_triggers() constraints = ConstraintFactory(configs).generate_constraints() constraint_ids = [constraint.id for constraint in constraints] print("Constraint IDs: {}".format(constraint_ids)) # We only have just the one robot...for now....... environment = Environment(items=items['items'], robot=items['robots'][0], constraints=constraints, triggers=triggers) constraint_analyzer = ConstraintAnalyzer(environment) user_input = "" while environment.robot._navigator.get_button_state( "right_button_back") != 2 or user_input == "q": stdin, stdout, stderr = select.select([sys.stdin], [], [], .0001) for s in stdin: if s == sys.stdin: user_input = sys.stdin.readline().strip() data = { "robot": environment.get_robot_state(), "items": environment.get_item_state(), "triggered_constraints": environment.check_constraint_triggers() } observation = Observation(data) print "Position: " + str(data["robot"]["position"]) print "Orientation: " + str(data["robot"]["orientation"]) print "Config" + str(data["robot"]["joint_angle"]) print(constraint_analyzer.evaluate(constraints, observation)) print(data["triggered_constraints"]) valid_constraints = constraint_analyzer.evaluate( environment.constraints, observation)[1] pub = rospy.Publisher('cairo_lfd/valid_constraints', Int8MultiArray, queue_size=10) msg = Int8MultiArray(data=valid_constraints) pub.publish(msg) rospy.sleep(1) if rospy.is_shutdown(): return 1