Uploaded by zalak0

Code description for Air traffic control code

advertisement
I decided for my code to use an array of structures, based on the ADBSPacket structure given
in ‘message.h’. This was done by defining another structure which stores the ADBSPacket
structures, the size of the array of structures and the capacity of the array and then defining a
function called ‘init_list’ (used in the first line in main), which initializes these variables and adds
enough memory to append any ADBSPacket structures (or plane data).
After initialization, I defined another function called ‘input_command’. This function continuously
processes any input by the user in stdin until the program is closed (using a for loop). It firstly
‘gets’ the input and removes any new line characters and replaces it with null-terminators (this is
important to avoid any infinite loops after closing). The code can take two paths after this.
If it is data from an airplane, the code quickly checks if the index of plane_data we are trying to
store exceeds the capacity, to avoid any errors. Then the code reads the data using the sscanf()
function and appends it to ‘my_list’ using the ‘append_data()’. The ‘append_data’ function
appends the data read to an already existing structure if the aircraftID collected matches with a
previous plane ID that was already stored or, it reallocates new memory and adds the aircraft
data as a new structure. This greatly reduces the amount of data I need to store, making the
code very economical.
If it is a command from Air Traffic Control, then we use the ‘process_command’ function, which
determines what the command is. If the command is ‘close’, the function will return an int = 1,
which will transfer into the ‘input_command’ function and also return a 1 to indicate ‘close’ was
encountered. This then is transferred into the main function where, after dynamic memory is
freed, checks if close was encountered. If so, the program prints ‘closing’ and exits.
If the command is ‘est_pos’, the code goes to the ‘print_est_position’ function, which extracts
the ID of the airplane from the command line (the index of the aircraft in ‘my_list’ is found using
the ‘find_aircraft_index’ function) and runs ‘est_pos()’. The ‘est_pos’ function runs the
calculations needed to estimate the position of the aircraft (refer to overview slide). It either
prints its estimated position or specifies the aircraft is NOT in the airspace. After printing, the
program returns 0 to input_command, indicating close was not encountered and continues with
the code.
The above is similar to the ‘check_separation’ and ‘num_contacts’ commands, except for
‘check_separation’ we must also extract the minimum radius of separation of the aircrafts and
‘num_contacts’ doesn’t need any extractions from the command line.
However both functions rely on ‘est_pos’ to work. ‘Num_contacts’ checks the ‘est_pos’ of each
aircraft at the time the command was called using a ‘for’ loop. If the airplane’s ‘est_pos’ is within
the operating radius and has a valid aircraft ID, then it counts as one plane.
‘Check_separation’ also finds the ‘est_pos’ of each aircraft after being called and does the
provided calculations in the overview slides with each aircraft compared to the aircraft ID
imputed into the command line. This is reiterated in a for loop until the most ‘dangerous’
separation issue is found by finding the minimum ∆𝑡(𝑖𝑛𝑡) using the ‘min’ function and prints the
separation issue (if there is any) accordingly, after freeing any dynamic memory.
The code reiterates this until the ‘close’ command, to which the program swiftly exists.
Download