Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. import sys
  4. sys.path.append("../lib")
  5.  
  6. import cflib.crtp
  7.  
  8. import logging
  9. import time
  10. from threading import Timer
  11.  
  12. import cflib.crtp
  13. from cfclient.utils.logconfigreader import LogConfig
  14. from cflib.crazyflie import Crazyflie
  15.  
  16. # Only output errors from the logging framework
  17. logging.basicConfig(level=logging.ERROR)
  18.  
  19. class LoggingExample:
  20. """
  21. Simple logging example class that logs the Stabilizer from a supplied
  22. link uri and disconnects after 5s.
  23. """
  24. def __init__(self, link_uri):
  25. """ Initialize and run the example with the specified link_uri """
  26.  
  27. # Create a Crazyflie object without specifying any cache dirs
  28. self._cf = Crazyflie()
  29.  
  30. # Connect some callbacks from the Crazyflie API
  31. self._cf.connected.add_callback(self._connected)
  32. self._cf.disconnected.add_callback(self._disconnected)
  33. self._cf.connection_failed.add_callback(self._connection_failed)
  34. self._cf.connection_lost.add_callback(self._connection_lost)
  35.  
  36. print "Connecting to %s" % link_uri
  37.  
  38. # Try to connect to the Crazyflie
  39. self._cf.open_link(link_uri)
  40.  
  41. # Variable used to keep main loop occupied until disconnect
  42. self.is_connected = True
  43.  
  44. def _connected(self, link_uri):
  45. """ This callback is called form the Crazyflie API when a Crazyflie
  46. has been connected and the TOCs have been downloaded."""
  47. print "Connected to %s" % link_uri
  48.  
  49. # The definition of the logconfig can be made before connecting
  50. self._lg_stab = LogConfig(name="Stabilizer", period_in_ms=10)
  51. self._lg_stab.add_variable("stabilizer.roll", "float")
  52. self._lg_stab.add_variable("stabilizer.pitch", "float")
  53. self._lg_stab.add_variable("stabilizer.yaw", "float")
  54.  
  55. # Adding the configuration cannot be done until a Crazyflie is
  56. # connected, since we need to check that the variables we
  57. # would like to log are in the TOC.
  58. self._cf.log.add_config(self._lg_stab)
  59. if self._lg_stab.valid:
  60. # This callback will receive the data
  61. self._lg_stab.data_received_cb.add_callback(self._stab_log_data)
  62. # This callback will be called on errors
  63. self._lg_stab.error_cb.add_callback(self._stab_log_error)
  64. # Start the logging
  65. self._lg_stab.start()
  66. else:
  67. print("Could not add logconfig since some variables are not in TOC")
  68.  
  69. # Start a separate thread to do the motor test.
  70. # Do not hijack the calling thread!
  71. Thread(target=self._ramp_motors).start()
  72.  
  73. # Start a timer to disconnect in 10s
  74. t = Timer(5, self._cf.close_link)
  75. t.start()
  76.  
  77. def _ramp_motors(self):
  78. thrust_mult = 1
  79. thrust_step = 500
  80. thrust = 20000
  81. pitch = 0
  82. roll = 0
  83. yawrate = 0
  84. while thrust >= 20000:
  85. self._cf.commander.send_setpoint(roll, pitch, yawrate, thrust)
  86. time.sleep(0.1)
  87. if thrust >= 25000:
  88. thrust_mult = -1
  89. thrust += thrust_step * thrust_mult
  90. self._cf.commander.send_setpoint(0, 0, 0, 0)
  91. # Make sure that the last packet leaves before the link is closed
  92. # since the message queue is not flushed before closing
  93. time.sleep(0.1)
  94.  
  95. def _stab_log_error(self, logconf, msg):
  96. """Callback from the log API when an error occurs"""
  97. print "Error when logging %s: %s" % (logconf.name, msg)
  98.  
  99. def _stab_log_data(self, timestamp, data, logconf):
  100. """Callback froma the log API when data arrives"""
  101. print "[%d][%s]: %s" % (timestamp, logconf.name, data)
  102.  
  103. def _connection_failed(self, link_uri, msg):
  104. """Callback when connection initial connection fails (i.e no Crazyflie
  105. at the speficied address)"""
  106. print "Connection to %s failed: %s" % (link_uri, msg)
  107. self.is_connected = False
  108.  
  109. def _connection_lost(self, link_uri, msg):
  110. """Callback when disconnected after a connection has been made (i.e
  111. Crazyflie moves out of range)"""
  112. print "Connection to %s lost: %s" % (link_uri, msg)
  113.  
  114. def _disconnected(self, link_uri):
  115. """Callback when the Crazyflie is disconnected (called in all cases)"""
  116. print "Disconnected from %s" % link_uri
  117. self.is_connected = False
  118.  
  119. if __name__ == '__main__':
  120. # Initialize the low-level drivers (don't list the debug drivers)
  121. cflib.crtp.init_drivers(enable_debug_driver=False)
  122. # Scan for Crazyflies and use the first one found
  123. print "Scanning interfaces for Crazyflies..."
  124. available = cflib.crtp.scan_interfaces()
  125. print "Crazyflies found:"
  126. for i in available:
  127. print i[0]
  128.  
  129. if len(available) > 0:
  130. le = LoggingExample(available[0][0])
  131. else:
  132. print "No Crazyflies found, cannot run example"
  133.  
  134. # The Crazyflie lib doesn't contain anything to keep the application alive,
  135. # so this is where your application should do something. In our case we
  136. # are just waiting until we are disconnected.
  137. while le.is_connected:
  138. time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement