Author: Tobias

Continuing from last Monday post where the hardware wiring part was discussed we now move on to the software side. The brushed motors are controlled with a normal PWM  where the duty cycle will adjust how much power goes into the motors. A brushless motor on the other hand needs more complicated controlling and uses it’s own micro-controller to handle this. These brushless motor controllers (BLMC or Brushless ESC) comes in many flavors and sizes but what is pretty common is how they are interfaced/controlled. This is inherited from how R/C receivers are controlling servos and how the receiver gets updates from the transmitter. This is a PWM where the width of the high pulse define the duty cycle. 1ms equals min and 2ms equals max and this is repeated every 20ms, thus giving an update rate of 50Hz.ServoPwm

This way of interfacing the BLMC is currently the most common way but interfacing with I2C, CAN, etc is getting more common.

To generate the servo PWM on the Crazyflie we have just reconfigured the timer a bit using a conversion macro so that setting the motor ratio of zero will result in a 1ms high pulse and setting it to max (uint16) will result in a 2ms pulse. The period time can be set with the BLMC_PERIOD define in motors.h. The standard period time of 20ms is actually a big drawback as it adds a latency from when a new output is calculated to when it is actually set. Therefore many motor controllers allow to shrink this period down to 2.5ms (400Hz) which result in lower latency and better flight stability.

Brushless prototype board

First you need to put together the brushless prototype board from the last post. The output will be generated on the pins marked BLMC 1,2,3,4 which should be connected in the same position an rotational direction as the brushed M1, M2, M3, M4 respectively. The output signal will be 0v – 3.0v which should work fine with 5V BLMC but it might be worth keeping that in mind.

BL proto descr

Building the brushless firmware

The code which contains the brushless functionality is currently on the bigmerge branch so start by pulling the latest changes and switching to that branch.

git pull
git checkout bigmerge

Then to activate the brushless functionality enable the brushless defines

BRUSHLESS_MOTORCONTROLLER
BRUSHLESS_PROTO_DECK_MAPPING

This can be done by by either creating defines in config.h or by creating  aconfig.mk file in the same directory as the Makefile with the content:

CFLAGS += -DBRUSHLESS_MOTORCONTROLLER
CFLAGS += -DBRUSHLESS_PROTO_DECK_MAPPING

The period time BLMC_PERIOD is by default set to 2.5ms (400Hz) so change that if needed.

Then build the firmware (make sure to clean first)

make clean
make

It will build for the Crazyflie 2.0 and for wireless bootloading by default. Put the Crazyflie 2.0 in bootloader mode by holding the power button until the blue led start to blink, then flash it with the wireless bootloader (Crazyradio required).

make cload

Causion!

You are now probably dealing with powerful and dangerous stuff so make sure to take precautions. E.g. don’t have propellers mounted when you test! When you have taken all the safety precautions do your first test. Remember the Crazyflie firmware has not yet been developed for big quads, it is all at your own risk! And another thing, even though the Crazyflie 2.0 can be controlled using a mobile device we don’t recommend this, use a Crazyradio.

Tuning

With that cautions being said it is great seeing a big quad fly and we will soon put out a video showing some of our builds. Our bigger quads have flown quite well with the stock tuning (PID parameters) but tuning should be done as well. Plenty of guides can be found on ways how to do it so I will not go into details here. The values can be found in pid.h and can be updated (but not saved) live using the cfclient. To save them the pid.h file must be changed and the firmware flashed again.

So last week we had a bit of fun connecting the Crazyflie 2.0 to a bigger quad frame. This is something we did a while ago with the Crazyflie Nano (1.0) , see this forum thread. This time it was the Crazyflie 2.0 turn and we wanted to use the deck port and the proto-deck so it would be easy to attach and remove, which is a great thing with the deck expansion port. Rewriting the current motor drivers would be the easiest way forward so finding 4 suitable timer outputs on the deck port was the first step. Looking at the pin mapping from the STM32F405 datasheet and the deck port you get something like this.Expansion deck pins

