HobbyKing.com New Products Flash Sale
Reply
Thread Tools
Old Feb 05, 2011, 12:02 PM
Registered User
Joined Aug 2007
118 Posts
Discussion
BESC firmware development in C

Last year I ported the popular brushless ESC firmware developed in ASM by Bernhard Konze to C and modified the Arduino bootloader to be safely used in a modified TP stile ESC.

Not many people have shown interest in contributing to this effort of making a better open source ESC firmware. But I think we can still benefit from each others knowledge. Even if only 1 or 2 developers out there give some inputs, it is already worth it.

The ported source code and some more details are at the thread: http://www.rcgroups.com/forums/showthread.php?t=1349333

The literal translation of the ASM algorithms to C has a serious constraint in processing capabilities. Auxiliary calculations run in the same thread as the commutation sequence, so any calculation has to be finished between two commutation cycles. That precludes even a simple 32bit integer division (e.g. to get RPM out of commutation time).

That is why I am working in a new version that handle all real-time tasks as ISRs.

When this is done, a lot of doors will open, as there is still more than 1/2 of the program memory of the uProcessor free, and complex non time-critical calculations can be performed at will.
renatopub is offline Find More Posts by renatopub
Reply With Quote
Sign up now
to remove ads between posts
Old Feb 05, 2011, 12:31 PM
I don't want to "Switch Now"
pmackenzie's Avatar
Toronto (Don Mills), Canada
Joined Dec 2002
16,545 Posts
Why do you need to calculate RPM? I assume it is to try to get governor mode?
Couldn't you work directly with the time?
i.e. do the target rpm to time conversion once (or whenever it changes) and then inside the governor work directly with the time.

Pat MacKenzie
pmackenzie is offline Find More Posts by pmackenzie
Reply With Quote
Old Feb 06, 2011, 07:40 PM
Speed = spectacular potential
United States, TX, Austin
Joined Jan 2011
274 Posts
Quote:
Originally Posted by renatopub View Post
Last year I ported the popular brushless ESC firmware developed in ASM by Bernhard Konze to C and modified the Arduino bootloader to be safely used in a modified TP stile ESC. ...
There is certainly a large Arduino community out there working in this kind of thing. Have you posted over at some of the major Arduino forums?

Quote:
That is why I am working in a new version that handle all real-time tasks as ISRs.
Sounds like a good plan. Interrupts are definitely the way to go if you are working in 16 bit PWM.

I am new to this forum, but I am an OpenSource dev for the AVR platform. If any one is interested, I am working on yet another open source ESC module designed to interface using SPI. I will post details about the project, the code, and gerber files, as soon as I get the pcb tested.
blacknoise is offline Find More Posts by blacknoise
Reply With Quote
Old Feb 07, 2011, 06:22 AM
Registered User
Joined Aug 2007
118 Posts
Quote:
Originally Posted by pmackenzie View Post
Why do you need to calculate RPM? I assume it is to try to get governor mode?
Couldn't you work directly with the time? (...)
It is not only for the governor. It is also necessary to limit the max PWM cording to RPM. As this simple ESC design can not measure current, the motor (and/or ESC) will certainly burn if full power is applied at low RPM.

Yes, it can be done with time if no division is needed in the calculation, like, using look up tables. But it is too limiting I think.
renatopub is offline Find More Posts by renatopub
Reply With Quote
Old Feb 07, 2011, 07:10 AM
Registered User
Joined Aug 2007
118 Posts
Quote:
Originally Posted by blacknoise View Post
There is certainly a large Arduino community out there working in this kind of thing. Have you posted over at some of the major Arduino forums?
(...) If any one is interested, I am working on yet another open source ESC module designed to interface using SPI. I will post details about the project, the code, and gerber files, as soon as I get the pcb tested.
The problem I found is there seems to be 2 extremes when it comes to ESC firmware development. There are developers that are 100% DIY, so ESC is designed from scratch, from PCB routing to firmware development. Or very down to business approach, like using a commercial platform (like and existing TowerPro ESC) and making an alternative firmware in ASM that will get the job done.

I started this thread because I think there is a 3rt way to do it that can save development time and cost but still have the flexibility by combining aspects of both sides, and that I never found in the Arduino forums (maybe I missed it?).

