I have written a code for maximum power point tracking. The purpose was to create DLL file and use it in a circuit base simulation program. I used Microsoft visual c++ 2008 express and created that DLL file but I only get first value from the created array in C. I mean circuit based simulation program only get first value (although I see all array values after executing in "ChSciTE"). Do I need to store data somewhere? I am kind of rookie about writing C code? The code is below and the algorithm is attached as pdf. If anyone help me I would be so happy... I have been playing with this for 3 weeks...

#include <math.h>

    main() {

	
    static double y[21];              //controlled variable
    static double m_A[21];         //modulation index          
    double dm_A=0.05;            //small change for modulation index
    int n;
    double i_cell[]={4.65, 4.68, 4.7, 3.7, 2.1, 2.8,4.68, 4.65, 3.9, 3.7, 2.1, 2.8,4.68, 4.65, 3.9, 4.3, 2.1, 3.3, 4, 3.7,4.6}; 
	
        
        
        for (n=1;n<=19;n++){
        
        m_A[0]=0.75;
        m_A[1]=0.65;
            
        y[n-1]=i_cell[n-1]*m_A[n-1];      //estimated initial controlled variable
        y[n]=i_cell[n]*m_A[n];  

    if (((m_A[n]>=m_A[n-1]) && (y[n]>=y[n-1])) || //algorithm for maximum power point tracking
        (((m_A[n]<m_A[n-1]) && (y[n]<y[n-1]))))	
                   
        m_A[n+1]=m_A[n]+dm_A;    //increase modulation index
                                         
    else     //while one of them is increasing and the other one decreasing
         
        m_A[n+1]=m_A[n]-dm_A; //decrease modulation index  
  
 
        printf("%f \n",m_A[n+1]); //display modulation indices
            
                        }
            }

The code above for seeing results in "ChSciTE" program but the one I pasted below creates that DLL file to use in simulation.

#include <math.h>
__declspec(dllexport) void simuser (t, delt, in, out)
double t, delt;
double *in, *out;
{


	static double y[21];      //controlled variable
	static double m_A[21]; //modulation index
	double i_cell=4.65;	 //estimated initial current value
	int n;
		
		
	for (n=1;n<=19;n++){
		m_A[0]=0.75;	//estimated initial current value
		m_A[1]=0.65;	//second estimated initial current value
	
		y[n-1]=i_cell*m_A[0];	//estimated initial controlled variable
		y[n]=in[0]*m_A[n];	//in[0] current input value  comes from sim.					

	
	if (((m_A[n]>=m_A[n-1]) && (y[n]>=y[n-1])) || 
		(((m_A[n]<m_A[n-1]) && (y[n]<y[n-1]))))	
            
		m_A[n+1]=m_A[n]+in[1];	//in[1] input from circuit based simulation
	
	else	//while one of them increases and the other one decreases
        m_A[n+1]=m_A[n]-in[1];	 //decrease modulation index

	out[0]=m_A[n+1];   // out[0] is output which is transfered to the sim. 
	
						}		
		 	

}

in[0]: first input comes from simulation program
in[1]: second input comes from simulation program
out[0]: output from C code or DLL file goes to simulation or taken by simulation program

Those values taken by simulation should be m_A[n+1]? As far as I understand which is an array. However only the first, I mean m_A[1+1] = m_A[2] has taken by simulation. How could I write the code all values from m_A[2], m_A[3]........m_A[20] can be taken by simulation or DLL sends those values to simulation program one by one for every 20 ms or different interval...

i'm not sure what your question is.

Hi, thanks for taking time to look at my code. I have an array output and I got 20 different numbers. However these numbers are transferred to simulation program via DLL block. This DLL block has been created with the second code that I have written.

When I run simulation program I only see first value. I do not see changing values because of 20 different array values. I thought I only send m_A[n+1] I mean m_A[2]. How could I send all array values from m_A[2].....m_A[20] ?

Regards,
Gurhan

1) Stop coding in ancient K&R original style. That style has not been used for at least 20 years that I know of.

__declspec(dllexport) void simuser (double t,double delt,double* in,double* out)
{

}

2) I don't see in the main() that you posted where simuser() is getting called.

static double y[21];              //controlled variable
    static double m_A[21];         //modulation index

why are those declared static in main() ? That makes no sense. Surly can't be to minimize stack usage because with VC++ 2008 Express and most other 32-bit compilers those arrays won't take up a very noticeable amount of stack space.

1) Stop coding in ancient K&R original style. That style has not been used for at least 20 years that I know of.

__declspec(dllexport) void simuser (double t,double delt,double* in,double* out)
{

}

2) I don't see in the main() that you posted where simuser() is getting called.

Thank you so much for answering me.

1) I do not know how to use other style.

2) This function is the only function in the DLL routine that is mandatory. This function is called by PSIM (power electronics simulation program) at each time step.

Why does the DLL did not get other values of the array??? I do not understand that.

I have changed the code like this below...

#include <math.h>
__declspec(dllexport) 
void RUNSIMUSER( 
double t, 
double delt, 
double *in, 
double *out, 
void **ptrUserData, 
int *pnError, 
char *szErrorMsg)
{
	double y[21];                         //controlled variable
    double m_A[21];	                   //modulation index
	double i_cell=4.65;		   //estimated initial current value
	int n;
		
		
	for (n=1;n<=19;n++){
		m_A[0]=0.75;  //estimated initial current value
		m_A[1]=0.65;  //second estimated initial current value
	
		y[n-1]=i_cell*m_A[0]; //estimated initial controlled variable
		y[n]=in[0]*m_A[n];     //in[0] current input comes from PSIM 					

	
	if (((m_A[n]>=m_A[n-1]) && (y[n]>=y[n-1])) || 
		(((m_A[n]<m_A[n-1]) && (y[n]<y[n-1]))))	
            
		m_A[n+1]=m_A[n]+in[1]; //in[1] second input from PSIM
	
	else		//one increasing the other one dec.
        m_A[n+1]=m_A[n]-in[1];       //decrease modulation index

	out[0]=m_A[n+1];		// out[0] output from "C" to PSIM
				}		 	

}

>>1) I do not know how to use other style.
Yes you do because I just showed you. And you used it correctly in the code you posted!


>>Why does the DLL did not get other values of the array??? I do not understand that.
I don't know either because I need to see what the calling function sent.

>> Why does the DLL did not get other values of the array??? I do not understand that.

You output to the very same memory address each calculation out[[B]0[/B]]=m_A[n+1]; // out[0] output from "C" to PSIM shouldn't you change that subscript to follow the for loop? So it would be out[n - 1]=m_A[n+1];

shouldn't you change that subscript to follow the for loop? So it would be out[n - 1]=m_A[n+1];

Every in[n] correspond to a pin in PSIM. I have two inputs and second input contains arrays. If it is in[2] which will not correspond anything in the DLL block because 3rd pin does not exist.

However out could be what you said. Maybe I should try. Thanks Ancient Dragon and you so far...
I will let you know about the outcome...

I have tried your suggestion

out[n - 1]=m_A[n+1];

but it gives the same result which is 0.70 for all simulation time. The simulation program only accepts out[0] because it has 1 output pin. I would like to export all array values to simulation instead of only first value.

This article has been dead for over six months. Start a new discussion instead.