Go Down

Topic: Timer Conflicts between different Libraries. Document? Solutions? (Read 17206 times) previous topic - next topic

terryking228

Nov 19, 2013, 03:33 pm Last Edit: Nov 19, 2013, 10:14 pm by terryking228 Reason: 1
Hi Everyone,

I am again playing  the Timer Conflict game.  Not fun, provoking some bad words...

Can anyone point to some documentation of the timers used in common Arduino and contributed libraries? I am  thinking about:

  • Servo

  • Tone

  • Ping/NewPing [Ultrasonic ranging]

  • Motor Drivers using pins 9,10

  • Audio/MP3

  • I2C [??Can be a problem??]

  • Timer/Scheduling Libraries

  • Others, I'm sure



My immediate clash is Servo, NewPing and a Motor Driver library I wrote/rewrote. This is for a simple Robot that moves, "looks around" with an Ultrasonic Sensor (SR04) mounted on a servo, and is supposed to Avoid Stuff.

I would really like to document what timers are used by various libraries, millis etc. on the ArduinoInfo.Info WIKI.  Hopefully we could document workarounds and alternate libraries and techniques.

If you have seen a table/list of this stuff I have missed please let us know!  

Other: IsThere any work that has been done to provide a common timer interface that can support multiple libraries? Other more complex RealTime systems use one or two hardware timers with a subsystem that can create several virtual timers that can service multiple tasks.  Is something like this workable for Arduino?

I'd REALLY like to understand this better personally, and document it for others.

Calling Rob Tillaart, Tim Eckel, cr0sh , mem, DC42, (some element?), Nick Gammon, Oddbot, Peter(RobotFreak) and Other Gurus who I didn't retrieve from Old Memory quickly!

Any help and suggestions very much appreciated.

Throwing some beginning stuff in the ArduinoInfo.Info WIKI HERE:  PLEASE request membership and edit. Or just throw stuff at me and I'll work on organizing it.

Some Reading:
The Timer-Interrupt tutorial by RobotFreak (From LetsMakeRobots)  HERE:

Simon Monk's "Arduino Timer" library (helps in applications. Libraries??)  HERE:
Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

nilton61

I had a look at these files:

  • ~Arduino\libraries\Servo\Servo.cpp: This uses Timer1 by default, this can be changed to Timer3 or Timer4 or Timer5

  • ping.cpp: does its timing by pulseIn() defined in wiring_pulse.c This uses clockCyclesToMicroseconds() and no interrupts

  • NewPing.cpp uses TIMER2 or TIMER4

  • I2C uses built in twi harware registers, no interrupt conflict (Wire.cpp & twi.c)



For others you will have to check the library files. There is no way to tell what conventions (if any) a contributor has followed.

Nice idea with a common timer interface, but nothing i know of has been started

terryking228

Quote
I had a look at these files:

Thanks! I will start a table of Libraries VS Timers. 
Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

terryking228

Hi,
Can anyone point to a list of the timers used in the libraries that are supplied with Arduino 1.05, like

- Servo
- Tone

etc??

Thanks!
Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

tylernt

I agree that libraries (as well as the Core functions) need to advertise what timers they are using.  (One of my other pet peeves is libraries that don't say how much flash they use.) I think documenting them as you are is probably best short-term solution.

Long term, we need to get the Arduino guys to adopt something like Simon Monk's timer API right into the Core. That would be awesome in two ways:

1. Fewer libraries would need to control the timers directly, and
2. We could stop teaching newbies to use delay() as much

Now, to be sure, some libraries will still need to take over a timer just because they need more speed/precision, but having a timed event API in the Core will surely help the situation.

As an aside, having a single central repository like Apple's iTunes / Androids App Store for all Arduino libraries would also be awesome. Yes, there's the Playground, but it's not organized really well and a lot of libraries never make it there. Having a link in the IDE to "Get more libraries" which takes you to a web page that has a good searchable/browseable database will make it easy to find libraries. And the database can also tell you what timers each library uses (and how much flash it takes!).


terryking228

Quote
Long term, we need to get the Arduino guys to adopt something like Simon Monk's timer API right into the Core.


I LIKE that idea a lot.  I was spoiled with "real" Real-Timer-Operating systems long ago like EDX that had good timer support.

Maybe Simon can join the discussion; I'll ask him.

I'd be Just Fine with the idea that libraries that need timer functionality would be dependent on a base library. We have many cases like that now, like Wire and 1Wire etc. 

This is one of the worst newbie frustration things. And maybe some of us Not-So-New-At-All  :)  I have tried to help a little with  THIS:  but it does not (yet) address timer and other resource conflicts. 

Any ideas, suggestions very welcome!
Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

tylernt


I have tried to help a little with  THIS: 
Perhaps a bit OT, but I followed that link to your "READ SENSORS, MAKE DECISIONS, TAKE ACTIONS" page. While we're petitioning the Arduino folks to make changes to the IDE anyway, we might as well also ask them to put that concept into an Example sketch that's bundled with the IDE. It's broadly applicable, easy to understand, and segues right into the concept of a Finite State Machine, a very useful programming construct.

nickgammon

I don't know of any documented list of timers (or interrupts for that matter).

Some libraries seem to "take over" all the hardware (SoftwareSerial comes to mind with pin change interrupts) whilst others seem to use a minimal amount (eg. Tone, and Servo) and then "make do" with interrupts. However of course the expense of saving a timer might be that you use an interrupt instead.

I don't know if there is a simple solution, except to check on a case-by-case basis. For example if two libraries only use timer, that could be good, but what if they both use the same one? Or both use the same interrupt to share the timer? Or even if that doesn't happen the interaction between them slows things down too much?

Personally I usually code my own pin-change (or external) interrupts, and go to the hardware for my own timers. I know that might be re-inventing the wheel, but at least I am aware of exactly what resources the sketch uses.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

tylernt

#9
Nov 21, 2013, 09:26 pm Last Edit: Nov 21, 2013, 09:46 pm by tylernt Reason: 1
I don't know if there is a simple solution, except to check on a case-by-case basis.
That's just it -- well, at least for me -- right now, it's really hard to even check. In most cases, you have to pore over the source code of each library, something extremely difficult for anyone who doesn't know how timers work (and tedious for those that do ;) ).

Yes we can work towards resolving conflicts too, but first it would be nice to easily determine when a conflict will even exist. An official repository for libraries with a database to track what libraries use what timer (as well as other resources like flash consumption, and dependencies on other libraries) will go a long way to reducing frustration. As an example, I was fighting with a (nonstandard) RTC library until I realized that the RTC library also required the Time library. Obvious in retrospect, yes, but at the time all the compiler errors were completely unhelpful. If I could have downloaded that RTC library from a central repository, it could have shown me a list of prerequisites next to the download link.

The wiki-style Playground is great in that anybody can contribute -- low barrier to entry. But there's no metadata -- no way to see what contributions run on what hardware with what timers and in how much flash. A database can show all that, and we can still let anyone add an entry to the database to keep the barrier to entry low and encourage contributions. And by contributions, I don't just mean libraries -- sketches and snippets can be entered too (that will thrill PaulS I'm sure :) ).

Actually this is starting to illustrate a larger problem -- the Arduino thing is getting really big these days. I don't want to sound like I know better than the folks who run Arduino -- I don't -- but... now that we have the Due, Tre, and Yun, each with hardware different from each other and VERY different than the AVRs, it becomes exponentially more difficult for users to figure out what code works on what hardware.

The "bazaar" style F/OSS is great, but I had my new TinyServo library half-written before I even discovered the existence of the SoftwareServo library. Oops! It was worse when I wrote my TallFont library, after I was all done it turns out two or three people before me had already done the same thing. It would sure be nice if there was an official, Arduino-endorsed master location I could use to search for "servo" and see right off that SoftwareServo supports ATtiny85, and thus avoid re-inventing the wheel. Yes, there's Google, but we all know what results you get depends on what query you type and how popular the pages are (PageRank). Googling "arduino servo" gets you 2,500,000 hits, 2,499,900 of which are irrelevant with some of the relevant hits on page 347 because they have a low PageRank.

I hope I don't come across as a whiner -- I love Arduino, and will keep using it even if nothing changes. I just think it can be better. And, to put my money where my mouth is, I volunteer my own time and PHP + MySQL skills to create the proposed web database, if the Arduino folks will put their stamp of approval on it.

EDIT: Oh, and don't forget shield stacking compatibility, another FAQ on this forum!

nilton61



I have tried to help a little with  THIS: 
Perhaps a bit OT, but I followed that link to your "READ SENSORS, MAKE DECISIONS, TAKE ACTIONS" page. While we're petitioning the Arduino folks to make changes to the IDE anyway, we might as well also ask them to put that concept into an Example sketch that's bundled with the IDE. It's broadly applicable, easy to understand, and segues right into the concept of a Finite State Machine, a very useful programming construct.


To continiue this OT (apologies for that) But that was one of the main reasons for my state machine library. Please have a look and PM me feedback

MichaelMeissner

#11
Nov 21, 2013, 10:03 pm Last Edit: Nov 21, 2013, 10:07 pm by MichaelMeissner Reason: 1

Quote
I had a look at these files:

Thanks! I will start a table of Libraries VS Timers.  

You probably need a third axis of machine type, i.e. ATmega328 (i.e. Uno), ATmega32u4 (i.e. Leonardo), ATmega2560 (i.e. Mega 2560), ATtiny85 (i.e. Digispark, Trinket, Gemma).  And likely the Arm processors (Due, Teensy 3.0, DigiX, etc.) are completely different.

A related thing is how many pins each machine has, how many analogs, PWM, etc.  For my own use, I created a google spreadsheet listing some of the processor pin assignments, memory, etc. https://docs.google.com/spreadsheet/ccc?key=0AqMAmYxHItUAdDdlcURnY1NRRlFfLWdIbkdWamxfckE#gid=0

<edit>
Another library that some people use is Ken Shirriff's IRremote library, where it uses a specific input pin that is based on the processor and interrupt to handle decoding IR pulses: http://www.righto.com/2009/08/multi-protocol-infrared-remote-library.html

tylernt

To continiue this OT (apologies for that) But that was one of the main reasons for my state machine library. Please have a look and PM me feedback
Yes I've seen your FSM code. My only complaint is that I wasn't clever enough to come up with it first. :) Nice job!

