Advertisement
Guest User

LaunchLeftRight

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