-
Notifications
You must be signed in to change notification settings - Fork 0
/
pub.c
108 lines (93 loc) · 3.97 KB
/
pub.c
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
//
// dummy publisher
//
/*
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
*/
#include "mb.h" // generated code and headers
#define MY_TOPIC "Voltage" // DDS topic name
static volatile int do_loop = 1; // global variables
static void sigint_handler (int sig) // signal handler
{
do_loop = 0;
}
int main (int argc, char *argv[])
{
// Change signal disposition
struct sigaction sat;
sat.sa_handler = sigint_handler;
sigemptyset (&sat.sa_mask);
sat.sa_flags = 0;
sigaction (SIGINT, &sat, NULL);
// Declare dds entities ------------------------
int status;
dds_qos_t* qos = NULL;
dds_entity_t domain_participant = NULL;
dds_entity_t voltage_topic = NULL;
dds_entity_t voltage_publisher = NULL;
dds_entity_t voltage_writer = NULL;
// Initialize DDS ------------------------
status = dds_init (0, NULL);
DDS_ERR_CHECK (status, DDS_CHECK_REPORT | DDS_CHECK_EXIT);
// Create participant
status = dds_participant_create ( // factory method to create domain participant
&domain_participant, // pointer to created domain participant entity
DDS_DOMAIN_DEFAULT, // domain id (DDS_DOMAIN_DEFAULT = -1)
qos, // Qos on created domain participant (can be NULL)
NULL // Listener on created domain participant (can be NULL)
);
DDS_ERR_CHECK (status, DDS_CHECK_REPORT | DDS_CHECK_EXIT);
// Create a publisher
status = dds_publisher_create ( // factory method to create publisher
domain_participant, // domain participant entity
&voltage_publisher, // pointer to created publisher entity
qos, // Qos on created publisher (can be NULL)
NULL // Listener on created publisher (can be NULL)
);
DDS_ERR_CHECK (status, DDS_CHECK_REPORT | DDS_CHECK_EXIT);
// Create topic for writer
status = dds_topic_create ( // factory method to create topic
domain_participant, // domain participant entity
&voltage_topic, // pointer to created topic entity
&Modbus_voltage_desc, // pointer to IDL generated topic descriptor
MY_TOPIC, // name of created topic
NULL, // Qos on created topic (can be NULL)
NULL // Listener on created topic (can be NULL)
);
DDS_ERR_CHECK (status, DDS_CHECK_REPORT | DDS_CHECK_EXIT);
// Create writer without Qos
status = dds_writer_create ( // factory method to create typed writer
domain_participant, // domain participant entity or publisher entity
&voltage_writer, // pointer to created writer entity
voltage_topic, // topic entity
NULL, // Qos on created writer (can be NULL)
NULL // Listener on created writer (can be NULL)
);
DDS_ERR_CHECK (status, DDS_CHECK_REPORT | DDS_CHECK_EXIT);
// Prepare samples ------------------------
Modbus_voltage writer_msg;
writer_msg.id = 3;
srand ((unsigned int)time(NULL));
float a = 5.0;
while ( do_loop )
{
// dds write
writer_msg.val = ((float)rand()/(float)(RAND_MAX)) * a;
status = dds_write (
voltage_writer, // writer entity
&writer_msg // pointer to topic structure
);
DDS_ERR_CHECK (status, DDS_CHECK_REPORT | DDS_CHECK_EXIT);
printf ("write: %f\n", writer_msg.val);
dds_sleepfor (DDS_MSECS(200));
}
// release resources
printf ("Sanitize\n");
dds_entity_delete (domain_participant);
dds_fini ();
exit (0);
}