Advertisement
Guest User

Untitled

a guest
Nov 12th, 2021
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. #
  3. # || ____ _ __
  4. # +------+ / __ )(_) /_______________ _____ ___
  5. # | 0xBC | / __ / / __/ ___/ ___/ __ `/_ / / _ \
  6. # +------+ / /_/ / / /_/ /__/ / / /_/ / / /_/ __/
  7. # || || /_____/_/\__/\___/_/ \__,_/ /___/\___/
  8. #
  9. # Copyright (C) 2017 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, write to the Free Software
  24. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25. # MA 02110-1301, USA.
  26. """
  27. A script to fly 5 Crazyflies in formation. One stays in the center and the
  28. other four fly around it in a circle. Mainly intended to be used with the
  29. Flow deck.
  30. The starting positions are vital and should be oriented like this
  31.  
  32. >
  33.  
  34. ^ + v
  35.  
  36. <
  37.  
  38. The distance from the center to the perimeter of the circle is around 0.5 m
  39.  
  40. """
  41. import math
  42. import time
  43.  
  44.  
  45. import cflib.crtp
  46. from basiclog import LoggingExample
  47. from cflib.crazyflie import Crazyflie
  48. from cflib.crazyflie.log import LogConfig
  49. from cflib.crazyflie.syncCrazyflie import SyncCrazyflie
  50. from cflib.crazyflie.syncLogger import SyncLogger
  51. from cflib.utils import uri_helper
  52. from cflib.crazyflie.swarm import CachedCfFactory
  53. from cflib.crazyflie.swarm import Swarm
  54. from datetime import datetime
  55. from threading import Timer
  56.  
  57. # Change uris according to your setup
  58. URI0 = 'radio://0/48/2M/E7E7E7E701'
  59. URI1 = 'radio://0/50/2M/E7E7E7E702'
  60. #URI0 = 'radio://0/25/2M/E7E7E7E702'
  61. done = 0
  62.  
  63. # d: diameter of circle
  64. # z: altitude
  65. params0 = {'d': 1.0, 'z': 0.3}
  66. params1 = {'d': 1.0, 'z': 0.3}
  67. #params2 = {'d': 0.0, 'z': 0.5}
  68. #params3 = {'d': 1.0, 'z': 0.3}
  69. #params4 = {'d': 1.0, 'z': 0.3}
  70.  
  71.  
  72. uris = {
  73. URI0,
  74. URI1,
  75. # URI2,
  76. # URI3,
  77. # URI4,
  78. }
  79.  
  80. params = {
  81. URI0: [params0],
  82. # URI1: [params1],
  83. # URI2: [params2],
  84. # URI3: [params3],
  85. # URI4: [params4],
  86. }
  87.  
  88.  
  89. def poshold(cf, t, z):
  90. steps = t * 10
  91.  
  92. for r in range(steps):
  93. cf.commander.send_hover_setpoint(0, 0, 0, z)
  94. time.sleep(0.1)
  95.  
  96.  
  97. def run_sequence(scf, params):
  98. cf = scf.cf
  99.  
  100. # Number of setpoints sent per second
  101. fs = 4
  102. fsi = 1.0 / fs
  103.  
  104. # Compensation for unknown error :-(
  105. comp = 1.3
  106.  
  107. # Base altitude in meters
  108. base = 0.15
  109.  
  110. d = params['d']
  111. z = params['z']
  112.  
  113. poshold(cf, 2, base)
  114. print("JUST BEFORE THE LOGGING")
  115. ramp = fs * 2
  116. for r in range(ramp):
  117. cf.commander.send_hover_setpoint(0, 0, 0, base + r * (z - base) / ramp)
  118. time.sleep(fsi)
  119.  
  120. poshold(cf, 2, z)
  121.  
  122. for _ in range(2):
  123. # The time for one revolution
  124. circle_time = 8
  125.  
  126. steps = circle_time * fs
  127. for _ in range(steps):
  128. cf.commander.send_hover_setpoint(d * comp * math.pi / circle_time,
  129. 0, 360.0 / circle_time, z)
  130. time.sleep(fsi)
  131.  
  132. poshold(cf, 2, z)
  133.  
  134. for r in range(ramp):
  135. cf.commander.send_hover_setpoint(0, 0, 0,
  136. base + (ramp - r) * (z - base) / ramp)
  137. time.sleep(fsi)
  138.  
  139. poshold(cf, 1, base)
  140.  
  141. cf.commander.send_stop_setpoint()
  142.  
  143.  
  144. def GetBattery(scf):
  145. ## log_conf = LogConfig(name='Battery Level', period_in_ms=1000)
  146. ## log_conf.add_variable('pm.batteryLevel', 'float')
  147. ## scf.cf.log.add_config(log_conf)
  148. ## #log_conf.data_received_cb.add_callback(lambda timestamp, data, logconf:
  149. ## log_conf.start()
  150. ## uri = scf.cf.link_uri
  151. ## now = datetime.now()
  152. ## dt_string = now.strftime("%m/%d/%Y %H:%M:%S")
  153. ##
  154. ## print(f'[{dt_string}][{uri}]: ', end='')
  155. ## #for name, value in data.items():
  156. ## print("Radio: " + uri +"\n")
  157. ## #print(log_conf.items())
  158. ## #time.sleep(1)
  159. #print("For this URI : ", list(uris.keys())[0])
  160. ## global done
  161. ## if done == 0:
  162. ## print("URI0")
  163. ## le = LoggingExample(URI0)
  164. ## done = done + 1
  165. ## elif done == 1:
  166. ## print("URI1")
  167. ## le2 = LoggingExample(URI1)
  168. le2 = LoggingExample(URI1)
  169.  
  170.  
  171. if __name__ == '__main__':
  172. cflib.crtp.init_drivers()
  173. #initialize logger for battery
  174. #lg_stab = LogConfig(name='Battery Level', period_in_ms=1000)
  175. #lg_stab.add_variable('pm.batteryLevel', 'float')
  176. factory = CachedCfFactory(rw_cache='./cache')
  177. with Swarm(uris, factory=factory) as swarm:
  178. # swarm.reset_estimators()
  179. #le = LoggingExample(URI0)
  180. print("INSIDE SWARM")
  181. print("STARTING BATTERY")
  182. swarm.parallel_safe(GetBattery)
  183. print("GOT BATTERY")
  184. #le = LoggingExample(URI0)
  185. print("SUCCESS")
  186. #swarm.parallel(run_sequence, args_dict=params)
  187. print("ENDING SWARM")
  188.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement