Background
I am programming an AVR MCU with Microchip Studio on a custom circuit board and I do not have any debugging tools, including any form of text output, which is why I have done lots of testing in other C environments, all of which do not have this problem. My main method of testing is a motor that spins based on the output of this program. This segment of code is very different then my full code, as it has been narrowed down to isolate the problem, but the purpose is to create a filter that takes external input, puts it into an array, adds that array together, and then divides the total by the length of the array. So the inputs [180, 180, 180, 180]
would be filtered to 180
, and [0, 50, 100, 150]
would be filtered to 75
. Since 180 is hard coded, I should expect the output to be 180 and for the motor to spin at at a constant speed, however changing one of the lines of code will cause the motor to have significant changes in speed intermittently (about every second).
Problem
I have a set of 4 arrays within the struct inputs
and I want to be able to access them like a 2D array. So inputs[0][0]
would be the first element of inputs.zRotation
and inputs[2][5]
would be the 5th element of inputs.xLinear
. I have narrowed down the problem to how I am accessing the members of inputs
.
The program works fine with this line of code, and the motor runs normally:
inputs.zLinear[filterIndex]=180;
Changing it to this line causes the motor to start changing speed, and therefore the output of the filter has changed:
// I can even hard-code mux to 1, and the effect is the same
inputAddresses[mux][filterIndex]=180;
From this, it is apparent that inputAddresses[1]
!= &inputs.zLinear
, and this does not make sense to me. If I change inputs.zLinear
to inputAddresses[1]
anywhere in the code, it stops working.
Rest of code:
#define filterLength 20
struct Inputs {
uint8_t zRotation[filterLength];
uint8_t zLinear[filterLength];
uint8_t xLinear[filterLength];
uint8_t yLinear[filterLength];
} inputs;
struct InputFilter {
float_t zRotation;
float_t zLinear;
float_t xLinear;
float_t yLinear;
} inputFilter;
uint8_t filterIndex = 0;
// This interrupt is called at 100Hz
ISR(TIMER0_COMPA_vect) {
cli();
uint8_t* inputAddresses[] = {inputs.zRotation, inputs.zLinear, inputs.xLinear, inputs.yLinear};
memset(&inputFilter, 0, sizeof(inputFilter));
for (uint8_t mux = 0; mux < 4; mux++) {
// 180 would be the ADC output but here is is made a constant
inputs.zLinear[filterIndex] = 180;
for (uint8_t i = 0; i < filterLength; i++) {
inputFilter.zLinear += inputs.zLinear[i];
}
inputFilter.zLinear /= filterLength;
}
filterIndex = (filterIndex + 1) % filterLength;
// send output through transmitter
sei();
}
This is very confusing, because as I had stated before I have done lots of testing of basically identical code in different environments, was changing it around accessing the struct members in all kinds of ways without issue. My leading assumption is that I have made a small syntactical error due to my limited knowledge, or that is that there is some discrepancy between compilers or the environment the code is running in.
Thanks for the help.