Chapter 3: ADA Basics

In this post, we will continue our exploration of the ADA programming language by diving into the basics – variables, data types, operators, control structures, and procedures. As an example, we’ll use an aerospace application, considering ADA’s long-standing significance in the aviation and aerospace industries. Let’s get started!

Variables and Data Types

One of the defining characteristics of the Ada programming language is its strong typing system. This system makes Ada suitable for applications that require a high degree of reliability and correctness, such as aerospace software.

Variables are fundamental in programming. They are named entities used to store data that can change during the execution of the program. In Ada, every variable is associated with a specific data type at the time of its declaration.

There are several basic types in Ada, which include:

Integer Types

These are used to represent whole numbers. For instance, in an aerospace context, we might use an integer to represent the altitude of an aircraft:

Altitude : Integer;

Floating-Point Types

These are used to represent real numbers (i.e., numbers that can have a fractional part). For example, the speed of an aircraft might be represented as a floating-point number:

Speed : Float;

Boolean Types

These are used to represent logical values, which can either be True or False. In our aerospace software, we could have a Boolean variable to indicate whether the aircraft’s engine is running:

Engine_Running : Boolean;

Character Types

These are used to represent single characters. They might be used, for instance, to represent a command that should be sent to the aircraft:

Command : Character;

Enumeration Types

These are used to represent a collection of named values. They could be used, for example, to represent the various states an aircraft could be in:

type Aircraft_State is (On_Ground, Taxiing, In_Air);
Current_State : Aircraft_State;

In addition to these basic types, Ada also supports more complex types, such as arrays and records. You can also define your own types in Ada, providing a great deal of flexibility in how you structure your data.

In the next section, we will discuss the various operators available in Ada and how they can be used to manipulate these data types.

Operators

Just as with any other programming language, Ada provides a variety of operators that enable us to manipulate data stored in variables. These operators make it possible to perform tasks such as arithmetic calculations, comparisons, logical operations, and assignments. Let’s discuss the key types of operators you’ll use in Ada.

Arithmetic Operators

Arithmetic operators perform mathematical operations such as addition, subtraction, multiplication, and division. For instance, if we wanted to calculate the new altitude of an aircraft based on its speed and time of travel, we might use the + and * operators:

New_Altitude := Altitude + Speed * Time;

Here, := is the assignment operator, + and * are arithmetic operators. Note that multiplication and division have higher precedence than addition and subtraction, similar to standard mathematical rules.

Relational Operators

Relational operators are used to compare two values. These include =, /=, <, <=, >, >=. For example, we might want to check if the altitude of the aircraft has reached a certain target:

if Altitude >= Target_Altitude then
   -- reduce engine power
end if;

Logical Operators

Logical operators combine or invert Boolean expressions. Ada uses the keywords and, or, and not for this purpose. For instance, we might need to check if both engines are running before taking off:

if Engine1_Running and Engine2_Running then
   -- ready for take-off
end if;

Assignment Operator

The assignment operator := is used to assign a value to a variable. It’s important to note the difference between = (equality operator) and := (assignment operator) in Ada. For instance, to change the altitude, we would write:

Altitude := New_Altitude;

These operators, when used with variables and control structures, form the foundation of Ada programming. They are essential for implementing the logic and functionality of any aerospace software application. Understanding and using these operators efficiently will significantly improve the reliability and performance of your Ada programs.

Control Structures: Conditional Statements and Loops

Control structures in Ada are used to dictate the flow of execution based on certain conditions or loops. They are crucial in implementing the logic of any application, including aerospace software.

Conditional Statements

Conditional statements allow us to execute a piece of code only if a particular condition is met. Here are two primary conditional statements in Ada:

If Statement

This is the simplest form of a conditional statement. The if statement checks whether a certain condition is true. If it is, the code inside the if block is executed. We can also use an else clause to specify code to be executed if the condition is not met. For instance, we might want to check if the aircraft’s altitude is safe:

if Altitude < Minimum_Safe_Altitude then
   -- Increase altitude
else
   -- Altitude is safe
end if;
Case Statement

The case statement in Ada is used to select one of many blocks of code to be executed. It functions similarly to a switch statement in other languages. We might use a case statement to handle different commands sent to the aircraft:

case Command is
   when 'U' => -- Code to move up
   when 'D' => -- Code to move down
   when others => -- Unknown command
end case;

Loops

Loops are used when we need to execute a block of code multiple times. Ada provides several loop constructs:

While Loop

This loop executes a block of code as long as a condition remains true. For instance, we might want to keep the aircraft ascending until it reaches a target altitude:

while Altitude < Target_Altitude loop
   -- Increase altitude
end loop;
For Loop

This loop iterates over a sequence of values. In Ada, the for loop is often used with ranges. Suppose we want to repeat a certain action, like sending a radio signal, ten times:

for I in 1..10 loop
   -- Send signal
end loop;

Understanding these control structures is critical to implementing the decision-making capabilities needed in complex applications like aerospace software. By effectively utilizing conditional statements and loops, we can ensure our programs respond appropriately to a wide range of scenarios and conditions.