From this map one can identify a couple of timers and on pin 7,8 left as well as pin 1,2 right there are some suitable ones, TIM3_CH2, TIM3_CH1, TIM2_CH3 and TIM2_CH4 respectively. The timers will be used to generate a PWM control signal for the brushless motor controller (BLMC). As the deck port also has the VUSB input, from where the Crazyflie 2.0 can be powered (and charged), it can be externally powered from 4.5V – 6V. The Crazyflie 2.0 electronics consumes about 100mA without power optimization, can be good to keep in mind when powering it from something else. With all this information we took a prototype deck, a 2.54mm header, some small wire and got to work. As we already had a frame we wanted to interface with we tailored the output for this, but one could of course tailor it for other quad setups.

BL proto descr

 

And viola, the final results looks like this.

BL-proto on frame from side

BL-proto on frame

We must inform you that this setup is very experimental, and as we are now dealing with dangerous things, much more care must be taken. So don’t do this if you don’t know what you are doing. The software also needs additional safety features before we think it is really usable in a bigger setup.

Next post will be about what to change in the software to make it all work, until then, happy hacking!

By the way Fred released a new version of the Android app last week, nicely done Fred!

So we are back at our office and the “great” Swedish weather. We don’t miss the California sunshine at all, we promise.

We have spent the last days documenting the setup we used to fly the Crazyflie autonomous using the Kinect2 for position control. It is still a work in progress but we think it worth sharing as we think many of you out there will be way better at improving it than we are. What we like so much with this setup is that it’s affordable compared to the professional systems such as VICON and PhaseSpace which makes it possible to play with at home, in the office or at school. It might be limiting in volume but until an affordable system such as the Valve’s lighthouse is available it is a pretty good alternative.

We have put all documentation on the wiki and hopefully it should be pretty straight forward to setup and get up and running.

crazyflie vision architecture

 

The Kinect uses its IR time of flight sensor to detect the altitude of the copter. The image processing is detecting the copter and then gets its position. The controller can the compare the current versus wanted position and then sends correction to bring the copter closer to the wanted position. These correction are sent to the copter until a new image is taken by the Kinect and a new correction is calculated. This closed loop process will keep the Crazyflie in the wanted position.

Here is a short video showing it in action.


We also thought it would be nice to show some more pictures from the Makar Faire Bay Area and our visit to USC!

So we did it again, forgot to post our Monday post on Monday. Are we getting sloppy or maybe it was the 16 hour travel to US that put us off track, well we need to shape up anyway! :) This Tuesday we have been visiting the USC robotics labs, more about that will come on a later post. Thanks Wolfgang for organising our visit and everybody for showing us around.

So now it is all about Maker Faire Bay Area for us. We have been putting in a lot of time to get the demos working and now we are getting really close. The autonomous control with Kinect 2 is starting to work but we must say, tuning the position PID controllers has turned out to be hard but we have some basic hovering working.

We have also been working on some LED-ring and buzzer control from the PC side. That is working really well and it is all setup using 0MQ to the cfclient. We are now able to play MIDI over the Crazyradio to the Crazyflie: MIDI -> 0MQ -> cfclient -> Crazyradio -> Crazyflie -> buzzer & LED-ring.

As soon as  we are back from Maker Faire we will make all the code available (because now it is in the quick and dirty stage)!

So once again, we would love to meet up so please come and visit ut as at our booth at the Maker Faire Bay Area!

Maker Faire Bay Area 2015

Yes, next week we are packing our bags and traveling the ~8500 Km from Malmö, Sweden to visit the Maker Faire Bay Area outside San Francisco and it is going to be great!

Malmö to Bay area We are planning some nice demos including autonomous flight with kinect2 and wireless computer controlled light effects using the LED-Ring so be sure to check us out! cf2 - kinect2 We are really looking forward to this event and we really hope you can pay us a visit if you are attending the Faire!

mf_bayarea_seeusthere

 

As many out there think is is more fun to fly the Crazyflie rather then develop upon it (like we do :-)) we have quickly looked at ways to pilot it with a RC transmitter. This forum thread is a good starting point for a developer discussion. To summarize it a bit there are many ways of implementing it which all require more or less development and has different pros/cons

  • Attach the Crazyradio PA to the expansion module/port of an RC transmitter.
  • Implement the E-Sky transmitter protocol or other nRF24L01+ protocols in the nRF51.
  • Using the DeviationTx code and a nRF24L01+ module.
  • Attaching a RC receiver to the deck (expansion port) interface.

