combining different types into single data structure

G

girays

I want to make a good design about combining information. Suppose you
have different data types, and each may have semantically same data. I
want to merge these different data types into a single data at run-
time. For example, sonar sensor has two internal data (bearing and
bearing_rate), and Radar has two as well (range, bearing.

struct sonar_data {
float bearing;
float bearing_rate;
};

struct radar_data {
float bearing;
float range;
};

sonar_data sd;
sd.bearing = 5.0f;
sd.bearing_rate = 0.1f;

radar_data rd;
rd.bearing = 3.0f;
rd.range = 12.0f;

I want to combine these two data into a single data structure:

data.bearing = 4.0f // average of sd(5.0f) and rd(3.0f)
data.bearing_rate = 0.1f; // only comes from sonar_data
data.range = 12.0f; // only comes from radar_data


I don’t know how to make a good design about this problem. Do you have
any template based (or other) suggestions?

Thanks in advance...

---Sgo---
 
U

utab

I want to make a good design about combining information. Suppose you
have different data types, and each may have semantically same data. I
want to merge these different data types into a single data at run-
time. For example, sonar sensor has two internal data (bearing and
bearing_rate), and Radar has two as well (range, bearing.

struct sonar_data {
float bearing;
float bearing_rate;
};

struct radar_data {
float bearing;
float range;
};

+ My humble idea: why not construct only one "data" structure to hold
sonar and radar information.

struct data
{
double sonar_bearing;
double sonar_bearing_rate;
double radar_bearing;
double radar_range;
};

+ Why are you using floats?
 
J

James Kanze

I want to make a good design about combining information.
Suppose you have different data types, and each may have
semantically same data. I want to merge these different data
types into a single data at run- time. For example, sonar
sensor has two internal data (bearing and bearing_rate), and
Radar has two as well (range, bearing.
struct sonar_data {
float bearing;
float bearing_rate;
};
struct radar_data {
float bearing;
float range;
};
sonar_data sd;
sd.bearing = 5.0f;
sd.bearing_rate = 0.1f;
radar_data rd;
rd.bearing = 3.0f;
rd.range = 12.0f;
I want to combine these two data into a single data structure:
data.bearing = 4.0f // average of sd(5.0f) and rd(3.0f)
data.bearing_rate = 0.1f; // only comes from sonar_data
data.range = 12.0f; // only comes from radar_data
I don?t know how to make a good design about this problem. Do
you have any template based (or other) suggestions?

What's wrong with just:

struct Data
{
sonar_data sonar ;
radar_data radar ;
} ;

?

If you want to enforce the fact that both have the same bearing,
then give the class a constructor.
 
P

Pascal J. Bourguignon

James Kanze said:
What's wrong with just:

struct Data
{
sonar_data sonar ;
radar_data radar ;
} ;

?

If you want to enforce the fact that both have the same bearing,
then give the class a constructor.

class Detector {
public:
float bearing;
float range;
};

class Radar:public Detector{
public:
float frequency;
};

class Sonar:public Detector{
public:
float rate;
};


Perhaps you need to read some tutorial about Object Oriented Programming?
 
H

hurcan solter

+ My humble idea: why not construct only one "data" structure to hold
sonar and radar information.

struct data
{
double sonar_bearing;
double sonar_bearing_rate;
double radar_bearing;
double radar_range;
};

+ Why are you using floats?
because precision of the float gives enough resolution the represent
all possible bearing values and radar coverage range (I know because
i work on the same project :) ). It also consumes half the space on
the wire (That was the main reason)
Since we lack reflection on C++ and cant dynamically create
classes on the fly, a straightforward implementation would be;

struct target_data {
float bearing;
float range ;
float bearing_rate;
};

struct combinator {

target_data
operator() (const sonar_data& snr , const radar_data& rdr)
{
target_data target;
target.bearing = algo_bearing_calc(snr.bearing,
rdr.bearing);
target.range = rdr.range;
target.bearing_rate =snr.bearing_rate;
}
};

and somewhere in your code;

std::transform (sonarvec.begin(), sonarvec.end(), radarvec.begin(),
back_inserter(targetvec), combinator());
 
G

girays

class Detector {
public:
   float bearing;
   float range;

};

class Radar:public Detector{
public:
   float frequency;

};

class Sonar:public Detector{
public:
   float rate;

};

Perhaps you need to read some tutorial about Object Oriented Programming?

It's not an object oriented question, sorry but I trust my oo
knowledge. I'm pursuing a run-time solution for this.

simple idea: each struct has attribute map, at run time you can
iterate them, same attribute's can be merged (averaged)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top