1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Moving Aircraft from external apllication

Discussion in 'SimConnect' started by felix81, 14/2/08.

  1. felix81

    felix81

    Joined:
    14/2/08
    Messages:
    8
    Hello,

    I would like to use the FSX as an external view for another dynamic-simulation. This dynamic-simulation is not fsx...

    I tried to understand the proceeding by taking the SetData-example from sdk. So I'm able to set the initial position.
    I know that the values long, lat, alt and heading are writeable. But I haven't found any example how to do that during simulation, so that the aircraft moves during the time.


    Can anybody help, please?

    Thanks in advance
    felix
  2. jimkeir

    jimkeir

    Joined:
    7/6/07
    Messages:
    87
    Country:
    unitedkingdom
    Hi,

    I'm doing something like this by just using 'SetDataOnSimObject' to set lat,long,alt and pitch,bank,heading per-frame. You also need to send the FREEZE_LATITUDE_LONGITUDE_SET, FREEZE_ALTITUDE_SET and FREEZE_ATTITUDE_SET events first to stop the sim controlling the AI.

    Cheers,
    Jim
  3. felix81

    felix81

    Joined:
    14/2/08
    Messages:
    8
    Can you tell me an example, please. I'm not very fit in C (more Beginner)

    Thanks a lot
    felix
  4. jsdratm

    jsdratm

    Joined:
    30/1/08
    Messages:
    11
    I had this exact same problem and here is what you gotta do.

    This part is the initialization that you need.
    Code:
    //Define an ID, name doesn't matter as long as it is consistently used
    static enum DATA_DEFINE_ID 
    {
        DEFINITION1
    };
    
    // Result of calls
    HRESULT hr;
    
    //Connect to Flight Sim
    SimConnect_Open(&hSimConnect, "Interface", NULL, 0, 0, 0);
    
    //Tell flight sim what data you want to set with definition 1
    hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION1, "Plane Altitude", "feet");
    
    Now we can set the variable with one call:
    Code:
    double YourAltitude = 0.0;
    
    hr = SimConnect_SetDataOnSimObject(hSimConnect, DEFINITION1, SIMCONNECT_OBJECT_ID_USER, 0, 0, sizeof(double), &YourAltitude );
    
    To add other variables, add more DEFINITIONS and AddToDataDefinition calls. Or I think you can make a struct containing multiple variables and use a single definition. I was lazy so just made multiple definitions.
  5. jsdratm

    jsdratm

    Joined:
    30/1/08
    Messages:
    11
    Oh yeah, make sure you pause flight simulator too or it will try to simulate itself and fight the external simulator. You can pause flight sim X and pass in data and the aircraft will move around on the screen.
  6. arno

    arno Administrator Staff Member FSDevConf team Resource contributor

    Joined:
    28/5/04
    Messages:
    21,302
    Country:
    netherlands
    If you freeze the position, attitude, etc as mentioned before it is not needed to pause FSX.
  7. jsdratm

    jsdratm

    Joined:
    30/1/08
    Messages:
    11
    arno,

    I didn't know you could do that, but it sounds preferable to having pause flash on the screen. Does freezing the lat/long and altitude also prevent the user from controlling the yaw, pitch, and roll? :scratchch
  8. felix81

    felix81

    Joined:
    14/2/08
    Messages:
    8
    Thanks that helps a lot. If I would like to set many variables toghter, can I use one definition like this?:

    HTML:
    //Tell flight sim what data you want to set with definition 1
    hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION1, "Plane Altitude", "feet");
    hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION1, "Plane Latitude", "degree");
    hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION1, "Plane Longitude", "degree");
    
    Setting the variables like this?

    HTML:
    double YourAltitude = 0.0;
    double YourLatitude = 0.0;
    double YourLongitude = 0.0;
    
    hr = SimConnect_SetDataOnSimObject(hSimConnect, DEFINITION1, SIMCONNECT_OBJECT_ID_USER, 0, 0, sizeof(double), &YourAltitude );
    hr = SimConnect_SetDataOnSimObject(hSimConnect, DEFINITION1, SIMCONNECT_OBJECT_ID_USER, 0, 0, sizeof(double), &YourLatitude );
    hr = SimConnect_SetDataOnSimObject(hSimConnect, DEFINITION1, SIMCONNECT_OBJECT_ID_USER, 0, 0, sizeof(double), &YourLongitude );
    
    
    
    Or is there a way to set all in one step?
  9. Pete Dowson

    Pete Dowson

    Joined:
    25/9/06
    Messages:
    315
    Country:
    unitedkingdom
    Yes, that's good, provided you always write all three together.

    No. By adding them all to one DEFINITION you've told SimConnect it is one data entity with all three values -- i.e. a struct. So you must then write like this:

    HTML:
    struct {
        double YourAltitude;
        double YourLatitude;
        double YourLongitude;
    } YourPosition = { 0.0, 0.0, 0.0 };
    
    hr = SimConnect_SetDataOnSimObject(hSimConnect, DEFINITION1, SIMCONNECT_OBJECT_ID_USER, 0, 0, sizeof(YourPosition), &YourPosition);
    
    Regards

    Pete
  10. felix81

    felix81

    Joined:
    14/2/08
    Messages:
    8
    Ok, setting the variables works.
    But when I tried to send the events FREEZE_LATITUDE_LONGITUDE_SET, FREEZE_ALTITUDE_SET and FREEZE_ATTITUDE_SET, I cannot move the aircraft anymore...

    HTML:
    struct {
    	double YourAltitude;
        double YourLatitude;
        double YourLongitude;
    	double YourPitch;
        double YourBank;
    	double YourHeading;
    } YourPosition = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
    
    
    
    static enum EVENT_ID{
        EVENT_SIM_START,
        EVENT_6,
    	KEY_FREEZE_LATITUDE_LONGITUDE_SET,
    	KEY_FREEZE_ALTITUDE_SET,
    	KEY_FREEZE_ATTITUDE_SET,
    };
    
    
    HTML:
    case EVENT_SIM_START:
                        {
    
    						/* action in callback */
    						hr = SimConnect_TransmitClientEvent(hSimConnect, SIMCONNECT_OBJECT_ID_USER, KEY_FREEZE_LATITUDE_LONGITUDE_SET, TRUE, SIMCONNECT_GROUP_PRIORITY_HIGHEST_MASKABLE, SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY);
    						hr = SimConnect_TransmitClientEvent(hSimConnect, SIMCONNECT_OBJECT_ID_USER, KEY_FREEZE_ALTITUDE_SET, TRUE, SIMCONNECT_GROUP_PRIORITY_HIGHEST_MASKABLE, SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY);
    						hr = SimConnect_TransmitClientEvent(hSimConnect, SIMCONNECT_OBJECT_ID_USER, KEY_FREEZE_ATTITUDE_SET, TRUE, SIMCONNECT_GROUP_PRIORITY_HIGHEST_MASKABLE, SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY);
    
    
                        }
                        break;
    

    HTML:
    
    		/* setup */
    		hr = SimConnect_MapClientEventToSimEvent(hSimConnect, KEY_FREEZE_LATITUDE_LONGITUDE_SET, "FREEZE_LATITUDE_LONGITUDE_SET");
    		hr = SimConnect_MapClientEventToSimEvent(hSimConnect, KEY_FREEZE_ALTITUDE_SET, "FREEZE_ALTITUDE_SET");
    		hr = SimConnect_MapClientEventToSimEvent(hSimConnect, KEY_FREEZE_ATTITUDE_SET, "FREEZE_ATTITUDE_SET");


    HTML:
    void test()
    {
    		HRESULT hr;
    		iterator=iterator+0.00001;
    
    		YourPosition.YourLatitude = 52.27830 + iterator;
    		YourPosition.YourLongitude = 9.08062 + iterator;
    		YourPosition.YourAltitude = 71.0 * 3.28083 + iterator *100.0 ;
    		YourPosition.YourPitch = 0.0;
    		YourPosition.YourBank = 0.0;
    		YourPosition.YourHeading = 0.0;
    
    
    
    		printf ("Lat: %lf, Lon: %lf, Alt: %lf \n", YourPosition.YourLatitude, YourPosition.YourLongitude, YourPosition.YourAltitude);
    		hr = SimConnect_SetDataOnSimObject(hSimConnect, DEFINITION_2, SIMCONNECT_OBJECT_ID_USER, 0, 0, sizeof(YourPosition), &YourPosition);
    		
    }

    If I don't freeze by plugin but pausing the simultaor, I can move the aircraft...

    Anybody an idea what I'm doing wrong?

    Thanks a lot
    felix
    Last edited: 15/2/08
  11. jsdratm

    jsdratm

    Joined:
    30/1/08
    Messages:
    11
    I've never tried the freezing, maybe you do have to unfreeze, change the variables, and freeze again? :confused:
  12. felix81

    felix81

    Joined:
    14/2/08
    Messages:
    8
    Big Thanks to all help so far!

    I tried out that, but it doesn't help :confused: Seems that setting the variables does however not work. Apparently fsx moves the aircraft by its own and not trough setting by my variables.
    Last edited: 15/2/08
  13. B21

    B21

    Joined:
    3/12/07
    Messages:
    149
    Country:
    unitedkingdom
    My working code is very similar to yours, but in case it's important here's the minor differences:
    Code:
    hr = SimConnect_TransmitClientEvent(hSimConnect,
    	probe_id[i], // I'm setting an AI object, not user a/c
    	EVENT_FREEZE_ATTITUDE,
    	1, // set freeze value to 1
    	SIMCONNECT_GROUP_PRIORITY_HIGHEST,
    	SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY);
    
    B21

    p.s. you *don't* freeze/unfreeze for each move. Just freeze once.
    Last edited: 18/2/08
  14. felix81

    felix81

    Joined:
    14/2/08
    Messages:
    8
    Hi B21,

    the AI-Aircraft has to be defined somewhere... How to do that???


    I have no idea, why my sample does not work. Therefore I post the complete code here. Maybe there is a little mistake and someone can try this on his machine, please?

    Many thanks!!!


    HTML:
    //------------------------------------------------------------------------------
    //
    //  SimConnect Set Data 
    // 
    //	Description:
    //				When sim is startet, Lat Lon Alt & Attitude are freezed. 
    //				The Aircraft should be moved by test-function
    //			
    //------------------------------------------------------------------------------
    
    #include <windows.h>
    #include <tchar.h>
    #include <stdio.h>
    #include <strsafe.h>
    
    #include "SimConnect.h"
    //#include "mico-ipc.h"
    //#include "CAircraftInterface.h" // Read ACI
    //#include "constants.h"
    
    int 	aciError = 0;
    bool 	firsttime=true;
    
    //aci
    //CAircraftInterface aircraftInterfaceReader;
    
    
    
    //void process_data();
    void test();
    
    int     quit = 0;
    double	iterator = 0;
    
    HANDLE  hSimConnect = NULL;
    
    
    struct {
    	double YourAltitude;
        double YourLatitude;
        double YourLongitude;
    	double YourPitch;
        double YourBank;
    	double YourHeading;
    } YourPosition = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
    
    
    
    static enum EVENT_ID{
    	KEY_FREEZE_LATITUDE_LONGITUDE_SET,
    	KEY_FREEZE_ALTITUDE_SET,
    	KEY_FREEZE_ATTITUDE_SET,
    	EVENT_SIM_START,
    };
    
    static enum DATA_DEFINE_ID {
    	DEFINITION_1,
    };
    
    static enum DATA_REQUEST_ID {
    	REQUEST_1,
        REQUEST_6,
    };
    
    
    
    void CALLBACK MyDispatchProcSD(SIMCONNECT_RECV* pData, DWORD cbData, void *pContext)
    {
     HRESULT hr;
        
        switch(pData->dwID)
        {
            case SIMCONNECT_RECV_ID_EVENT:
            {
                SIMCONNECT_RECV_EVENT *evt = (SIMCONNECT_RECV_EVENT*)pData;
    
                switch(evt->uEventID)
                {
    
    				case EVENT_SIM_START:
                        {
    						printf ("\n Send first events \n");
    						
                            // Turn the ctrl-shift-u input event on now
                            //hr = SimConnect_SetInputGroupState(hSimConnect, INPUT_6, SIMCONNECT_STATE_ON);
    
    						/* action in callback */
    						hr = SimConnect_TransmitClientEvent(hSimConnect, SIMCONNECT_OBJECT_ID_USER, KEY_FREEZE_LATITUDE_LONGITUDE_SET, 1, SIMCONNECT_GROUP_PRIORITY_HIGHEST, SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY);
    						hr = SimConnect_TransmitClientEvent(hSimConnect, SIMCONNECT_OBJECT_ID_USER, KEY_FREEZE_ALTITUDE_SET, 1, SIMCONNECT_GROUP_PRIORITY_HIGHEST, SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY);
    						hr = SimConnect_TransmitClientEvent(hSimConnect, SIMCONNECT_OBJECT_ID_USER, KEY_FREEZE_ATTITUDE_SET, 1, SIMCONNECT_GROUP_PRIORITY_HIGHEST, SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY);
    
    
    						//hr = SimConnect_TransmitClientEvent(hSimConnect, SIMCONNECT_OBJECT_ID_USER, EVENT_FREEZE_ATTITUDE, 1, SIMCONNECT_GROUP_PRIORITY_HIGHEST, SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY);
    
                        }
                        break;
    
    
                    default:
                        break;
                }
                break;
            }
    
            case SIMCONNECT_RECV_ID_QUIT:
            {
                quit = 1;
                break;
            }
    
            default:
    			break;
        }
    
    
    
    		
    }
    
    
    
    void testDataSet()
    {
        HRESULT hr;
    
        if (SUCCEEDED(SimConnect_Open(&hSimConnect, "Interface", NULL, 0, 0, 0)))
        {
    		printf("\nConnected to Flight Simulator!");   
            
    		
    		//Tell flight sim what data you want to set with definition 1
    		hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "Plane Altitude", "feet");
    		hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "Plane Latitude", "degree");
    		hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "Plane Longitude", "degree");
    		hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "Plane Pitch", "radians");
    		hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "Plane Bank", "radians");
    		hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "Plane Heading", "radians");
    
    		/* setup */
    		hr = SimConnect_MapClientEventToSimEvent(hSimConnect, KEY_FREEZE_LATITUDE_LONGITUDE_SET, "FREEZE_LATITUDE_LONGITUDE_SET");
    		hr = SimConnect_MapClientEventToSimEvent(hSimConnect, KEY_FREEZE_ALTITUDE_SET, "FREEZE_ALTITUDE_SET");
    		hr = SimConnect_MapClientEventToSimEvent(hSimConnect, KEY_FREEZE_ATTITUDE_SET, "FREEZE_ATTITUDE_SET");
    
    		 // Request a simulation start event
            hr = SimConnect_SubscribeToSystemEvent(hSimConnect, EVENT_SIM_START, "SimStart");
    
    		
    //		hr = SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP_KEYS, EVENT_FREEZE_LATITUDE_LONGITUDE_TOGGLE);
    
    
    		
            while( 0 == quit )
            {
                SimConnect_CallDispatch(hSimConnect, MyDispatchProcSD, NULL);
    		
    			// Now the sim is running, request information on the user aircraft
    			//hr = SimConnect_RequestDataOnSimObjectType(hSimConnect, REQUEST_1, DEFINITION_3, 0, SIMCONNECT_SIMOBJECT_TYPE_USER);
    			
    			//process_data();
    			test();
                Sleep(1);
            } 
    
            hr = SimConnect_Close(hSimConnect);
        }
    }
    
    
    
    
    void test()
    {
    		HRESULT hr;
    		iterator=iterator+0.0000001;
    
    		YourPosition.YourAltitude = 200.0 * 3.28083 + iterator *100.0 ;
    		YourPosition.YourLatitude = 52.24630 + iterator;
    		YourPosition.YourLongitude = 9.02662 + iterator;
    		YourPosition.YourPitch = 0.0;
    		YourPosition.YourBank = 0.0;
    		YourPosition.YourHeading = 0.0;
    
    
    
    		printf ("Lat: %lf, Lon: %lf, Alt: %lf \n", YourPosition.YourLatitude, YourPosition.YourLongitude, YourPosition.YourAltitude);
    		hr = SimConnect_SetDataOnSimObject(hSimConnect, DEFINITION_1, SIMCONNECT_OBJECT_ID_USER, 0, 0, sizeof(YourPosition), &YourPosition);
    		
    }
    
    int __cdecl _tmain(int argc, _TCHAR* argv[])
    {
    
        testDataSet();
    
    	return 0;
    }
    
    
    
    
    
    
    
  15. ronh

    ronh

    Joined:
    2/11/06
    Messages:
    397
    Country:
    canada
    Felix,

    I have played with your code. A learning experience for me. I have it moving, however the altitude part does not work. Seems you need to ensure you freeze and unfreeze.

    HTH

    EDIT ****************

    no not really working other than I have it freezing and unfreezing. The data does not get loaded into the aircraft position/altitude. I can still control the plane with the joystick.

    end EDIT ******************

    EDIT 2 **********************

    okay now it works. freeze and un freeze not the issue. The datadef needs to be broken out for each value - alt, lat lon. You cannot set data with a structure or single definition.

    end edit 2 ********************

    Attached Files:

    Last edited: 20/2/08
  16. Daniel

    Daniel

    Joined:
    21/12/06
    Messages:
    139
    Country:
    netherlands
    There is one thing to keep in mind regarding the Freezing: animation will continue.

    For example: a car moving with rotating wheels based upon the wheel angle variable will stay put when frozen but the wheels will keep turning.

    Daniel
  17. felix81

    felix81

    Joined:
    14/2/08
    Messages:
    8
    Hello Ron,

    great, setting Lat, Lon & Alt works now! Many thanks for your help!!!

    What I do not get to work is setting Pitch, Roll & Heading although I freezed the ATTITUDE... Does this work at your Simulation?

    I tried to set the values with
    HTML:
    YourPosition.YourAltitude = sAircraftInterface.sFm.sPos.alt_wgs84_m * 3.28083;	
    YourPosition.YourLatitude = sAircraftInterface.sFm.sPos.lat_wgs84;
    YourPosition.YourLongitude = sAircraftInterface.sFm.sPos.lon_wgs84;
    YourPosition.YourPitch = sAircraftInterface.sFm.sAtt.theta_rad;
    YourPosition.YourBank = sAircraftInterface.sFm.sAtt.phi_rad;
    YourPosition.YourHeading = sAircraftInterface.sFm.sAtt.psi_rad_trueN;
    
    printf ("Lat: %lf, Lon: %lf, Alt: %lf \, Pitch: %lf, Bank: %lf, %Heading: %lf \n", YourPosition.YourLatitude, YourPosition.YourLongitude, YourPosition.YourAltitude, YourPosition.YourPitch, YourPosition.YourBank, YourPosition.YourHeading);
    
    		hr = SimConnect_SetDataOnSimObject(hSimConnect, DEFINITION_1, SIMCONNECT_OBJECT_ID_USER, 0, 0, sizeof(YourPosition.YourAltitude), &YourPosition.YourAltitude);
    		hr = SimConnect_SetDataOnSimObject(hSimConnect, DEFINITION_2, SIMCONNECT_OBJECT_ID_USER, 0, 0, sizeof(YourPosition.YourLatitude), &YourPosition.YourLatitude);
    		hr = SimConnect_SetDataOnSimObject(hSimConnect, DEFINITION_3, SIMCONNECT_OBJECT_ID_USER, 0, 0, sizeof(YourPosition.YourLongitude), &YourPosition.YourLongitude);
    		hr = SimConnect_SetDataOnSimObject(hSimConnect, DEFINITION_4, SIMCONNECT_OBJECT_ID_USER, 0, 0, sizeof(YourPosition.YourPitch), &YourPosition.YourPitch);
    		hr = SimConnect_SetDataOnSimObject(hSimConnect, DEFINITION_5, SIMCONNECT_OBJECT_ID_USER, 0, 0, sizeof(YourPosition.YourBank), &YourPosition.YourBank);
    		hr = SimConnect_SetDataOnSimObject(hSimConnect, DEFINITION_6, SIMCONNECT_OBJECT_ID_USER, 0, 0, sizeof(YourPosition.YourHeading), &YourPosition.YourHeading);
    		

    @Daniel
    Thx, the animations (cars and so on) are no problem..

    Cheers
    felix
  18. ronh

    ronh

    Joined:
    2/11/06
    Messages:
    397
    Country:
    canada
    Felix,

    Your previous code has incorrect data in the addtodatadef.

    "Plane Pitch" should be "Plane Pitch degrees"

    "Plane Bank" should be "Plane Bank Degrees"

    "Plane Heading" should be "Plane Heading degrees magnetic"

    I am wondering now if the single datadef part did not work due to you having incorrect data. Try going back to a single def and see if it works.

    HTH

    I am doing this from memory - I am not at my SDK computer - please review the SDK

    Edit *************************

    my earlier statement of separating out the defs is incorrect - I have confirmed you only need one def not 6

    end edit ********************************
    Last edited: 21/2/08
  19. felix81

    felix81

    Joined:
    14/2/08
    Messages:
    8

    Hello Ron,

    that was exactly my fault - I had wrong Datarefs. What a shame, I have copy-pasted from dataref although later written wrong in code...

    It works now with single data-definition :)

    Many thanks for help

    *cheers*
    felix
  20. Marky2245

    Marky2245

    Joined:
    4/2/08
    Messages:
    8
    Country:
    us-newjersey
    This is interesting

    I have an external sim that sends lat, long etc to FS2004,
    but I have it set up in slew mode.
    I can run FSX, but it is very slow. :confused:

Share This Page