-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlightPublisher.cpp
135 lines (99 loc) · 3.71 KB
/
lightPublisher.cpp
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
/*
* Vortex OpenSplice DDS
*
* This software and documentation are Copyright 2006 to 2017 PrismTech
* Limited and its licensees. All rights reserved. See file:
*
* $OSPL_HOME/LICENSE
*
* for full copyright notice and license terms.
*
*/
#include <iostream>
#include <string>
#include <sstream>
#include <unistd.h>
#include "VortexGrove_DCPS.hpp"
/* C API for GrovePi Module */
#include "grovepi.h"
using namespace std;
int main (int argc, char *argv[])
{
/* Get the program parameters
* Parameters: rotaryPublisher [Number of cycles] */
int cycles;
if (argv[1])
{
cycles = atoi(argv[1]);
cout << "=== [lightPublisher] Cycles: " << cycles << endl;
}
else
{
cycles = 10000;
cout << "=== [lightPublisher] Cycles: 10,000, CTRL - C to exit." << endl;
}
/** A dds::domain::DomainParticipant is created for the default domain. */
dds::domain::DomainParticipant participant(org::opensplice::domain::default_id());
cout << "=== [lightPublisher] Participant Created" << endl;
/* A dds::topic::qos::TopicQos is created with Reliability set to Reliable to
* guarantee delivery. */
dds::topic::qos::TopicQos reliableTopicQos = participant.default_topic_qos()
<< dds::core::policy::Reliability::Reliable();
/* Set the Reliable TopicQos as the new default */
participant.default_topic_qos(reliableTopicQos);
/* A dds::topic::Topic is created for the Analogue Sensor types on the
* domain participant.
* The Topic data type used for the sensors is defined in the IDL file.
* Here we create a Topic of type VortexGrove::AnalogueSensor called
* VortexGrove_LightSensor */
dds::topic::Topic<VortexGrove::AnalogueSensor> LightSensorTopic(participant,
"VortexGrove_LightSensor", reliableTopicQos);
cout << "=== [lightPublisher] Topic VortexGrove_LightSensor Created " << endl;
/* Create a Publisher and Data Writer.
* This Publisher will use the Analogue Sensor Topic to pass Temperature and
* Lux values into the DDS */
dds::pub::qos::PublisherQos pubQos = participant.default_publisher_qos();
dds::pub::Publisher lightPublisher(participant, pubQos);
dds::pub::qos::DataWriterQos dwQos = LightSensorTopic.qos();
dds::pub::DataWriter<VortexGrove::AnalogueSensor> sensorDW(lightPublisher, LightSensorTopic, dwQos);
cout << "=== [lightPublisher] Publisher and DataWriter Created " << endl;
/* Initialise Grove Pi Board */
if (init() == -1)
{
cout << "=== [lightPublisher] GrovePi Failed to initialise" << endl;
cout << "=== [lightPublisher] Check the GrovePi is online (green LED on top)\n"
<< "=== [lightPublisher] Test the I2C port connection with command: \"i2cdetect -y 1\"" << endl;
return 0;
}
else
{
cout << "=== [lightPublisher] Grove Pi initialised" << endl;
}
/* Create a unique reference for the Lux values.
* This reference is also used as the Topic Key
* The unit measurement of light is called Lux */
short Lux = 200;
int count = 0;
int lightData = 0;
int lightPin = 0;
/* Set the pins to input */
pinMode(0, 0);
/* Create a Topic Instance of the VortexGrove_AnalogueSensor Topic data type */
VortexGrove::AnalogueSensor currVal;
for (int i = 0; i < cycles; i++)
{
/* Read data from pin 0 */
lightData = analogRead(lightPin);
/* Pass the data to the Topic Instance */
currVal.id() = Lux;
currVal.rValue() = lightData;
cout << "=== [lightPublisher] Sending Sample No.["
<< count << "] Data: [" << lightData << "]" << endl;
/* Send the data sample to the data writer */
sensorDW << currVal;
/* Wait for a second */
sleep(1);
count++;
}
return 0;
}