The idea is to use as hardware platform the cheap, easy to find and proven TowerPro stile ESCs because:
-You can get one for under 10Eur each on e-bay (ok, add 1h of work soldering a couple of wires as to get the RS232 port of the AVR free).
-HW design is proven to work (one trace in the wrong place, one ground not here but there and EMC will make it a mess out of it. I have seen this even in commercial systems. High current PWM + CPU + analogue lines = not something I like to mess with).
-It is very flexible, as the AVR can easily be reprogrammable.

Unless one of the goal is really to design PCBs and solder SMDs or some more advanced stuff is really needed, like current sensing, or advanced BEMF filters, I would definitely stick to COTS.

But the best firmware I could find for the TowerPro ESC is written in ASM, that is really efficient as a drop-in solution for the users. But ASM is hard to understand and modify, so my idea was to port the firmware to Arduino C.

The idea to use Arduino instead of WinAVR is that new firmwares can be modified and tested in seconds once the Arduino bootloader is burned into the TowerPro ESC by using the same 3 wires that are used for controlling the ESC.

I mean, each community has it's own goals. What I am looking for is to use all of my available time for software development. With the a nice extra that, when it is finished, anyone with a cheap TowerPro ESC will be able to use it.

Well, if there is already a community working on this approach please let me know, as I don't want to waist time re-inventing the wheel .
renatopub is offline Find More Posts by renatopub
Reply With Quote
Old Feb 07, 2011, 09:13 AM
Registered User
Joined Aug 2007
118 Posts
Test rig

Just posting some pictures of the test rig I am using.

As both, commands and firmware update are done by the same 3 wires (standard servo connector), I used a spare Arduino Duemilanove without the AVR as USB->5V_rs232 converter, just because that was what I had at hand...

I also made some useful adapters out of Dean connectors (unbelievable how these are prone to off-label use ):
- 0.01OHM shunt for current measurement.
- Car fuse support.
- Car headlight lamp support (thanks for the tip Eduardo (agressiva)!)

I use either the fuse or lamp depending on the kind of test. In a low current test (well, "supposed" to be low-current at least), the lamp is better as it offers extra visual information and limits the current to a couple of amps thus saving the FETs. For full power tests the lamp will drop the voltage too much, hence the fuse.

The digital kitchen scale is to measure thrust. But as I have RPM information directly from the ESC and knowing that for the same Reynolds Number range thrust goes with the square of RPM, I need to use it only once for a given propeller. Thrust measurement is not really necessary for firmware development, but I just felt like doing it...
renatopub is offline Find More Posts by renatopub
Reply With Quote
Old Feb 07, 2011, 07:49 PM
Speed = spectacular potential
United States, TX, Austin
Joined Jan 2011
274 Posts
Quote:
Originally Posted by renatopub View Post
The problem I found is there seems to be 2 extremes when it comes to ESC firmware development. There are developers that are 100% DIY, so ESC is designed from scratch, from PCB routing to firmware development. Or very down to business approach, like using a commercial platform (like and existing TowerPro ESC) and making an alternative firmware in ASM that will get the job done.

I started this thread because I think there is a 3rt way to do it that can save development time and cost but still have the flexibility by combining aspects of both sides, and that I never found in the Arduino forums (maybe I missed it?).

The idea is to use as hardware platform the cheap, easy to find and proven TowerPro stile ESCs because:
-You can get one for under 10Eur each on e-bay (ok, add 1h of work soldering a couple of wires as to get the RS232 port of the AVR free).
-HW design is proven to work (one trace in the wrong place, one ground not here but there and EMC will make it a mess out of it. I have seen this even in commercial systems. High current PWM + CPU + analogue lines = not something I like to mess with).
-It is very flexible, as the AVR can easily be reprogrammable.

Unless one of the goal is really to design PCBs and solder SMDs or some more advanced stuff is really needed, like current sensing, or advanced BEMF filters, I would definitely stick to COTS.

But the best firmware I could find for the TowerPro ESC is written in ASM, that is really efficient as a drop-in solution for the users. But ASM is hard to understand and modify, so my idea was to port the firmware to Arduino C.

The idea to use Arduino instead of WinAVR is that new firmwares can be modified and tested in seconds once the Arduino bootloader is burned into the TowerPro ESC by using the same 3 wires that are used for controlling the ESC.

I mean, each community has it's own goals. What I am looking for is to use all of my available time for software development. With the a nice extra that, when it is finished, anyone with a cheap TowerPro ESC will be able to use it.

