Advertisement
Guest User

Escape

a guest
May 11th, 2015
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.45 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. #
  3. #     ||          ____  _ __
  4. #  +------+      / __ )(_) /_______________ _____  ___
  5. #  | 0xBC |     / __  / / __/ ___/ ___/ __ `/_  / / _ \
  6. #  +------+    / /_/ / / /_/ /__/ /  / /_/ / / /_/  __/
  7. #   ||  ||    /_____/_/\__/\___/_/   \__,_/ /___/\___/
  8. #
  9. #  Copyright (C) 2014 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.  
  23. #  You should have received a copy of the GNU General Public License
  24. #  along with this program; if not, write to the Free Software
  25. #  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  26. #  MA  02110-1301, USA.
  27.  
  28. """
  29. Simple scripted escape
  30. """
  31.  
  32. import time, sys
  33. from threading import Thread
  34. from cfclient.utils.logconfigreader import LogConfig
  35.  
  36.  
  37. #FIXME: Has to be launched from within the example folder
  38. sys.path.append("../lib")
  39. import cflib
  40. from cflib.crazyflie import Crazyflie
  41.  
  42. import logging
  43. logging.basicConfig(level=logging.ERROR)
  44.  
  45. class Escape:
  46.     """Example that connects to a Crazyflie , escape and then disconnect"""
  47.     def __init__(self, link_uri):
  48.         """ Initialize and run the example with the specified link_uri """
  49.  
  50.         self._start_alt = 0
  51.         self._alt = 0
  52.         self._takeoff = False
  53.  
  54.         self._cf = Crazyflie()
  55.  
  56.         self._cf.connected.add_callback(self._connected)
  57.         self._cf.disconnected.add_callback(self._disconnected)
  58.         self._cf.connection_failed.add_callback(self._connection_failed)
  59.         self._cf.connection_lost.add_callback(self._connection_lost)
  60.  
  61.         self._cf.open_link(link_uri)
  62.  
  63.         print "Connecting to %s" % link_uri
  64.  
  65.     def _connected(self, link_uri):
  66.         """ This callback is called form the Crazyflie API when a Crazyflie
  67.        has been connected and the TOCs have been downloaded."""
  68.  
  69.         print "Connected to %s" % link_uri
  70.  
  71.         # The definition of the logconfig can be made before connecting
  72.         self._lg_alt = LogConfig(name="altitude", period_in_ms=10)
  73.         self._lg_alt.add_variable("baro.asl", "float")
  74.  
  75.         # Adding the configuration cannot be done until a Crazyflie is
  76.         # connected, since we need to check that the variables we
  77.         # would like to log are in the TOC.
  78.         self._cf.log.add_config(self._lg_alt)
  79.         if self._lg_alt.valid:
  80.             # This callback will receive the data
  81.             self._lg_alt.data_received_cb.add_callback(self._alt_log_data)
  82.             # This callback will be called on errors
  83.             self._lg_alt.error_cb.add_callback(self._alt_log_error)
  84.             # Start the logging
  85.             self._lg_alt.start()
  86.         else:
  87.             print("Could not add logconfig since some variables are not in TOC")
  88.  
  89.  
  90.         # Start a separate thread to do the motor test.
  91.         # Do not hijack the calling thread!
  92.         Thread(target=self._do_escape).start()
  93.  
  94.     def _alt_log_error(self, logconf, msg):
  95.         """Callback from the log API when an error occurs"""
  96.         print "Error when logging %s: %s" % (logconf.name, msg)
  97.  
  98.     def _alt_log_data(self, timestamp, data, logconf):
  99.         """Callback froma the log API when data arrives"""
  100.         if logconf.name == "altitude":
  101.             if not self._takeoff:
  102.                 self._start_alt = data['baro.asl']
  103.                 self._takeoff = True
  104.             else:
  105.                 self._alt = data['baro.asl']
  106.         print "{}".format(self._alt - self._start_alt)
  107.  
  108.     def _connection_failed(self, link_uri, msg):
  109.         """Callback when connection initial connection fails (i.e no Crazyflie
  110.        at the speficied address)"""
  111.         print "Connection to %s failed: %s" % (link_uri, msg)
  112.  
  113.     def _connection_lost(self, link_uri, msg):
  114.         """Callback when disconnected after a connection has been made (i.e
  115.        Crazyflie moves out of range)"""
  116.         print "Connection to %s lost: %s" % (link_uri, msg)
  117.  
  118.     def _disconnected(self, link_uri):
  119.         """Callback when the Crazyflie is disconnected (called in all cases)"""
  120.         print "Disconnected from %s" % link_uri
  121.  
  122.     def _do_escape(self):
  123.  
  124.         while not self._takeoff:
  125.             pass
  126.  
  127.         #Unlock startup thrust protection
  128.         self._cf.commander.send_setpoint(0, 0, 0, 0)
  129.         time.sleep(0.1)
  130.  
  131.         #                               (Roll, Pitch, Yaw,     Thrust)
  132.         self._cf.commander.send_setpoint(   0,    40,   0, 0.75*64768)
  133.         time.sleep(0.2)
  134.  
  135.         self._cf.commander.send_setpoint(0, 20, 0, 0.75*64768)
  136.         time.sleep(0.2)
  137.  
  138.         self._cf.commander.send_setpoint(0, 5, 0, 0.75*64768)
  139.         # Wait for Crazyflie to reach some altitude, could be replaced by a sleep and needs a timeout!
  140.         while self._alt < (self._start_alt + 1):
  141.             pass
  142.  
  143.         print "0, Going down!"
  144.  
  145.         self._cf.commander.send_setpoint(0, 5, 0, 0.3*64768)
  146.         time.sleep(0.3)
  147.  
  148.         self._cf.commander.send_setpoint(0, 0, 0, 0.55*64768)
  149.         # Wait for Crazyflie to come back to 0, could be replaced by a sleep and needs a timeout!
  150.         while self._alt > (self._start_alt+0.2):
  151.             pass
  152.  
  153.         self._cf.commander.send_setpoint(0, 0, 0, 0.3*64768)
  154.         time.sleep(0.1)
  155.  
  156.         self._cf.commander.send_setpoint(0, 0, 0, 0)
  157.  
  158.         # Make sure that the last packet leaves before the link is closed
  159.         # since the message queue is not flushed before closing
  160.         time.sleep(0.1)
  161.         self._cf.close_link()
  162.  
  163. if __name__ == '__main__':
  164.     # Initialize the low-level drivers (don't list the debug drivers)
  165.     cflib.crtp.init_drivers(enable_debug_driver=False)
  166.  
  167.     # To quickly connect a single Crazyflie
  168.     #le = Escape("radio://0/45/2M")
  169.  
  170.     # Or, to scan for Crazyflies and use the first one found
  171.     print "Scanning interfaces for Crazyflies..."
  172.     available = cflib.crtp.scan_interfaces()
  173.     print "Crazyflies found:"
  174.     for i in available:
  175.         print i[0]
  176.  
  177.     if len(available) > 0:
  178.         le = Escape(available[0][0])
  179.     else:
  180.         print "No Crazyflies found, cannot run example"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement