Minimal Python Example?

Firmware/software/electronics/mechanics
Post Reply
eddieparker
Beginner
Posts: 4
Joined: Fri Feb 06, 2015 6:14 am

Minimal Python Example?

Post by eddieparker »

Hey:

I'm trying to write a minimal python example that will have my Crazyflie 2 hover. Unfortunately I have a weird situation where if the Crazyflie is plugged in via USB it's relatively fine (occasionally I get a random "Too many packets lost" and it tears down the connection. :/), but if I take it *off* the USB, and try to run the code via the radio that way, it just shuts down the Crazyflie (the lights on the board all go out) and I get a "Too many packets lost" error.

Here's my code, in case I'm doing something horribly wrong:

Code: Select all

import logging
import sys
from threading import Thread
import time

sys.path.insert(0,"lib")

from cflib.crazyflie import Crazyflie
from cflib import crtp

class Hover:

	def __init__(self):
		self.m_bShuttingDown = False
		self.m_CrazyFlie = None

	def Run(self):
		logging.basicConfig()
		logging.getLogger().setLevel(logging.INFO)
		#logging.getLogger().setLevel(logging.DEBUG)

		logging.info("Initializing drivers.")
		# Init drivers
		crtp.init_drivers()
		availableLinks = crtp.scan_interfaces()
		logging.info("Available links: %s"%(availableLinks))
		logging.info("Initializing Crazyflie.")
		self.m_CrazyFlie = Crazyflie(ro_cache="cachero", rw_cache="cacherw")

		logging.info("Setting radio link.")
		if(len(availableLinks) == 0):
			logging.error("Error, no links.  Aborting.")
			return

		linkUri = availableLinks[0][0]

		self.m_CrazyFlie.connected.add_callback(self.OnConnected)
		self.m_CrazyFlie.open_link(linkUri)

		try:
			while not self.m_bShuttingDown:
				time.sleep(0.1)
		except:
			logging.error("Shutting down due to exception.")
			self.m_bShuttingDown = True
			self.m_CrazyFlie.close_link()

	def OnConnected(self,linkUri):
		logging.info("OnConnectSetupFinished")

		def Pulse():
			while not self.m_bShuttingDown:
				roll = 0
				pitch = 0
				yawrate = 0
				thrust = 32000
				self.m_CrazyFlie.commander.send_setpoint(roll, pitch, yawrate, thrust)
				time.sleep(2)

		Pulse()


Hover().Run()
And here's the log I get:

Code: Select all

INFO:root:Initializing drivers.
INFO:cflib.crtp.radiodriver:v0.53 dongle with serial N/A found
INFO:cflib.drivers.cfusb:Looking for devices....
INFO:root:Available links: [['radio://0/77/250K', ''], ['radio://0/80/250K', '']]
INFO:root:Initializing Crazyflie.
INFO:root:Setting radio link.
INFO:cflib.crazyflie:Callback->Connection initialized[radio://0/77/250K]
INFO:cflib.crazyflie:We are connected[radio://0/77/250K], request connection setup
INFO:cflib.crazyflie:Callback->Connected to [radio://0/77/250K]
INFO:cflib.crazyflie.toc:TOC for port [5] found in cache
INFO:cflib.crazyflie:Log TOC finished updating
INFO:cflib.crazyflie.mem:Requesting number of memories
INFO:cflib.crazyflie.mem:1 memories found
INFO:cflib.crazyflie.mem:Requesting first id
INFO:cflib.crazyflie.mem:1 memories found
INFO:cflib.crazyflie.mem:Memory: id=0, type=I2C, size=8191
INFO:cflib.crazyflie.mem:Done getting all the memories, start reading the OWs
INFO:cflib.crazyflie:Memories finished updating
INFO:cflib.crazyflie.toc:TOC for port [2] found in cache
INFO:cflib.crazyflie:Param TOC finished updating
INFO:cflib.crazyflie:Callback->Connection setup finished [radio://0/77/250K]
INFO:root:OnConnectSetupFinished
WARNING:cflib.crazyflie:Got link error callback [Too many packets lost] in state [2]
INFO:cflib.crazyflie:Callback->Disconnected from [radio://0/77/250K]
INFO:cflib.crazyflie:Callback->Connection lost to [radio://0/77/250K]: Too many packets lost
If there's another minimal example for me to look at to make sure I'm setting things up correctly, I'd appreciate it!

Thanks,

-e-
tobias
Bitcraze
Posts: 2339
Joined: Mon Jan 28, 2013 7:17 pm
Location: Sweden

Re: Minimal Python Example?

Post by tobias »

It's a pwm noise issue that can be fixed with a firmware update. Please see this post.
eddieparker
Beginner
Posts: 4
Joined: Fri Feb 06, 2015 6:14 am

Re: Minimal Python Example?

Post by eddieparker »

Thanks tobias.

So unfortunately I can't seem to flash my firmware. Running the PC Client, trying to get into the bootloader, if I do the Initiate bootloader cold boot, it just times out with a message about "Trying to connect cold bootloader, restart the Crazyflie to connect". I'm doing it as per the instructions, but nothing happens.

It looks like my code works fine with/without the cable attached now, the only issue is if my thrust is set over 30000 (is that a limit?). Unfortunately if I do anything less it doesn't take off.

Is there some magic to flashing the CF2 that I'm forgetting? Is there a limit on the thrust?

Thanks,

-e-
chromebookbob
Beginner
Posts: 12
Joined: Sat Dec 06, 2014 11:40 pm

Re: Minimal Python Example?

Post by chromebookbob »

in python thrust must be over a certain value, I think it's around 30000. To get into bootloader mode you have to hold the power button when turning the crazyflie 2.0 on for a few seconds. Then you can use the bootloader mode in the client.
akbarhash
Beginner
Posts: 12
Joined: Sat Jan 31, 2015 8:19 am

Re: Minimal Python Example?

Post by akbarhash »

eddieparker wrote:Thanks tobias.

So unfortunately I can't seem to flash my firmware. Running the PC Client, trying to get into the bootloader, if I do the Initiate bootloader cold boot, it just times out with a message about "Trying to connect cold bootloader, restart the Crazyflie to connect". I'm doing it as per the instructions, but nothing happens.

It looks like my code works fine with/without the cable attached now, the only issue is if my thrust is set over 30000 (is that a limit?). Unfortunately if I do anything less it doesn't take off.

Is there some magic to flashing the CF2 that I'm forgetting? Is there a limit on the thrust?

Thanks,

-e-
Even I had same problem. It is issue of outdated PC client. Here try from here the latest version. It will work.
https://github.com/bitcraze/crazyflie-c ... n/releases
Post Reply