File: main.cpp

package info (click to toggle)
flightgear 3.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 39,572 kB
  • sloc: cpp: 184,963; ansic: 158,755; sh: 9,807; perl: 4,475; python: 2,218; asm: 642; java: 314; makefile: 289; ruby: 120; xml: 85
file content (143 lines) | stat: -rw-r--r-- 2,494 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <windows.h>
#include <time.h>
#include <iostream>
using namespace std;

#include <net_fdm.hxx>

double htond (double x)	
{
    int * p = (int*)&x;
    int tmp = p[0];
    p[0] = htonl(p[1]);
    p[1] = htonl(tmp);

    return x;
}

float htonf (float x)	
{
    int * p = (int *)&x;
    *p = htonl(*p);
    return x;
}

SOCKET sendSocket = -1;
struct sockaddr_in sendAddr;

// IP and port where FG is listening
char * fg_ip = "127.0.0.1";
int fg_port = 5500;

// update period.  controls how often updates are
// sent to FG.  in seconds.
int update_period = 1000;

void run();

int main(int argc, char ** argv)
{
    WSAData wd;
    if (WSAStartup(MAKEWORD(2,0),&wd) == 0)
    {
        memset(&sendAddr,0,sizeof(sendAddr));
        sendAddr.sin_family = AF_INET;
        sendAddr.sin_port = htons(fg_port);
        sendAddr.sin_addr.S_un.S_addr = inet_addr(fg_ip);

        sendSocket = socket(AF_INET,SOCK_DGRAM,0);
        if (sendSocket != INVALID_SOCKET)
        {
            run();
        }
        else
        {
            cout << "socket() failed" << endl;
        }
    }
    else
    {
        cout << "WSAStartup() failed" << endl;
    }

    return 0;
}

#define D2R (3.14159 / 180.0)

void run()
{
    double latitude = 45.59823; // degs
    double longitude = -120.69202; // degs
    double altitude = 150.0; // meters above sea level
 
    float roll = 0.0; // degs
    float pitch = 0.0; // degs
    float yaw = 0.0; // degs

    float visibility = 5000.0; // meters

    while (true)
    {
        Sleep(update_period);

        FGNetFDM fdm;
        memset(&fdm,0,sizeof(fdm));
        fdm.version = htonl(FG_NET_FDM_VERSION);

        fdm.latitude = htond(latitude * D2R);
        fdm.longitude = htond(longitude * D2R);
        fdm.altitude = htond(altitude);

        fdm.phi = htonf(roll * D2R);
        fdm.theta = htonf(pitch * D2R);
        fdm.psi = htonf(yaw * D2R);

        fdm.num_engines = htonl(1);

        fdm.num_tanks = htonl(1);
        fdm.fuel_quantity[0] = htonf(100.0);

        fdm.num_wheels = htonl(3);

        fdm.cur_time = htonl(time(0));
        fdm.warp = htonl(1);

        fdm.visibility = htonf(visibility);

        sendto(sendSocket,(char *)&fdm,sizeof(fdm),0,(struct sockaddr *)&sendAddr,sizeof(sendAddr));

        static bool flag = true;
        if (flag)
        {
            roll += 5.0;
        }
        else
        {            
            roll -= 5.0;
        }
        flag = !flag;
    }
}