controller_sa.BASE.sa 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import PowerWindowModel
  2. module Controller_SA
  3. semantic adaptation power_sa
  4. for fmu controller
  5. input ports armature_current, passenger_up, passenger_down, passenger_stop, driver_up, driver_down, driver_stop;
  6. param REL_TOL = 0.0001;
  7. param ABS_TOL = 1e-8;
  8. param CROSSING = 5;
  9. param init_armature_current = CROSSING;
  10. param init_up = 0;
  11. param init_down = 0;
  12. in var previous_arm_current := init_armature_current;
  13. in var stored_arm_current := init_armature_current;
  14. in var stored_displacement := init_displacement;
  15. in var stored_speed := init_speed;
  16. in var next_time_step := -1;
  17. in var step_size;
  18. in var aux_obj_detected := 0;
  19. in rules {
  20. next_time_step < 0 -> {
  21. next_time_step := get_next_time_step(controller) + t;
  22. /*
  23. The get_next_time_step(controller) function is equivalent to the following snippet:
  24. transaction(controller);
  25. internal_transition := do_step(controller, t, MAX);
  26. next_time_step := t + internal_transition;
  27. rollback(controller);
  28. */
  29. } --> { };
  30. true -> {
  31. stored_arm_current := armature_current
  32. } --> {
  33. obj_detected := aux_obj_detected; // Sets this input to the FMU
  34. };
  35. }
  36. control rules {
  37. aux_obj_detected := false;
  38. step_size := H;
  39. if ((not is_close(previous_arm_current, CROSSING, REL_TOL, ABS_TOL) and previous_arm_current < CROSSING)
  40. and (not is_close(stored_arm_current, CROSSING, REL_TOL, ABS_TOL) and stored_arm_current > CROSSING)) { // crossing, but not within tolerance
  41. var negative_value := previous_arm_current - CROSSING;
  42. var positive_value := stored_arm_current - CROSSING;
  43. step_size := (H * (- negative_value)) / (positive_value - negative_value);
  44. } else {
  45. if ((not is_close(previous_arm_current, CROSSING, REL_TOL, ABS_TOL) and previous_arm_current < CROSSING)
  46. and is_close(stored_arm_current, CROSSING, REL_TOL, ABS_TOL )) { // crossing within tolerance found
  47. aux_obj_detected := true;
  48. }
  49. }
  50. if (obj_detected == true or t >= next_time_step) {
  51. var aux_h = do_step(controller, t-e, e); // do a step, then decide next internal transition
  52. assert aux_h == e; // this must always be the case, otherwise it is better not to use the timed transition adaptation.
  53. next_time_step := -1; // next time the setValues is called, the internal transition will be set again.
  54. }
  55. if is_close(step_size, H, REL_TOL, ABS_TOL) {
  56. // Step accepted, so store the known input.
  57. // We cannot store the armature current at the in rules because we may not accept the step and because they may be called multiple times. If that happens, we want to still have the old value of armature current, to compare it with the new one.
  58. previous_arm_current := stored_arm_current;
  59. }
  60. return step_size; // the step size is calculated in the in_rules.
  61. }
  62. out var stored_up := init_up;
  63. out var stored_down := init_down;
  64. out rules {
  65. true -> {
  66. /*
  67. Previously, there was this intruction here:
  68. internal_transition := get_next_time_step(controller) + t
  69. However, it cannot be here, since there is no guarantee in the control rules block, that the doStep of the controller will be called.
  70. */
  71. } --> {};
  72. /*
  73. What does "otherwise var" mean?
  74. Suppose I have the following rules:
  75. var1 == true and var2 == false -> ...
  76. otherwise var1 -> ...
  77. controller.up == true -> { stored_up := 1; } --> { };
  78. otherwise up -> { } --> { };
  79. down == true -> { down := 1; } --> { };
  80. otherwise down -> { } --> { };
  81. stop == true -> { up := 0; down := 0; } --> { };
  82. otherwise stop -> { } --> { };
  83. true -> { up := stored_up; stored_up := up; } --> { };
  84. true -> { down := stored_down; stored_down := down; } --> { };
  85. */
  86. controller.up -> {stored_up := 1} --> {u := stored_up};
  87. not controller.up -> {stored_up := 0} --> {u := stored_up};
  88. controller.down -> {stored_down := 1} --> {d := stored_down};
  89. not controller.down -> {stored_down := 0} --> {d := stored_down};
  90. controller.stop -> {stored_down := 0; stored_up :=0 } --> {u := stored_up ; d := stored_down};
  91. }