Best Python code snippet using localstack_python
brainAttacker.py
Source:brainAttacker.py  
...87        self.roi_width = msg.ball.roi_width88        self.roi_height = msg.ball.roi_height89        self.ball_centered, self.ball_close, self.motorhead = self.thoughts.vision(self.x_ball, self.y_ball, self.roi_width, self.roi_height, self.ball)90        91        self.update_state_machine()92    # Metodo para a visão na robo simulada93    def callback_vision_sim(self, msg): # Acionado toda vez que uma mensagem da Visão chega94        self.searching = msg.searching95        self.ball = msg.found96        self.x_ball = msg.x # Coordenada horizontal da bola97        self.y_ball = msg.y # Coordenada vertical da bola98        self.roi_width = msg.roi_width99        self.roi_height = msg.roi_height100        self.ball_centered, self.ball_close, self.motorhead = self.thoughts.vision(self.x_ball, self.y_ball, self.roi_width, self.roi_height, self.ball)101        102        self.update_state_machine()103    def callback_head(self, msg):104        self.neck_horizontal_position = msg.data[18]105        self.neck_vertical_position = msg.data[19]106        self.body_alignment, self.motor_limit_reached = self.thoughts.movement(self.neck_horizontal_position, self.neck_vertical_position)107        self.walking = msg.source108        self.update_state_machine()109    def walk_service(self, first_pose, move_head, walk_flag, test_mode):110        111        rospy.wait_for_service('/humanoid_walking/walking_cmd')112        try:113            service_walk = rospy.ServiceProxy('/humanoid_walking/walking_cmd', LipCmdSrv)114            service_walk(first_pose, move_head, walk_flag, False, test_mode, self.vx, self.vy, self.vz)115        except rospy.ServiceException as e:116            print("Service call failed: {e}")117    118    def callback_sensor(self, msg):119        #pegar valores pertinentes do sensor IMU 120        self.x_sensor = msg.x121        self.y_sensor = msg.y122        self.z_sensor = msg.z123        self.falled = self.thoughts.sensor_think(self.x_sensor, self.y_sensor, self.z_sensor)124        self.update_state_machine()125    126    def call_predefined_movement(self, predef_name):127        self.finished_page = 'not_finished'128        rospy.wait_for_service('/movement_predefined/request_txt')129        130        try:131            service_predef_mov = rospy.ServiceProxy('/movement_predefined/request_txt', PredefinedMovementSrv)132            resp = service_predef_mov(predef_name)133            self.finished_page = 'finished'134            self.update_state_machine()135        except rospy.ServiceException as e:136            print("Service call failed: {e}")137    # Método destinado a repassar as variveis para a máquina de estados138    def update_state_machine(self):139        # Visão140        self.robot.ball = self.ball141        self.robot.searching = self.searching142        self.robot.ball_centered = self.ball_centered143        self.robot.ball_close = self.ball_close144        # Movimento145        self.robot.moving = self.moving146        self.robot.falling = self.falled147        self.robot.finish_kicking = self.finish_kicking148        self.robot.motor_limit_reached = self.motor_limit_reached149    # Método responsavel por passar comandos ao movimento150    def run_movement(self):151        if self.robot.state != 'S_Search_ball':152            self.walk_service(first_pose=False, move_head = False, walk_flag = False, test_mode = self.test_mode)153        if self.robot.state == 'S_Getting_up':154            # Movimento desligar motores momentos antes de a robô realmente cair155            self.moving = False156            time.sleep(1) # Esperar um pouco para garantir que ela caiu157            self.position_falled = self.thoughts.falled_position(self.x_sensor, self.y_sensor, self.z_sensor) #verificar a posição que a robo caiu 158            print (self.position_falled) 159            if self.walking == 'not_walking' or self.finished_page == 'finished':               160                if self.position_falled == 'front':161                    self.call_predefined_movement('stand_up_front')162                    print('stand_up_front')163            164                elif self.position_falled == 'back':165                    self.call_predefined_movement('stand_up_back')166                    print('stand_up_back')167                elif self.position_falled == 'left_side':168                    self.call_predefined_movement('stand_up_left')169                    print('stand_up_left')170                171                elif self.position_falled == 'right_side':172                    self.call_predefined_movement('stand_up_right')173                    print('stand_up_right')174            elif self.finished_page == 'finished':175                self.falled = False176                self.moving = False177                self.update_state_machine()    178        elif (self.robot.state == 'S_Walking' and self.finished_page == 'finished'):179            180            count = 0181            182            if self.body_alignment == 'body_centralized':183                self.moving = True184                185                #Chamando page de andar reto varias vezes, pois anda pouco186                while count <= 4:187                    self.call_predefined_movement('go_ahead')188                    print('Andando reto')189                    count += 1190                191            elif self.body_alignment == 'turn_left':192                self.moving = True193                #Chamando page de virar para a esquerda194                self.call_predefined_movement('turn_left')195                print('Girando para esquerda')196            elif self.body_alignment == 'turn_right':197                self.moving = True198                #Chamando page de virar para a direita 199                self.call_predefined_movement('turn_right')200                print('Girando para direita')201            202            203            self.update_state_machine()204            self.robot.state = 'S_Search_ball'    205        elif (self.robot.state == 'S_Kick' and self.finished_page == 'finished'):206            count = 0207            while count <= 4:208                self.call_predefined_movement('go_ahead')209                print('go_ahead no kick')210                count += 1211            self.call_predefined_movement('weak_kick')212            print('chamou kick')213            #self.finish_kicking = True214            self.update_state_machine()215        elif (self.robot.state == 'S_Stand_still' and self.finished_page == 'finished'):216            self.call_predefined_movement('first_pose')217            print('first_pose no stand_still')218            self.update_state_machine()219        elif (self.robot.state == 'S_Search_ball' and self.finished_page == 'finished'):220            self.headMsg.pos = self.motorhead221            self.pub_comm_head_params.publish(self.headMsg)222            if self.motorhead == 4: #Procurou demais223                self.call_predefined_movement('turn_left')224                print('turn_left no search_ball')225            elif self.motorhead == 3: #Casos legitimos226                self.walk_service(self.first_pose, move_head = True, walk_flag = False, test_mode = self.test_mode)227            228            else:229                self.walk_service(self.first_pose, move_head = True, walk_flag = False, test_mode = self.test_mode)230            self.update_state_machine()231            232        elif (self.robot.state == 'S_Body_alignment' and self.finished_page == 'finished'):    233            self.headMsg.pos = -self.motorhead234            self.pub_comm_head_params.publish(self.headMsg)235            self.walk_service(self.first_pose, move_head = True, walk_flag = False, test_mode = self.test_mode)236            time.sleep(5)237            self.walk_service(self.first_pose, move_head = False, walk_flag = False, test_mode = self.test_mode)238            239            if self.body_alignment == 'turn_left':240                self.moving = True241                print('Body_alignment virando a esquerda com cabeça à ', self.motorhead)242                self.call_predefined_movement('turn_left')243            elif self.body_alignment == 'turn_right':244                self.moving = True245                print('Body_alignment virando a direita com cabeça à ', self.motorhead)246                self.call_predefined_movement('turn_right')247            248            self.robot.state = 'S_Search_ball'249            self.update_state_machine()250    251    def start(self):252        rospy.Subscriber('/webots_natasha/vision_inference', WebotsVisionMsg, self.callback_vision) #Subscrição precisa ser feita apenas uma vez253        rospy.Subscriber('/webots_natasha/behaviour_controller', Vector3, self.callback_sensor)254        rospy.Subscriber('/opencm/request', OpencmRequestMsg, self.callback_head)255        #rospy.Subscriber('objects_sim', Ball, self.callback_vision_sim)256        #rospy.Subscriber('robot_actions', StateMachineActionsMsg, self.callback_test)257        self.pub_comm_head_params = rospy.Publisher('/motor_comm/head_params', HeadMoveMsg, queue_size = 100)258       259        self.robot.game_controller = True260        self.update_state_machine() # Método para atualizar a máquina de estados261        262        self.test_mode = True263        self.first_pose = False264        self.walk_service(self.first_pose, move_head = False, walk_flag = False, test_mode = self.test_mode)265        266        time.sleep(10)267        self.first_pose = True268        self.walk_service(self.first_pose, move_head = False, walk_flag = False, test_mode = self.test_mode)269        self.call_predefined_movement('first_pose')270        time.sleep(10)271        self.first_pose = False272        self.walk_service(self.first_pose, move_head = False, walk_flag = False, test_mode = self.test_mode)273        # Loop que mantém o Behaviour em execução274        while not rospy.is_shutdown():...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!!
