Advertisement
Guest User

commander.py

a guest
Jun 20th, 2014
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.41 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. #     ||          ____  _ __
  5. #  +------+      / __ )(_) /_______________ _____  ___
  6. #  | 0xBC |     / __  / / __/ ___/ ___/ __ `/_  / / _ \
  7. #  +------+    / /_/ / / /_/ /__/ /  / /_/ / / /_/  __/
  8. #   ||  ||    /_____/_/\__/\___/_/   \__,_/ /___/\___/
  9. #
  10. #  Copyright (C) 2011-2013 Bitcraze AB
  11. #
  12. #  Crazyflie Nano Quadcopter Client
  13. #
  14. #  This program is free software; you can redistribute it and/or
  15. #  modify it under the terms of the GNU General Public License
  16. #  as published by the Free Software Foundation; either version 2
  17. #  of the License, or (at your option) any later version.
  18. #
  19. #  This program is distributed in the hope that it will be useful,
  20. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22. #  GNU General Public License for more details.
  23.  
  24. #  You should have received a copy of the GNU General Public License
  25. #  along with this program; if not, write to the Free Software
  26. #  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  27. #  MA  02110-1301, USA.
  28.  
  29. """
  30. Used for sending control setpoints to the Crazyflie
  31. """
  32.  
  33. __author__ = 'Bitcraze AB'
  34. __all__ = ['Commander']
  35.  
  36. from cflib.crtp.crtpstack import CRTPPacket, CRTPPort
  37. import struct
  38.  
  39.  
  40. class Commander():
  41.     """
  42.    Used for sending control setpoints to the Crazyflie
  43.    """
  44.  
  45.     def __init__(self, crazyflie=None):
  46.         """
  47.        Initialize the commander object. By default the commander is in
  48.        +-mode (not x-mode).
  49.        """
  50.         self._cf = crazyflie
  51.         self._x_mode = False
  52.  
  53.     def set_client_xmode(self, enabled):
  54.         """
  55.        Enable/disable the client side X-mode. When enabled this recalculates
  56.        the setpoints before sending them to the Crazyflie.
  57.        """
  58.         self._x_mode = enabled
  59.  
  60.     def send_setpoint(self, roll, pitch, yaw, thrust, r=0, g=0, b=0):
  61.         """
  62.        Send a new control setpoint for roll/pitch/yaw/thust to the copter
  63.  
  64.        The arguments roll/pitch/yaw/trust is the new setpoints that should
  65.        be sent to the copter
  66.        """
  67.         if self._x_mode:
  68.             roll = 0.707 * (roll - pitch)
  69.             pitch = 0.707 * (roll + pitch)
  70.  
  71.         pk = CRTPPacket()
  72.         pk.port = CRTPPort.COMMANDER
  73.         pk.data = struct.pack('<fffHBBB', roll, -pitch, yaw, thrust, r, g, b)
  74.         self._cf.send_packet(pk)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement