Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * main.c
- *
- * Created on: Oct 8, 2016
- * Author: Aden
- */
- #include<stdio.h>
- #include<stdlib.h>
- #include<conio.h>
- #include<unistd.h>
- double calcMach(float alt, float vel);
- int main(){
- FILE *fp1, *fp2;
- char buffer[4], swap[4];
- float value, alt, vel, mach, timeS;
- int timeMS=0;
- int i=1;
- // printf("Hello World\n");
- // system("PAUSE");
- fp1 = fopen("tmp/Alt_AS.dat", "rb");
- fp2 = fopen("tmp/flightdata.csv", "w");
- fprintf(fp2,"time (s),altitude (ft),velocity (knots),mach\n");
- fclose (fp2); // Create blank file with headder
- fp2 = fopen("tmp/flightdata.csv", "a");
- while (fread(buffer, 1, 4, fp1) == 4) { // Read the data file
- swap[3]=buffer[0];
- swap[2]=buffer[1]; // Byte swap 4 bytes
- swap[1]=buffer[2];
- swap[0]=buffer[3];
- value=*((float*)&swap); // Convert to Float
- if (i==1) { // Alternate* every other swap between Altitude and Velocity
- timeS = (timeMS*0.1);
- printf("Time: %*.1f\n", 6, timeS); // Display while running
- alt=value;
- // printf("Alt: %*.0f\n", 7, alt);
- fprintf(fp2,"%.1f,%g,", timeS, alt); // Record altitude to CSV
- } else {
- vel=value;
- // printf("Vel: %*.2f\n", 7, vel);
- mach=calcMach(alt, vel); // Calculate the Mach Speed
- // printf("Mach: %*.2f\n\n", 6, mach);
- timeMS+=1;
- fprintf(fp2,"%g,%.2f\n", vel, mach); // Record the Velocity & Mach Speed
- }
- i*=-1; // Alternate*
- }
- fclose (fp1);
- fclose (fp2); // Close Files
- // system("PAUSE");
- return 0;
- }
- double calcMach(float alt, float vel) {
- float mach=0;
- if (alt > 4000){
- mach=573;
- } else if (alt>30000){
- mach=alt-30000;
- mach*=-0.0016;
- mach+=589;
- } else if (alt>20000){
- mach=alt-20000;
- mach*=-0.0025;
- mach+=614;
- } else if (alt>10000){
- mach=alt-10000;
- mach*=-0.0024;
- mach+=638;
- } else if (alt>0){
- mach*=-0.0023;
- mach+=661;
- } else {
- mach=661;
- }
- mach=vel/mach;
- return mach;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement