Previous Thread
Next Thread
Print Thread
Rate This Thread
Hop To
#4229042 - 02/14/16 02:27 PM Center an axis with autoscroll  
Joined: Feb 2016
Posts: 2
Crysli Offline
Junior Member
Crysli  Offline
Junior Member

Joined: Feb 2016
Posts: 2
Italy
Hello, I have this code for scroll the DX_SLIDER_AXIS in opposite direction when the THR_FC has a positive or negative value and the APENG button is pressed.
But this way the axis continues to scroll, when I need to stop it at the center.
Any ideas?

Code:
include "target.tmh"

int CenterAxis()
		{
		if (Throttle[THR_FC] < 0)
		ActKey(KEYON+AXIS(DX_SLIDER_AXIS, 5, 1));
        
        else if (Throttle[THR_FC] > 0)
        ActKey(KEYON+AXIS(DX_SLIDER_AXIS, -5, 1));
		}
		
int main()
{
	
	if(Init(&EventHandle)) return 1;
    SetKBRate(25, 33);
	SetKBLayout(KB_ENG);
	
	MapAxis(&Throttle, THR_FC, DX_SLIDER_AXIS);
	MapKey(&Throttle, APENG, KEYON+PULSE+EXEC("CenterAxis();"));
}
//event handler
int EventHandle(int type, alias o, int x)
{
    DefaultMapping(&o, x);
	//add event handling code here
}

Inline advert (2nd and 3rd post)

#4229567 - 02/16/16 01:39 AM Re: Center an axis with autoscroll [Re: Crysli]  
Joined: Mar 2007
Posts: 483
Aullido Offline
Member
Aullido  Offline
Member

Joined: Mar 2007
Posts: 483
Give a try to this code:

Code:

  int
   CenterAxis()
    {
     // While APENG is pressed.

//   if (Axis[DX_SLIDER_AXIS].pos > 0)
//    DXAxis(DX_SLIDER_AXIS,Axis[DX_SLIDER_AXIS].pos - 1);
//		  
//   if (Axis[DX_SLIDER_AXIS].pos < 0)
//    DXAxis(DX_SLIDER_AXIS,Axis[DX_SLIDER_AXIS].pos + 1);

     // One single APENG press.
		 
     if (Axis[DX_SLIDER_AXIS].pos < 0)
      {         
       do
        {
         DXAxis(DX_SLIDER_AXIS,Axis[DX_SLIDER_AXIS].pos + 1);
        }
       while (Axis[DX_SLIDER_AXIS].pos < 0); 
      }
		  
     if (Axis[DX_SLIDER_AXIS].pos > 0)
      {		 
       do
        {
	 DXAxis(DX_SLIDER_AXIS,Axis[DX_SLIDER_AXIS].pos - 1);
        }
       while (Axis[DX_SLIDER_AXIS].pos > 0);
      } 
		  
     // Only one, without returning.

//   DXAxis(DX_SLIDER_AXIS, 0);
		
  }


#4231496 - 02/20/16 04:51 PM Re: Center an axis with autoscroll [Re: Crysli]  
Joined: Feb 2016
Posts: 2
Crysli Offline
Junior Member
Crysli  Offline
Junior Member

Joined: Feb 2016
Posts: 2
Italy
Thanks for the code Aullido.
In your do-while cycle you have to invert the while < > for it to work.
Anyhow I need a REXEC to execute it.
After many tries I ended up with this code:

Code:
include "target.tmh"

int APENGcount;

int AxisOut()
    {

    // When axis position is negative

    if (Axis[DX_SLIDER_AXIS].pos <= -10000)
    	{
	DXAxis(DX_SLIDER_AXIS,Axis[DX_SLIDER_AXIS].pos + 30);
    }

    if (Axis[DX_SLIDER_AXIS].pos > -10000 & Axis[DX_SLIDER_AXIS].pos <= -6000)
	{
	DXAxis(DX_SLIDER_AXIS,Axis[DX_SLIDER_AXIS].pos + 20);
    }

    if (Axis[DX_SLIDER_AXIS].pos > -6000 & Axis[DX_SLIDER_AXIS].pos <= -3000)
	{
	DXAxis(DX_SLIDER_AXIS,Axis[DX_SLIDER_AXIS].pos + 12);
    }
	
    if (Axis[DX_SLIDER_AXIS].pos > -3000 & Axis[DX_SLIDER_AXIS].pos <= -200)
	{
	DXAxis(DX_SLIDER_AXIS,Axis[DX_SLIDER_AXIS].pos + 6);
    }

    if (Axis[DX_SLIDER_AXIS].pos > -200 & Axis[DX_SLIDER_AXIS].pos < 0)
	{
	DXAxis(DX_SLIDER_AXIS,Axis[DX_SLIDER_AXIS].pos + 1);
    }

    // When axis position is positive

    if (Axis[DX_SLIDER_AXIS].pos >= 10000)
	{
	DXAxis(DX_SLIDER_AXIS,Axis[DX_SLIDER_AXIS].pos - 30);
    }

    if (Axis[DX_SLIDER_AXIS].pos < 10000 & Axis[DX_SLIDER_AXIS].pos >= 6000)
	{
	DXAxis(DX_SLIDER_AXIS,Axis[DX_SLIDER_AXIS].pos - 20);
    }

    if (Axis[DX_SLIDER_AXIS].pos < 6000 & Axis[DX_SLIDER_AXIS].pos >= 3000)
	{
	DXAxis(DX_SLIDER_AXIS,Axis[DX_SLIDER_AXIS].pos - 12);
    }

    if (Axis[DX_SLIDER_AXIS].pos < 3000 & Axis[DX_SLIDER_AXIS].pos >= 200)
	{
	DXAxis(DX_SLIDER_AXIS,Axis[DX_SLIDER_AXIS].pos - 6);
    }

    if (Axis[DX_SLIDER_AXIS].pos < 200 & Axis[DX_SLIDER_AXIS].pos > 0)
	{
	DXAxis(DX_SLIDER_AXIS,Axis[DX_SLIDER_AXIS].pos - 1);
    }
}

