Advertisement
Guest User

Tester

a guest
May 11th, 2015
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.49 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 example that connects to the first Crazyflie found, launches it, flies it around and disconnects.
  30. """
  31.  
  32. import time, sys
  33. from threading import Thread
  34.  
  35. #FIXME: Has to be launched from within the example folder
  36. sys.path.append("../lib")
  37. import cflib
  38. from cflib.crazyflie import Crazyflie
  39.  
  40. import logging
  41. logging.basicConfig(level=logging.ERROR)
  42.  
  43. class Tester:
  44.     """Example that connects to a Crazyflie and flies it around and
  45.    then disconnects"""
  46.     def __init__(self, link_uri):
  47.         """ Initialize and run the example with the specified link_uri """
  48.  
  49.         self._cf = Crazyflie()
  50.  
  51.         self._cf.connected.add_callback(self._connected)
  52.         self._cf.disconnected.add_callback(self._disconnected)
  53.         self._cf.connection_failed.add_callback(self._connection_failed)
  54.         self._cf.connection_lost.add_callback(self._connection_lost)
  55.  
  56.         self._cf.open_link(link_uri)
  57.  
  58.         print "Connecting to %s" % link_uri
  59.  
  60.     def _connected(self, link_uri):
  61.         """ This callback is called form the Crazyflie API when a Crazyflie
  62.        has been connected and the TOCs have been downloaded."""
  63.  
  64.         # Start a separate thread to do the motor test.
  65.         # Do not hijack the calling thread!
  66.         Thread(target=self._ramp_motors).start()
  67.  
  68.     def _connection_failed(self, link_uri, msg):
  69.         """Callback when connection initial connection fails (i.e no Crazyflie
  70.        at the speficied address)"""
  71.         print "Connection to %s failed: %s" % (link_uri, msg)
  72.  
  73.     def _connection_lost(self, link_uri, msg):
  74.         """Callback when disconnected after a connection has been made (i.e
  75.        Crazyflie moves out of range)"""
  76.         print "Connection to %s lost: %s" % (link_uri, msg)
  77.  
  78.     def _disconnected(self, link_uri):
  79.         """Callback when the Crazyflie is disconnected (called in all cases)"""
  80.         print "Disconnected from %s" % link_uri
  81.  
  82.     def TestFlight(self):
  83.         self._cf.commander.send_setpoint(0, 0, 0, 0.75*63768)
  84.         time.sleep(0.3)
  85.        
  86.         self._cf.commander.send_setpoint(0, 0, 0, 0.75*63768)
  87.         time.sleep(0.3)
  88.        
  89.         self._cf.commander.send_setpoint(0, 0, 0, 0.75*63768)
  90.         time.sleep(0.3)
  91.        
  92.         self._cf.commander.send_setpoint(2, 2, 0, 0.50*57590)
  93.         time.sleep(0.1)
  94.        
  95.         self._cf.commander.send_setpoint(2, 2, 0, 0.55*57590)
  96.         time.sleep(0.1)
  97.        
  98.         self._cf.commander.send_setpoint(2, 2, 0, 0.60*57590)
  99.         time.sleep(0.1)
  100.        
  101.         self._cf.commander.send_setpoint(2, 2, 0, 0.65*57590)
  102.         time.sleep(0.1)
  103.  
  104.     self._cf.commander.send_setpoint(2, 2, 0, 0.67*57590)
  105.         time.sleep(2.0)
  106.  
  107.     self._cf.commander.send_setpoint(2, 20, 0, 0.6825*57590)
  108.         time.sleep(1.0)
  109.    
  110.     self._cf.commander.send_setpoint(2, 5, 0, 0.675*57590)
  111.         time.sleep(0.5)
  112.        
  113.     self._cf.commander.send_setpoint(20, 2, 0, 0.6825*57590)
  114.         time.sleep(1.0)
  115.    
  116.     self._cf.commander.send_setpoint(5, 2, 0, 0.675*57590)
  117.         time.sleep(0.5)
  118.    
  119.     self._cf.commander.send_setpoint(2, -20, 0, 0.6825*57590)
  120.         time.sleep(1.0)
  121.    
  122.     self._cf.commander.send_setpoint(2, -3, 0, 0.675*57590)
  123.         time.sleep(0.5)
  124.    
  125.     self._cf.commander.send_setpoint(-20, 2, 0, 0.6825*57590)
  126.         time.sleep(1.0)
  127.    
  128.     self._cf.commander.send_setpoint(-5, 2, 0, 0.675*57590)
  129.         time.sleep(0.5)
  130.    
  131.     self._cf.commander.send_setpoint(2, 0, 0, 0.67*57590)
  132.         time.sleep(0.2)
  133.     print "0, Going down!"
  134.        
  135.         self._cf.commander.send_setpoint(0, 0, 0, 0.3*64768)
  136.         time.sleep(0.3)
  137.        
  138.         self._cf.commander.send_setpoint(0, 0, 0, 0.3*64768)
  139.         time.sleep(0.1)
  140.        
  141.         self._cf.commander.send_setpoint(0, 0, 0, 0)
  142.         # Make sure that the last packet leaves before the link is closed
  143.         # since the message queue is not flushed before closing
  144.         time.sleep(0.1)
  145.         self._cf.close_link()
  146.  
  147. if __name__ == '__main__':
  148.     # Initialize the low-level drivers (don't list the debug drivers)
  149.     cflib.crtp.init_drivers(enable_debug_driver=False)
  150.     # Scan for Crazyflies and use the first one found
  151.     print "Scanning interfaces for Crazyflies..."
  152.     available = cflib.crtp.scan_interfaces()
  153.     print "Crazyflies found:"
  154.     for i in available:
  155.         print i[0]
  156.  
  157.     if len(available) > 0:
  158.         le = Tester(available[0][0])
  159.     else:
  160.         print "No Crazyflies found, cannot run example"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement