Updated C example program to conform to the new client API.
This commit is contained in:
@@ -16,38 +16,18 @@
|
|||||||
#define sleep(n) Sleep(n * 1000)
|
#define sleep(n) Sleep(n * 1000)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int main() {
|
int main()
|
||||||
int i, j;
|
{
|
||||||
struct xpcSocket sendfd, readfd;
|
printf("XPlaneConnect Example Script\n- Setting up Simulation\n");
|
||||||
float data[4][9] = { 0 };
|
|
||||||
char DREF[100] = { 0 };
|
|
||||||
char DREFArray[100][100];
|
|
||||||
char DREFArray2[100][100];
|
|
||||||
short DREFSizes[100];
|
|
||||||
float *recDATA[100];
|
|
||||||
float POSI[9] = { 0.0 };
|
|
||||||
float CTRL[5] = { 0.0 };
|
|
||||||
float gear;
|
|
||||||
char IP[16] = "127.0.0.1"; //IP Address of computer running X-Plane
|
|
||||||
short PORT = 49009; //xpcPlugin Receiving port (usually 49009)
|
|
||||||
|
|
||||||
printf("xplaneconnect Example Script\n- Setting up Simulation\n");
|
// Open Socket
|
||||||
|
const char* IP = "127.0.0.1"; //IP Address of computer running X-Plane
|
||||||
for (i = 0; i < 100; i++) {
|
const unsigned short PORT = 49009; //xpcPlugin Receiving port (usually 49009)
|
||||||
recDATA[i] = (float *)malloc(40 * sizeof(float));
|
XPCSocket sock = aopenUDP(IP, PORT);
|
||||||
memset(DREFArray[i], 0, 100);
|
|
||||||
memset(DREFArray2[i], 0, 100);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Open Sockets
|
|
||||||
readfd = openUDP(49055, IP, PORT); //Open socket for receiving
|
|
||||||
sendfd = openUDP(49077, IP, PORT); //Open socket for sending
|
|
||||||
|
|
||||||
// Set up Connection
|
|
||||||
setCONN(sendfd, 49055); // Setup so data will be received on port 49055
|
|
||||||
|
|
||||||
// Set Location/Orientation (sendPOSI)
|
// Set Location/Orientation (sendPOSI)
|
||||||
// Set Up Position Array
|
// Set Up Position Array
|
||||||
|
float POSI[9] = { 0.0 };
|
||||||
POSI[0] = 37.524; // Lat
|
POSI[0] = 37.524; // Lat
|
||||||
POSI[1] = -122.06899; // Lon
|
POSI[1] = -122.06899; // Lon
|
||||||
POSI[2] = 2500; // Alt
|
POSI[2] = 2500; // Alt
|
||||||
@@ -56,22 +36,30 @@ int main() {
|
|||||||
POSI[5] = 0; // Heading
|
POSI[5] = 0; // Heading
|
||||||
POSI[6] = 1; // Gear
|
POSI[6] = 1; // Gear
|
||||||
|
|
||||||
sendPOSI(sendfd, 0, 7, POSI);
|
// Set position of the player aircraft
|
||||||
|
psendPOSI(sock, POSI, 7);
|
||||||
|
|
||||||
POSI[0] = 37.52465; // Move a second aircraft a bit North of us
|
POSI[0] = 37.52465; // Move a second aircraft a bit North of us
|
||||||
POSI[4] = 20; // Give that aircraft a bit or right roll
|
POSI[4] = 20; // Give that aircraft a bit or right roll
|
||||||
sendPOSI(sendfd, 1, 7, POSI);
|
// Set position of a multiplayer aircraft
|
||||||
|
sendPOSI(sock, POSI, 7, 1);
|
||||||
|
|
||||||
// Set Rates (sendDATA)
|
// Set Rates (sendDATA)
|
||||||
|
float data[3][9] = { 0 };
|
||||||
for (i = 0; i < 4; i++) { // Set array to -999
|
// Initialize data values to -998 to not overwrite values.
|
||||||
for (j = 0; j < 9; j++) data[i][j] = -999;
|
for (int i = 0; i < 3; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < 9; j++)
|
||||||
|
{
|
||||||
|
data[i][j] = -998;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Set up Data Array (first item in row is item number (example: 20=position)
|
// Set up Data Array (first item in row is item number (example: 20=position)
|
||||||
data[0][0] = 18; // Alpha
|
data[0][0] = 18; // Alpha
|
||||||
data[0][1] = 0;
|
data[0][1] = 0;
|
||||||
data[0][3] = 0;
|
data[0][3] = 0;
|
||||||
|
|
||||||
data[1][0] = 3;//21; //Velocity
|
data[1][0] = 3; //Velocity
|
||||||
data[1][1] = 130;
|
data[1][1] = 130;
|
||||||
data[1][2] = 130;
|
data[1][2] = 130;
|
||||||
data[1][3] = 130;
|
data[1][3] = 130;
|
||||||
@@ -82,21 +70,19 @@ int main() {
|
|||||||
data[2][2] = 0;
|
data[2][2] = 0;
|
||||||
data[2][3] = 0;
|
data[2][3] = 0;
|
||||||
|
|
||||||
sendDATA(sendfd, data, 3); // Velocity/Alpha/PQR
|
sendDATA(sock, data, 3);
|
||||||
|
|
||||||
// Set CTRL
|
// Set throttle on the player aircraft using sendCTRL
|
||||||
|
float CTRL[5] = { 0.0 };
|
||||||
CTRL[3] = 0.8; // Throttle
|
CTRL[3] = 0.8; // Throttle
|
||||||
|
psendCTRL(sock, CTRL, 5);
|
||||||
sendCTRL(sendfd, 4, CTRL);
|
|
||||||
|
|
||||||
// pauseSim
|
// pauseSim
|
||||||
pauseSim(sendfd, 1); // Sending 1 to pause
|
pauseSim(sock, 1); // Sending 1 to pause
|
||||||
|
sleep(5); // Pause for 5 seconds
|
||||||
// Pause for 5 seconds
|
|
||||||
sleep(5);
|
|
||||||
|
|
||||||
// Unpause
|
// Unpause
|
||||||
pauseSim(sendfd, 0); // Sending 0 to unpause
|
pauseSim(sock, 0); // Sending 0 to unpause
|
||||||
printf("- Resuming Simulation\n");
|
printf("- Resuming Simulation\n");
|
||||||
|
|
||||||
// Simulate for 10 seconds
|
// Simulate for 10 seconds
|
||||||
@@ -105,28 +91,34 @@ int main() {
|
|||||||
// SendDREF (Landing Gear)
|
// SendDREF (Landing Gear)
|
||||||
printf("- Stowing Landing Gear\n");
|
printf("- Stowing Landing Gear\n");
|
||||||
|
|
||||||
strcpy(DREF, "sim/cockpit/switches/gear_handle_status"); // Gear handle data reference
|
const char* dref = "sim/cockpit/switches/gear_handle_status"; // Gear handle data reference
|
||||||
DREFSizes[0] = strlen(DREF);
|
float gear = 0; // Stow gear
|
||||||
gear = 1; // Stow gear
|
setDREF(sock, dref, &gear, 1); // Set gear to stow
|
||||||
|
|
||||||
sendDREF(sendfd, DREF, DREFSizes[0], &gear, 1); // Set gear to stow
|
|
||||||
|
|
||||||
// Simulate for 10 seconds
|
// Simulate for 10 seconds
|
||||||
sleep(10);
|
sleep(10);
|
||||||
|
|
||||||
// Check Landing gear, Pause
|
// Check Landing gear, Pause
|
||||||
printf("- Confirming Gear Status\n");
|
printf("- Confirming Gear Status\n");
|
||||||
strcpy(DREFArray2[0], "sim/cockpit/switches/gear_handle_status");
|
const char* drefs[2] =
|
||||||
strcpy(DREFArray2[1], "sim/operation/override/override_planepath");
|
{
|
||||||
for (i = 0; i < 2; i++) {
|
"sim/cockpit/switches/gear_handle_status",
|
||||||
DREFSizes[i] = (int)strlen(DREFArray2[i]);
|
"sim/operation/override/override_planepath"
|
||||||
|
};
|
||||||
|
float* results[2];
|
||||||
|
int sizes[2] = { 20, 20 };
|
||||||
|
for (int i = 0; i < 2; ++i)
|
||||||
|
{
|
||||||
|
results[i] = (float*)malloc(20 * sizeof(float));
|
||||||
}
|
}
|
||||||
requestDREF(sendfd, readfd, DREFArray2, DREFSizes, 2, recDATA, DREFSizes); // Request 2 values
|
getDREFs(sock, drefs, results, 2, sizes);
|
||||||
|
|
||||||
if (*(recDATA[0]) == 0) {
|
if (results[0][0] == 0)
|
||||||
|
{
|
||||||
printf("\tGear Stowed\n");
|
printf("\tGear Stowed\n");
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
printf("\tERROR: Gear Stowage unsuccessful\n");
|
printf("\tERROR: Gear Stowage unsuccessful\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user