Challenge

Project 2 - Measuring the distance of an object using ultrasonic sensor and also smoothen the sensor data using moving average filter

#include <stdio.h> //PREPROCESSOR
#include "AIR.h" //HEADER FILE INCLUDING

static void (*state_table[])(void) = {gear_down, checking_before_takeoff, raising_gear,
gear_up, checking_before_landing, lowering_gear}; //DECLARING VARIABLES TO STATE TABLE


State_Type current_state; //DEFINING CURRENT STATE AS STATE TYPE//


int main() //MAIN FUNCTION//
{
init_state_machine(); //INITIALIZE STATE MACHINE//
while(1) //WHILE LOOP//
{
state_table[current_state](); //CALLING STATE TABLE//

}
}

void init_state_machine() //FUNCTION INITIAL STATE MACHINE CALLED//
{
current_state = GEARDOWN; //ASSIGNING CURRENT STATE TO LOWERING GEAR//
printf("\nFinite state machine is initialized & program is in gear down state & light status:%s\n",state_machine[current_state].light_status); //DISPLAY LIGHT STATE//
}
void gear_down(void) //FUNCTION GEAR DOWN CALLED//
{
current_state = GEARDOWN; //ASSIGNING CURRENT STATE TO GEAR DOWN//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nEnter the pilot lever position & squat switch status:"); //PRINT THE MESSAGE//
fflush(stdout); //CLEAR CACHE MEMORY//
scanf("%d %d",&PilotLever,&squat_switch); //READS AND STORES THE DATA//
if(PilotLever==RAISINGEDGE && squat_switch==OPENED) //IF CONDITION TO CHECK THE CONDITIONS//
{
current_state = CHKBFRTKOFF; //IF CONDITION IS TRUE ASSIGN CURRENT STATE TO CHECK BEFORE TAKEOFF//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nlight status: %s\n",state_machine[current_state].light_status); //DISPLAY LIGHT STATE//
}
else //IF CONDITIONS ARE NOT SATISFIED GO TO GEAR DOWN FUNCTION//
{
gear_down(); //REPEAT//
}
}
void checking_before_takeoff() //FUNCTION CHECKING BEFORE LANDING CALLED//
{
current_state=CHKBFRTKOFF; //ASSIGNING CURRENT STATE TO CHECK BEFORE TAKEOFF//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nEnter the pilot lever position,squat switch status & status of hydraulic mechanism:"); //PRINT THE MESSAGE//
fflush(stdout); //CLEAR CACHE MEMORY//
scanf("%d %d %d",&PilotLever,&squat_switch,&HydraulicMechanism); //READS AND STORES THE DATA//
printf("\nwait for 2 secs\n"); //PRINT THE MESSAGE//
if(PilotLever==FALLINGEDGE || squat_switch==CLOSED) //IF CONDITION TO CHECK THE CONDITIONS//
{
gear_down(); //IF CONDITION IS TRUE GO TO GEAR DOWN FUNCTION//
}
else if(PilotLever==UP && squat_switch==OPENED && HydraulicMechanism==WORKING) //NESTED IF CONDITION TO CHECK OTHER CONDITION//
{
current_state = RSGGEAR; //IF CONDITION IS TRUE ASSIGN CURRENT STATE TO RISING GEAR//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nlight status: %s\n",state_machine[current_state].light_status); //DISPLAY LIGHT STATE//
printf("direction valve status:%s\n",state_machine[current_state].direction_valve_status); //DISPLAY DIRECTION VALVE STATUS//
}
else if(PilotLever==UP && squat_switch==OPENED && HydraulicMechanism==MALFUNCTIONING) //NESTED IF CONDITION TO CHECK OTHER CONDITION//
{
current_state=CHKBFRTKOFF;//IF CONDITION IS NOT SATISFIED ASSIGN CURRENT STATE TO RISING GEAR//
printf("\nlight status: %s\n",state_machine[current_state].light_status); //DISPLAY LIGHT STATE//
printf("direction valve status:%s\n",state_machine[current_state].direction_valve_status); //DISPLAY DIRECTION VALVE STATUS//
printf("gas pressurized spring status:%s\n",state_machine[current_state].gas_pressurized_spring_status[HydraulicMechanism]); //DISPLAY GAS PRESSURIZED SPRING STATUS//
}
}

