first commit

This commit is contained in:
Your Name
2022-10-20 20:29:11 +08:00
commit 4d531f8044
3238 changed files with 1387862 additions and 0 deletions

18
examples/dds/README Normal file
View File

@@ -0,0 +1,18 @@
Data Distribution Service (DDS) logger
This example uses a waitset to listens to the FlightGear GUI, FDM adn Ctrls
DDS topics and logs some data for any of them when new data arrives.
Run FlightGear with any combination of the following command line arguments to
get it to produce new DDS samples:
fgfs
--native-gui=dds,out,30
--native-fdm=dds,out,60
--native-ctrls=dds,out,10
The executable has to be linkes against the CycloneDDS ddsc component and
all *.c files in src/Network/DDS:
gcc -I ../../src fgfdm_log.c ../../src/Network/DDS/*.c -l ddsc -o fgfdm_log

219
examples/dds/fgfdm_log.c Normal file
View File

@@ -0,0 +1,219 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dds/dds.h>
#include <Network/DDS/dds_gui.h>
#include <Network/DDS/dds_fdm.h>
#include <Network/DDS/dds_ctrls.h>
/* An array of one message(aka sample in dds terms) will be used. */
#define MAX_SAMPLES 1
#ifndef _WIN32
# include <termios.h>
void set_mode(int want_key)
{
static struct termios old, new;
if (!want_key) {
tcsetattr(STDIN_FILENO, TCSANOW, &old);
return;
}
tcgetattr(STDIN_FILENO, &old);
new = old;
new.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &new);
}
int get_key()
{
int c = 0;
struct timeval tv;
fd_set fs;
tv.tv_usec = tv.tv_sec = 0;
FD_ZERO(&fs);
FD_SET(STDIN_FILENO, &fs);
select(STDIN_FILENO + 1, &fs, 0, 0, &tv);
if (FD_ISSET(STDIN_FILENO, &fs)) {
c = getchar();
}
return c;
}
#else
# include <conio.h>
int get_key()
{
if (kbhit()) {
return getch();
}
return 0;
}
void set_mode(int want_key)
{
}
#endif
int main()
{
dds_entity_t participant;
dds_entity_t gui_topic;
dds_entity_t fdm_topic;
dds_entity_t ctrls_topic;
dds_entity_t gui_reader;
dds_entity_t fdm_reader;
dds_entity_t ctrls_reader;
dds_entity_t rdcond;
void *samples[MAX_SAMPLES];
dds_sample_info_t infos[MAX_SAMPLES];
dds_return_t rc;
dds_qos_t *qos;
int status;
/* Initialize sample buffer, by pointing the void pointer within
* the buffer array to a valid sample memory location. */
samples[0] = FG_DDS_FDM__alloc ();
/* Create a Participant. */
participant = dds_create_participant(DDS_DOMAIN_DEFAULT, NULL, NULL);
if(participant < 0)
DDS_FATAL("dds_create_participant: %s\n", dds_strretcode(-participant));
/* Create a wait set */
dds_entity_t waitset = dds_create_waitset(participant);
qos = dds_create_qos();
dds_qset_reliability(qos, DDS_RELIABILITY_RELIABLE, DDS_SECS(10));
/* Create a GUI Topic. */
gui_topic = dds_create_topic(
participant, &FG_DDS_GUI_desc, "FG_DDS_GUI", NULL, NULL);
if(gui_topic < 0)
DDS_FATAL("dds_create_topic: %s\n", dds_strretcode(-gui_topic));
gui_reader = dds_create_reader(participant, gui_topic, qos, NULL);
if(gui_reader < 0)
DDS_FATAL("dds_create_reader: %s\n", dds_strretcode(-gui_reader));
rdcond = dds_create_readcondition(gui_reader, DDS_NOT_READ_SAMPLE_STATE);
status = dds_waitset_attach(waitset, rdcond, gui_reader);
if (status < 0)
DDS_FATAL("ds_waitset_attach: %s\n", dds_strretcode(-status));
/* Create an FDM Topic. */
fdm_topic = dds_create_topic(
participant, &FG_DDS_FDM_desc, "FG_DDS_FDM", NULL, NULL);
if(fdm_topic < 0)
DDS_FATAL("dds_create_topic: %s\n", dds_strretcode(-fdm_topic));
fdm_reader = dds_create_reader(participant, fdm_topic, qos, NULL);
if(fdm_reader < 0)
DDS_FATAL("dds_create_reader: %s\n", dds_strretcode(-fdm_reader));
rdcond = dds_create_readcondition(fdm_reader, DDS_NOT_READ_SAMPLE_STATE);
status = dds_waitset_attach(waitset, rdcond, fdm_reader);
if (status < 0)
DDS_FATAL("ds_waitset_attach: %s\n", dds_strretcode(-status));
/* Create a Ctrls Topic. */
ctrls_topic = dds_create_topic(
participant, &FG_DDS_Ctrls_desc, "FG_DDS_Ctrls", NULL, NULL);
if(ctrls_topic < 0)
DDS_FATAL("dds_create_topic: %s\n", dds_strretcode(-ctrls_topic));
ctrls_reader = dds_create_reader(participant, ctrls_topic, qos, NULL);
if(ctrls_reader < 0)
DDS_FATAL("dds_create_reader: %s\n", dds_strretcode(-ctrls_reader));
rdcond = dds_create_readcondition(ctrls_reader, DDS_NOT_READ_SAMPLE_STATE);
status = dds_waitset_attach(waitset, rdcond, ctrls_reader);
if (status < 0)
DDS_FATAL("ds_waitset_attach: %s\n", dds_strretcode(-status));
dds_delete_qos(qos);
printf("\n=== [fgfdm_log] Waiting for a sample ...\n");
fflush(stdout);
/* Wait for a new packet. */
set_mode(1);
size_t num = 1;
dds_attach_t results[num];
dds_duration_t timeout = DDS_MSECS(500);
while(dds_waitset_wait(waitset, results, num, timeout) >= 0)
{
/* Do the actual read.
* The return value contains the number of read samples. */
rc = dds_take(gui_reader, samples, infos, 1, 1);
if(rc < 0)
DDS_FATAL("dds_read: (gui) %s\n", dds_strretcode(-rc));
if((rc > 0) &&(infos[0].valid_data))
{
FG_DDS_GUI *gui = (FG_DDS_GUI*) samples[0];
printf("=== [fgfdm_log] Received : ");
printf("GUI Message:\n");
printf(" version: %i\n", gui->version);
printf(" tuned_freq: %lf\n", gui->tuned_freq);
printf(" nav_radial: %lf\n", gui->nav_radial);
printf(" dist_nm: %lf\n", gui->dist_nm);
}
rc = dds_take(fdm_reader, samples, infos, 1, 1);
if(rc < 0)
DDS_FATAL("dds_read: (fdm) %s\n", dds_strretcode(-rc));
/* Check if we read some data and it is valid. */
if((rc > 0) &&(infos[0].valid_data))
{
/* Print Message. */
FG_DDS_FDM *fdm = (FG_DDS_FDM*) samples[0];
printf("=== [fgfdm_log] Received : ");
printf("FDM Message:\n");
printf(" version: %i\n", fdm->version);
printf(" longitude: %lf\n", fdm->longitude);
printf(" latitude: %lf\n", fdm->latitude);
printf(" altitude: %lf\n", fdm->altitude);
}
rc = dds_take(ctrls_reader, samples, infos, 1, 1);
if(rc < 0)
DDS_FATAL("dds_read: (ctrls) %s\n", dds_strretcode(-rc));
/* Check if we read some data and it is valid. */
if((rc > 0) &&(infos[0].valid_data))
{
/* Print Message. */
FG_DDS_Ctrls *ctrls = (FG_DDS_Ctrls*) samples[0];
printf("=== [fgfdm_log] Received : ");
printf("Ctrls Message:\n");
printf(" version: %i\n", ctrls->version);
printf(" aileron: %lf\n", ctrls->aileron);
printf(" elevator: %lf\n", ctrls->elevator);
printf(" rudder: %lf\n", ctrls->rudder);
}
fflush(stdout);
if (get_key()) break;
}
set_mode(0);
/* Free the data location. */
FG_DDS_FDM_free (samples[0], DDS_FREE_ALL);
/* Deleting the participant will delete all its children recursively as well. */
rc = dds_delete(participant);
if(rc != DDS_RETCODE_OK)
DDS_FATAL("dds_delete: %s\n", dds_strretcode(-rc));
return EXIT_SUCCESS;
}

26
examples/netfdm/README Normal file
View File

@@ -0,0 +1,26 @@
David Calkins writes:
I've attached my sample code which works with FlightGear v0.9.3. Perhaps this
will be of some help to others. I'm running FG with the launcher wizard, which
uses the below command line options. The sample code I attached just rolls
back and forth <20> 5 degrees so it isn't that interesting, but it works.
C:\Program Files\FlightGear-0.9.3\bin\Win32\fgfs.exe
--fg-root=C:\Program Files\FlightGear-0.9.3\data
--fg-scenery=C:\Program Files\FlightGear-0.9.3\data\Scenery
--aircraft=c172
--control=joystick
--disable-random-objects
--fdm=external
--vc=0
--bpp=32
--timeofday=noon
--native-fdm=socket,in,1,,5500,udp
One point of interest is the cur_time field in the FGNetFDM structure.
I noticed that it didn't seem to matter what time I passed in. In looking at
the source, it appears this field is ignored completely. So, it looks like the
time of day would need to be determined by the command line parameters used to
launch FlightGear.
Dave

143
examples/netfdm/main.cpp Normal file
View File

@@ -0,0 +1,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;
}
}