def set_speed(self, new_speed): """ Set the default speed to the value specified in the range [10,100] :param new_speed: :return: True if the command suceeded and False otherwise """ # keep track of the new speed in the simulator speed = ensure_speed_within_limits(new_speed) self.constant_speed = speed # and send it to the tello if it exists if (self._tello is not None): # tell the tello to move self._tello.set_speed(new_speed)
def right_cm(self, cm, speed=None): """ Moves ONLY RIGHT in cm. If the user gives it a negative number, the number is made positive. Also enforces cm between 20 and 500 (per SDK) :param cm: amount to move RIGHT in cm [20,500] :param speed: optional command to tell it to fly right that much at a speed in the range [10,100] cm/s """ if (self._tello is None): if (self.location.z > 0): distance = ensure_distance_within_limits(cm) if (speed is None): speed = self.constant_speed speed = ensure_speed_within_limits(speed) if (speed < 10): # Prevent velocity from being set to 0 ie infinite loop return # Convert speed from cm/s to m/s speed = speed / 100 # set the right velocities self.velocity.x = np.sin(self.location.orientation) * speed self.velocity.y = np.cos(self.location.orientation) * speed print(self.velocity.x, self.velocity.y) self.velocity.z = 0 self.velocity.rotational = 0 # and now check the distance traveled and stop if you reach it or if you crash orig_location = copy.copy(self.location) target_distance_m = distance / 100.0 while (orig_location.distance_from_x_y( self.location.x, self.location.y) < target_distance_m and not self.is_crashed): time.sleep(0.01) # stop when you reach the right location self.velocity.x = 0 self.velocity.y = 0 else: # tell the tello to move self._tello.right_cm(cm, speed)
def backward_cm(self, cm, speed=None): """ Moves ONLY BACKWARDS in cm. If the user gives it a negative number, the number is made positive. Also enforces cm between 20 and 500 (per SDK) :param cm: amount to move BACKWARDS in cm [20,500] :param speed: optional command to tell it to fly backward that much at a speed in the range [10,100] cm/s """ if (self._tello is None): if (self.location.z > 0): distance = ensure_distance_within_limits(cm) if (speed is None): speed = self.constant_speed else: speed = ensure_speed_within_limits(speed) # set the right velocities self.velocity.x = -np.cos(self.location.orientation) * speed self.velocity.y = -np.sin(self.location.orientation) * speed self.velocity.z = 0 self.velocity.rotational = 0 # and now check the distance traveled and stop if you reach it or if you crash orig_location = copy.copy(self.location) target_distance_m = distance / 100.0 while (orig_location.distance_from_x_y( self.location.x, self.location.y) < target_distance_m and not self.is_crashed): time.sleep(0.02) # stop when you reach the right location self.velocity.x = 0 self.velocity.y = 0 else: # tell the tello to move self._tello.backward_cm(cm, speed)