Code that includes new library will upload but not run

I am currently working on a quadrocopter library for the arduino, but I have discovered a peculiar issue. I redid all of the sensor classes this evening, and then uploaded the code sucessfully. The Init() method of the Quadrotor class is supposed to print debug statements to the serial monitor, but nothing is being sent. I discovered that even printing something BEFORE calling the Init() method doesn’t work. Simply declaring an instance of the class is enough to cause this problem. Here is my sketch:

#include <Quadrotor.h>
#include <Wire.h>
#include <Servo.h>

Quadrotor quad;

void setup() {
//  quad.Init(1500, 750, 750, 5000);
Serial.begin(115200);
Serial.println("Hello");
}

void loop() {
}

Even though the Init() method isn’t called in the above example, the ‘Hello’ statement is never sent.

My library code can be found here: Raspbercopter/Arduino at master · EricMiddleton/Raspbercopter · GitHub.

Thanks,
Eric

EricMiddleton: The Init() method of the Quadrotor class is supposed to print debug statements to the serial monitor, but nothing is being sent.

Before you do Serial.begin() you mean?

I run Serial.begin(115200) inside the Quadrotor.Init() method, as you can see in the link.

I run Serial.begin(115200) inside the Quadrotor.Init() method, as you can see in the link.

You mean in the commented out call to Quadrotor::Init()?

Just looking at the list of include files, the first thing that comes to mind is that you have run out of memory. What Arduino are you using?

EricMiddleton: I run Serial.begin(115200) inside the Quadrotor.Init() method, as you can see in the link.

In the constructor? Not a good idea, sorry.

The constructor is called before the Arduino init() function is called.

I call Serial.begin(115200) inside the Init method, not the constructor. I am using an Arduino 2560.

In the Serial Monitor, did you make sure to set the baud rate to the 115200?

Your library with that code doesn't even compile for me. The first error is the backslashes in the paths ("#include "utility\whatever.h")

Does it work if you include the library but don't create an instance of the class?

I suggest you change the backslashes to forward slashes. Then it will compile on Mac/Linux:

#include "utility/Datatypes.h"
#include "utility/Model.h"
#include "utility/Locator.h"
#include "utility/Communicator.h"
#include "utility/Battery.h"
#include "utility/Altimeter.h"

I confirm your results. Once I did that it compiled, and did nothing. So adding debugging about how much memory is free won't be much help (it probably won't run).

I don't know what the problem is, exactly, but I can narrow it down a bit.

The problem occurs by including an instance of the class Quadrotor. As you say, Quadrotor does not have a (non-implicit) constructor, however merely including it will cause all the constructors of the classes inside Quadrotor to be called. And you have quite a few of them. Vector, Angle, Model, Locator.

All I can suggest is stripping out stuff (which in itself will probably cause compile errors so it may be a long job) to try to narrow down what the issue is.

I can't see anything obviously wrong. No huge arrays I have spotted, nor massive literal strings. However the guess that you are out of memory could well be right.

Alternatively, and quite possibly, the "static initialization order fiasco". It could be that class constructors are simply being called in the wrong order, and one class is expecting another one to be initialized, when it isn't.

[quote author=Nick Gammon link=topic=115485.msg869946#msg869946 date=1343080650] I suggest you change the backslashes to forward slashes. Then it will compile on Mac/Linux:

#include "utility/Datatypes.h"
#include "utility/Model.h"
#include "utility/Locator.h"
#include "utility/Communicator.h"
#include "utility/Battery.h"
#include "utility/Altimeter.h"

I confirm your results. Once I did that it compiled, and did nothing. So adding debugging about how much memory is free won't be much help (it probably won't run).

I don't know what the problem is, exactly, but I can narrow it down a bit.

The problem occurs by including an instance of the class Quadrotor. As you say, Quadrotor does not have a (non-implicit) constructor, however merely including it will cause all the constructors of the classes inside Quadrotor to be called. And you have quite a few of them. Vector, Angle, Model, Locator.

All I can suggest is stripping out stuff (which in itself will probably cause compile errors so it may be a long job) to try to narrow down what the issue is.

I can't see anything obviously wrong. No huge arrays I have spotted, nor massive literal strings. However the guess that you are out of memory could well be right.

Alternatively, and quite possibly, the "static initialization order fiasco". It could be that class constructors are simply being called in the wrong order, and one class is expecting another one to be initialized, when it isn't. [/quote]

I've been working on this all afternoon, and I have gotten it to start, but it does stop after a few seconds. I rewrote all the classes that used constructors to initialize everything and moved the code into explicit Init() methods. That enabled the code to actually start, but now it stops either while initializing the quad, or right after (when the interrupts are supposed to take over). I also took care of the slash issue, and will put the new code on the github. I don't have a lot of experience with the arduino specifically, but it sure seems like a memory issue to me now, but try as I might, I cannot seem to fix it. I have removed a lot of code that I don't use anymore and slimmed down all the variables I could.

Thank you all for the help so far, it's really appreciated.

I can get the interrupt code to run (somewhat) now, but I've stumbled upon another odd problem. The USB Serial port keeps sending periods (with no line breaks) at a very fast rate. I'm sending status messages in the interrupt that is supposed to fire about 20 times per second, but it's now about once per second because the periods keep slowing it down. I've checked all my code files multiple times and I'm certain that I'm not sending the periods.

I'm sending status messages in the interrupt that is supposed to fire about 20 times per second

I hope that you are aware that sending serial data in an interrupt handler is not recommended. Sending serial data relies on interrupts, which are disabled while your ISR is running.

PaulS:

I’m sending status messages in the interrupt that is supposed to fire about 20 times per second

I hope that you are aware that sending serial data in an interrupt handler is not recommended. Sending serial data relies on interrupts, which are disabled while your ISR is running.

ISR(TIMER3_OVF_vect) {
	TIMSK3 = 0<<TOIE3;
	interrupts();
//it continues
}

The interrupt handler disables that timer’s interrupt and enables interrupts globally so that shouldn’t be an issue.

	TIMSK3 = 0<<TOIE3;

Wouldn’t this be simpler? …

	TIMSK3 = 0;

It makes it clearer you are setting all bits to zero, not just the one you mention.

I’m not wildly enthusiastic about an ISR which turns on interrupts, does serial printing, and does lots of other stuff. Especially when it runs at 10 Hz. Why not just check millis() in loop, and do your periodical stuff there?

//Let's update our quadrocopter
//Independently of whatever the user
//Puts in their loop(){} function!

OK, you are trying to make it independent. But what if, in loop(), the program is printing to the serial port? Then your prints come in the middle of that. Or updating a variable that your interrupt routine is updating? I think you are solving one problem and creating others.

These are definitely valid concerns that I have been considering. The final version won’t do any serial printing; it’s all used for debugging at this point. Also, I am using methods like quad.GetVariable() instead of quad.variable to grab things, so that should help the other problem. The only things that the user will have write access to (in the final version) is the GPS waypoint list and toggling the quad on/off, which will be implemented through methods like quad.AddWaypoint(GpsWaypoint gps), quad.Activate(), quad.Deactivate().