Programmable Flight Demonstration
-
- Beginner
- Posts: 5
- Joined: Mon Feb 16, 2015 6:04 am
Programmable Flight Demonstration
Hello All,
I've recently bought a new CrazyFlie 2.0 and wanted to see if there was existing code to demonstrate the CrazyFlie 2.0 from flying up, moving left and right a couple of inches, and then coming down. Simply to show that it can autonomously fly.
Any help, suggestions, or links to similar existing posts that I have missed would be great!!
Also, I'm a little confused on how you program the crazyflie with a script (.py)? Can this be done through a loader on the client?
Thanks again for any help guys!
I've recently bought a new CrazyFlie 2.0 and wanted to see if there was existing code to demonstrate the CrazyFlie 2.0 from flying up, moving left and right a couple of inches, and then coming down. Simply to show that it can autonomously fly.
Any help, suggestions, or links to similar existing posts that I have missed would be great!!
Also, I'm a little confused on how you program the crazyflie with a script (.py)? Can this be done through a loader on the client?
Thanks again for any help guys!
Re: Programmable Flight Demonstration
Hi,
I will first answer your last question: The copter is not programmed with python. The Crazyflie is programmed in C and Python is used on the PC side to send commands to it using the radio dongle Crazyradio (PA). If you want to start 'easy' without configuring a lot of things you can run our virtual machine that contains all software pre-setup. The best example to get started is to run example/ramp.py from the Crazyflie client project (you can run either from console or from pycharm).
I recently played a bit with controlling the copter in 'open loop' so just sending timed commands, I ended up with escape.py:
Warning: run this code only when the copter is in a room that does not have breakable items and NEVER outdoor as I cannot guarantee it will not fly away. Also if you have problems just disconnect the radio from the computer and Crazyflie will stop.
This code should be put in the client example folder and run from there, it is based on ramp.py which is a good example to use as a starting point. This example will take off while going forward and then when reading an altitude of 1m it starts descending until it reaches the start altitude. It is using the log capability and the altitude sensor to get the copter altiture. There is a lot of over-simplified things there but it works quite well as an example.
I will first answer your last question: The copter is not programmed with python. The Crazyflie is programmed in C and Python is used on the PC side to send commands to it using the radio dongle Crazyradio (PA). If you want to start 'easy' without configuring a lot of things you can run our virtual machine that contains all software pre-setup. The best example to get started is to run example/ramp.py from the Crazyflie client project (you can run either from console or from pycharm).
I recently played a bit with controlling the copter in 'open loop' so just sending timed commands, I ended up with escape.py:
Code: Select all
# -*- coding: utf-8 -*-
#
# || ____ _ __
# +------+ / __ )(_) /_______________ _____ ___
# | 0xBC | / __ / / __/ ___/ ___/ __ `/_ / / _ \
# +------+ / /_/ / / /_/ /__/ / / /_/ / / /_/ __/
# || || /_____/_/\__/\___/_/ \__,_/ /___/\___/
#
# Copyright (C) 2014 Bitcraze AB
#
# Crazyflie Nano Quadcopter Client
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
"""
Simple scripted escape
"""
import time, sys
from threading import Thread
from cfclient.utils.logconfigreader import LogConfig
#FIXME: Has to be launched from within the example folder
sys.path.append("../lib")
import cflib
from cflib.crazyflie import Crazyflie
import logging
logging.basicConfig(level=logging.ERROR)
class Escape:
"""Example that connects to a Crazyflie , escape and then disconnect"""
def __init__(self, link_uri):
""" Initialize and run the example with the specified link_uri """
self._start_alt = 0
self._alt = 0
self._takeoff = False
self._cf = Crazyflie()
self._cf.connected.add_callback(self._connected)
self._cf.disconnected.add_callback(self._disconnected)
self._cf.connection_failed.add_callback(self._connection_failed)
self._cf.connection_lost.add_callback(self._connection_lost)
self._cf.open_link(link_uri)
print "Connecting to %s" % link_uri
def _connected(self, link_uri):
""" This callback is called form the Crazyflie API when a Crazyflie
has been connected and the TOCs have been downloaded."""
print "Connected to %s" % link_uri
# The definition of the logconfig can be made before connecting
self._lg_alt = LogConfig(name="altitude", period_in_ms=10)
self._lg_alt.add_variable("baro.asl", "float")
# Adding the configuration cannot be done until a Crazyflie is
# connected, since we need to check that the variables we
# would like to log are in the TOC.
self._cf.log.add_config(self._lg_alt)
if self._lg_alt.valid:
# This callback will receive the data
self._lg_alt.data_received_cb.add_callback(self._alt_log_data)
# This callback will be called on errors
self._lg_alt.error_cb.add_callback(self._alt_log_error)
# Start the logging
self._lg_alt.start()
else:
print("Could not add logconfig since some variables are not in TOC")
# Start a separate thread to do the motor test.
# Do not hijack the calling thread!
Thread(target=self._do_escape).start()
def _alt_log_error(self, logconf, msg):
"""Callback from the log API when an error occurs"""
print "Error when logging %s: %s" % (logconf.name, msg)
def _alt_log_data(self, timestamp, data, logconf):
"""Callback froma the log API when data arrives"""
if logconf.name == "altitude":
if not self._takeoff:
self._start_alt = data['baro.asl']
self._takeoff = True
else:
self._alt = data['baro.asl']
print "{}".format(self._alt - self._start_alt)
def _connection_failed(self, link_uri, msg):
"""Callback when connection initial connection fails (i.e no Crazyflie
at the speficied address)"""
print "Connection to %s failed: %s" % (link_uri, msg)
def _connection_lost(self, link_uri, msg):
"""Callback when disconnected after a connection has been made (i.e
Crazyflie moves out of range)"""
print "Connection to %s lost: %s" % (link_uri, msg)
def _disconnected(self, link_uri):
"""Callback when the Crazyflie is disconnected (called in all cases)"""
print "Disconnected from %s" % link_uri
def _do_escape(self):
while not self._takeoff:
pass
#Unlock startup thrust protection
self._cf.commander.send_setpoint(0, 0, 0, 0)
time.sleep(0.1)
# (Roll, Pitch, Yaw, Thrust)
self._cf.commander.send_setpoint( 0, 40, 0, 0.75*64768)
time.sleep(0.2)
self._cf.commander.send_setpoint(0, 20, 0, 0.75*64768)
time.sleep(0.2)
self._cf.commander.send_setpoint(0, 5, 0, 0.75*64768)
# Wait for Crazyflie to reach some altitude, could be replaced by a sleep and needs a timeout!
while self._alt < (self._start_alt + 1):
pass
print "0, Going down!"
self._cf.commander.send_setpoint(0, 5, 0, 0.3*64768)
time.sleep(0.3)
self._cf.commander.send_setpoint(0, 0, 0, 0.55*64768)
# Wait for Crazyflie to come back to 0, could be replaced by a sleep and needs a timeout!
while self._alt > (self._start_alt+0.2):
pass
self._cf.commander.send_setpoint(0, 0, 0, 0.3*64768)
time.sleep(0.1)
self._cf.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
time.sleep(0.1)
self._cf.close_link()
if __name__ == '__main__':
# Initialize the low-level drivers (don't list the debug drivers)
cflib.crtp.init_drivers(enable_debug_driver=False)
# To quickly connect a single Crazyflie
#le = Escape("radio://0/45/2M")
# Or, to scan for Crazyflies and use the first one found
print "Scanning interfaces for Crazyflies..."
available = cflib.crtp.scan_interfaces()
print "Crazyflies found:"
for i in available:
print i[0]
if len(available) > 0:
le = Escape(available[0][0])
else:
print "No Crazyflies found, cannot run example"
This code should be put in the client example folder and run from there, it is based on ramp.py which is a good example to use as a starting point. This example will take off while going forward and then when reading an altitude of 1m it starts descending until it reaches the start altitude. It is using the log capability and the altitude sensor to get the copter altiture. There is a lot of over-simplified things there but it works quite well as an example.
-
- Beginner
- Posts: 5
- Joined: Mon Feb 16, 2015 6:04 am
Re: Programmable Flight Demonstration
Arnaud,
This is great!! Thank you so much for all the initial guidance. Two follow up questions, can you link something to "our virtual machine" that you mentioned was pre-setup? Or is it on the forum somewhere?
Also, I for some reason don't have an examples folder in the CrazyFlie client folder (C:\Program Files (x86)\Crazyflie client), should I re-download it?
Thanks again for all your help!
This is great!! Thank you so much for all the initial guidance. Two follow up questions, can you link something to "our virtual machine" that you mentioned was pre-setup? Or is it on the forum somewhere?
Also, I for some reason don't have an examples folder in the CrazyFlie client folder (C:\Program Files (x86)\Crazyflie client), should I re-download it?
Thanks again for all your help!
Re: Programmable Flight Demonstration
Hi BlitzProgrammer,
Sorry I usually put links but I guess it is becoming a bit too obvious for me
We have made a Linux virtual machine (VM) that can be run with Virtualbox: http://wiki.bitcraze.se/projects:virtualmachine:index
If you want to develop with the Client you need to work in the Client source code, it contains the example folder: https://github.com/bitcraze/crazyflie-clients-python
Of course this project is already in the virtual machine, as well as all the dependencies.
You can also run from source on windows, the readme of the project and the wiki contains info on how to install dependencies on Windows but it can sometimes be a bit tricky (http://wiki.bitcraze.se/doc:crazyflie:dev:env:python)
To get started you can try to follow the two videos we did last year on how to develop for Crazyflie and the PC client, it was for Crazyflie 1 but the basic is still the same: https://www.youtube.com/watch?v=chWrNh73YBw and https://www.youtube.com/watch?v=cutgIMfHwyQ
Sorry I usually put links but I guess it is becoming a bit too obvious for me
We have made a Linux virtual machine (VM) that can be run with Virtualbox: http://wiki.bitcraze.se/projects:virtualmachine:index
If you want to develop with the Client you need to work in the Client source code, it contains the example folder: https://github.com/bitcraze/crazyflie-clients-python
Of course this project is already in the virtual machine, as well as all the dependencies.
You can also run from source on windows, the readme of the project and the wiki contains info on how to install dependencies on Windows but it can sometimes be a bit tricky (http://wiki.bitcraze.se/doc:crazyflie:dev:env:python)
To get started you can try to follow the two videos we did last year on how to develop for Crazyflie and the PC client, it was for Crazyflie 1 but the basic is still the same: https://www.youtube.com/watch?v=chWrNh73YBw and https://www.youtube.com/watch?v=cutgIMfHwyQ
-
- Beginner
- Posts: 5
- Joined: Mon Feb 16, 2015 6:04 am
Re: Programmable Flight Demonstration
Thanks Arnaud!
I actually managed to setup the virtual machine and am currently trying to get escape.py (you're program) to run but am running into a compiling error as stated below:
So far loving my new device and hope to learn as much as possible with it
Again, thanks Arnaud for all the help thus far
I actually managed to setup the virtual machine and am currently trying to get escape.py (you're program) to run but am running into a compiling error as stated below:
However, ramp.py works great and really cool!!/usr/bin/python2.7 /home/bitcraze/projects/crazyflie-clients-python/examples/escape.py
Traceback (most recent call last):
File "/home/bitcraze/projects/crazyflie-clients-python/examples/escape.py", line 34, in <module>
from cfclient.utils.logconfigreader import LogConfig
ImportError: No module named cfclient.utils.logconfigreader
Process finished with exit code 1
So far loving my new device and hope to learn as much as possible with it
Again, thanks Arnaud for all the help thus far
-
- Beginner
- Posts: 5
- Joined: Mon Feb 16, 2015 6:04 am
Re: Programmable Flight Demonstration
Just kidding, seem to have got it running!
Battery was low so can't test at the moment but will keep everyone up to date on the code shortly!
Thanks
Battery was low so can't test at the moment but will keep everyone up to date on the code shortly!
Thanks
Re: Programmable Flight Demonstration
I double-clicked ramp.py and it ran a python3.4 window for less than a second then closed without any logs or anything. How do I run it to connect? Also, how do I open a Python editor to make a program for this?
Re: Programmable Flight Demonstration
Never mind, I got it to work. I am also trying to do what BlitzProgrammer did for a school project. The problem is, my CF2 is unbalanced and when hovering, leans in one direction. My altitude hold button generates an error saying that the althold is not in the TOC. Can you give me some guidance for those things?
Thanks!
Thanks!
Re: Programmable Flight Demonstration
You need to run these through the Python interpreter... Are you using Windows, Linux, or Mac? You will need to run the example scripts, including this one, from the command line. Also, python scripts are plaintext so you can edit them in any text editor.qubitter wrote:I double-clicked ramp.py and it ran a python3.4 window for less than a second then closed without any logs or anything. How do I run it to connect? Also, how do I open a Python editor to make a program for this?
Crazyflier - my CF journal...
4x Crazyflie Nano (1.0) 10-DOF + NeoPixel Ring mod.
3x Crazyflie 2.0 + Qi Charger and LED Decks.
Raspberry Pi Ground Control.
Mac OS X Dev Environment.
Walkera Devo7e, ESky ET6I, PS3 and iOS Controllers.
4x Crazyflie Nano (1.0) 10-DOF + NeoPixel Ring mod.
3x Crazyflie 2.0 + Qi Charger and LED Decks.
Raspberry Pi Ground Control.
Mac OS X Dev Environment.
Walkera Devo7e, ESky ET6I, PS3 and iOS Controllers.
Re: Programmable Flight Demonstration
Please open a new thread for things like this qubitter...qubitter wrote:Never mind, I got it to work. I am also trying to do what BlitzProgrammer did for a school project. The problem is, my CF2 is unbalanced and when hovering, leans in one direction. My altitude hold button generates an error saying that the althold is not in the TOC. Can you give me some guidance for those things?
Thanks!
To balance the props, try doing this. Make sure your battery is as centered as possible. Make sure your propellers are not pressed down too far so they're rubbing on the motor casing. If all these things are done and it still pulls in one direction or another, set roll and pitch trim values in the GUI client or in the firmware configuration block to counteract the tendency.
Crazyflier - my CF journal...
4x Crazyflie Nano (1.0) 10-DOF + NeoPixel Ring mod.
3x Crazyflie 2.0 + Qi Charger and LED Decks.
Raspberry Pi Ground Control.
Mac OS X Dev Environment.
Walkera Devo7e, ESky ET6I, PS3 and iOS Controllers.
4x Crazyflie Nano (1.0) 10-DOF + NeoPixel Ring mod.
3x Crazyflie 2.0 + Qi Charger and LED Decks.
Raspberry Pi Ground Control.
Mac OS X Dev Environment.
Walkera Devo7e, ESky ET6I, PS3 and iOS Controllers.