Rubics

Bitcraze Swarm

Jul 28th, 2022
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.23 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. #
  4. #     ||          ____  _ __
  5. #  +------+      / __ )(_) /_______________ _____  ___
  6. #  | 0xBC |     / __  / / __/ ___/ ___/ __ `/_  / / _ \
  7. #  +------+    / /_/ / / /_/ /__/ /  / /_/ / / /_/  __/
  8. #   ||  ||    /_____/_/\__/\___/_/   \__,_/ /___/\___/
  9. #
  10. #  Copyright (C) 2019 Bitcraze AB
  11. #
  12. #  This program is free software; you can redistribute it and/or
  13. #  modify it under the terms of the GNU General Public License
  14. #  as published by the Free Software Foundation; either version 2
  15. #  of the License, or (at your option) any later version.
  16. #
  17. #  This program is distributed in the hope that it will be useful,
  18. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. #  GNU General Public License for more details.
  21. # You should have received a copy of the GNU General Public License
  22. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  23. """
  24. Simple example of a synchronized swarm choreography using the High level
  25. commander.
  26.  
  27. The swarm takes off and flies a synchronous choreography before landing.
  28. The take-of is relative to the start position but the Goto are absolute.
  29. The sequence contains a list of commands to be executed at each step.
  30.  
  31. This example is intended to work with any absolute positioning system.
  32. It aims at documenting how to use the High Level Commander together with
  33. the Swarm class to achieve synchronous sequences.
  34. """
  35. import threading
  36. import time
  37. from collections import namedtuple
  38. from queue import Queue
  39.  
  40. import cflib.crtp
  41. from cflib.crazyflie.swarm import CachedCfFactory
  42. from cflib.crazyflie.swarm import Swarm
  43.  
  44. # Time for one step in second
  45. STEP_TIME = 2
  46.  
  47. # Possible commands, all times are in seconds
  48. Takeoff = namedtuple('Takeoff', ['height', 'time'])
  49. Land = namedtuple('Land', ['time'])
  50. Goto = namedtuple('Goto', ['x', 'y', 'z', 'time'])
  51. # RGB [0-255], Intensity [0.0-1.0]
  52. Ring = namedtuple('Ring', ['r', 'g', 'b', 'intensity', 'time'])
  53. # Reserved for the control loop, do not use in sequence
  54. Quit = namedtuple('Quit', [])
  55.  
  56. uris = [
  57.     'radio://0/80/2M/E7E7E7E7E7',  # cf_id 0, startup position [-0.5, -0.5]
  58.     'radio://0/80/2M/E7E7E7E7E8',  # cf_id 1, startup position [ 0, 0]
  59.     #'radio://0/10/2M/E7E7E7E703',  # cf_id 3, startup position [0.5, 0.5]
  60.     # Add more URIs if you want more copters in the swarm
  61. ]
  62.  
  63. sequence = [
  64.     # Step, CF_id,  action
  65.     (0,    0,      Takeoff(0.5, 2)),
  66.     #(0,    2,      Takeoff(0.5, 2)),
  67.    
  68.     (1,    1,      Takeoff(1.0, 2)),
  69.  
  70.     (2,    0,      Goto(-0.5,  -0.5,   0.5, 1)),
  71.    # (2,    2,      Goto(0.5,  0.5,   0.5, 1)),
  72.  
  73.     (3,    1,      Goto(0,  0,   1, 1)),
  74.  
  75.     (4,    0,      Ring(255, 255, 255, 0.2, 0)),
  76.     (4,    1,      Ring(255, 0, 0, 0.2, 0)),
  77.    # (4,    2,      Ring(255, 255, 255, 0.2, 0)),
  78.  
  79.     (5,    0,      Goto(0.5, -0.5, 0.5, 2)),
  80.    # (5,    2,      Goto(-0.5, 0.5, 0.5, 2)),
  81.  
  82.     (7,    0,      Goto(0.5, 0.5, 0.5, 2)),
  83.     #(7,    2,      Goto(-0.5, -0.5, 0.5, 2)),
  84.  
  85.     (9,    0,      Goto(-0.5, 0.5, 0.5, 2)),
  86.     #(9,    2,      Goto(0.5, -0.5, 0.5, 2)),
  87.  
  88.     (11,   0,      Goto(-0.5, -0.5, 0.5, 2)),
  89.     #(11,   2,      Goto(0.5, 0.5, 0.5, 2)),
  90.  
  91.     (13,    0,      Land(2)),
  92.     (13,    1,      Land(2)),
  93.     #(13,    2,      Land(2)),
  94.  
  95.     (15,    0,      Ring(0, 0, 0, 0, 5)),
  96.     (15,    1,      Ring(0, 0, 0, 0, 5)),
  97.    # (15,    2,      Ring(0, 0, 0, 0, 5)),
  98. ]
  99.  
  100.  
  101. def activate_high_level_commander(scf):
  102.     scf.cf.param.set_value('commander.enHighLevel', '1')
  103.  
  104.  
  105. def activate_mellinger_controller(scf, use_mellinger):
  106.     controller = 1
  107.     if use_mellinger:
  108.         controller = 2
  109.     scf.cf.param.set_value('stabilizer.controller', str(controller))
  110.  
  111.  
  112. def set_ring_color(cf, r, g, b, intensity, time):
  113.     cf.param.set_value('ring.fadeTime', str(time))
  114.  
  115.     r *= intensity
  116.     g *= intensity
  117.     b *= intensity
  118.  
  119.     color = (int(r) << 16) | (int(g) << 8) | int(b)
  120.  
  121.     cf.param.set_value('ring.fadeColor', str(color))
  122.  
  123.  
  124. def crazyflie_control(scf):
  125.     cf = scf.cf
  126.     control = controlQueues[uris.index(cf.link_uri)]
  127.  
  128.     activate_mellinger_controller(scf, False)
  129.  
  130.     commander = scf.cf.high_level_commander
  131.  
  132.     # Set fade to color effect and reset to Led-ring OFF
  133.     set_ring_color(cf, 0, 0, 0, 0, 0)
  134.     cf.param.set_value('ring.effect', '14')
  135.  
  136.     while True:
  137.         command = control.get()
  138.         if type(command) is Quit:
  139.             return
  140.         elif type(command) is Takeoff:
  141.             commander.takeoff(command.height, command.time)
  142.         elif type(command) is Land:
  143.             commander.land(0.0, command.time)
  144.         elif type(command) is Goto:
  145.             commander.go_to(command.x, command.y, command.z, 0, command.time)
  146.         elif type(command) is Ring:
  147.             set_ring_color(cf, command.r, command.g, command.b,
  148.                            command.intensity, command.time)
  149.             pass
  150.         else:
  151.             print('Warning! unknown command {} for uri {}'.format(command,
  152.                                                                   cf.uri))
  153.  
  154.  
  155. def control_thread():
  156.     pointer = 0
  157.     step = 0
  158.     stop = False
  159.  
  160.     while not stop:
  161.         print('Step {}:'.format(step))
  162.         while sequence[pointer][0] <= step:
  163.             cf_id = sequence[pointer][1]
  164.             command = sequence[pointer][2]
  165.  
  166.             print(' - Running: {} on {}'.format(command, cf_id))
  167.             controlQueues[cf_id].put(command)
  168.             pointer += 1
  169.  
  170.             if pointer >= len(sequence):
  171.                 print('Reaching the end of the sequence, stopping!')
  172.                 stop = True
  173.                 break
  174.  
  175.         step += 1
  176.         time.sleep(STEP_TIME)
  177.  
  178.     for ctrl in controlQueues:
  179.         ctrl.put(Quit())
  180.  
  181.  
  182. if __name__ == '__main__':
  183.     controlQueues = [Queue() for _ in range(len(uris))]
  184.  
  185.     cflib.crtp.init_drivers()
  186.     factory = CachedCfFactory(rw_cache='./cache')
  187.     with Swarm(uris, factory=factory) as swarm:
  188.         swarm.parallel_safe(activate_high_level_commander)
  189.         swarm.reset_estimators()
  190.  
  191.         print('Starting sequence!')
  192.  
  193.         threading.Thread(target=control_thread).start()
  194.  
  195.         swarm.parallel_safe(crazyflie_control)
  196.  
  197.         time.sleep(1)
  198.  
Advertisement
Add Comment
Please, Sign In to add comment