Advertisement
Rubics

Crazyflie Problem

Apr 7th, 2022
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.12 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. #
  3. #     ||          ____  _ __
  4. #  +------+      / __ )(_) /_______________ _____  ___
  5. #  | 0xBC |     / __  / / __/ ___/ ___/ __ `/_  / / _ \
  6. #  +------+    / /_/ / / /_/ /__/ /  / /_/ / / /_/  __/
  7. #   ||  ||    /_____/_/\__/\___/_/   \__,_/ /___/\___/
  8. #
  9. #  Copyright (C) 2019 Bitcraze AB
  10. #
  11. #  Crazyflie Nano Quadcopter Client
  12. #
  13. #  This program is free software; you can redistribute it and/or
  14. #  modify it under the terms of the GNU General Public License
  15. #  as published by the Free Software Foundation; either version 2
  16. #  of the License, or (at your option) any later version.
  17. #
  18. #  This program is distributed in the hope that it will be useful,
  19. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. #  GNU General Public License for more details.
  22. # You should have received a copy of the GNU General Public License
  23. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  24. """
  25. Simple example that connects to one crazyflie, sets the initial position/yaw
  26. and flies a trajectory.
  27.  
  28. The initial pose (x, y, z, yaw) is configured in a number of variables and
  29. the trajectory is flown relative to this position, using the initial yaw.
  30.  
  31. This example is intended to work with any absolute positioning system.
  32. It aims at documenting how to take off with the Crazyflie in an orientation
  33. that is different from the standard positive X orientation and how to set the
  34. initial position of the kalman estimator.
  35. """
  36. import math
  37. import time
  38.  
  39. import cflib.crtp
  40. from cflib.crazyflie import Crazyflie
  41. from cflib.crazyflie.log import LogConfig
  42. from cflib.crazyflie.syncCrazyflie import SyncCrazyflie
  43. from cflib.crazyflie.syncLogger import SyncLogger
  44. from cflib.utils import uri_helper
  45.  
  46. # URI to the Crazyflie to connect to
  47. uri = uri_helper.uri_from_env(default='radio://0/80/2M/E7E7E7E7E8')
  48.  
  49. # Change the sequence according to your setup
  50. #             x    y    z
  51. sequence = [
  52.     (1, 1, 1.0),
  53.     (1, 1, 1.0),
  54. ]
  55.  
  56.  
  57. def wait_for_position_estimator(scf):
  58.     print('Waiting for estimator to find position...')
  59.  
  60.     log_config = LogConfig(name='Kalman Variance', period_in_ms=500)
  61.     log_config.add_variable('kalman.varPX', 'float')
  62.     log_config.add_variable('kalman.varPY', 'float')
  63.     log_config.add_variable('kalman.varPZ', 'float')
  64.  
  65.     var_y_history = [1000] * 10
  66.     var_x_history = [1000] * 10
  67.     var_z_history = [1000] * 10
  68.  
  69.     threshold = 0.001
  70.  
  71.     with SyncLogger(scf, log_config) as logger:
  72.         for log_entry in logger:
  73.             data = log_entry[1]
  74.  
  75.             var_x_history.append(data['kalman.varPX'])
  76.             var_x_history.pop(0)
  77.             var_y_history.append(data['kalman.varPY'])
  78.             var_y_history.pop(0)
  79.             var_z_history.append(data['kalman.varPZ'])
  80.             var_z_history.pop(0)
  81.  
  82.             min_x = min(var_x_history)
  83.             max_x = max(var_x_history)
  84.             min_y = min(var_y_history)
  85.             max_y = max(var_y_history)
  86.             min_z = min(var_z_history)
  87.             max_z = max(var_z_history)
  88.  
  89.             # print("{} {} {}".
  90.             #       format(max_x - min_x, max_y - min_y, max_z - min_z))
  91.  
  92.             if (max_x - min_x) < threshold and (
  93.                     max_y - min_y) < threshold and (
  94.                     max_z - min_z) < threshold:
  95.                 break
  96.  
  97.  
  98. def set_initial_position(scf, x, y, z, yaw_deg):
  99.     scf.cf.param.set_value('kalman.initialX', x)
  100.     scf.cf.param.set_value('kalman.initialY', y)
  101.     scf.cf.param.set_value('kalman.initialZ', z)
  102.  
  103.     yaw_radians = math.radians(yaw_deg)
  104.     scf.cf.param.set_value('kalman.initialYaw', yaw_radians)
  105.  
  106.  
  107. def reset_estimator(scf):
  108.     cf = scf.cf
  109.     cf.param.set_value('kalman.resetEstimation', '1')
  110.     time.sleep(0.1)
  111.     cf.param.set_value('kalman.resetEstimation', '0')
  112.  
  113.     wait_for_position_estimator(cf)
  114.  
  115.  
  116. def run_sequence(scf, sequence, base_x, base_y, base_z, yaw):
  117.     cf = scf.cf
  118.  
  119.     for position in sequence:
  120.         print('Setting position {}'.format(position))
  121.  
  122.         x = position[0] + base_x
  123.         y = position[1] + base_y
  124.         z = position[2] + base_z
  125.  
  126.         for i in range(50):
  127.             cf.commander.send_position_setpoint(x, y, z, yaw)
  128.             time.sleep(0.1)
  129.  
  130.     cf.commander.send_stop_setpoint()
  131.     # Make sure that the last packet leaves before the link is closed
  132.     # since the message queue is not flushed before closing
  133.     time.sleep(0.1)
  134.  
  135.  
  136. if __name__ == '__main__':
  137.     cflib.crtp.init_drivers()
  138.  
  139.     # Set these to the position and yaw based on how your Crazyflie is placed
  140.     # on the floor
  141.     initial_x = 1.0
  142.     initial_y = 1.0
  143.     initial_z = 0.0
  144.     initial_yaw = 90  # In degrees
  145.     # 0: positive X direction
  146.     # 90: positive Y direction
  147.     # 180: negative X direction
  148.     # 270: negative Y direction
  149.  
  150.     with SyncCrazyflie(uri, cf=Crazyflie(rw_cache='./cache')) as scf:
  151.         set_initial_position(scf, initial_x, initial_y, initial_z, initial_yaw)
  152.         reset_estimator(scf)
  153.         run_sequence(scf, sequence,
  154.                      initial_x, initial_y, initial_z, initial_yaw)
  155.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement