Previous Thread
Next Thread
Print Thread
Rate This Thread
Hop To
#3948821 - 05/04/14 05:59 AM Looking to create a timer for Airbrake indications  
Joined: Jan 2001
Posts: 2,477
HomeFries Offline
Air Dominance Project
HomeFries  Offline
Air Dominance Project
Member

Joined: Jan 2001
Posts: 2,477
I'm trying to get fancy with the airbrake routine in my DCS profile. The functionality I want is that the LMFD LED1 will remain lit when the airbrake is fully extended, and will be unlit when the airbrake is fully closed. If the airbrake is in a transitional state (or deployed in a partial state) the LED should flash.

What I really need is a counter function that will work with DeferCall and a while loop. Here is what I have so far (two approaches, neither of which work at the moment)

note: AirBrakeExtended is a global variable of the amount of airbrake extension in percentage (necessary since deployment and retraction often have different durations requiring a percentage of deployment). BrakeOnCycleTime/BrakeOffCycleTime are the times it takes to fully deploy and retract the brakes. "count" is in each of the functions to convert the physical percentage of deployment into a time to complete deployment or retraction.
Code:
int AnalogBrakeOutLED(int dtime, int led, int posit=0)		//dtime is time to fully deploy the brake, led is whether flashing is allowed, and posit is the percent of brake deployment
{
int count;
count = dtime*posit/100;
printf("dtime = %i, count = %i\xa",dtime,count);

while(count < dtime)
	{
	if (HCougar[T9]) DeferCall(count, &EXEC, "count = count+500;");
	else AirBrakeExtended = (count*100)/dtime;
	}
if (count >= dtime)
	{
	AirBrakeExtended = 100;
	if (led >0) Lights_LLED1(1);
	}
}


int AnalogBrakeInLED(int dtime, int posit=100)  //dtime is the time to fully retract the brakes, posit is the deployment position in percent
{
int count;
count = dtime*posit/100;
printf("dtime = %i, count = %i\xa",dtime,count);

while (count > dtime)
	{
	DeferCall(500, &EXEC, "if (!HCougar[T10]) AirBrakeExtended = (count*100)/dtime;");
	count = count - 500;
	//printf("%i > %i\xa",count,dtime);
	}
if (count <= 0)
	{
	AirBrakeExtended = 0;
	Lights_LLED1(0);
	}	
}


And the macros that call the functions:
Code:
	Analog_Pulse_AirbrakeOn = 	CHAIN(PULSE+DX27, 	EXEC("if (AirBrakeExtended == 0) flashLLED1(1,500);AnalogBrakeOutLED(BrakeOnCycleTime, 1, AirBrakeExtended);"));		
	Analog_Pulse_AirbrakeOff = 	CHAIN(PULSE+DX28, 	EXEC("if (AirBrakeExtended > 0) flashLLED1(1,500);AnalogBrakeInLED(BrakeOffCycleTime, AirBrakeExtended);"));


Thanks in advance for any help,


-Home Fries

"Pacifism is a shifty doctrine under which a man accepts the benefits of the social group without being willing to pay - and claims a halo for his dishonesty."
- Robert A. Heinlein

The average naval aviator, despite the sometimes swaggering exterior, is very much capable of such feelings as love, affection, intimacy, and caring. These feelings just don't involve anyone else.

Inline advert (2nd and 3rd post)

#3952849 - 05/13/14 04:34 AM Re: Looking to create a timer for Airbrake indications [Re: HomeFries]  
Joined: Jan 2001
Posts: 2,477
HomeFries Offline
Air Dominance Project
HomeFries  Offline
Air Dominance Project
Member

Joined: Jan 2001
Posts: 2,477
I have since tried moving the counter to a separate function, and leaving DeferCall in the main function to create a timer. Unfortunately, I still get caught in an infinite loop that can eat up close to 1.5Gb of RAM.

Here's the code for deploying the brake (just to keep it simple):
Code:
Analog_Pulse_AirbrakeOn = 	CHAIN(PULSE+DX27, 	EXEC("AnalogBrakeOutLED(BrakeOnCycleTime, 1, AirBrakeExtended);"));

int AnalogBrakeOutLED(int dtime, int led, int posit=0)		
//dtime is time to fully deploy the brake,
//led is whether flashing is allowed, 
//and posit is the percentage of brake deployment (indicating time to go before full deployment)
{
int count;
count = dtime*posit/100;
printf("dtime = %i, count = %i\xa",dtime,count);	//for test

do
	{
	if (HCougar[T9])
		{
		DeferCall(count, &Speedbrake_Counter, count);
		printf("count = %i\xa",count);		//for test
		}
	else
		{
		AirBrakeExtended = (count*100)/dtime;
		printf("%i < %i\xa",count,dtime);	//for test
		}
	}
while(count < dtime);

if (count >= dtime)
	{
	AirBrakeExtended = 100;
	if (led >0)
		{
		Lights_LLED1(1);
		}
	}
}

int Speedbrake_Counter(int start, int incr=500)
{
start = start+incr;
printf("start = %i\xa",start);
return start;
}


EDIT: I also tried nesting the do loop within the if/then to see if that would do anything. Same results.
Code:
int AnalogBrakeOutLED(int dtime, int led, int posit=0)	
{
int count;
count = dtime*posit/100;
printf("dtime = %i, count = %i\xa",dtime,count);	//for test

if (HCougar[T9])
	{
	do
		{
		DeferCall(count, &Speedbrake_Counter, count);
		printf("count = %i\xa",count);	//for test
		}
	while(count < dtime);
	}
else
	{
	AirBrakeExtended = (count*100)/dtime;
	printf("%i < %i\xa",count,dtime);	//for test
	}


if (count >= dtime)
	{
	AirBrakeExtended = 100;
	if (led >0)
		{
		Lights_LLED1(1);
		}
	}
}


Again, any assistance is greatly appreciated.

Last edited by HomeFries; 05/13/14 04:45 AM.

-Home Fries

"Pacifism is a shifty doctrine under which a man accepts the benefits of the social group without being willing to pay - and claims a halo for his dishonesty."
- Robert A. Heinlein

The average naval aviator, despite the sometimes swaggering exterior, is very much capable of such feelings as love, affection, intimacy, and caring. These feelings just don't involve anyone else.

#3953361 - 05/14/14 10:35 AM Re: Looking to create a timer for Airbrake indications [Re: HomeFries]  
Joined: Oct 2010
Posts: 83
Nicu Offline
Junior Member
Nicu  Offline
Junior Member

Joined: Oct 2010
Posts: 83
Code:
define AirBrakePoll            500      // ms
define AirBrakeInc 10	                // percent per second
int Analog_Pulse_AirbrakeOn;
int Analog_Pulse_AirbrakeOff;
int AirBrakePos;
int AirBrakeOn;


int main()
{
    if(Init(&EventHandle)) return 1; // declare the event handler, return on error
    AutoRepeat(0, AirBrakePoll, &AirBrakeProc, 0);
    Analog_Pulse_AirbrakeOn = 	CHAIN(PULSE+DX27, 	EXEC("AirBrakeOn = 1;"));		
    Analog_Pulse_AirbrakeOff = 	CHAIN(PULSE+DX28, 	EXEC("AirBrakeOn = 0;"));
}

int LEDState;
int AirBrakeProc(int p)
{
	if(AirBrakeOn) AirBrakePos = AirBrakePos + AirBrakeInc * AirBrakePoll / 1000;
	else AirBrakePos = AirBrakePos - AirBrakeInc * AirBrakePoll / 1000;
	
	if(AirBrakePos >= 100) 
	{
		AirBrakePos = 100;
		LEDState = 1;
	}
	else if(AirBrakePos <= 0) 
	{
		AirBrakePos = 0;
		LEDState = 0;
	}
	else LEDState = !LEDState;
	
	Lights_LLED1(LEDState);
}

#3953914 - 05/15/14 03:10 PM Re: Looking to create a timer for Airbrake indications [Re: HomeFries]  
Joined: Jan 2001
Posts: 2,477
HomeFries Offline
Air Dominance Project
HomeFries  Offline
Air Dominance Project
Member

Joined: Jan 2001
Posts: 2,477
Wow, Nicu! This solution is both elegant and simple.

I've tried googling "AutoRepeat" and I've had no luck finding the command parameters, though it appears like a more fundamental version of REXEC. This is definitely another tool I'll add to my tool bag.

I've gone ahead and made a few changes, in particular going from AirBrakeOn (renamed AirBrakeSwitch) being Boolean to being essentially an operand (and a zero value will keep AirBrakePos in its current position). This allows the LED to flash while the brake is in a fixed state inbetween fully extended and fully retracted.

BrakeOnCycleTime/BrakeOffCycleTime allows for brakes with different extension/retraction times (e.g. the A-10C extends in 500ms and retracts in 2000ms, while the F-15C extends in 2000ms and retracts in 5000ms).

Analog_Pulse_AirbrakeOn/Off (and Analog_AirbrakeOn/Off) set AirBrakeSwitch to 1 or -1 respectively, and Analog_Pulse_AirbrakeOn/OffStop has been added to set "AirBrakeSwitch = 0" while still pulsing DX27/28 as required for the F-15C airbrake. Analog_AirbrakeStop (just setting "AirBrakeSwitch = 0") is useful for the A-10C.
Code:


    Analog_AirbrakeOn 			= 	CHAIN(DX27, 		EXEC("AirBrakeSwitch = 1;"));		
    Analog_AirbrakeOff 			= 	CHAIN(DX28, 		EXEC("AirBrakeSwitch =-1;"));
    Analog_AirbrakeStop			=				EXEC("AirBrakeSwitch = 0;");
    Analog_Pulse_AirbrakeOn 		= 	CHAIN(PULSE+DX27, 	EXEC("AirBrakeSwitch = 1;"));		
    Analog_Pulse_AirbrakeOff 		= 	CHAIN(PULSE+DX28, 	EXEC("AirBrakeSwitch =-1;"));
    Analog_Pulse_AirbrakeOnStop		= 	CHAIN(PULSE+DX27, 	EXEC("AirBrakeSwitch = 0;"));		
    Analog_Pulse_AirbrakeOffStop 	= 	CHAIN(PULSE+DX28, 	EXEC("AirBrakeSwitch = 0;"));

int AirBrakeProc(int p)
{
//	100%*1000(ms) / cycle time (ms)} * poll (ms)/1000(ms) = % per poll time, AirBrakeOn determines operand (-1 for retract)
//	1000ms on front end numerator/back end denominator keeps value as integer

	if	(AirBrakeSwitch > 0)	AirBrakePos = AirBrakePos + 100000/BrakeOnCycleTime  * AirBrakePoll/1000;
	else if (AirBrakeSwitch < 0) 	AirBrakePos = AirBrakePos - 100000/BrakeOffCycleTime * AirBrakePoll/1000;
	
	if(AirBrakePos >= 100) 
	{
		AirBrakePos = 100;
		AirbrakeLEDState = 1;
	}
	else if(AirBrakePos <= 0) 
	{
		AirBrakePos = 0;
		AirbrakeLEDState = 0;
	}
	else AirbrakeLEDState = !AirbrakeLEDState;
	
	Lights_LLED1(AirbrakeLEDState);
}


You've been given credit for the routine in my code, and you'll be given credit in the next iteration of the manual as well.

Best Regards,
HF

Last edited by HomeFries; 05/15/14 03:58 PM. Reason: updated code

-Home Fries

"Pacifism is a shifty doctrine under which a man accepts the benefits of the social group without being willing to pay - and claims a halo for his dishonesty."
- Robert A. Heinlein

The average naval aviator, despite the sometimes swaggering exterior, is very much capable of such feelings as love, affection, intimacy, and caring. These feelings just don't involve anyone else.


Moderated by  RacerGT 

Quick Search
Recent Articles
Support SimHQ

If you shop on Amazon use this Amazon link to support SimHQ
.
Social


Recent Topics
CD WOFF
by Britisheh. 03/28/24 08:05 PM
Carnival Cruise Ship Fire....... Again
by F4UDash4. 03/26/24 05:58 PM
Baltimore Bridge Collapse
by F4UDash4. 03/26/24 05:51 PM
The Oldest WWII Veterans
by F4UDash4. 03/24/24 09:21 PM
They got fired after this.
by Wigean. 03/20/24 08:19 PM
Grown ups joke time
by NoFlyBoy. 03/18/24 10:34 PM
Anyone Heard from Nimits?
by F4UDash4. 03/18/24 10:01 PM
RIP Gemini/Apollo astronaut Tom Stafford
by semmern. 03/18/24 02:14 PM
Copyright 1997-2016, SimHQ Inc. All Rights Reserved.

Powered by UBB.threads™ PHP Forum Software 7.6.0