Advertisement
Guest User

Ramp

a guest
May 11th, 2015
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.26 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, ramps up/down
  30. the motors and disconnects.
  31. """
  32.  
  33. import time, sys
  34. from threading import Thread
  35.  
  36. #FIXME: Has to be launched from within the example folder
  37. sys.path.append("../lib")
  38. import cflib
  39. from cflib.crazyflie import Crazyflie
  40.  
  41. import logging
  42. logging.basicConfig(level=logging.ERROR)
  43.  
  44. class MotorRampExample:
  45.     """Example that connects to a Crazyflie and ramps the motors up/down and
  46.    the disconnects"""
  47.     def __init__(self, link_uri):
  48.         """ Initialize and run the example with the specified link_uri """
  49.  
  50.         self._cf = Crazyflie()
  51.  
  52.         self._cf.connected.add_callback(self._connected)
  53.         self._cf.disconnected.add_callback(self._disconnected)
  54.         self._cf.connection_failed.add_callback(self._connection_failed)
  55.         self._cf.connection_lost.add_callback(self._connection_lost)
  56.  
  57.         self._cf.open_link(link_uri)
  58.  
  59.         print "Connecting to %s" % link_uri
  60.  
  61.     def _connected(self, link_uri):
  62.         """ This callback is called form the Crazyflie API when a Crazyflie
  63.        has been connected and the TOCs have been downloaded."""
  64.  
  65.         # Start a separate thread to do the motor test.
  66.         # Do not hijack the calling thread!
  67.         Thread(target=self._ramp_motors).start()
  68.  
  69.     def _connection_failed(self, link_uri, msg):
  70.         """Callback when connection initial connection fails (i.e no Crazyflie
  71.        at the speficied address)"""
  72.         print "Connection to %s failed: %s" % (link_uri, msg)
  73.  
  74.     def _connection_lost(self, link_uri, msg):
  75.         """Callback when disconnected after a connection has been made (i.e
  76.        Crazyflie moves out of range)"""
  77.         print "Connection to %s lost: %s" % (link_uri, msg)
  78.  
  79.     def _disconnected(self, link_uri):
  80.         """Callback when the Crazyflie is disconnected (called in all cases)"""
  81.         print "Disconnected from %s" % link_uri
  82.  
  83.     def _ramp_motors(self):
  84.         thrust_mult = 1
  85.         thrust_step = 500
  86.         thrust = 20000
  87.         pitch = 0
  88.         roll = 0
  89.         yawrate = 0
  90.  
  91.         #Unlock startup thrust protection
  92.         self._cf.commander.send_setpoint(0, 0, 0, 0)
  93.  
  94.         while thrust >= 20000:
  95.             self._cf.commander.send_setpoint(roll, pitch, yawrate, thrust)
  96.             time.sleep(0.1)
  97.             if thrust >= 25000:
  98.                 thrust_mult = -1
  99.             thrust += thrust_step * thrust_mult
  100.         self._cf.commander.send_setpoint(0, 0, 0, 0)
  101.         # Make sure that the last packet leaves before the link is closed
  102.         # since the message queue is not flushed before closing
  103.         time.sleep(0.1)
  104.         self._cf.close_link()
  105.  
  106. if __name__ == '__main__':
  107.     # Initialize the low-level drivers (don't list the debug drivers)
  108.     cflib.crtp.init_drivers(enable_debug_driver=False)
  109.     # Scan for Crazyflies and use the first one found
  110.     print "Scanning interfaces for Crazyflies..."
  111.     available = cflib.crtp.scan_interfaces()
  112.     print "Crazyflies found:"
  113.     for i in available:
  114.         print i[0]
  115.  
  116.     if len(available) > 0:
  117.         le = MotorRampExample(available[0][0])
  118.     else:
  119.         print "No Crazyflies found, cannot run example"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement