Best Python code snippet using uiautomator
hydra_system.py
Source:hydra_system.py  
1#! /usr/bin/env python32import numpy as np3import os4from threading import Lock5import logging6# ROS7import rospy8import sensor_msgs.msg9import geometry_msgs.msg10import tf2_ros11import tf12import razer_hydra.msg13import meshcat14import meshcat.geometry as meshcat_geom15import meshcat.transformations as meshcat_tf16import pydrake17from pydrake.all import (18    AbstractValue,19    AngleAxis,20    BasicVector,21    BodyIndex,22    ExternallyAppliedSpatialForce,23    LeafSystem,24    List,25    Quaternion,26    QueryObject,27    RollPitchYaw,28    RotationMatrix,29    RigidTransform,30    SpatialForce,31    Value32)33def ros_tf_to_rigid_transform(msg):34    return RigidTransform(35        p=[msg.translation.x, msg.translation.y, msg.translation.z],36        R=RotationMatrix(Quaternion(msg.rotation.w, msg.rotation.x,37                                    msg.rotation.y, msg.rotation.z)))38class HydraInteractionLeafSystem(LeafSystem):39    ''' Handles comms with the Hydra, and uses QueryObject inputs from the SceneGraph40    to pick closests points when required. Passes this information to the HydraInteractionForceElement41    at every update tick.42    Construct by supplying the MBPlant and SceneGraph under sim + a list of the body IDs43    that are manipulable. Given ZMQ information, visualizes hand pose with a hand model.44    TODO: Hand geometric may want to be handed to SceneGraph to visualize; would need to45    investigate piping for manually specifying poses of objects not in MBP. '''46    def __init__(self, mbp, sg, all_manipulable_body_ids=[], zmq_url="default"):47        LeafSystem.__init__(self)48        self.all_manipulable_body_ids = all_manipulable_body_ids49        self.set_name('HydraInteractionLeafSystem')50        self._geometry_query_input_port = self.DeclareAbstractInputPort(51            "geometry_query", AbstractValue.Make(QueryObject()))52        self.robot_state_input_port = self.DeclareVectorInputPort(53            "robot_state", BasicVector(mbp.num_positions() + mbp.num_velocities()))54        forces_cls = Value[List[ExternallyAppliedSpatialForce]]55        self.spatial_forces_output_port = self.DeclareAbstractOutputPort(56            "spatial_forces_vector",57            lambda: forces_cls(),58            self.DoCalcAbstractOutput)59        self.DeclarePeriodicPublish(0.01, 0.0)60        if zmq_url == "default":61            zmq_url = "tcp://127.0.0.1:6000"62        if zmq_url is not None:63            logging.info("Connecting to meshcat-server at zmq_url=" + zmq_url + "...")64        self.vis = meshcat.Visualizer(zmq_url=zmq_url)65        fwd_pt_in_hydra_frame = RigidTransform(p=[0.0, 0.0, 0.0])66        self.vis["hydra_origin"]["hand"].set_object(67            meshcat_geom.ObjMeshGeometry.from_file(68                os.path.join(os.path.dirname(os.path.abspath(__file__)), "hand-regularfinal-scaled-1.obj"))69        )70        self.vis["hydra_origin"]["hand"].set_transform(meshcat_tf.compose_matrix(scale=[0.001, 0.001, 0.001], angles=[np.pi/2, 0., np.pi/2], translate=[-0.25, 0., 0.]))71        #self.vis["hydra_origin"]["center"].set_object(meshcat_geom.Sphere(0.02))72        #self.vis["hydra_origin"]["center"].set_transform(meshcat_tf.translation_matrix([-0.025, 0., 0.]))73        #self.vis["hydra_origin"]["mid"].set_object(meshcat_geom.Sphere(0.015))74        #self.vis["hydra_origin"]["mid"].set_transform(meshcat_tf.translation_matrix([0.0, 0., 0.]))75        #self.vis["hydra_origin"]["fwd"].set_object(meshcat_geom.Sphere(0.01))76        #self.vis["hydra_origin"]["fwd"].set_transform(fwd_pt_in_hydra_frame.matrix())77        #self.vis["hydra_grab"].set_object(meshcat_geom.Sphere(0.01), 78        #                                  meshcat_geom.MeshLambertMaterial(79        #                                     color=0xff22dd,80        #                                     alphaMap=0.1))81        self.vis["hydra_grab"]["grab_point"].set_object(meshcat_geom.Sphere(0.01), 82                                                   meshcat_geom.MeshLambertMaterial(83                                                      color=0xff22dd,84                                                      alphaMap=0.1))85        # Hide it sketchily86        self.vis["hydra_grab"].set_transform(meshcat_tf.translation_matrix([0., 0., -1000.]))87        # State for selecting objects88        self.grab_needs_update = False89        self.grab_in_progress = False90        self.grab_update_hydra_pose = None91        self.selected_body = None92        self.selected_pose_in_body_frame = None93        self.desired_pose_in_world_frame = None94        self.stop = False95        self.freeze_rotation = False96        self.previously_freezing_rotation = False97        # Set up subscription to Razer Hydra98        self.mbp = mbp99        self.mbp_context = mbp.CreateDefaultContext()100        self.sg = sg101        self.hasNewMessage = False102        self.lastMsg = None103        self.hydra_origin = RigidTransform(p=[1.0, 0., -0.1],104                                   rpy=RollPitchYaw([0., 0., 0.]))105        self.hydra_prescale = 3.0106        self.callback_lock = Lock()107        self.hydraSubscriber = rospy.Subscriber("/hydra_calib", razer_hydra.msg.Hydra, self.callback, queue_size=1)108        logging.info("Waiting for hydra startup...")109        while not self.hasNewMessage  and not rospy.is_shutdown():110            rospy.sleep(0.01)111        logging.info("Got hydra.")112    def DoCalcAbstractOutput(self, context, y_data):113        self.callback_lock.acquire()114        if self.selected_body and self.grab_in_progress:115            # Simple inverse dynamics PD rule to drive object to desired pose.116            body = self.selected_body117            # Load in robot state118            x_in = self.EvalVectorInput(context, 1).get_value()119            self.mbp.SetPositionsAndVelocities(self.mbp_context, x_in)120            TF_object = self.mbp.EvalBodyPoseInWorld(self.mbp_context, body)121            xyz = TF_object.translation()122            R = TF_object.rotation().matrix()123            TFd_object = self.mbp.EvalBodySpatialVelocityInWorld(self.mbp_context, body)124            xyzd = TFd_object.translational()125            Rd = TFd_object.rotational()126            # Match the object position directly to the hydra position.127            xyz_desired = self.desired_pose_in_world_frame.translation()128            # Regress xyz back to just the hydra pose in the attraction case129            if self.previously_freezing_rotation != self.freeze_rotation:130                self.selected_body_init_offset = TF_object131                self.grab_update_hydra_pose = RigidTransform(self.desired_pose_in_world_frame)132            self.previously_freezing_rotation = self.freeze_rotation133            if self.freeze_rotation:134                R_desired = self.selected_body_init_offset.rotation().matrix()135            else:136                # Figure out the relative rotation of the hydra from its initial posture137                to_init_hydra_tf = self.grab_update_hydra_pose.inverse()138                desired_delta_rotation = to_init_hydra_tf.multiply(self.desired_pose_in_world_frame).GetAsMatrix4()[:3, :3]139                # Transform the current object rotation into the init hydra frame, apply that relative tf, and140                # then transform back141                to_init_hydra_tf_rot = to_init_hydra_tf.GetAsMatrix4()[:3, :3]142                R_desired = to_init_hydra_tf_rot.T.dot(143                    desired_delta_rotation.dot(to_init_hydra_tf_rot.dot(144                        self.selected_body_init_offset.rotation().matrix())))145            # Could also pull the rotation back, but it's kind of nice to be able to recenter the object146            # without messing up a randomized rotation.147            #R_desired = (self.desired_pose_in_world_frame.rotation().matrix()*self.attract_factor +148            #             R_desired*(1.-self.attract_factor))149            # Apply PD in cartesian space150            xyz_e = xyz_desired - xyz151            xyzd_e = -xyzd152            f = 100.*xyz_e + 10.*xyzd_e153            R_err_in_body_frame = np.linalg.inv(R).dot(R_desired)154            aa = AngleAxis(R_err_in_body_frame)155            tau_p = R.dot(aa.axis()*aa.angle())156            tau_d = -Rd157            tau = tau_p + 0.1*tau_d158            exerted_force = SpatialForce(tau=tau, f=f)159            out = ExternallyAppliedSpatialForce()160            out.F_Bq_W = exerted_force161            out.body_index = self.selected_body.index()162            y_data.set_value([out])163        else:164            y_data.set_value([])165        self.callback_lock.release()166    def DoPublish(self, context, event):167        # TODO(russt): Copied from meshcat_visualizer.py. 168        # Change this to declare a periodic event with a169        # callback instead of overriding DoPublish, pending #9992.170        LeafSystem.DoPublish(self, context, event)171        self.callback_lock.acquire()172        if self.stop:173            self.stop = False174            if context.get_time() > 0.5:175                self.callback_lock.release()176                raise StopIteration177        #query_object = self.EvalAbstractInput(context, 0).get_value()178        #pose_bundle = self.EvalAbstractInput(context, 0).get_value()179        x_in = self.EvalVectorInput(context, 1).get_value()180        self.mbp.SetPositionsAndVelocities(self.mbp_context, x_in)181        182        if self.grab_needs_update:183            hydra_tf = self.grab_update_hydra_pose184            self.grab_needs_update = False185            # If grab point is colliding...186            #print [x.distance for x in query_object.ComputeSignedDistanceToPoint(hydra_tf.matrix()[:3, 3])]187            # Find closest body to current pose188            grab_center = hydra_tf.GetAsMatrix4()[:3, 3]189            closest_distance = np.inf190            closest_body = self.mbp.get_body(BodyIndex(2))191            for body_id in self.all_manipulable_body_ids:192                body = self.mbp.get_body(body_id)193                offset = self.mbp.EvalBodyPoseInWorld(self.mbp_context, body)194                dist = np.linalg.norm(grab_center - offset.translation())195                if dist < closest_distance:196                    closest_distance = dist197                    closest_body = body198            self.selected_body = closest_body199            self.selected_body_init_offset = self.mbp.EvalBodyPoseInWorld(200                self.mbp_context, self.selected_body)201        self.callback_lock.release()202    def callback(self, msg):203        ''' Control mapping: 204            Buttons: [Digital trigger, 1, 2, 3, 4, start, joy click]205            Digital trigger: buttons[0]206            Analog trigger: trigger207            Joy: +x is right, +y is fwd208        '''209        self.callback_lock.acquire()210        self.lastMsg = msg211        self.hasNewMessage = True212        pad_info = msg.paddles[0]213        hydra_tf_uncalib = ros_tf_to_rigid_transform(pad_info.transform)214        hydra_tf_uncalib.set_translation(hydra_tf_uncalib.translation()*self.hydra_prescale)215        hydra_tf = self.hydra_origin.multiply(hydra_tf_uncalib)216        self.desired_pose_in_world_frame = hydra_tf217        self.vis["hydra_origin"].set_transform(hydra_tf.GetAsMatrix4())218        # Interpret various buttons for changing to scaling219        if pad_info.buttons[0] and not self.grab_in_progress:220            logging.info("Grabbing.")221            self.grab_update_hydra_pose = hydra_tf222            self.grab_needs_update = True223            self.grab_in_progress = True224        elif self.grab_in_progress and not pad_info.buttons[0]:225            self.grab_in_progress = False226            self.selected_body = None227        self.freeze_rotation = pad_info.trigger > 0.15228        if pad_info.buttons[5]:229            self.stop = True230        # Optional: use buttons to adjust hydra-reality scaling.231        # Disabling for easier onboarding of new users...232        #if pad_info.buttons[1]:233        #    # Scale down234        #    self.hydra_prescale = max(0.01, self.hydra_prescale * 0.98)235        #    print("Updated scaling to ", self.hydra_prescale)236        #if pad_info.buttons[3]:237        #    # Scale up238        #    self.hydra_prescale = min(10.0, self.hydra_prescale * 1.02)239        #    print("Updated scaling to ", self.hydra_prescale)240        #if pad_info.buttons[2]:241        #    # Translate down242        #    translation = self.hydra_origin.translation().copy()243        #    translation[2] -= 0.01244        #    print("Updated translation to ", translation)245        #    self.hydra_origin.set_translation(translation)246        #if pad_info.buttons[4]:247        #    # Translate up248        #    translation = self.hydra_origin.translation().copy()249        #    translation[2] += 0.01250        #    print("Updated translation to ", translation)251        #    self.hydra_origin.set_translation(translation)252        #if abs(pad_info.joy[0]) > 0.01 or abs(pad_info.joy[1]) > 0.01:253        #    # Translate up254        #    translation = self.hydra_origin.translation().copy()255        #    translation[1] -= pad_info.joy[0]*0.01256        #    translation[0] += pad_info.joy[1]*0.01257        #    print("Updated translation to ", translation)258        #self.hydra_origin.set_translation(translation)...settings.py
Source:settings.py  
...8#ä½è
æä¾ç False为un-freeze rotationæè¯¯9#Fasle为æå¼èªå¨æè½¬ï¼é»è®¤ä¸ºå
³éèªå¨æè½¬10@step(u'æå¼èªå¨æè½¬å±å¹$')11def freezeRotation(step):12    d.freeze_rotation(False)13    d.wait.idle()14@step(u'å
³éèªå¨æè½¬å±å¹$')15def freezeRotation(step):16    d.freeze_rotation()17    d.wait.idle()    18@step(u'ç¹äº®å±å¹$')19def screenon(step):20    d.screen.on()21    d.wait.idle()  22@step(u'ççå±å¹$')23def screenoff(step):24    d.screen.off()25    d.wait.idle()  26    27#ä¿å.xmlå°ç¸å¯¹è·¯å¾28#ç¨æ³ä¸¾ä¾:29#ä¿åå¨å½ålettuceæ§è¡ç®å½ä¸,æä»¶åå为layout.xml30# ä¸è½½çé¢ç»ææä»¶ä¿å为ãlayout.xmlã...通过uiautomator库和app通信,不使用appium.py
Source:通过uiautomator库和app通信,不使用appium.py  
...38d.set_orientation("l") # or "left"39d.set_orientation("r") # or "right"40d.set_orientation("n") # or "natural"41# å»ç»/è§£å»æè½¬åè½42d.freeze_rotation()  # å»ç»æè½¬43d.freeze_rotation(False)  # è§£å»æè½¬44# å±å¹æªå¾45d.screenshot("home.png")46# è·åå±å¹å±çº§(hierachy)XML47xml = d.dump_hierarchy()48# æå¼éç¥æ æå¿«é设置æ 49d.open_notification()50d.open_quick_settings()51# å¯å¨App52d.app_start("com.meizu.mzbbs")53# æç´¢54d(resourceId="com.meizu.mzbbs:id/j0").click()55# è¾å
¥å
³é®å56d(resourceId="com.meizu.mzbbs:id/p9").set_text("flyme")57# æç´¢æé®...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
