Advertisement
WayGroovy

Untitled

Oct 13th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.95 KB | None | 0 0
  1. /*
  2.  * main.c
  3.  *
  4.  *  Created on: Oct 8, 2016
  5.  *      Author: Aden
  6.  */
  7.  
  8. #include<stdio.h>
  9. #include<stdlib.h>
  10. #include<conio.h>
  11. #include<unistd.h>
  12.  
  13. double calcMach(float alt, float vel);
  14.  
  15. int main(){
  16.  
  17.     FILE *fp1, *fp2;
  18.     char buffer[4], swap[4];
  19.     float value, alt, vel, mach, timeS;
  20.     int timeMS=0;
  21.     int i=1;
  22.  
  23. //  printf("Hello World\n");
  24. //  system("PAUSE");
  25.  
  26.     fp1 = fopen("tmp/Alt_AS.dat", "rb");
  27.     fp2 = fopen("tmp/flightdata.csv", "w");
  28.     fprintf(fp2,"time (s),altitude (ft),velocity (knots),mach\n");
  29.     fclose (fp2);                                   // Create blank file with headder
  30.  
  31.     fp2 = fopen("tmp/flightdata.csv", "a");
  32.  
  33.     while (fread(buffer, 1, 4, fp1) == 4) {         // Read the data file
  34.         swap[3]=buffer[0];
  35.         swap[2]=buffer[1];                          // Byte swap 4 bytes
  36.         swap[1]=buffer[2];
  37.         swap[0]=buffer[3];
  38.         value=*((float*)&swap);                     // Convert to Float
  39.         if (i==1) {                                 // Alternate* every other swap between Altitude and Velocity
  40.             timeS = (timeMS*0.1);
  41.             printf("Time: %*.1f\n", 6, timeS);      // Display while running
  42.             alt=value;
  43. //          printf("Alt: %*.0f\n", 7, alt);
  44.             fprintf(fp2,"%.1f,%g,", timeS, alt);    // Record altitude to CSV
  45.         } else {
  46.             vel=value;
  47. //          printf("Vel: %*.2f\n", 7, vel);
  48.             mach=calcMach(alt, vel);                // Calculate the Mach Speed
  49. //          printf("Mach: %*.2f\n\n", 6, mach);
  50.             timeMS+=1;
  51.             fprintf(fp2,"%g,%.2f\n", vel, mach);    // Record the Velocity & Mach Speed
  52.         }
  53.  
  54.         i*=-1;                                      // Alternate*
  55.  
  56.     }
  57.  
  58.  
  59.     fclose (fp1);
  60.     fclose (fp2);                                   // Close Files
  61. //  system("PAUSE");
  62.     return 0;
  63. }
  64.  
  65. double calcMach(float alt, float vel) {
  66.     float mach=0;
  67.  
  68.     if (alt > 4000){
  69.         mach=573;
  70.     } else if (alt>30000){
  71.         mach=alt-30000;
  72.         mach*=-0.0016;
  73.         mach+=589;
  74.     } else if (alt>20000){
  75.         mach=alt-20000;
  76.         mach*=-0.0025;
  77.         mach+=614;
  78.     } else if (alt>10000){
  79.         mach=alt-10000;
  80.         mach*=-0.0024;
  81.         mach+=638;
  82.     } else if (alt>0){
  83.         mach*=-0.0023;
  84.         mach+=661;
  85.     } else {
  86.         mach=661;
  87.     }
  88.     mach=vel/mach;
  89.  
  90.     return mach;
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement