After each command has been sent we wait for the observation to change as expected and a frame.

def waitForNextState( self ):
        '''After each command has been sent we wait for the observation to change as expected and a frame.'''
        # wait for the observation position to have changed
        print('Waiting for observation...', end=' ')
        while True:
            world_state = self.agent_host.peekWorldState()
            if not world_state.is_mission_running:
                print('mission ended.')
                break
            if not all(e.text=='{}' for e in world_state.observations):
                obs = json.loads( world_state.observations[-1].text )
                self.curr_x   = obs[u'XPos']
                self.curr_y   = obs[u'YPos']
                self.curr_z   = obs[u'ZPos']
                self.curr_yaw = obs[u'Yaw']
                if self.require_move:
                    if math.fabs( self.curr_x - self.prev_x ) > self.tolerance or\
                       math.fabs( self.curr_y - self.prev_y ) > self.tolerance or\
                       math.fabs( self.curr_z - self.prev_z ) > self.tolerance:
                        print('received a move.')
                        break
                elif self.require_yaw_change:
                    if math.fabs( self.curr_yaw - self.prev_yaw ) > self.tolerance:
                        print('received a turn.')
                        break
                else:
                    print('received.')
                    break
        # wait for the render position to have changed
        print('Waiting for render...', end=' ')
        while True:
            world_state = self.agent_host.peekWorldState()
            if not world_state.is_mission_running:
                print('mission ended.')
                break
            if len(world_state.video_frames) > 0:
                frame = world_state.video_frames[-1]
                curr_x_from_render   = frame.xPos
                curr_y_from_render   = frame.yPos
                curr_z_from_render   = frame.zPos
                curr_yaw_from_render = frame.yaw
                if self.require_move:
                    if math.fabs( curr_x_from_render - self.prev_x ) > self.tolerance or\
                       math.fabs( curr_y_from_render - self.prev_y ) > self.tolerance or\
                       math.fabs( curr_z_from_render - self.prev_z ) > self.tolerance:
                        print('received a move.')
                        break
                elif self.require_yaw_change:
                    if math.fabs( curr_yaw_from_render - self.prev_yaw ) > self.tolerance:
                        print('received a turn.')
                        break
                else:
                    print('received.')
                    break
            
        num_frames_before_get = len(world_state.video_frames)
        world_state = self.agent_host.getWorldState()

        if save_images:
            # save the frame, for debugging
            if world_state.is_mission_running:
                assert len(world_state.video_frames) > 0, 'No video frames!?'
                frame = world_state.video_frames[-1]
                image = Image.frombytes('RGB', (frame.width, frame.height), bytes(frame.pixels) )
                self.iFrame = self.iFrame + 1
                image.save( 'rep_' + str(self.rep).zfill(3) + '_saved_frame_' + str(self.iFrame).zfill(4) + '.png' )
            
        if world_state.is_mission_running:
            assert len(world_state.video_frames) > 0, 'No video frames!?'
            num_frames_after_get = len(world_state.video_frames)
            assert num_frames_after_get >= num_frames_before_get, 'Fewer frames after getWorldState!?'
            frame = world_state.video_frames[-1]
            obs = json.loads( world_state.observations[-1].text )
            self.curr_x   = obs[u'XPos']
            self.curr_y   = obs[u'YPos']
            self.curr_z   = obs[u'ZPos']
            self.curr_yaw = obs[u'Yaw']
            print('New position from observation:',self.curr_x,',',self.curr_y,',',self.curr_z,'yaw',self.curr_yaw, end=' ')
            if math.fabs( self.curr_x   - self.expected_x   ) > self.tolerance or\
               math.fabs( self.curr_y   - self.expected_y   ) > self.tolerance or\
               math.fabs( self.curr_z   - self.expected_z   ) > self.tolerance or\
               math.fabs( self.curr_yaw - self.expected_yaw ) > self.tolerance:
                print(' - ERROR DETECTED! Expected:',self.expected_x,',',self.expected_y,',',self.expected_z,'yaw',self.expected_yaw)
                exit(1)
            else:
                print('as expected.')
            curr_x_from_render   = frame.xPos
            curr_y_from_render   = frame.yPos
            curr_z_from_render   = frame.zPos
            curr_yaw_from_render = frame.yaw
            print('New position from render:',curr_x_from_render,',',curr_y_from_render,',',curr_z_from_render,'yaw',curr_yaw_from_render, end=' ')
            if math.fabs( curr_x_from_render   - self.expected_x   ) > self.tolerance or\
               math.fabs( curr_y_from_render   - self.expected_y   ) > self.tolerance or \
               math.fabs( curr_z_from_render   - self.expected_z   ) > self.tolerance or \
               math.fabs( curr_yaw_from_render - self.expected_yaw ) > self.tolerance:
                print(' - ERROR DETECTED! Expected:',self.expected_x,',',self.expected_y,',',self.expected_z,'yaw',self.expected_yaw)
                exit(1)
            else:
                print('as expected.')
            self.prev_x   = self.curr_x
            self.prev_y   = self.curr_y
            self.prev_z   = self.curr_z
            self.prev_yaw = self.curr_yaw
            
        return world_state