New program to control the crazyflie with your keyboard

Firmware/software/electronics/mechanics
eldraco
Beginner
Posts: 5
Joined: Thu Jan 02, 2014 7:56 pm

New program to control the crazyflie with your keyboard

Post by eldraco »

Hi list.

Yesterday I wanted to control the crazyflie with my keyboard directly and I did't find anything so I made a python program. it is useful if you just buy it and you still don't have a gamepad, or if you controller is broken or you just forget it at home. If this is your case, don't worry! you can now control your crazyflie with your keyboard and at the same time you have the sensors values on the console. It can be seen as a 'poor man' controller.

It is called keyboard_control.py and you can find it here:
https://bitbucket.org/eldraco2000/crazyflie-programs

The program connects to the first crazyflie that it can find (so you can change the wifi channel without updating your programs) and prints on the console the values of the sensors (roll, pitch, yaw and thrust). Then you can press these letters to control it:

w -> More thrust
s -> Less thrust
d -> Yaw right
a -> Yaw left
i -> More pitch (go ahead)
k -> Less pitch (go back)
l -> Roll to the right
j -> Roll to the left
q -> Stops flying but it does not disconnect from the crazyflie. Just to save the crazyflie when it hits some wall. You can continue flying.
e -> Exit. Stops flying, disconnects from the crazyflie and stops the program.

This resembles the common mode 2 of operation in a gamepad.

Also notice the following functionalities:
- If you keep any of these letters pressed, the corresponding value continues to grow (or decrease), but if you stop pressing it the value goes to 0. For example, the more time you press 'l' to roll right, the more it will roll. But if you stop pressing 'l' the crazyflie stop rolling. It simulates the behavior of going 'back' to the center of the gamepad.
- The thurst does not have this behavior. So if the crazyflie is flying you don't have to press 'w' to keep it on the air. Just take it as high as you want and stop pressing 'w', it will stay there like some cheap 'hovering' mode without using the altimeter.

It takes so time to get used to it and it is perhaps not so easy to fly, but it is possible. I will try to configure the controls better.

I hope you like it and it is useful! In the future I will add:
- Vi mode. Control the crazyflie with vi letters (hjkl) and exit with Esc. Don't know if it is good, but let's try.
- Possibility to trim the control during fly.

see you
sebas
bherbert
Beginner
Posts: 1
Joined: Fri Jan 03, 2014 10:32 pm

Re: New program to control the crazyflie with your keyboard

Post by bherbert »

Awesome. I got my Crazyflie a few days ago and immediately began messing with it using the VM. I've been wanting to control it from my computer, and could not believe it hadn't been done already!

Will definitely mess with this today. :)
eldraco
Beginner
Posts: 5
Joined: Thu Jan 02, 2014 7:56 pm

Re: New program to control the crazyflie with your keyboard

Post by eldraco »

Hey, tell me if you have any issues, or just modify it.

