test.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. ============================================================================
  3. Name : pw_case.c
  4. Author : Joachim Denil
  5. Version :
  6. Copyright : Your copyright notice
  7. ============================================================================
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include "fmi2.h"
  12. #include "sim_support.h"
  13. #define START_TIME 0.0
  14. #define STOP_TIME 9.0
  15. #define STEP_SIZE 0.1
  16. #define _in_armature_current 0
  17. #define _in_driver_up 0
  18. #define _in_driver_up_stop 1
  19. #define _in_driver_down 2
  20. #define _in_driver_down_stop 3
  21. #define _in_passenger_up 4
  22. #define _in_passenger_up_stop 5
  23. #define _in_passenger_down 6
  24. #define _in_passenger_down_stop 7
  25. #define _out_u 9
  26. #define _out_d 10
  27. FMU fmu_control_sa;
  28. int main(void) {
  29. puts("FMI_controller_sa: Loading Dlls\n");
  30. /* loading */
  31. loadDll("libFMI_controller_sa.dll", &fmu_control_sa, "PW_CONTROLLER_SA_");
  32. puts("instantiating fmus\n");
  33. fmi2Component c_control_sa;
  34. char *fmuResourceLocation_control_sa = "libFMI_controller_sa.dll";
  35. fmi2CallbackFunctions callbacks_control_sa = {fmuLogger, calloc, free, NULL, &fmu_control_sa};
  36. c_control_sa = fmu_control_sa.instantiate("control_sa", fmi2CoSimulation, "1", fmuResourceLocation_control_sa, &callbacks_control_sa, fmi2False, fmi2False);
  37. fmi2Boolean toleranceDefined = fmi2True; // Enables zero crossing location
  38. fmi2Real tolerance = 0; // used in setting up the experiment
  39. puts("FMU components instantiated, setting up experiments\n");
  40. fmi2Status fmi2Flag;
  41. fmi2Flag = fmu_control_sa.setupExperiment(c_control_sa, toleranceDefined, tolerance, START_TIME, fmi2True, STOP_TIME);
  42. if (fmi2Flag == fmi2Error){
  43. return -1;
  44. }
  45. puts("Experiment setup, entering init mode\n");
  46. fmi2Flag = fmu_control_sa.enterInitializationMode(c_control_sa);
  47. if (fmi2Flag == fmi2Error){
  48. return -1;
  49. }
  50. puts("Experiment setup, exiting init mode\n");
  51. fmi2Flag = fmu_control_sa.exitInitializationMode(c_control_sa);
  52. if (fmi2Flag == fmi2Error){
  53. return -1;
  54. }
  55. fmi2ValueReference vr_in_control_sa_from_env[8]={0,1,2,3,4,5,6,7};
  56. fmi2Boolean b_out_env[9];
  57. fmi2ValueReference vr_out_control_sa[2]={9,10};
  58. fmi2Boolean b_out_control_sa[2];
  59. fmi2ValueReference vr_in_control_sa_from_window[1] = {0};
  60. fmi2Real r_out_power[3];
  61. double currentTime = START_TIME;
  62. double next_step_size = STEP_SIZE;
  63. while(currentTime <= STOP_TIME){
  64. printf("\n----master new loop, ct:%f, h:%f\n",currentTime,next_step_size);
  65. fmi2Component ctemp_control_sa;
  66. fmu_control_sa.getFMUstate(c_control_sa, &ctemp_control_sa);
  67. if(currentTime < 0.01){
  68. r_out_power[_in_armature_current] = 0.0f;
  69. b_out_env[_in_driver_up] = 0;
  70. b_out_env[_in_driver_up_stop] = 0;
  71. b_out_env[_in_driver_down] = 0;
  72. b_out_env[_in_driver_down_stop] = 0;
  73. b_out_env[_in_passenger_up] = 0;
  74. b_out_env[_in_passenger_up_stop] = 0;
  75. b_out_env[_in_passenger_down] = 0;
  76. b_out_env[_in_passenger_down_stop] = 0;
  77. }else if(currentTime < 4){
  78. r_out_power[_in_armature_current] = 1.44*(currentTime + next_step_size); // crossing occurs at 3.47...
  79. b_out_env[_in_driver_up] = 1;
  80. b_out_env[_in_driver_up_stop] = 0;
  81. b_out_env[_in_driver_down] = 0;
  82. b_out_env[_in_driver_down_stop] = 0;
  83. b_out_env[_in_passenger_up] = 0;
  84. b_out_env[_in_passenger_up_stop] = 0;
  85. b_out_env[_in_passenger_down] = 0;
  86. b_out_env[_in_passenger_down_stop] = 0;
  87. } else {
  88. r_out_power[_in_armature_current] = 1.44*3.47 - (currentTime + next_step_size - 3.47);
  89. b_out_env[_in_driver_up] = 1;
  90. b_out_env[_in_driver_up_stop] = 0;
  91. b_out_env[_in_driver_down] = 0;
  92. b_out_env[_in_driver_down_stop] = 0;
  93. b_out_env[_in_passenger_up] = 0;
  94. b_out_env[_in_passenger_up_stop] = 0;
  95. b_out_env[_in_passenger_down] = 0;
  96. b_out_env[_in_passenger_down_stop] = 0;
  97. }
  98. fmi2Flag = fmu_control_sa.setBoolean(c_control_sa,vr_in_control_sa_from_env,8,&b_out_env[0]);
  99. if(fmi2Flag != fmi2OK){
  100. printf("Failed setBoolean\n");
  101. fflush(stdout);
  102. return 1;
  103. }
  104. fmi2Flag = fmu_control_sa.setReal(c_control_sa,vr_in_control_sa_from_window,1, &r_out_power[0]);
  105. if(fmi2Flag != fmi2OK){
  106. printf("Failed setReal\n");
  107. fflush(stdout);
  108. return 1;
  109. }
  110. fmi2Flag = fmu_control_sa.doStep(c_control_sa, currentTime, next_step_size, fmi2True);
  111. if (fmu_control_sa.getBoolean(c_control_sa, vr_out_control_sa, 2, &b_out_control_sa[0]) != fmi2OK){
  112. printf("Failed getBoolean\n");
  113. fflush(stdout);
  114. return 1;
  115. }
  116. printf("%f,%d,%d\n",
  117. currentTime,
  118. b_out_control_sa[0],
  119. b_out_control_sa[1]);
  120. if (fmi2Flag == fmi2Discard){
  121. printf("Discard occurred!\n");
  122. fmi2Real newtime;
  123. fmu_control_sa.getRealStatus(c_control_sa, fmi2LastSuccessfulTime, &newtime);
  124. fmi2Real the_FMU_new_step = newtime - currentTime;
  125. if(the_FMU_new_step < next_step_size){
  126. printf("Control_sa simulated partially with a step size of %f!\n", the_FMU_new_step);
  127. next_step_size = the_FMU_new_step;
  128. printf("Rolling back Control_sa...\n");
  129. fmu_control_sa.setFMUstate(c_control_sa, ctemp_control_sa);
  130. }
  131. } else {
  132. printf("Step accepted, advancing time and adjusting next step size...\n");
  133. currentTime += next_step_size;
  134. next_step_size = STEP_SIZE;
  135. }
  136. fflush(stdout);
  137. }
  138. return EXIT_SUCCESS;
  139. }