void raising_gear() //FUNCTION RAISING GEAR CALLED//
{
current_state=RSGGEAR; //ASSIGNING CURRENT STATE TO RISING GEAR//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nEnter the limit switch status,Lever Status position:"); //PRINT THE MESSAGE//
fflush(stdout); //CLEAR CACHE MEMORY//
scanf("%d %d",&PilotLever,&limit_switch); //READS AND STORES THE DATA//
printf("\nwait for 1 sec\n"); //PRINT THE MESSAGE//
if(PilotLever==FALLINGEDGE && limit_switch==OPENED) //IF CONDITION TO CHECK THE CONDITIONS//
{
current_state = CHKBFRLAND; //IF CONDITION IS TRUE ASSIGN CURRENT STATE TO CHECK BEFORE LANDING//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nlight status: %s\n",state_machine[current_state].light_status); //DISPLAY LIGHT STATE//
printf("hydraulic control for landing gear status:%s\n",state_machine[current_state].hydraulic_pump_control_status); //DISPLAY HYDRAULIC PUMP CONTROL STATUS//
}
else if(PilotLever==UP && limit_switch==CLOSED) //NESTED IF CONDITION TO CHECK OTHER CONDITION//
{
current_state=GEARUP; //IF CONDITION IS TRUE ASSIGN CURRENT STATE TO GEAR UP//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nlight status: %s\n",state_machine[current_state].light_status); //DISPLAY LIGHT STATE//
printf("hydraulic control for landing gear status:%s\n",state_machine[current_state].hydraulic_pump_control_status); //DISPLAY HYDRAULIC PUMP CONTROL STATUS//
}
else //IF CONDITIONS ARE NOT SATISFIED GO TO RAISING GEAR FUNCTION//
{
raising_gear(); //REPEAT//
}
}

void gear_up() //FUNCTION GEAR UP CALLED//
{
current_state=GEARUP; //ASSIGNING CURRENT STATE TO GEAR UP//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nEnter the pilot lever position:"); //PRINT THE MESSAGE//
fflush(stdout); //CLEAR CACHE MEMORY//
scanf("%d",&PilotLever); //READS AND STORES THE DATA//
if(PilotLever==FALLINGEDGE) //IF CONDITION TO CHECK THE CONDITIONS//
{
current_state = CHKBFRLAND; //IF CONDITION IS TRUE ASSIGN CURRENT STATE TO CHECK BEFORE LANDING//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nlight status: %s\n",state_machine[current_state].light_status); //DISPLAY LIGHT STATE//
printf("hydraulic control for landing gear status:%s\n",state_machine[current_state].hydraulic_pump_control_status); //DISPLAY HYDRAULIC PUMP CONTROL STATUS//
}
else //IF CONDITIONS ARE NOT SATISFIED GO TO GEAR UP FUNCTION//
{
gear_up(); //REPEAT//
}
}

void checking_before_landing() //FUNCTION CHECKING BEFORE LANDING CALLED//
{
current_state=CHKBFRLAND; //ASSIGNING CURRENT STATE TO CHECK BEFORE LANDING//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nenter the pilot lever position & status of hydraulic mechanism:"); //PRINT THE MESSAGE//
fflush(stdout); //CLEAR CACHE MEMORY//
scanf("%d %d",&PilotLever,&HydraulicMechanism); //READS AND STORES THE DATA//
if(PilotLever==DOWN && HydraulicMechanism==WORKING) //IF CONDITION TO CHECK THE CONDITIONS//
{
current_state = LWRGEAR; //IF CONDITION IS TRUE ASSIGN CURRENT STATE TO LOWERING GEAR//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nlight status: %s\n",state_machine[current_state].light_status); //DISPLAY LIGHT STATE//
printf("\ndirection valve status:%s\n",state_machine[current_state].direction_valve_status); //DISPLAY DIRECTION VALVE STATUS//
}
else if(PilotLever==DOWN && HydraulicMechanism==MALFUNCTIONING) //NESTED IF CONDITION TO CHECK OTHER CONDITION//
{
current_state = LWRGEAR; //IF CONDITION IS TRUE ASSIGN CURRENT STATE TO LOWERING GEAR//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nlight status: %s\n",state_machine[current_state].light_status); //DISPLAY LIGHT STATE//
printf("\ndirection valve status:%s\n",state_machine[current_state].direction_valve_status); //DISPLAY DIRECTION VALVE STATUS//
printf("gas pressurized spring status:%s\n",state_machine[current_state].gas_pressurized_spring_status[HydraulicMechanism]); //DISPLAY GAS PRESSURIZED SPRING STATUS//
}
else if (PilotLever == ( RAISINGEDGE || FALLINGEDGE || UP )) //NESTED IF CONDITION TO CHECK OTHER CONDITION//

{
checking_before_landing(); //REPEAT//
}
}

void lowering_gear() //FUNCTION LOWERING GEAR CALLED//
{
current_state=LWRGEAR; //ASSIGNING CURRENT STATE TO LOWERING GEAR//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nenter the limit switch status,pilot lever position:"); //PRINT THE MESSAGE//
fflush(stdout); //CLEAR CACHE MEMORY//
scanf("%d %d",&limit_switch,&PilotLever); //READS AND STORES THE DATA//
if (limit_switch == CLOSED && PilotLever==RAISINGEDGE) //IF CONDITION TO CHECK THE CONDITIONS//
{
current_state = CHKBFRTKOFF; //IF CONDITION IS TRUE ASSIGN CURRENT STATE TO CHECK BEFORE TAKEOFF//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nlight state: %s\n",state_machine[current_state].light_status); //DISPLAY LIGHT STATE//
}
else if(limit_switch==OPENED && PilotLever==DOWN) //NESTED IF CONDITION TO CHECK OTHER CONDITION//
{
current_state = GEARDOWN; //IF CONDITION IS TRUE ASSIGN CURRENT STATE TO GEAR DOWN//
printf("\ncurrent state: %s\n",state_machine[current_state].current_state_indication); //DISPLAY CURRENT STATE//
printf("\nlight status: %s\n",state_machine[current_state].light_status); //DISPLAY LIGHT STATE//
printf("hydraulic control for landing gear status:%s\n",state_machine[current_state].hydraulic_pump_control_status); //DISPLAY HYDRAULIC PUMP CONTROL STATUS//
}
else //IF CONDITIONS ARE NOT SATISFIED GO TO LOWERING GEAR FUNCTION//
{
lowering_gear(); //REPEAT//
}
}

#ifndef AIR_H_ //INCLUDING HEADER FILES//
#define AIR_H_
#include <stdio.h> //PREPROCESSOR
void init_state_machine(); //FUNCTION DECLARING INITIAL STATE MACHINE//
void gear_down(); //FUNCTION DECLARING GEAR DOWN//
void checking_before_takeoff(); //FUNCTION DECLARING CHECKING BEFORE TAKE OFF//
void raising_gear(); //FUNCTION DECLARING RISING GEAR//
void gear_up(); //FUNCTION DECLARING GEAR UP//
void checking_before_landing(); //FUNCTION DECLARING CHECKING BEFORE LANDING//
void lowering_gear(); //FUNCTION DECLARING LOWERING GEAR//

typedef enum State { //CONDITIONS OF AEROPLANE//
GEARDOWN,
CHKBFRTKOFF,
RSGGEAR,
GEARUP,
CHKBFRLAND,
LWRGEAR
}State_Type;
typedef enum Switch {//CONDITIONS OF LIMIT SWITCH & SQUAT SWITCH//
OPENED,
CLOSED
}SwitchState;
typedef enum PilotLever {//STATUS OF PILOT LEVER//
RAISINGEDGE,
FALLINGEDGE,
UP,
DOWN
}LeverState;
typedef enum HydraulicMechanisam{//HYDRAULIC CONDITIONS//
WORKING,
MALFUNCTIONING
}HydraulicState;
static volatile SwitchState squat_switch; //INPUT VARIABLE FOR CHANGING THE STATES//
static volatile SwitchState limit_switch; //INPUT VARIABLE FOR CHANGING THE STATES//
static volatile LeverState PilotLever; //INPUT VARIABLE FOR CHANGING THE STATES//
static volatile HydraulicState HydraulicMechanism; //INPUT VARIABLE FOR CHANGING THE STATES//

typedef struct //DEFINING STATE TABLE VARIABLES AS TYPEDEF STRUCTURE WITH CHARACTER DATATYPE//
{
char* current_state_indication;
char* light_status;
char* direction_valve_status;
char* hydraulic_pump_control_status;
char* gas_pressurized_spring_status[2];
}State_table;
static State_table state_machine[6] = {
{"gear down","green","down","enabled",{0,0}},
{"checking before take off","green","down","enabled",{0,0}},
{"raising gear","red","up","enabled",{0,0}},
{"gear up","off","null","disabled",{0,0}},
{"checking before landing","red","down","enabled",{0,0}},
{"lowering gear","green","down","enabled",{"disabled","enabled"}
}}; //STATE TABLE ARRAY DECLARATION//
#endif /* AIR_H_ *///END OF HEADER FILE//

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.