I also only have it for a few days and in my experience the ranking of easiest ways to fly it are:
1- The hover mode (http://forum.bitcraze.se/viewtopic.php?f=6&t=523). Keeps altitude perfectly.
2- The keyboard control (because of the artificial hover)
3- Normal without hover

With the normal control I always have hard times keeping the altitude. I need more practice surely.

I tried the modification using the compass but it did not work for me.

cheers
sebas
csholmq
Beginner
Posts: 12
Joined: Fri Dec 27, 2013 1:20 pm

Re: New program to control the crazyflie with your keyboard

Post by csholmq »

Hi. Just tried it out but immediately ran into trouble.
Initializing drivers
Searching for available devices
Connecting to interface with URI [radio://0/10/250K] and name
No handlers could be found for logger "cflib.crazyflie"
WARNING:root:[5]: Was expecting 0 but got 9
WARNING:root:[5]: Was expecting 0 but got 9
WARNING:root:[5]: Was expecting 0 but got 9
WARNING:root:[5]: Was expecting 0 but got 9
WARNING:cflib.crazyflie.toccache:Could not save cache, no writable directory
WARNING:cflib.crazyflie:ExpectAnswer: ERROR! Older timer whas running while scheduling new one on [2]
WARNING:cflib.crazyflie:ExpectAnswer: ERROR! Older timer whas running while scheduling new one on [2]
WARNING:cflib.crazyflie.toccache:Could not save cache, no writable directory
WARNING:cflib.crazyflie:Exception while doing callback on port [2]

Traceback (most recent call last):
File "/Users/csholmq/Downloads/cfclient-2013.4.2/lib/cflib/crazyflie/__init__.py", line 334, in run
cb[4](pk)
File "/Users/csholmq/Downloads/cfclient-2013.4.2/lib/cflib/crazyflie/toc.py", line 199, in _new_packet_cb
self._toc_fetch_finished()
File "/Users/csholmq/Downloads/cfclient-2013.4.2/lib/cflib/crazyflie/toc.py", line 151, in _toc_fetch_finished
self.finishedCallback()
File "/Users/csholmq/Downloads/cfclient-2013.4.2/lib/cflib/crazyflie/__init__.py", line 136, in _param_toc_updated_cb
self.connectSetupFinished.call(self.link_uri)
File "/Users/csholmq/Downloads/cfclient-2013.4.2/lib/cflib/utils/callbacks.py", line 56, in call
cb(*args)
File "keyboard_control.py", line 93, in connectSetupFinished
self.logStab.add_variable("stabilizer.roll", "float")
AttributeError: LogConfig instance has no attribute 'add_variable'
Any ideas where this error is from?
csholmq
Beginner
Posts: 12
Joined: Fri Dec 27, 2013 1:20 pm

Re: New program to control the crazyflie with your keyboard

Post by csholmq »

Nevermind. The problem was that I downloaded the "latest" cfclient which was from 2013.11. So I grabbed the latest source and now it works great!

Fairly difficult to fly since it's so "zippy". I imagine I have to learn it's dynamics with a regular controller first. Beautiful script none the less.
eldraco
Beginner
Posts: 5
Joined: Thu Jan 02, 2014 7:56 pm

Re: New program to control the crazyflie with your keyboard

Post by eldraco »

Yes, it is still a little difficult to control. I will try to make i easier if I can.
Also I would love to make it work with hoover mode, so it is easier to control altitude.

thanks for trying it!
sebas
kikill
Beginner
Posts: 7
Joined: Sat Dec 28, 2013 9:47 am

Re: New program to control the crazyflie with your keyboard

Post by kikill »

thank you eldraco for sharing those programs

-> keyboard_control.py
i noticed you wrote this code twice

Code: Select all

start_roll = 0
roll_increment = 30
min_roll = -50
max_roll = 50
talking about the hovering, does this code get the altitude (from sea level) ?

Code: Select all

self.logData.add_variable("baro.aslLong", "float")
i modified your keyboard_control.py to program the crazyflie to hover at a given altitude, i'll upload it on bitbucket when i get home.

here is a sample :

Code: Select all

altitude = ""
...
    def print_baro_data(self, ident, data, logconfig):
        global altitude
        altitude = data["baro.aslLong"]
        Thread(target=self.increasing_step).start()

    def increasing_step(self):
        global altitude
        ...
        max_altitude = 200.0

        while (altitude != max_altitude):
            if (altitude < max_altitude):
                thrust += thrust_increment
            else:
                thrust -= thrust_increment
            self.crazyflie.commander.send_setpoint(roll, pitch, yaw, thrust)

        self.crazyflie.commander.send_setpoint(0,0,0,0)
        # Make sure that the last packet leaves before the link is closed
        # since the message queue is not flushed before closing
        self.crazyflie.close_link()

TestFlight()
yvanhoe
Beginner
Posts: 1
Joined: Fri Jan 24, 2014 8:12 am

Re: New program to control the crazyflie with your keyboard

Post by yvanhoe »

Did you get home? :-)

I am interested as I was planning to do the same. I guess you will be using the barometer for that? I only have a 6DOF version (damn seeed studio who was out of stock of 10 DOF before christmas) so if you give the code, I'll try to adapt it to use the accelerometers. It of course won't be that good, but it will make the whole thing a bit easier to pilot...
brownyoda
Beginner
Posts: 3
Joined: Sun Jan 26, 2014 11:29 am

Re: New program to control the crazyflie with your keyboard

Post by brownyoda »

Hey. I just started working with crazyflie.

I wanted to try your program. I'm using the virtual machine and I'm not sure where to put your file to run it. This might sound really easy and I apologize, but I'm new to this. I don't have a game controller and I want to control my crazyflie with a program like you created. Please help.
orcinus
Member
Posts: 36
Joined: Thu Jan 23, 2014 11:03 pm

Re: New program to control the crazyflie with your keyboard

Post by orcinus »

It appears the script tends to crash after you reach a certain throttle level (i'm assuming due to an overflow of some kind).
Post Reply