Repository navigation
Expand file tree
/
Copy pathrosnodeinterface.cpp
More file actions
160 lines (126 loc) · 3.6 KB
/
Copy pathrosnodeinterface.cpp
File metadata and controls
160 lines (126 loc) · 3.6 KB
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
152
153
154
155
156
157
158
159
160
// Copyright 2019-2021 The MathWorks, Inc.
// Generated 11-Nov-2021 21:35:53
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4244)
#pragma warning(disable : 4265)
#pragma warning(disable : 4458)
#pragma warning(disable : 4100)
#else
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wredundant-decls"
#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
#pragma GCC diagnostic ignored "-Wdelete-non-virtual-dtor"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wshadow"
#endif //_MSC_VER
#include "accel.h"
#include "rosnodeinterface.h"
#include <thread>
#include <chrono>
#include <utility>
namespace ros
{
namespace matlab
{
NodeInterface::NodeInterface()
: mNode()
, mBaseRateSem()
, mBaseRateThread()
, mSchedulerThread()
, mStopSem()
, mRunModel(true)
{
}
NodeInterface::~NodeInterface()
{
terminate();
}
void NodeInterface::initialize(int argc, char ** argv)
{
try {
mNode = std::make_shared<ros::NodeHandle>();
ROS_INFO("** Starting the model \"accel\" **\n");
// initialize the model which will initialize the publishers and subscribers
rtmSetErrorStatus(accel_M, (NULL));
accel_initialize();
// create the threads for the rates in the Model
mBaseRateThread = std::make_shared<std::thread>(&NodeInterface::
baseRateTask, this);
// create scheduler thread
mSchedulerThread = std::make_shared<std::thread>(&NodeInterface::
schedulerThread, this);
} catch (std::exception& ex) {
std::cout << ex.what() << std::endl;
throw ex;
}
}
int NodeInterface::run()
{
ros::spin();
mRunModel = false;
return 0;
}
boolean_T NodeInterface::getStopRequestedFlag(void)
{
#ifndef rtmGetStopRequested
return (!(rtmGetErrorStatus(accel_M)
== (NULL)));
#else
return (!(rtmGetErrorStatus(accel_M)
== (NULL)) || rtmGetStopRequested(accel_M));
#endif
}
void NodeInterface::terminate(void)
{
if (mBaseRateThread.get()) {
mBaseRateSem.notify();
mBaseRateThread->join();
mRunModel = false;
mBaseRateThread.reset();
if (mSchedulerThread.get()) {
mSchedulerThread->join();
mSchedulerThread.reset();
}
accel_terminate();
mNode.reset();
}
}
//
// Scheduler Task using ROS Wall clock timer to run base-rate
//
void NodeInterface::schedulerThread(void)
{
while (mRunModel) {
std::this_thread::sleep_for(std::chrono::nanoseconds(50000000));
mBaseRateSem.notify();
}
}
// Base-rate task
void NodeInterface::baseRateTask(void)
{
mRunModel = (rtmGetErrorStatus(accel_M) ==
(NULL));
while (mRunModel) {
mBaseRateSem.wait();
#ifdef MW_DEBUG_LOG
ROS_INFO("** Base rate task semaphore received\n");
#endif
if (!mRunModel)
break;
accel_step();
mRunModel = !NodeInterface::getStopRequestedFlag();
}
// Shutdown the ROS node
ros::shutdown();
}
} //namespace matlab
} //namespace ros
#ifdef _MSC_VER
#pragma warning(pop)
#else
#pragma GCC diagnostic pop
#endif //_MSC_VER