Arduino Char Strings and RAM

This is a general programing issue, not flying. I was adding some functions to a experimental version of 2.7. Things didn't work so I started adding Serial.print statements at certain points for de-bugging purposes. Ther was a lot of stuff printing on the serial monitor so I started adding long lines of *,#,% ect. to make the lines more visible as they rolled by. Strange things started happening that were logically impossible. The more de-bugging flags I added it got worse and then it just locked up after about 10 sec. I saw that I was using a lot of RAM (from the freeRAM() function), and when it got down to 106 bytes it would just lock up. I didn't understand the quickly increasing RAM usage. Then I did a little testing and quickly found that every char you have  in a Serial.print statement uses a byte of RAM. I had assumed that they would be in program space. I commented out a lot of unnecessary Serial.print statements and gained 400 bytes of RAM. I tried putting the strings in the defines and it didn't have very much affect. It even used a little more. I don't know if any thing can be done about this. I put the freeRAM() in my Ardustation and saw that it was down to 213 bytes out of 2048, and I wanted to ad more stuff. Will have to abbreviate the lcd.print  statements as they seem to work the same way. There must be a way to put char arrays into flash mem but that's way beyond my skills. My only C experience is with the Ardupilot, there's a lot I don't know. Maybe it's not fixable? 
Any one here know?

You need to be a member of diydrones to add comments!

Join diydrones

Email me when people reply –

Replies

  • Developer
    I haven't bothered to add this to 2.7, but in Mega we're using an AVR macro for getting strings out of program memory It's once it's set up.

    Serial.printf_P(PSTR("r: %d\tp: %d\t"), (roll_sensor/100), (pitch_sensor/100));

    The _P means its looking for a pointer to flash memory.

    If you include:
    #include
    #include

    AP_Common.h is part of the Mega Libraries. You should have access to print_f and the PSTR macro. Maybe Michael Smith can correct me if I've missed something.
    Jason
This reply was deleted.

Activity

Neville Rodrigues liked Neville Rodrigues's profile
Jun 30
Santiago Perez liked Santiago Perez's profile
Jun 21
More…