You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
5.4 KiB

  1. #include <unistd.h> // For sleep()
  2. #include <pthread.h>
  3. #include "types.h"
  4. #include "assemblage_includes.h"
  5. #include "assemblage.h"
  6. // This should be set to 1 to run in "real-time" in the sense
  7. // that the simulation time is close to the real world time
  8. #define RUN_WITH_REAL_TIME 0
  9. // Task set
  10. struct nonencoded_task_params* tasks;
  11. // I/O
  12. output_t outs;
  13. uint64_t step_simu;
  14. uint64_t max_step_simu;
  15. // Barriers for thread synchro
  16. pthread_barrier_t cycle_start_b;
  17. pthread_barrier_t engine_elevator_b;
  18. pthread_barrier_t filter_b;
  19. pthread_barrier_t control_b;
  20. pthread_barrier_t output_b;
  21. // Output variables
  22. extern double aircraft_dynamics495_Va_Va_filter_100449_Va[2];
  23. extern double aircraft_dynamics495_az_az_filter_100458_az[2];
  24. extern double aircraft_dynamics495_Vz_Vz_filter_100452_Vz[2];
  25. extern double aircraft_dynamics495_q_q_filter_100455_q[2];
  26. extern double aircraft_dynamics495_h_h_filter_100446_h[2];
  27. extern double Va_control_50474_delta_th_c_delta_th_c;
  28. extern double Vz_control_50483_delta_e_c_delta_e_c;
  29. void copy_output_vars(output_t* v, uint64_t step){
  30. v->sig_outputs.Va = aircraft_dynamics495_Va_Va_filter_100449_Va[step%2];
  31. v->sig_outputs.Vz = aircraft_dynamics495_Vz_Vz_filter_100452_Vz[step%2];
  32. v->sig_outputs.q = aircraft_dynamics495_q_q_filter_100455_q[step%2];
  33. v->sig_outputs.az = aircraft_dynamics495_az_az_filter_100458_az[step%2];
  34. v->sig_outputs.h = aircraft_dynamics495_h_h_filter_100446_h[step%2];
  35. v->sig_delta_th_c = Va_control_50474_delta_th_c_delta_th_c;
  36. v->sig_delta_e_c = Vz_control_50483_delta_e_c_delta_e_c;
  37. }
  38. void rosace_init() {
  39. // Init barriers
  40. pthread_barrier_init(&cycle_start_b, NULL, 5);
  41. pthread_barrier_init(&engine_elevator_b, NULL, 2);
  42. pthread_barrier_init(&filter_b, NULL, 5);
  43. pthread_barrier_init(&control_b, NULL, 2);
  44. pthread_barrier_init(&output_b, NULL, 2);
  45. // Initial values
  46. outs.sig_outputs.Va = 0;
  47. outs.sig_outputs.Vz = 0;
  48. outs.sig_outputs.q = 0;
  49. outs.sig_outputs.az = 0;
  50. outs.sig_outputs.h = 0;
  51. outs.t_simu = 0;
  52. step_simu = 0;
  53. // Get the task set (required for CALL() macro)
  54. int tmp;
  55. get_task_set(&tmp, &tasks);
  56. }
  57. #define CALL(val) tasks[(val)].ne_t_body(NULL)
  58. void* thread1(void* arg) {
  59. uint64_t mystep_simu = step_simu;
  60. while(step_simu<max_step_simu) {
  61. pthread_barrier_wait(&cycle_start_b);
  62. // --- 200 Hz ---
  63. CALL(ENGINE);
  64. pthread_barrier_wait(&engine_elevator_b);
  65. // --- End 200 Hz ---
  66. // --- 100 Hz ---
  67. if(mystep_simu%2 == 0) {
  68. pthread_barrier_wait(&filter_b);
  69. CALL(VZ_FILTER);
  70. }
  71. // --- End 100 Hz ---
  72. // --- 10 Hz ---
  73. if(mystep_simu%20 == 0)
  74. CALL(VA_C0);
  75. // --- End 10 Hz ---
  76. // --- 50 Hz ---
  77. if(mystep_simu%4 == 0) {
  78. pthread_barrier_wait(&control_b);
  79. CALL(VA_CONTROL);
  80. }
  81. if(mystep_simu%4 == 3) {
  82. pthread_barrier_wait(&output_b);
  83. CALL(DELTA_TH_C0);
  84. }
  85. // --- End 50 Hz ---
  86. step_simu = step_simu + 1;
  87. outs.t_simu += 5;
  88. // Print output
  89. copy_output_vars(&outs, step_simu);
  90. if (step_simu%10)
  91. ROSACE_write_outputs(&outs);
  92. if(RUN_WITH_REAL_TIME)
  93. usleep(5000); // "Real-time" execution
  94. mystep_simu++;
  95. }
  96. return NULL;
  97. }
  98. void* thread2(void* arg) {
  99. uint64_t mystep_simu = step_simu;
  100. while(step_simu<max_step_simu) {
  101. pthread_barrier_wait(&cycle_start_b);
  102. // --- 200 Hz ---
  103. CALL(ELEVATOR);
  104. pthread_barrier_wait(&engine_elevator_b);
  105. CALL(AIRCRAFT_DYN);
  106. // --- End 200 Hz ---
  107. // --- 100 Hz ---
  108. if(mystep_simu%2 == 0) {
  109. pthread_barrier_wait(&filter_b);
  110. CALL(H_FILTER);
  111. }
  112. // --- End 100 Hz ---
  113. // --- 10 Hz ---
  114. if(mystep_simu%20 == 0)
  115. CALL(H_C0);
  116. // --- End 10 Hz ---
  117. // --- 50 Hz ---
  118. if(mystep_simu%4 == 0) {
  119. CALL(ALTI_HOLD);
  120. pthread_barrier_wait(&control_b);
  121. CALL(VZ_CONTROL);
  122. }
  123. if(mystep_simu%4 == 3) {
  124. pthread_barrier_wait(&output_b);
  125. CALL(DELTA_E_C0);
  126. }
  127. // --- End 50 Hz ---
  128. mystep_simu++;
  129. }
  130. return NULL;
  131. }
  132. void* thread3(void* arg) {
  133. uint64_t mystep_simu = step_simu;
  134. while(step_simu<max_step_simu) {
  135. pthread_barrier_wait(&cycle_start_b);
  136. // --- 100 Hz ---
  137. if(mystep_simu%2 == 0) {
  138. pthread_barrier_wait(&filter_b);
  139. CALL(Q_FILTER);
  140. }
  141. // --- End 100 Hz ---
  142. mystep_simu++;
  143. }
  144. return NULL;
  145. }
  146. void* thread4(void* arg) {
  147. uint64_t mystep_simu = step_simu;
  148. while(step_simu<max_step_simu) {
  149. pthread_barrier_wait(&cycle_start_b);
  150. // --- 100 Hz ---
  151. if(mystep_simu%2 == 0) {
  152. pthread_barrier_wait(&filter_b);
  153. CALL(VA_FILTER);
  154. }
  155. // --- End 100 Hz ---
  156. mystep_simu++;
  157. }
  158. return NULL;
  159. }
  160. void* thread5(void* arg) {
  161. uint64_t mystep_simu = step_simu;
  162. while(step_simu<max_step_simu) {
  163. pthread_barrier_wait(&cycle_start_b);
  164. // --- 100 Hz ---
  165. if(mystep_simu%2 == 0) {
  166. pthread_barrier_wait(&filter_b);
  167. CALL(AZ_FILTER);
  168. }
  169. // --- End 100 Hz ---
  170. mystep_simu++;
  171. }
  172. return NULL;
  173. }
  174. int run_rosace(uint64_t nbstep){
  175. int i;
  176. // Variables for thread management
  177. void* fcts[] = {&thread1, &thread2, &thread3, &thread4, &thread5};
  178. pthread_t threads[5];
  179. rosace_init();
  180. max_step_simu = nbstep;
  181. // Set first command
  182. ROSACE_update_altitude_command(11000.0);
  183. // Create the 5 threads
  184. for(i=0; i<5; i++)
  185. pthread_create( &(threads[i]), NULL, fcts[i], NULL);
  186. // SCENARIO
  187. /* sleep(20);
  188. ROSACE_update_altitude_command(10500.0);
  189. sleep(20);
  190. ROSACE_update_altitude_command(11500.0);
  191. sleep(20);
  192. ROSACE_update_altitude_command(10000.0);
  193. sleep(20);
  194. ROSACE_update_altitude_command(9000.0);
  195. */
  196. // Exit
  197. for(i=0; i<5; i++)
  198. pthread_join(threads[i], NULL);
  199. return 0;
  200. }