Jump to content
A.Elgogary

State Space Model yield incorrect result in Systemc AMS 2.0 beta

Recommended Posts

I was using systemc ams 2.3.1 with systemc ams 2.0 beta, i was using State Space model TDF with my controller but i noticed that the result is a function of sampling  time , i mean to let SS yield a correct output, sampling time should be 1s, otherwise the output is divided by the sampling time for example if sampling time is 10ms than the result will be divided by 1000 and so on. i created a small example to show what i got, and i need to understand is this behavior  correct ? and why ? or it is a bug ?

 

if you changed set_timestep(1, SC_MS); and simmulation time to 2 ms you will see the difference, also i compare to matlab state space model and the one with one sec is the correct.
 

Output with 1s Sampling time

Y SS Systemc                   0                    0 
 
Y  Matrix
170
230
Y SS Systemc                 170                  230 
 
Y  Matrix
170
230


Result with 1ms 
 

 
Y SS Systemc                   0                    0 
 
Y  Matrix
170
230
Y SS Systemc                0.17                 0.23 
 
Y  Matrix
170
230
/*
 * State-Space.cpp
 *
 *  Created on: Mar 14, 2016
 *      Author: elgogary
 */

#include "systemc.h"
#include "systemc-ams.h"
#include <Eigen/Dense>
using Eigen::MatrixXd;

SCA_TDF_MODULE( StateSpace ) {

	sca_tdf::sca_out< double>  q;

	void initialize() {



		a(0, 0) = 0;
		a(0, 1) = 0;
		a(1, 0) = 0;
		a(1, 1) = 0;

		b(0, 0) = 5;
		b(0, 1) = 6;
		b(1, 0) = 7;
		b(1, 1) = 8;

		c(0, 0) =  1; c(0, 1) = 0;

		c(1, 0) = 0; c(1, 1) = 1;



		d(0, 0) = 0;
		d(0, 1) = 0;
		d(0, 0) = 0;
		d(0, 1) = 0;


		s(0) = 0;
		s(1) = 0;


	}
	void set_attributes() {

		set_timestep(1, SC_SEC);
	}

	void processing() {

		sca_util::sca_vector<double> x;

		x(0) = 10;
	    x(1) = 20;

		//sca_core::sca_time timestep;

		sca_util::sca_vector<double> y = q_ss(a, b, c, d, s, x );

		cout << "Y SS Systemc" << y <<endl;

		MatrixXd B(2,2);
	    MatrixXd inp(2,1);

				inp(0,0) = 10;
				inp(1,0) = 20;

				B(0, 0) = 5;
				B(0, 1) = 6;
				B(1, 0) = 7;
				B(1, 1) = 8;

				cout << "Y  Matrix" <<endl<< B*inp<<endl;


	}




	SCA_CTOR( StateSpace )
	:  q("q"), a(2,2), b(2,2), c(2,2), d(2,2)

	{
	}

private:

	sca_tdf::sca_ss q_ss; // state-space equation
	sca_util::sca_matrix<double> a, b, c, d; // state-space matrices
	sca_util::sca_vector<double> s; // state vector

};




#include "State-Space.cpp"

SC_MODULE( Crane ) {

	StateSpace * StateSpace0;


	 sca_tdf::sca_signal<double> q;


	SC_CTOR( Crane ) {




		StateSpace0 = new StateSpace("StateSpace0");
		StateSpace0->q(q);



	}

	~Crane() {


		delete StateSpace0;

	}

};

Crane * Crane0 = NULL;

int sc_main(int argc, char* argv[]) {

	Crane0 = new Crane("Crane0");

	sc_start(2, SC_SEC);

	return (0);
}




			
		

Share this post


Link to post
Share on other sites

the result is independent from the timestep as long as the timestep is at least smaller than half of the time constant of the equations.

 

I checked your model - the required timestep seems to be smaller around 1ms

 

with 10ms timestep after 1sec simulation time

Y SS Systemc               338.3                457.7

 

 

after 1 sec simulation I get the result:

339.83               459.77 

 

If I change the timestep to 0.1 ms I get after 1sec simulation the result:

 

Y SS Systemc             339.983              459.977 

 

with 0.01ms after 1sec simulation time:

Y SS Systemc             339.998              459.998

 

 

Hopefully, this answers your question

 

Best regards

Karsten

 

 

 

 

Share this post


Link to post
Share on other sites

i partially understand you but why it is yielding different results than Matlab? my be because the timestep larger than half of the time constant of the equations but is there is a way to edit it ? without changing the controller timestep which is 10ms ?

Share this post


Link to post
Share on other sites

In your State-space function you did not explicitly specify the time step. In such case, the State-space function will take the module time step. It could be that this module time step is too coarse for your analog State-space equation. In that case, you should specify a more fine-grained timestep as argument for the State-space function.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×