Browse Source

innacurate zero crossing in controller_sa

Cláudio Gomes 7 years ago
parent
commit
e6183ed6b0

+ 13 - 22
SemanticAdaptationForFMI/Experiments/power_window_case_study/fmus/FMI_controller_sa/FMI_GM.c

@@ -350,18 +350,18 @@ fmi2Status fmi2DoStep(fmi2Component fc , fmi2Real currentCommPoint, fmi2Real com
 	fi->aux_obj_detected = 0;
 	fi->step_size = commStepSize;
 
+	fmi2Boolean crossedTooMuch = (
+									(!is_close(fi->previous_arm_current, fi->CROSSING , fi->REL_TOLERANCE, fi->ABS_TOLERANCE))
+									&& fi->previous_arm_current < fi->CROSSING
+								) && (
+									(!is_close(fi->stored_arm_current,fi->CROSSING,fi->REL_TOLERANCE,fi->ABS_TOLERANCE))
+									&& fi->stored_arm_current > fi->CROSSING
+								);
+
 	if (fi->toleranceDefined){
 		printf("%s: Accurate checking for threshold crossing...\n",fi->instanceName);
-		if( (
-					(!is_close(fi->previous_arm_current, fi->CROSSING , fi->REL_TOLERANCE, fi->ABS_TOLERANCE))
-					&& fi->previous_arm_current < fi->CROSSING
-				)
-				&&
-				(
-					(!is_close(fi->stored_arm_current,fi->CROSSING,fi->REL_TOLERANCE,fi->ABS_TOLERANCE))
-					&& fi->stored_arm_current > fi->CROSSING
-				)
-			   ){
+
+		if(crossedTooMuch){
 				double negative_value = fi->previous_arm_current - fi->CROSSING;
 				double positive_value = fi->stored_arm_current - fi->CROSSING;
 				fi->step_size = (commStepSize * (-negative_value)) / (positive_value - negative_value);
@@ -384,23 +384,14 @@ fmi2Status fmi2DoStep(fmi2Component fc , fmi2Real currentCommPoint, fmi2Real com
 			}
 	} else {
 		printf("%s: Non-accurate checking for threshold crossing...\n",fi->instanceName);
-		if( (
-				(!is_close(fi->previous_arm_current, fi->CROSSING , fi->REL_TOLERANCE, fi->ABS_TOLERANCE))
-				&& fi->previous_arm_current < fi->CROSSING
-			)
-			&&
-			(
-				(!is_close(fi->stored_arm_current,fi->CROSSING,fi->REL_TOLERANCE,fi->ABS_TOLERANCE))
-				&& fi->stored_arm_current > fi->CROSSING
-			)
-		   ){
+		if(crossedTooMuch){
 			fi->aux_obj_detected = 1;
-			printf("%s: crossed just right... \n",fi->instanceName);
+			printf("%s: crossed too much but since this is non accurate, it is fine... \n",fi->instanceName);
 		}
 	}
 
 	if (externalSimStatus == fmi2OK){ // only do the internal step if the current step is OK
-		if (((fi->next_time_step != -1 && currentCommPoint > fi->next_time_step ) || 1)){
+		if (((fi->next_time_step != -1 && currentCommPoint > fi->next_time_step ) || 1)){ // TODO: check for changes in the
 			fmi2ValueReference vr_in_star[8] = {1,2,3,4,5,6,7,8};
 			fi->fmu[0].setBoolean(fi->c_fmu[0], vr_in_star,8, &(fi->b[0]));
 			fmi2ValueReference vr_aux_obj_detected[1] = {0};

+ 1 - 1
SemanticAdaptationForFMI/Experiments/power_window_case_study/fmus/FMI_controller_sa/test.c

@@ -47,7 +47,7 @@ int main(void) {
 
     c_control_sa = fmu_control_sa.instantiate("control_sa", fmi2CoSimulation, "1", fmuResourceLocation_control_sa, &callbacks_control_sa, fmi2False, fmi2False);
 
-    fmi2Boolean toleranceDefined = fmi2True;  // Enables zero crossing location
+    fmi2Boolean toleranceDefined = fmi2True;  // Enables accurate zero crossing location. Set fmi2False to enable innaccurate crossing
     fmi2Real tolerance = 0;                    // used in setting up the experiment
 
     puts("FMU components instantiated, setting up experiments\n");