6 Replies - 870 Views - Last Post: 17 October 2007 - 04:15 PM Rate Topic: -----

#1 boschow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 07-May 07

Some problems with structure and array . . .

Posted 16 October 2007 - 12:45 PM

Hi all,
i am having some problem with this part of the program and i don't know what is wrong if you could please check it out . . .
int main()
{
	float rezult;
	pid_para_global *pid_global_par;
	pid_para *pid_par;
	cout << "P parameter value? " << endl;
	cin >> pid_par->p_par[0];
	cout << "I parameter value? " << endl;
	cin >> pid_par->i_par[0];
	cout << "Value of deadband " << endl;
	cin >> pid_par->dead_band[0];
	cout << "Setpoint value ?" << endl;
	cin >> pid_par->setpoint[0];
	do {	
	cout << "Actual value ?" << endl;
	cin >> pid_par->actual[0];
	rezult = PID(pid_global_par, pid_par);
	cout << "Calculation rezult is " << rezult << endl << endl;
	}while (pid_par->actual[0] != 0);
}



After i insert the last value the program closes . . . and i don't know why . . . Without making structures and arrays it was working perfectly now it doesn't. Could you please check out what is wrong ?

Thanks for your time and help,
Best regards,
BoSCHoW.

Is This A Good Question/Topic? 0
  • +

Replies To: Some problems with structure and array . . .

#2 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Some problems with structure and array . . .

Posted 16 October 2007 - 12:51 PM

We would need to see your structure definition as well...can you post your entire code as you have tried to run it?

-jjh
Was This Post Helpful? 0
  • +
  • -

#3 boschow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 07-May 07

Re: Some problems with structure and array . . .

Posted 16 October 2007 - 10:52 PM

Yes of course, here is my entire code.
//PID_test.cpp
// PidOut = P (E - Elast) + P * Ts * E / I + PidOutlast;

#include <iostream>

using namespace std;
typedef struct {
	float pid_output; 
	float pid_out_last;
	float error; 
	float error_act; 
	float error_last;
	float temp_p;
	float temp_i;
}pid_para_global;

typedef struct {
	float setpoint[4];
	float actual[4];
	float p_par[4];
	float i_par[4];
	float dead_band[4];
}pid_para;
	
float PID(pid_para_global *pid_glob, pid_para *pid_para)
{  
	if ((pid_para->actual[0] > (pid_para->dead_band[0] + pid_para->setpoint[0]))
	|| (pid_para->actual[0] < (pid_para->setpoint[0] - pid_para->dead_band[0])))
	{
	   pid_glob->error_act = pid_para->setpoint - pid_para->actual;
	   pid_glob->error = pid_glob->error_act - pid_glob->error_last;
	} else { pid_glob->error = 0; }
	// error_last = error;
	pid_glob->temp_p = pid_para->p_par[0] * pid_glob->error;
	pid_glob->temp_i = pid_para->p_par[0] * (pid_glob->error / pid_para->i_par[0]);
	pid_glob->pid_out_last = pid_glob->temp_p + pid_glob->temp_i;
	pid_glob->pid_output = pid_glob->pid_output + pid_glob->pid_out_last;
	if (pid_glob->pid_output < 0) pid_glob->pid_output = 0;
	if (pid_glob->pid_output > 100) pid_glob->pid_output = 100;
	return pid_glob->pid_output;
}

int main()
{
	float rezult;
	pid_para_global *pid_global_par;
	pid_para *pid_par;
	cout << "P parameter value? " << endl;
	cin >> pid_par->p_par[0];
	cout << "I parameter value? " << endl;
	cin >> pid_par->i_par[0];
	cout << "Value of deadband " << endl;
	cin >> pid_par->dead_band[0];
	cout << "Setpoint value ?" << endl;
	cin >> pid_par->setpoint[0];
	do {	
	cout << "Actual value ?" << endl;
	cin >> pid_par->actual[0];
	rezult = PID(pid_global_par, pid_par);
	cout << "Calculation rezult is " << rezult << endl << endl;
	}while (pid_par->actual[0] != 0);
}



Thanks and best regards,
BoSCHoW.
Was This Post Helpful? 0
  • +
  • -

#4 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Some problems with structure and array . . .

Posted 16 October 2007 - 11:25 PM

You've created a pointer, but it's not actually pointing at anything. The call pid_para *pid_par; allocates space for the pointer, but not the actual object. You have the same problem with your pid_para_global object as well.

You need to actually allocate the space for the object itself using the new operator:
pid_para_global *pid_global_par=new pid_para_global;
pid_para *pid_par=new pid_para;


With those changes, the rest of the code should work as it is written - at least, there won't be any further runtime errors. Let us know if you have any further problems.

-jjh
Was This Post Helpful? 0
  • +
  • -

#5 boschow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 07-May 07

Re: Some problems with structure and array . . .

Posted 17 October 2007 - 09:36 AM

Hi again, i have two questions?

1. So this is what i have to do to insert numbers in the structure! Is there any other way or it is just like this ?

2. At the and of this little program i want to check the value of the PID "pid_output" but it always prints out zero. Perhaps you have the solution to this as well.

Thanks and best regards,
BoSCHoW.
Was This Post Helpful? 0
  • +
  • -

#6 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Some problems with structure and array . . .

Posted 17 October 2007 - 12:22 PM

Frick, I just gave you a memory leak. Whoops.

If you use the new statements as I advised, you also need to add the lines:
delete pid_global_par;
delete pid_par;

after the steps where you actually use the structures. This frees the memory that was allocated for the objects before the program exits; if you don't do this, you get a memory leak. Not a huge problem since these are pretty small structures, but I just can't believe that I broke one of my own memory management rules when I posted that code. Any time you use new to allocate memory, you need to use delete to free the memory before the program exits.

There is one other way to do it, but it requries an extra step. You still need to actually allocate the memory for the object, and not just the pointer. To do so, you create an object of each type, and then assign the address of each object to the appropriate pointer:
pid_para_global obj1;
pid_para_global *pid_global_par=&obj1;

pid_para obj2;
pid_para *pid_par=&obj2;


As for the output you're getting, you've got no comments in your code and you didn't describe its intent, so I've got no idea what the expected output should be. That makes it a little hard to say why it's not giving the expected output.

Hope that helps,

-jjh
Was This Post Helpful? 0
  • +
  • -

#7 boschow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 07-May 07

Re: Some problems with structure and array . . .

Posted 17 October 2007 - 04:15 PM

I solved the problem but a new question is running through my mind lets say if i want to have lets say 5 PID regulators all i have to do is add a few arrays to the variables in the struct . . . and this should look something like this ?

typedef  struct {
		int pid_output[4];
		int pid_error[4];
		.
		.
		.
}pid_var; // this are PIDs variables that change during the // calculation

typedef  struct {
		 int p_par[4];
		 int i_par[4];
		 .
		 .
		 .
} pid_para; // this are the parameters set by the user



It is possible to do something like this for easier programing to make a struct that contains lets say 5 arrays
of all the variables parameters and internal variables used for calculation ... the code should look something like this :

typedef struct {
		 .
		 .
		 .
} pid_para;

typedef struct {
		.
		.
		.
}pid_var;

typedef struct {
	  pid_para param;
	  pid_var vary;
}reg;

typedef struct {
	   reg pidReg[4];
}regWith;



So in the function would be possible to use just regWith.pidReg[0].param *pid_param and the next array
regWith.pidReg[0].vary *pid_vary. So if i would use 5 pids i would call the same function with the same structures the array number would varray . . . see the code for more explanations....

float PID(regWith.pidReg[0].param *pid_param, regWith.pidReg[0].vary *pid_vary)
{
.
.
.
}

.
.
.


float PID(regWith.pidReg[4].param *pid_param, regWith.pidReg[4].vary *pid_vary)
{
.
.
.
}

.
.
.



It is possible to do something like this, is any better way to do something like this ?

Thanks for Your help,
Best Regards,
BoSChoW.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1