0

I am fairly new to embedded c++ outside of Arduino, but so far I have been able to fix all the problems I have run into, except this one. I am using Atmel Studio on an Atmega 1284P, coding in C++. I am using the following variables to compare to TTL Serial input (the serial input is between 0 and 255 transferred in a single unsigned char byte):

const unsigned char STEER_DEADZONE_MIN = 120;
const unsigned char STEER_DEADZONE_MAX = 120;
const unsigned char THROTTLE_DEADZONE_MIN = 136;
const unsigned char THROTTLE_DEADZONE_MAX = 136;

When I try to use STEER_DEADZONE_MIN or any of the listed unsigned chars they come out as 12. I have confirmed that my program sees it as 12 using both the Atmel Studio simulator watch tool and by printing it out to the LCD on the embedded device. I have actually come up with a fix for the unsigned char which is to remove const, but I have const there for a reason, since I don't want the value changed. Declaring it as:

unsigned char TEST = 120;
unsigned char TEST1 = 136;

this causes the value to correctly be 120 or 136, but then the value can be accidentally changed. It also seems that if I do the assignment inside my main loop as:

const unsigned char TEST = 120;

This also fixes the value, but introduces other problems since then none of my functions can access it.

I am also having a seemingly related issue When it comes to a const unsigned int. When I declare it outside the main loop:

const unsigned int SERVO_ESC_SPEED = 200;
const unsigned int SERVO_STEER_SPEED = 200;
const unsigned int SERVO_DISTANCE_SENSOR_SPEED = 200;

The value comes out as 37900, but, I have tried declaring it inside my main loop as:

const unsigned int TEST = 200;

That corrects the value, but as above, does not help since my functions no longer have access to it. In this case, removing const outside the main loop doesn't fix the value. I am really at a loss at this point. I only other thing that I can think of to try at this point is to create a settings class with all these const variables and see if that corrects the values. I will be trying that next and updating with results.

  • 3
    Maybe you have a bug somewhere in your code that is trashing the area where `const` variables are stored; or alternatively perhaps something is wrong with the linker or the program loader and it is not setting the `const` variables area correctly in the first place. Try making a minimal program to test, i.e. one that only contains `main()` (or whatever your entry point is) and one `const` global variable and the absolute minimum amount of code necessary to check that variable's value. – M.M Apr 01 '15 at 04:13
  • Ok, so I was able to confirm the same issue by creating a new Atmel Studio project using the atmega1284P template, then building a completely barebones main.cpp that only had those variables defined and a main() that only printed them directly to the LCD. At this point, I can only assume it's a bug in avr-gcc, atmel studio, or the toolchain/linker template. Anyway, I found a workaround by building a Settings class in a Settings.h file and declaring all those constants there, inside the class. – theDoktorJ Apr 01 '15 at 06:10
  • 1
    I wouldn't trust any further code as long as a workaround for some elementary thing like you described is required. – Rev Apr 01 '15 at 07:03
  • You are right. That was more of a test and a temporary workaround. After being up all night working on it, I finally found a fix as opposed to a workaround. The template that my board manufacturer included was turning on the following compiler optimizations: fdata-sections, and ffunctions-sections. Turning these two optimizations off, solved the problem completely. fdata-sections was causing the problem with the unsigned chars, and ffunctions-sections was causing the problem with the unsigned ints. I have now moved my constants back to where the belong and all is working properly. – theDoktorJ Apr 01 '15 at 10:15
  • You should probably investigate further. See if you can look in the linker map to see where those variables are (normally they're in the .text or .rodata). Your program code is normally in .text . Maybe you could try and check that .rodata (if that is where these constants are) is being loaded; or it may or may not be possible to put the constants into .text instead of .rodata . – M.M Apr 01 '15 at 11:39
  • I've got to be honest, I have no idea where to even start with that. While the problem is fixed for now, by removing the compiler optimizations, I am curious to learn more. Is .text and .rodata a file or part of a file or what? How would I check this? – theDoktorJ Apr 02 '15 at 06:20

1 Answers1

0

What about making them static too?

static const unsigned char STEER_DEADZONE_MIN = 120;
static const unsigned char STEER_DEADZONE_MAX = 120;
static const unsigned char THROTTLE_DEADZONE_MIN = 136;
static const unsigned char THROTTLE_DEADZONE_MAX = 136;
ryancdotnet
  • 2,015
  • 16
  • 33
  • Sorry for the bad formatting, I can't figure out how to mark as code or start a new line in the comments... I tried that, it didn't help, unfortunately. They still show as 12. I was able to make it work as listed below, but no such luck with the ints: `static unsigned char STEER_DEADZONE_MIN = 120; static unsigned char STEER_DEADZONE_MAX = 120; static unsigned char THROTTLE_DEADZONE_MIN = 136; static unsigned char THROTTLE_DEADZONE_MAX = 136;` – theDoktorJ Apr 01 '15 at 04:37
  • Interesting. My suspicion is on the const type, the fact that its global, and that you are compiling for embedded. I found some interesting info here, maybe this will help spawn some thoughts: http://www.embedded.com/electronics-blogs/programming-pointers/4023879/Enumeration-Constants-vs-Constant-Objects Man you are right..comment formatting blows! – ryancdotnet Apr 01 '15 at 04:46
  • Interesting. I found that the workaround of creating an external Settings class with all the variables and calling them as Settings::STEER_DEADZONE_MIN seems to work, but that is an interesting read and I plan to continue trying to work out just why this fails. Thanks for the help. – theDoktorJ Apr 01 '15 at 05:19