Well, if there is already a community working on this approach please let me know, as I don't want to waist time re-inventing the wheel .
I completely agree that is is better to have a community developed product born of iterations of trial and error resulting in a better product. if you can get momentum behind that approach, that is the way to go. I have read a lot about people useing Arduino for DIY Drones, Quadcopters, etc. Perhaps you can hook up with some one sharing those interest and hopefully get something rolling? In the mean time, if you need specific help with low-level libraries of any kind, let me know and I might be up to pitching in.
blacknoise is offline Find More Posts by blacknoise
Reply With Quote
Old Feb 07, 2011, 08:06 PM
Speed = spectacular potential
United States, TX, Austin
Joined Jan 2011
274 Posts
Quote:
Originally Posted by renatopub View Post
Just posting some pictures of the test rig I am using.

As both, commands and firmware update are done by the same 3 wires (standard servo connector), I used a spare Arduino Duemilanove without the AVR as USB->5V_rs232 converter, just because that was what I had at hand...

I also made some useful adapters out of Dean connectors (unbelievable how these are prone to off-label use ):
- 0.01OHM shunt for current measurement.
- Car fuse support.
- Car headlight lamp support (thanks for the tip Eduardo (agressiva)!)

I use either the fuse or lamp depending on the kind of test. In a low current test (well, "supposed" to be low-current at least), the lamp is better as it offers extra visual information and limits the current to a couple of amps thus saving the FETs. For full power tests the lamp will drop the voltage too much, hence the fuse.

The digital kitchen scale is to measure thrust. But as I have RPM information directly from the ESC and knowing that for the same Reynolds Number range thrust goes with the square of RPM, I need to use it only once for a given propeller. Thrust measurement is not really necessary for firmware development, but I just felt like doing it...
Very nice test setup! What type of esc pcb is that? What is your goal of this testing? Are you trying to simulate 50hz pwm signals via the standard servo interface to control speed?
blacknoise is offline Find More Posts by blacknoise
Reply With Quote
Old Feb 08, 2011, 03:12 AM
Registered User
quax's Avatar
NRW, Germany
Joined Apr 2004
711 Posts
measurement equipment

I have also build my test equipment. My aim was to measure not only the thrust but also the torque of the motor to get the efficiency of the motor as well as of the propeller. For the axis I used an old harddrive. The two kitchen scales have a digital output. The RPM I will get from the BLC. So, in the end, I will be able to run complete measurements automatically.

In the moment I can run them only by hand, but with thrust and torque.

cu
quax
quax is offline Find More Posts by quax
Reply With Quote
Old Feb 08, 2011, 05:16 AM
Registered User
Prague, Czech_Republic
Joined Aug 2010
90 Posts
A newbie in your forum