int AxisZero() // When axis recenter is done
    {
    StopAutoRepeat(1);
    StopAutoRepeat(2);
    APENGcount = 0;
}

int CenterAxis()
    {
    if (Axis[DX_SLIDER_AXIS].pos < 0 & APENGcount < 1 | Axis[DX_SLIDER_AXIS].pos > 0 & APENGcount < 1) // Execute only if APENGcount < 1 to avoid multiple press of the button
	{
	LockAxis(&Throttle, THR_FC, 1); // Lock the THR_FC lever from change the axis position while auto-centering
	ActKey(CHAIN(
	KEYON+REXEC(1, 1, "if (Axis[DX_SLIDER_AXIS].pos != 0) AxisOut(); else AxisZero();",RNOSTOP),
		
        D(1), // Need delay to ensure the REXEC below to execute last
		
	KEYON+REXEC(2, 1, "if (Axis[DX_SLIDER_AXIS].pos == 0 & Throttle[THR_FC] == 0) LockAxis(&Throttle, THR_FC, 0);",RNOSTOP))); // If THR_FC lever is centered while the axis is scrolling,
		                                                                                                                   // regain control when the axis reach zero
		                                                                                                                   // (otherwise THR_FC lever need to be centered 2 times)
		
	//KEYON+REXEC(2, 1, "if (Axis[DX_SLIDER_AXIS].pos == 0 & Throttle[THR_FC] > -256 & Throttle[THR_FC] < 256) LockAxis(&Throttle, THR_FC, 0);",RNOSTOP))); // Alternative line with THR_FC center tolerance

    }     	
}       	

int main()
    {
    if(Init(&EventHandle)) return 1;

    MapAxis(&Throttle, THR_FC, DX_SLIDER_AXIS); // Map axis to THR_FC

    MapKey(&Throttle, APENG, CHAIN(
				EXEC("CenterAxis();"),
				EXEC("if (Throttle[APENG] & Axis[DX_SLIDER_AXIS].pos < 0 | Throttle[APENG] & Axis[DX_SLIDER_AXIS].pos > 0) APENGcount = 1;"))); // Set APENGcount = 1
}

int EventHandle(int type, alias o, int x)
{
    DefaultMapping(&o, x);

    if (Axis[DX_SLIDER_AXIS].pos == 0 & Throttle[THR_FC] == 0) LockAxis(&Throttle, THR_FC, 0); // After the axis has been auto-centered, THR_FC need to be centered to regain control of the axis
}

Last edited by Crysli; 02/20/16 09:02 PM.

Moderated by  RacerGT 

Quick Search
Recent Articles
Support SimHQ

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


Recent Topics
Exodus
by RedOneAlpha. 04/18/24 05:46 PM
Grumman Wildcat unique landing gear
by Coot. 04/17/24 03:54 PM
Peter Higgs was 94
by Rick_Rawlings. 04/17/24 12:28 AM
Whitey Herzog was 92
by F4UDash4. 04/16/24 04:41 PM
Anyone can tell me what this is?
by NoFlyBoy. 04/16/24 04:10 PM
10 Years ago MV Sewol
by wormfood. 04/15/24 08:25 PM
Pride Of Jenni race win
by NoFlyBoy. 04/15/24 12:22 AM
It's Friday: grown up humor for the weekend.
by NoFlyBoy. 04/12/24 01:41 PM
OJ Simpson Dead at 76
by bones. 04/11/24 03:02 PM
They wokefied tomb raider !!
by Blade_RJ. 04/10/24 03:09 PM
Copyright 1997-2016, SimHQ Inc. All Rights Reserved.

Powered by UBB.threads™ PHP Forum Software 7.6.0