Today I will write a bit more about the DeviationTx as it is a great open source project and that it has support for the Crazyflie. It was over a year ago we got contacted by Victor who wanted to implement the Crazyflie protocol in the DeviationTx code base, which he did pretty quickly. We feel ashamed for taking so long to try it out. So during the weekend I freed up some time and gave it a shot. The DeviationTx project replaces the firmware in Walkera transmitters with a better one which has the possibility to support a great amount of RC models. However many of them use different transceivers modules so this must be added to the hardware. Well a bit of hardware hacking is always fun and we had a nRF24L01+PA module laying around. They have a module installation document but I found it easier follow this guide as I had the same type of module and transmitter (Devo7e).

devo7e nRF24L01 module

The module installation was done pretty quickly but what took time was to update the firmware. The instructions tells one to download the walkera update tool but I just couldn’t find it on their website, nor the original walkera devo7e firmware (which they recommend to test with first). Thankfully I could find one using google and ended up using this link. Next thing was to fire up a windows 7 virtual machine to install it in which it worked without problems. So did the flashing of the DeviationTx firmware, I flashed the nightly build 4.0.1 and copying the file system according to the instructions. What I forgot though was to edit the hardware.ini file to enable the module which I understood when the protocol selection had a star in front of the name <*CFlie> (OK, I admit, I thought the hardware wasn’t working at first…). Then I setup a new model using the <CFlie> and I used the Fixed ID to setup the address, The data rate and channel are combined in the fixed id using channel as lowest two decimal digits and the rate the first were 0, 1, and 2 for 250kbit/s, 1Mbit/s, and 2Mbit/s respectively. So channel 80 on 2Mbit/s is encoded as 280 and channel 80 on 250kbit/s as 80.

And boy I was happy when I saw the green led (radio com led) blink on the Crazyflie 2.0, but nothing happened when I pushed the trust… Then I remembered, we recently implemented a lock so that a zero thrust must be sent at least once for it to unlock (to prevent Crazyflie to fly away if the gamepad is not setup correctly and constantly sends 100% thrust :). Looking into the source code one could see the trust was at minimum 5535 which was never unlocking the thrust. Removing this lock in the Crayflie 2.0 firmware and it was flying! but in plus mode… The Deviation cflie module code seem to rotate the pitch/roll setpoints.

Next step will be to do some modifications to the Deviation cflie module for Crazyflie 2.0, adding code to unlock the thrust and disabling the axis rotation, then make a pull request to the DeviationTx project for everyone to enjoy.

A big thanks to the DeviationTx project and to Victor for the cflie module implementation! Ohh, and by the way, they are making a universialTx module that will support almost any RC model out there, including Crazyflie, can’t wait to see that.

We have finally managed together with Seeedstudio to improve the CCW propellers and to setup a process to acquire a free set of CCW replacement props. As continuing from this post we have discovered that many of the CCW propellers from the first batch of Crazyflie 2.0 where a bit too unbalanced. Too be a bit more technical the tolerance distribution for the CCW propellers was a bit offset, so instead of the majority being decently balanced, many where unbalanced and needed balancing. This is not really a big issue and can pretty easily be corrected by balancing them with some tape according to this guide and we still recommend them to be balanced to get the best flight performance. We however think there where to many unbalanced propellers so we offer every one that bought a Crazyflie 2.0 kit from the first batch a free set of CCW propellers including shipping from China. To acquire a free set of CCW propellers please fill in this form.

Propeller guy

Finally here is the last, and probably most interesting part of the “Measuring propeller RPM” series, see part 1 and part 2 to catch up. Now it is time to gather some data and display what we get.

As described in part 2 we are now able to measure each propeller RPM with the expansion board we made and with the code we added. This can, as been described in part 2, be logged to a csv file using the cfclient. Next thing would be to do something useful with it. To do that we use Octave (very similar to Matlab but open source). The first thing that could be interesting to know is the step response for a  motor and propeller. This was acquired by altering the Crazyflie 2.0 firmware a bit to do a step from zero to max trust and then to zero again. It was done on M1 with a fully charged battery.

M1 step response

 

