-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotaryPublisher.cpp
151 lines (111 loc) · 4.02 KB
/
rotaryPublisher.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* 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 << "=== [rotaryPublisher] Cycles: " << cycles << endl;
}
else
{
cycles = 10000;
cout << "=== [rotaryPublisher] 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 << "=== [rotaryPublisher] 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_RotarySensor */
dds::topic::Topic<VortexGrove::AnalogueSensor> RotarySensorTopic(participant,
"VortexGrove_RotarySensor", reliableTopicQos);
cout << "=== [rotaryPublisher] Topic VortexGrove_RotarySensor 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 rotaryPublisher(participant, pubQos);
dds::pub::qos::DataWriterQos dwQos = RotarySensorTopic.qos();
dds::pub::DataWriter<VortexGrove::AnalogueSensor> sensorDW(rotaryPublisher, RotarySensorTopic, dwQos);
cout << "=== [rotaryPublisher] Publisher and DataWriter Created " << endl;
/* Initialise Grove Pi Board */
if (init() == -1)
{
cout << "=== [rotaryPublisher] GrovePi Failed to initialise" << endl;
cout << "=== [rotaryPublisher] Check the GrovePi is online (green LED on top)\n"
<< "=== [rotaryPublisher] Test the I2C port connection with command: \"i2cdetect -y 1\"" << endl;
return 0;
}
else
{
cout << "=== [rotaryPublisher] Grove Pi initialised" << endl;
}
/* Create a unique reference for the Potentiometer values.
* This reference is also used as the Topic Key */
short Rotary = 100;
/* For Rotary Angle Sensor
* Reference voltage of ADC is 5v
* Vcc of the grove interface is normally 5v
* Full Rotary Angle is 300 DEG */
int adc_ref = 5;
int grove_vcc = 5;
int full_angle = 300;
float voltage;
float degrees;
int count = 0;
int rotaryData = 0;
int rotaryPin = 1;
/* Set the pins to input */
pinMode(1, 0);
/* Create a Topic Instance of the grove_AnalogueSensor Topic */
VortexGrove::AnalogueSensor currVal;
for (int i = 0; i < cycles; i++)
{
/* Read data from pin 1 */
rotaryData = analogRead(rotaryPin);
/* Pass the data to the Topic Instance */
currVal.id() = Rotary;
currVal.rValue() = rotaryData;
/* Calculate the Degrees based on the potentiometer value */
voltage = round((float)(rotaryData)* adc_ref / 1023);
degrees = round((voltage * full_angle) / grove_vcc);
currVal.value() = degrees;
cout << "=== [rotaryPublisher] Sending Sample No.[" << count << "]"
<< "=== [rotaryPublisher] Data: " << rotaryData << endl;
/* Send the data sample to the data writer */
sensorDW << currVal;
/* Wait for a second */
sleep(1);
count++;
}
return 0;
}