Example-1

Here is an example of Ada code that includes an if condition, a case statement, a for loop, and a while loop. This simplistic simulation monitors an aircraft’s flight, adjusting altitude and fuel usage, and responding to various commands:

with Ada.Text_IO; use Ada.Text_IO;

procedure Flight_Simulation is
   Altitude : Integer := 0; -- in feet
   Fuel : Float := 100.0; -- in percentage
   Command : Character;
   Target_Altitude : Integer := 10000; -- Target altitude in feet
   Fuel_Consumption_Rate : Float := 0.1; -- Fuel consumption rate per second

   procedure Adjust_Altitude is
   begin
      if Altitude < Target_Altitude then
         Altitude := Altitude + 500; -- Increase altitude by 500 feet
         Put_Line("Increasing altitude. Current altitude: " & Integer'Image(Altitude));
      elsif Altitude > Target_Altitude then
         Altitude := Altitude - 500; -- Decrease altitude by 500 feet
         Put_Line("Decreasing altitude. Current altitude: " & Integer'Image(Altitude));
      end if;
   end Adjust_Altitude;

   procedure Use_Fuel is
   begin
      Fuel := Fuel - Fuel_Consumption_Rate;
      Put_Line("Fuel: " & Float'Image(Fuel) & "%");
   end Use_Fuel;

begin
   -- Ascend to target altitude
   while Altitude < Target_Altitude loop
      Adjust_Altitude;
      Use_Fuel;
      delay 1.0; -- delay for 1 second
   end loop;

   -- Follow commands for 10 seconds
   for Second in 1..10 loop
      Put("Enter command (U: Up, D: Down, F: Full stop): ");
      Get(Command);
      case Command is
         when 'U' =>
            Target_Altitude := Target_Altitude + 1000;
         when 'D' =>
            Target_Altitude := Target_Altitude - 1000;
         when 'F' =>
            Target_Altitude := Altitude; -- Maintain current altitude
         when others =>
            Put_Line("Unknown command");
      end case;
      Adjust_Altitude;
      Use_Fuel;
      delay 1.0; -- delay for 1 second
   end loop;
end Flight_Simulation;

In this example, the procedure Flight_Simulation contains a while loop that ascends the aircraft to the target altitude while there is enough fuel. A for loop then follows commands for 10 seconds, adjusting altitude and using fuel each second. An if condition in Adjust_Altitude checks whether the altitude should be increased or decreased, and a case statement in the for loop responds to different commands.

Example-2

here is an example of an Ada code for a simplified Terrain Awareness Warning System (TAWS), which uses condition (if-else statements), case, for loop, and while loop:

with Ada.Text_IO; use Ada.Text_IO;

procedure TAWS is
   type Warning_Level is (No_Threat, Caution, Warning, Critical);
   Terrain_Elevation : Integer := 500;  -- in feet, this can be determined by a sensor
   Aircraft_Altitude : Integer := 1000;  -- in feet, this can be determined by a sensor
   Warning : Warning_Level := No_Threat;

   procedure Check_Warning is
   begin
      if Aircraft_Altitude - Terrain_Elevation <= 100 then
         Warning := Critical;
      elsif Aircraft_Altitude - Terrain_Elevation <= 200 then
         Warning := Warning;
      elsif Aircraft_Altitude - Terrain_Elevation <= 500 then
         Warning := Caution;
      else
         Warning := No_Threat;
      end if;
   end Check_Warning;

begin
   -- Assume we have data for 100 seconds
   for Second in 1 .. 100 loop
      -- The actual altitude and elevation can be changed here based on the real-time data
      Aircraft_Altitude := Aircraft_Altitude - 5;  -- Assume the aircraft is descending 5 feet per second
      Terrain_Elevation := Terrain_Elevation + 3;  -- Assume the terrain elevation is rising 3 feet per second

      -- Check the warning level
      Check_Warning;

      -- Display the warning based on the level
      case Warning is
         when No_Threat =>
            Ada.Text_IO.Put_Line("No threat detected.");
         when Caution =>
            Ada.Text_IO.Put_Line("Caution! Adjust the altitude.");
         when Warning =>
            Ada.Text_IO.Put_Line("Warning! Adjust the altitude immediately.");
         when Critical =>
            Ada.Text_IO.Put_Line("Critical warning! Perform evasive action!");
      end case;

      -- Let's assume there's a condition under which the system should stop checking
      -- For instance, if the aircraft has landed (Aircraft_Altitude = 0)
      if Aircraft_Altitude = 0 then
         Ada.Text_IO.Put_Line("The aircraft has landed. The system will stop.");
         exit;
      end if;

      -- Add some delay
      delay 1.0;
   end loop;
end TAWS;

This is a basic example where the TAWS checks the aircraft altitude and terrain elevation every second for 100 seconds and provides warnings accordingly. The for loop is used to simulate the passing of time (each iteration is one second), while the if and case statements are used to determine and display the warning level. The system stops checking when the aircraft has landed (Aircraft_Altitude = 0).

Chapter 3: ADA Basics
Scroll to top
error: Content is protected !!