The motor goes from zero to 25k rpm in about 180ms, quite impressive. What is also interesting is that the falling slope in the range 15k to 20k rpm is quite similar to the rising slope in the same region. The inertia of the turning parts of the motor and the propeller is quite low so this energy will quickly be consumed when the motor and propeller starts to coast. This is good as it will lead to better maneuverability.

 

 

 

cf2 thrust fixtureKnowing the RPM is great but it is the thrust that is most interesting. So to get this we need a rig where we can measure it. We made one out of a bottle to which we glued a prototype board on that we put on a scale, not a perfect rig but simple and probably good enough. Then we made a program that ramped up the PWM from 0% to 93.75% in 16 steps with stabilization disabled. We also connected the Crazyflie 2.0 to a power box where we could measure the current. The program would increase the thrust in 16 steps and during each step we took a reading of the thrust and current. The RPM, voltage and PWM was logged over the cfclient at the same time. Some manual work was needed afterwards to correlate the data and get it into a table. While in the table a lot of interesting plots could be graphed.

rpm and thrustThe first one would be the rpm and thrust as this transfer function is interesting. By using the Octave polyfit function the second order fit could be found. It is a pretty accurate fit and with this fit it is easier to get the thrust, as the rpm is the only variable needed, which is easier to measure. The fitting function returned by polyfit is:

Thrust (in gram) = 1.0942e-07*rpm² – 2.1059e-04*rpm + 0.15417.

 

 

 

Here is a combined plot of some other interesting parameters. Notice that the voltage graph is ~quadratic, the current ~square-root and the power linear. From the power graph one can find out that the thrust efficiency is about 3.8 gram/W. Not super good but pretty good for a quad of the Crazyflie 2.0 size.

thrust_v_a_w_rpm_tight

Another interesting graph is the graph of the flight time. Since we know the power consumption and the battery characteristics we can plot a graph of an estimated flight time depending on the battery size. This graph makes the assumption the battery capacity per weight is 32mAh/g.

flighttime_capacity

And finally the transfer function we set out to find, the PWM to thrust. The problem with this is that motors are controlled by voltage (PWM) and the voltage comes from the battery, which varies with the power taken from the battery due to internal resistance. Therefor we made two fitting functions, one based on measured battery voltage while applying PWM, and one based on the PWM. Looking at the graph one interesting thing is that the polyfit is making a better fit (red curve) for the PWM. I would have expected the opposite. Something that we will have to investigate further. Or maybe one of our readers have the answer?

pwm_to_thrust

On the wiki we will update this analysis with some more details and the raw data so if you are interested please have a look! Hopefully using this information we can make the Crazyflie 2.0 fly even better.

On the news side there is a new version of the Android client available on Google play, thanks Fred! Also Fred has uploaded the slides of his Crazyflie lightning talk at Fosdem 2015.

We finally got some time to follow up on the previous blog post Measuring propeller RPM: Part 1. In this post we will continue where we left of and describe a little about the software we have implemented.

The sensor output, as showed in the scope picture, can be connected directly to a digital input. We connected them to the expansion port signals TX2, RX2, IO2 and IO3 as they are all internally connected to a timer with capture capability, TIM5_CH3, TIM5_CH4, TIM3_CH2, TIM3_CH1 respectively. With the capture functionality it is possible to trigger a timer read on events like a pin change. Thus it is not that difficult to calculate the period time of the input signal which can be used to calculate the rpm. Setting up the timer to run at 1us tick was a bit tricky but manageable. With 1us tick it will wrap around, it’s a 16 bit timer, so after 1us * 65536 = 65.5ms. This means the slowest rpm that can be measured is about 458 rpm (1/(2*65.5ms)*60). The division by 2 is because there are 2 blades on the prop. Since hovering happens way over 458 rpm it doesn’t matter much that we can’t measure any slower speed.