Thanks to quax to directing me to this forum and his original firmware, also to Renato for all the work he's done and he's going to do!!!
I started loving a multicopters (I'm flying a tricopter and now I built also quad).
Currently I'm using KK multicontroller and PPM ESC's for flying, Arduino and I2C is planned when my knowledge allows it. With C I unfortunately ended at "Hello world" so it will need a lot of time to get to state when I could be helpful for the project.
I'd like to adapt your C firmware for ESC's I have - for now with the smallest possible work because I want to fly asap and not after a year
My ESC's have Atmega48 at 8MHZ internal oscillator and feedback signals at PORT C 2,3,4 pins.
Honestly I currently don't understand very much of the code, can you give me few tips?
1) Do you think I have a chance to modify it for 8 MHz and Atmega 48? (Because of performance and my limited knowledge...) Where can I enter processor frequency and the processor type in the code?
2) Can be feedback pins defined on another pins (special functions on pins)?
3) Can be firmware burned directly into the chip (Atmega48 doesn't have boot section). Will it work then?
Sorry for such "stupid" questions, in Arduino I'm really beginner, what I need primary is to modify ESC's I already bought and want to use, developement should come later...
Thanks for any answer.
RBirdie001 is offline Find More Posts by RBirdie001
Reply With Quote
Old Feb 08, 2011, 10:38 AM
Registered User
Prague, Czech_Republic
Joined Aug 2010
90 Posts
ESC using ATmega48

Addition to my previous post:
I tested compiling your code to see how it works (my first compiling under Arduino enviroment) and I found that Arduino offers me only known Arduino boards and obviously none with ATmega48 exists. Except this I found that code compiled for ATmega8 have 4.6kB so it probably won't fit to 4kB flash of ATmega48. Do you guess there can be for my purpose removed enough unnecessary parts of code to get under 4kB?
Thanks!
RBirdie001 is offline Find More Posts by RBirdie001
Reply With Quote
Old Feb 08, 2011, 01:04 PM
Registered User
Joined Aug 2007
118 Posts
Quote:
Originally Posted by RBirdie001 View Post
Thanks to quax to directing me to this forum and his original firmware, also to Renato for all the work he's done and he's going to do!!!
I started loving a multicopters (I'm flying a tricopter and now I built also quad).
Currently I'm using KK multicontroller and PPM ESC's for flying, Arduino and I2C is planned when my knowledge allows it. With C I unfortunately ended at "Hello world" so it will need a lot of time to get to state when I could be helpful for the project.
I'd like to adapt your C firmware for ESC's I have - for now with the smallest possible work because I want to fly asap and not after a year
My ESC's have Atmega48 at 8MHZ internal oscillator
I think there are no Arduinos using atmega48.There is some info at http://www.thinkcreate.org/index.php...g-to-atmega48/, but I have no clue if it will work. This C code will probably work in your processor, but with some extra work... anyway, this project is definitely not in the "Fly ASAP!" category yet, but maybe one day I hope .
renatopub is offline Find More Posts by renatopub
Reply With Quote
Old Feb 08, 2011, 01:32 PM
Registered User
Joined Aug 2007
118 Posts
Quote:
Originally Posted by blacknoise View Post
Very nice test setup! What type of esc pcb is that? What is your goal of this testing? Are you trying to simulate 50hz pwm signals via the standard servo interface to control speed?
That is a typical TowerPro 20A ESC. The input is 5V RS232 commands.
renatopub is offline Find More Posts by renatopub
Reply With Quote
Old Feb 08, 2011, 01:47 PM
Registered User
Joined Aug 2007
118 Posts
Quote:
Originally Posted by quax View Post
I have also build my test equipment. My aim was to measure not only the thrust but also the torque of the motor to get the efficiency of the motor as well as of the propeller. (...)
Nice idea the torque measurement. My priorities have been more like avoiding FETs from burning so far .

Is your goal to profile propellers as well? Or is it just like some extra data after testing a motor/ESC?

Do you notice any remarkable difference in efficiency between motors? From my (purely theoretical) point of view, the only factors would be magnet field strength and copper conductivity... I am a bit skeptical about all those expensive motors. But do you notice something with your measurements?
renatopub is offline Find More Posts by renatopub
Reply With Quote
Old Feb 08, 2011, 02:53 PM
Registered User
Prague, Czech_Republic
Joined Aug 2010
90 Posts
ESC using ATmega48

Quote:
Originally Posted by renatopub View Post
I think there are no Arduinos using atmega48.There is some info at http://www.thinkcreate.org/index.php...g-to-atmega48/, but I have no clue if it will work. This C code will probably work in your processor, but with some extra work... anyway, this project is definitely not in the "Fly ASAP!" category yet, but maybe one day I hope .
Renato, thanks, this link helped me again understand a bit more about Arduino. I'm now collecting fragments of knowledge...It will need more time to understand more.
What I'll definitly need to know is if the feedback signals can be wired to any pin of PORT C and what parts of code can I easily remove to save some flash size and get under 4kB... (When I don't need governor, serial, etc...)
Can you advise me some tips?
Thanks even for that!
RBirdie001 is offline Find More Posts by RBirdie001
Reply With Quote
Reply


Thread Tools

Similar Threads
Category Thread Thread Starter Forum Replies Last Post
Discussion New motor in development from the guys that brought you Gens Ace batteries! RC Dbutz Vendor Talk 4 May 23, 2011 01:17 PM
Question General Use Lubricant - In Development Q&A gca52 Car Talk 1 Nov 06, 2010 04:27 PM
Discussion Problems with Triton 2 EQ A/C D/C in our club is it happening with yours??? roberted5 Batteries and Chargers 5 Sep 14, 2010 12:32 PM
Discussion 5.8Giga R/C system under development? Dimitris76 FPV Talk 0 Jun 29, 2010 04:38 PM