C Array of Structs problem

Status
Not open for further replies.

svdsinner

Ars Legatus Legionis
15,152
Subscriptor
Despite how much my C has improve lately, I don't know what the problem is here. The math all works, and I've debugged that the code is actually being hit.

Short story: In the code below, after I set the value of the struct below, the values all still read 0 :confused:
Code:
struct channelConfig {
	int Min;
	int Max;
	int Zero;
	int Safe;
	float NegativeFactor;
	float PositiveFactor;
	float ZeroTime;
};

static struct channelConfig configuration[PPM_Channels_Used];
uint8_t subframeMillis = 2;

//Times are 1024 * the percentage of the pulse would be high for the value minus 1.  (0-1023)
//Values should be based on actual servo needs.
//Test values current being used below are [0-8],256,512,768,512 
void ConfigurePPMChannel(uint8_t channel, int minTime, int zeroTime,
		int maxTime, int safeValue) {
	if (channel >= PPM_Channels_Used || minTime == 0 || maxTime == 0
			|| zeroTime == 0 || maxTime < zeroTime || zeroTime < minTime
			|| minTime > 1023 || maxTime > 1023 || zeroTime > 1023
			|| safeValue > 1023)
		//Invalid values.  (This isn't hit, which is good)
		return;
	configuration[channel].Min = minTime;
	configuration[channel].Max = maxTime;
	configuration[channel].Zero = zeroTime;
	configuration[channel].Safe = safeValue;
	configuration[channel].NegativeFactor = (.25f *  subframeMillis * (zeroTime -  minTime))
			/(-512.0f);
	configuration[channel].PositiveFactor = (.25f * subframeMillis * (maxTime - zeroTime))
			/ (512.0f);
	configuration[channel].ZeroTime = zeroTime / 512.0f;
	if (configuration[channel].NegativeFactor > -1 || configuration[channel].PositiveFactor < 1)
		Serial.println("Configuration Failure.");  //This is always hit, but shouldn't be.
}
 

Ben_H

Ars Scholae Palatinae
1,059
From Serial.println I'm guessing this is code for Arduino? There's definitely someone who might be able to pinpoint what's going wrong for you but it seems like your code works from a logic perspective, at least for me (other than the calculations for NegativeFactor and PositiveFactor, which I'm not sure about because I don't know the inner workings your program to know what the details of those values are. They seem really low if you actually want them to be >1 and <-1 respectively. I tested other values and they basically always are below the threshold you set). I'm guessing it might be something specific to the Arduino flavour of C acting funny. Or something else completely. There's people far wiser in C than me on here so hopefully someone can figure it out.

I looked over your code and couldn't see anything noticeably wrong so as a sanity check I ran it quickly using plain ol' GCC to make sure it worked and acted as we think. Just gave it a go and here's my results. It prints from the static array of structs and the values are the test values you provided. I just replaced your Serial.println with printf.:

Code:
~ ./a.out
Min: 256
Max: 768
Zero: 512
Safe: 512
NegativeFactor: -0.25000
PositiveFactor: 0.25000
ZeroTime: 0.00000
Configuration Failure.

Here's the code I used. I just basically modified it to print the values of the struct for that particular channel after they have been computed. I also added a couple includes so it would compile for me (stdio.h for printf and stdint.h for uint8_t). It compiled clean without warning (other than for C++ style comments) using the Mac port of GNU GCC 4.9 with the using -Wall -Wextra -pedantic flags.

Code:
#include <stdio.h>
#include <stdint.h>

struct channelConfig {
   int Min;
   int Max;
   int Zero;
   int Safe;
   float NegativeFactor;
   float PositiveFactor;
   float ZeroTime;
};

#define PPM_Channels_Used 2

static struct channelConfig configuration[PPM_Channels_Used];
uint8_t subframeMillis = 2;

//Times are 1024 * the percentage of the pulse would be high for the value minus 1.  (0-1023)
//Values should be based on actual servo needs.
//Test values current being used below are [0-8],256,512,768,512 
void ConfigurePPMChannel(uint8_t channel, int minTime, int zeroTime,
      int maxTime, int safeValue) {
   if (channel >= PPM_Channels_Used || minTime == 0 || maxTime == 0
         || zeroTime == 0 || maxTime < zeroTime || zeroTime < minTime
         || minTime > 1023 || maxTime > 1023 || zeroTime > 1023
         || safeValue > 1023)
      //Invalid values.  (This isn't hit, which is good)
      return;
   configuration[channel].Min = minTime;
   configuration[channel].Max = maxTime;
   configuration[channel].Zero = zeroTime;
   configuration[channel].Safe = safeValue;
   configuration[channel].NegativeFactor = (.25f *  subframeMillis * (zeroTime -  minTime))
         /(-512.0f);
   configuration[channel].PositiveFactor = (.25f * subframeMillis * (maxTime - zeroTime))
         / (512.0f);

   printf("Min: %d\nMax: %d\nZero: %d\nSafe: %d\nNegativeFactor: %0.5f\nPositiveFactor: %0.5f\nZeroTime: %0.5f\n", 
          configuration[channel].Min, configuration[channel].Max, configuration[channel].Zero, configuration[channel].Safe,
          configuration[channel].NegativeFactor, configuration[channel].PositiveFactor, configuration[channel].ZeroTime);

   configuration[channel].ZeroTime = zeroTime / 512.0f;
   if (configuration[channel].NegativeFactor > -1 || configuration[channel].PositiveFactor < 1)
      printf("Configuration Failure.\n");  //This is always hit, but shouldn't be.
}

int main(void) {
   ConfigurePPMChannel(0, 256, 512, 768, 512);

   return 0;
}
 

RAOF

Ars Tribunus Militum
2,733
Code:
   if (configuration[channel].NegativeFactor > -1 || configuration[channel].PositiveFactor < 1)
      Serial.println("Configuration Failure.");  //This is always hit, but shouldn't be.

The comment seems wrong? From your test data, you're calculating NegativeFactor as 0.5 * 256 / -512, which is (modulo float precision) -0.25, which is > -1.
Likewise, it looks like PositiveFactor is 0.25, which is < 1?

How do you know the struct values are all 0 at this point?
 

Ben_H

Ars Scholae Palatinae
1,059
[url=http:/civis/viewtopic.php?p=28884321#p28884321:2tcaf5pi said:
RAOF[/url]":2tcaf5pi]
Code:
   if (configuration[channel].NegativeFactor > -1 || configuration[channel].PositiveFactor < 1)
      Serial.println("Configuration Failure.");  //This is always hit, but shouldn't be.

The comment seems wrong? From your test data, you're calculating NegativeFactor as 0.5 * 256 / -512, which is (modulo float precision) -0.25, which is > -1.
Likewise, it looks like PositiveFactor is 0.25, which is < 1?
This is what I'm starting to think also. Even at the maximum allowed value of 1023/-512 * 0.5 it is above -1 (-0.99...). I'm guessing this is supposed to be strictly a negative and positive check (and thus < 0 and > 0 respectively).

Here's the result of using the minimum allowed Min value, and the other 2 values maxed out within constraints (so Min=1, Zero=1022, Max=1023):

Code:
~ ./a.out
Min: 1
Max: 1023
Zero: 1022
Safe: 512
NegativeFactor: -0.99707
PositiveFactor: 0.00098
ZeroTime: 0.00000
Configuration Failure.

As it stands, with the given constraints of values having to be less than or equal to 1023, the configuration test will not pass ever unless the subframeMillis value is decreased.
 
Status
Not open for further replies.