Next thing was to setup the capture compare. The ST library already provides the basic API, TIM_ICInitStructure, so it was more or less just a matter of finding how to configure it. The capture part of the timer can do quite fancy things but in the end we decided it was easiest to just use it the simple way, were it stores the timer value in the corresponding capture compare register on a predefined edge. As the register values will be overwritten when the next pulse comes we setup a interrupt handler to take care of the result. What the interrupt handler ended up to do was to copy the timer value, use that to calculate the rpm and then save the result in a variable. It turned out to be good to do an average of the two latest values as the pulses was not fully symmetrical because of the two blades. This was then done the same way for each propeller input and, voilà, rpm measurements was taken. (Well, there certainly was a bit of hair pulling before it all worked but in the end it worked! Love that feeling)

Getting it down to a cfclient was a piece of cake using the logging framework. We just defined the variables to be logged, setup the logging in the cfclient and it could all be plotted. The variables was defined using the macros below.

LOG_GROUP_START(rpm)
LOG_ADD(LOG_UINT16, m1, &m1rpm)
LOG_ADD(LOG_UINT16, m2, &m2rpm)
LOG_ADD(LOG_UINT16, m3, &m3rpm)
LOG_ADD(LOG_UINT16, m4, &m4rpm)
LOG_GROUP_STOP(rpm)

Then by connecting the the Crazyflie a log configuration could be created called “motor” containing the rpm values as well as voltage and PWM.

log configuration rpm

After that by opening the Log Blocks tab the “motor” log configuration could be enabled a written to files. The data log output folder can be opened from the menu by selecting “Settings -> Open config folder”. In my case in “logdata/20150202T17-29-49/motor-20150202T17-35-09.csv”. This CSV file can then be imported to a program such as Octave, Matlab, Spreadsheet etc for further analysis.

Log write to file

The data can also be plotted in real-time using the plotter as in the picture below. Hmmm, interesting, the Crazyflie 2.0 seems to hover at about 19000 rpm with the RPM measurement board attached.

plotting of rpm

 

The idea was to do the analysis part in this post as well but it turned out to be a little bit to much, therefore stay tuned for the final analyzing part!

We have had numerous request to get a transfer function from the motor PWM output to propeller RPM. The next step would then be to get the propeller RPM to thrust transfer function as well. With that it is easier to do calculations on the system and mathematical models. So this and probably next post will be about how to obtain this function and also give a bit of insight in how one can do development with the Crazyflie 2.0.

First thing how do one do propeller RPM measurement? A quick search on the internet and you will find that using an optical switch is a common method. I also found this guide written for Arduino which was a great start. Since I preferable wanted to measure the RPM while flying the switch needed to be small and lightweight. I found two types that could be useful. A slotted type and a reflective type. The reflective type, QRD1114, is small and promising but would it work? I got some of each type just in case.

Now the optical switch needs interfacing and power. Sparkfun made a good tutorial using the QRD1114 sensor so I will not go into details. Since we use 3V instead I adjusted the resistor for the LED to 82 ohms instead. This will give me ~20mA emitter current. I also played a bit with the sensor output pull-up resistor. If you go with a to strong pull-up, the sensor will need a lot of light to pull it down and if it is to week, it will rise to slow. 12k pull-up seams like a good compromise in my lighting conditions.

Optical switch circuit

 

As a first thing I wired it up on a breadboard using the Crazyflie 2.0 breakout board to get a sense if it would work or not. My finding by measuring the output signal with a multimeter is that it is pretty sensitive to surrounding light but that could be solved by flying in a dim room since it is mainly intended to be used for research.

Now it was time to build a circuit using the prototyping expansion board. I also multiplied the circuit 4 times so I can measure all 4 motors. The inputs I use was TX2, RX2, IO2 and IO3. This because they are all connected to timers so I could use the input capture timer functionality later when I get to the software part. Bending the legs on the QRD1114 was a pretty fiddly job but worth it as it came out so cool in the end. Before I connected it to the Crazyflie 2.0 I measured the current draw and it all seamed OK, ~80mA (4 x 20mA). I also double checked all the connections since it is easy to put a lot of time thinking it is a software fault later if things aren’t working as they should.

As a first test I just turned on one of the motors at a PWM of 10000 and measuerd the sensor output with a scope. The black color of the propeller wasn’t so good so I searched around in the office and found some reflective paint we used a while ago. I painted the backside of the propeller and it made a big difference. In the pictures below you can find the scope picture using a 12k pull-up and some pictures of the painted props.

Next part I will start doing the software and analysis so stay tuned!