Example #1
0
class BaseDrone ():
    """ BaseDrone class that is used to communicate to a crazyflie2.1 drone.

    The BaseDrone class establishes a connection and gives a small set of functions to verify that the connection is set e.g. communicating with the led ring.
    """


################################################################################
#               Initialization and cleanup code
################################################################################

    def __init__(self, uri):
        """Initialize the connection to the Drone.

        The uri is depending on the drone and has to be given at initialization. 
        """
        self.uri = uri

        cflib.crtp.init_drivers(enable_debug_driver=False)

        self.cf = Crazyflie(rw_cache='./cache')
        self.scf = SyncCrazyflie (self.uri, cf=self.cf)

    def __enter__(self):
        """Establishes the link to the drone and returns the BaseDroneObject"""
        self.scf.open_link()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        """Closes the connection to the drone to secure a clean shutdown even in case of an exeption"""
        self.close_link()

    def close_link(self):
        """Closes the Link to the drone and removes possible callbacks"""
        self.scf.cf.close_link()
        self.scf._remove_callbacks()
        self.scf._is_link_open = False


################################################################################
#        Higher Functionality like setting the color of the LED's
#                       (get creative here)
################################################################################

    def red_green_color(self, sequence):
        """Gets an array of values and displays a green or a red light on the led ring

        if sequence[n] - startXPositionOfDrone < 0: red light
        else: green light
        """
        cf = self.scf.cf
        
        self.setRingEffect(7)
        
        for position in sequence:
            x = position[0]
            if x<0:
                self.setRingColor(red=100)
            else:
                self.setRingColor(green=100)
            time.sleep(1) # time for led to be shown

    def setRingEffect(self, value):
        """Sets the ring.effect param of the cf to the specified value
        
        If the value is not in range of the specified effects (see https://wiki.bitcraze.io/projects:crazyflie2:expansionboards:ledring), it will print an warning
        """
        
        MAX_NUM_EFFECTS = 13
        if value >= 0 and value < MAX_NUM_EFFECTS:
            self.scf.cf.param.set_value('ring.effect',str(value))
        else:
            print("WARNING: Could not set ring.effect to value: {}. Use a value between 0 and {}. For further Information see https://wiki.bitcraze.io/projects:crazyflie2:expansionboards:ledring".format(value,MAX_NUM_EFFECTS))

    
    def setRingColor(self, red = 0, green = 0, blue = 0):
        """Sets the color of the ring to a specific value.

        Red, Green and Blue can be numbers between 0 and 100.
        TODO: test if ring effect has to be set to 7 all the time.
        """
        if red < 0 or red > 100 or green < 0 or green > 100 or blue < 0 or blue > 100:
            return False
        else:
            self.scf.cf.param.set_value('ring.solidRed', str(red))
            self.scf.cf.param.set_value('ring.solidGreen', str(green))
            self.scf.cf.param.set_value('ring.solidBlue', str(blue))
            return True