Commit 8f953a6f authored by Ozmen O's avatar Ozmen O
Browse files

Serial port communications between WebAPILoad and Simulation models are accomplished

parent 55af708c
Loading
Loading
Loading
Loading
+23 −15
Original line number Diff line number Diff line
@@ -18,13 +18,13 @@ class FMUModel:
		FMUModel(const char* shared_lib):TransactiveSystemWholeSystem(shared_lib)
	{
			temp = 0.0;
			request = false;
			request = "";
	}
	double time_event_func(const double* q)
	{
		double tSup = FMI<IO_Type>::time_event_func(q);
		double event = 10000;
		if (request == true){
		if (request != ""){

			event = 0;
		}
@@ -59,11 +59,16 @@ class FMUModel:
	void output_func(const double *q, const bool* state_event,
	   adevs::Bag<IO_Type>& yb)
	{
		if (request == true){
		if (request != ""){

			std::string str;
			std::ostringstream strs;
			strs << get_home1_y(); // get Temperature
			if (request == "HVAC"){
				strs << ((get_home1_y()*9/5)+32); // get home temperature
			}
			if(request == "WH"){
				strs << ((get_wh1_T()*9/5)+32);
			}
			str = strs.str();
			str = str+"\n";
			char* buffer = new char[str.length()];
@@ -72,7 +77,7 @@ class FMUModel:
			IO_Type out(send,c);
			yb.insert(out);
		}
		request = false; // Make request false
		request = ""; // request taken care of
	}
	void gc_output(adevs::Bag<IO_Type>& gb)
	{
@@ -88,11 +93,10 @@ class FMUModel:
		string str; // to hold the message (xb) value in a string
		for (auto x: xb){
			for (unsigned i = 0; i < x.value->size; i++){
				//cout << "x: " << x->buf[i] << endl;
				s=s+x.value->buf[i];
			}
			std::string delimiter = ",";
			std::string mes_sep = "/n";
			std::string mes_sep = "\n";
			size_t pos1 = 0;
			size_t pos2 = 0;
			std::string token1;
@@ -110,18 +114,22 @@ class FMUModel:
					count=count+1;
				}
				if (from == "HVAC"){
					set_hvac1_Setpoint_k(atof(token1.c_str()));
					std::cout << token1 << std::endl;
					if ((token1 != "OutdoorTempF") & (token1 != "UpperTankTemp")){
						double temp = (atof(token1.c_str())-32)*5/9; 	
						set_hvac1_Setpoint_k(temp);
					}
				}
				if (from == "WH"){
					set_wh1_Setpoint1_k(atof(token1.c_str()));
					std::cout << token1 << std::endl;
					if ((token1 != "OutdoorTempF") & (token1 != "UpperTankTemp")){
						double temp = (atof(token1.c_str())-32)*5/9;						
						set_wh1_Setpoint1_k(temp);
					}
				}
				if (token1 == "OutdoorTempF"){
					request = true;
					request = "HVAC";
				}
				else{
					request =  false;
				if (token1 == "UpperTankTemp"){
					request = "WH";
				}
				s.erase(0,pos1 + mes_sep.length());
			}
@@ -131,7 +139,7 @@ class FMUModel:
		// Temperature
		double temp;
		// Temperature request
		bool request;
		std::string request = "";
		// Model input port.
		static const int receive = 0;
		// Model output port.
+55 −30
Original line number Diff line number Diff line
@@ -18,13 +18,13 @@ class FMUModel:
		FMUModel(const char* shared_lib):TransactiveSystemWholeSystem(shared_lib)
	{
			temp = 0.0;
			request = false;
			request = "";
	}
	double time_event_func(const double* q)
	{
		double tSup = FMI<IO_Type>::time_event_func(q);
		double event = 10000;
		if (request == true){
		if (request != ""){

			event = 0;
		}
@@ -41,8 +41,8 @@ class FMUModel:
	{

		FMI<IO_Type>::external_event(q,e,xb);
		// Change relay information
		changeRelays(q,e,xb);
		// Change setpoints
		setPoints(q,e,xb);
		FMI<IO_Type>::time_event_func(q);

	}
@@ -52,18 +52,23 @@ class FMUModel:
		FMI<IO_Type>::confluent_event(q,state_event,xb);
		// Internal
		// External
		// Change relay information
		changeRelays(q,e,xb);
		// Change setpoints
		setPoints(q,e,xb);

	}
	void output_func(const double *q, const bool* state_event,
	   adevs::Bag<IO_Type>& yb)
	{
		if (request == true){
		if (request != ""){

			std::string str;
			std::ostringstream strs;
			strs << get_home1_y(); // get Temperature
			if (request == "HVAC"){
				strs << ((get_home1_y()*9/5)+32); // get home temperature
			}
			if(request == "WH"){
				strs << ((get_wh1_T()*9/5)+32);
			}
			str = strs.str();
			str = str+"\n";
			char* buffer = new char[str.length()];
@@ -72,7 +77,7 @@ class FMUModel:
			IO_Type out(send,c);
			yb.insert(out);
		}
		request = false; // Make request false
		request = ""; // request taken care of
	}
	void gc_output(adevs::Bag<IO_Type>& gb)
	{
@@ -82,32 +87,51 @@ class FMUModel:
			delete (*i).value;
		}
	}
	void changeRelays(double* q, double e,
	void setPoints(double* q, double e,
			const adevs::Bag<IO_Type>& xb)
	{
		string str; // to hold the message (xb) value in a string
		int v = 0;
		for (auto x: xb){
			for (unsigned i = 0; i < x.value->size; i++){
				str=str+x.value->buf[i];
				s=s+x.value->buf[i];
			}
			std::string delimiter = ",";
			std::string mes_sep = "\n";
			size_t pos1 = 0;
			size_t pos2 = 0;
			std::string token1;
			std::string token2;
			while ((pos1 = s.find(mes_sep)) != std::string::npos) {
				token1 = s.substr(0, pos1);
				int count = 0;
				std::string from = "";
				while ((pos2 = token1.find(delimiter)) != std::string::npos) {
					token2 = token1.substr(0, pos2);
					if (count == 1){
						from = token2;
					}
					token1.erase(0,pos2 + delimiter.length());
					count=count+1;
				}
				if (from == "HVAC"){
					if ((token1 != "OutdoorTempF") & (token1 != "UpperTankTemp")){
						double temp = (atof(token1.c_str())-32)*5/9; 	
						set_hvac1_Setpoint_k(temp);
					}
				}
				if (from == "WH"){
					if ((token1 != "OutdoorTempF") & (token1 != "UpperTankTemp")){
						double temp = (atof(token1.c_str())-32)*5/9;						
						set_wh1_Setpoint1_k(temp);
					}
			v = atoi(str.c_str()); // Value of the relay
			if (v == 3){ // If 3 then just send the temperature
				request = true;
				
				}
			else if (v == 1) {
				// Set relays here using FMU functions
				//set_CoolingRelay1(true);
				//set_CoolingRelay2(false);
				if (token1 == "OutdoorTempF"){
					request = "HVAC";
				}
			else if (v == 2){
				//set_CoolingRelay2(true);
				//set_CoolingRelay1(false);
				if (token1 == "UpperTankTemp"){
					request = "WH";
				}
			else if (v == 0){
				//set_CoolingRelay2(false);
				//set_CoolingRelay1(false);
				s.erase(0,pos1 + mes_sep.length());
			}
		}
	}
@@ -115,11 +139,12 @@ class FMUModel:
		// Temperature
		double temp;
		// Temperature request
		bool request;
		std::string request = "";
		// Model input port.
		static const int receive = 0;
		// Model output port.
		static const int send = 1;
		std::string s; //String coming from serial port
};

#endif
+1 −1
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@ class TransactiveSystemWholeSystem:
			adevs::FMI<IO_Type>
			(
				"TransactiveSystemWholeSystem",
				"{f9154443-87da-427f-9612-52c3e0701862}",
				"{7c19bff9-b6e3-4320-a437-e7e441359856}",
				8,
				36,
				shared_lib,
Loading