Quote
You probably need a third axis of machine type, i.e. ATmega328 (i.e. Uno), ATmega32u4 (i.e. Leonardo), ATmega2560 (i.e. Mega 2560), ATtiny85 (i.e. Digispark, Trinket, Gemma).

A related thing is how many pins each machine has, how many analogs, PWM, etc.
Sounds like a job for... a database. :D

In other news, I just now found this:

http://playground.arduino.cc/Main/LibraryList

I've been using Arduino for a year and never knew it existed. When I click Sketch -> Import Library... in the IDE, why is there no link to this page?!

Then there's also

http://arduino.cc/en/Reference/Libraries

which duplicates and scatters the information, so I have to look in two places. And, of course, neither has much metadata.

terryking228

Lots of good and important ideas.

First, I would like to offer a place to do whatever we can get going. I write the http://ArduinoInfo.Info WIKI and have plenty of space there. I also have plenty of web space that can support MySQL stuff.  I'd be happy if Arduino.cc ended up taking all or part of it.  But I don't think we can wait for them to set something up..

Nick has a wonderful collection of Arduino and related material he has worked hard on. I have quite a bit.  I think if we integrated some of this They Would Come...

Some specifics:

- Arduino 328, PLUS 2560 and others, YES that is needed. 

- There are nice graphics from Nick and others that show pinouts and functions, and the nice stuff from Alberto Piganti. See the QuickReference pages HERE: I want to add Nick's graphic etc. to those. And Michael's spreadsheet type of tabulated information. 

Quote
that link to your "READ SENSORS, MAKE DECISIONS, TAKE ACTIONS" page. While we're petitioning the Arduino folks to make changes to the IDE anyway, we might as well also ask them to put that concept into an Example sketch that's bundled with the IDE.
I'd love to push FSM concepts.  I need to look at your library better.  Years ago I developed (with a guy from IBM Germany) a visual State Machine builder for factory tool communication.  History.  But I think it could be done for Arduino.  With basically one thread plus interrupts we need some organization.   I have a Home Automation app running on a Mega2560 that uses the Tabs on the IDE for "READ SENSORS, MAKE DECISIONS, TAKE ACTIONS", I/O definitions, Timed taks etc. Just a start but I hope to build it into a good example for people doing stuff like that.

Quote
An official repository for libraries with a database to track what libraries use what timer (as well as other resources like flash consumption, and dependencies on other libraries) will go a long way to reducing frustration.


I'd be happy to help start something UNofficial.  Big Bangs take too long to wait for, at least at MyAge  :)

Quote
Personally I usually code my own pin-change (or external) interrupts, and go to the hardware for my own timers. I know that might be re-inventing the wheel, but at least I am aware of exactly what resources the sketch uses.

IfOnly we were all Nick Gammon!  8)  Wow, I'm trying to Avoid doing that for a simple robot right now.

I'll volunteer to try to paste together whatever can be suggested. And if a Database is better than tables (maybe derived from spreadsheets) let's discuss that.  My own SQL skills are stuck in the WayBack of QBE or something..


Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

tylernt

#14
Nov 21, 2013, 10:50 pm Last Edit: Nov 21, 2013, 10:53 pm by tylernt Reason: 1
Quote
An official repository for libraries with a database to track what libraries use what timer (as well as other resources like flash consumption, and dependencies on other libraries) will go a long way to reducing frustration.


I'd be happy to help start something UNofficial.  Big Bangs take too long to wait for, at least at MyAge  :)
...
And if a Database is better than tables (maybe derived from spreadsheets) let's discuss that.


It just so happens I've done web development professionally, and I've got no problem doing the work if there's a place to host it. My only hesitation would be spending a lot of time doing it outside of Arduino.cc, and then have to spend a lot of time refactoring it move it to Arduino.cc later. But, I suppose what we can do is put it on your Wiki and then on the various applicable Arduino.cc Wiki pages, link to yours so people can find it.

You say your Wiki supports MySQL -- how about PHP? If so, let's get started!

Things the database should track:

Libraries (PWM pins, ADC pins, RAM, Flash, Timers, UART/USART/USI, Interrupts, dependencies on other libraries, ???)
Sketches (ditto)
Platforms (Uno/Leo/Nano/Micro/etc, Mega, ATtiny84/85/2313/861, Due, Tre, Yun, Teensy, ???)
Platform attributes (pinouts, PWM pins, ADC pins, RAM, Flash, Timers, UART/USART/USI, Interrupts, ???)
Shields (pins used, pins passed through, ???)

Any other things we should track?

Go Up