Beeper.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
10 #include "Beeper.h"
11 #include <rtm/CorbaNaming.h>
12 #include<deque>
13 
14 // Variables for beep_thread
15 struct BeepData
16 {
20 };
21 
22 bool is_initialized = false;
24 double m_dt = 0.002;
25 std::deque<BeepData> beep_command_buffer; // Used for communication between beepthread and real-time thread
26 
27 // Module specification
28 // <rtc-template block="module_spec">
29 static const char* beeper_spec[] =
30  {
31  "implementation_id", "Beeper",
32  "type_name", "Beeper",
33  "description", "beeper",
34  "version", HRPSYS_PACKAGE_VERSION,
35  "vendor", "AIST",
36  "category", "example",
37  "activity_type", "DataFlowComponent",
38  "max_instance", "10",
39  "language", "C++",
40  "lang_type", "compile",
41  // Configuration variables
42  "conf.default.debugLevel", "0",
43  ""
44  };
45 // </rtc-template>
46 
47 void* call_beep (void* args)
48 {
49  // Initialize
50  init_beep();
51  bool wait_for_initialized = true;
52  while (wait_for_initialized) { // Wait until m_dt is set
53  usleep(2000);
54  pthread_mutex_lock( &beep_mutex );
55  wait_for_initialized = !is_initialized;
56  pthread_mutex_unlock( &beep_mutex );
57  }
58  // Loop
59  bool prev_beep_start=false;
60  int prev_beep_length = 1000;
61  while (1) {
62  // Get beepCommand from buffer
63  pthread_mutex_lock( &beep_mutex );
64  BeepData bd = beep_command_buffer.front();
65  if (beep_command_buffer.size() > 1) beep_command_buffer.pop_front();
66  pthread_mutex_unlock( &beep_mutex );
67 // if (!prev_beep_start && tmp_beep_start) {
68 // std::cerr << "BP START" << std::endl;
69 // } else if (prev_beep_start && !tmp_beep_start) {
70 // std::cerr << "BP STOP" << std::endl;
71 // }
72  // Beep
73  if (bd._beep_start) {
74  usleep(std::max(static_cast<int>(prev_beep_length*1000),0));
76  } else if (prev_beep_start) { // If !beep_start and prev_beep_start, stop_beep just once.
77  usleep(static_cast<size_t>(1000000*m_dt));
78  stop_beep();
79  } else {
80  usleep(static_cast<size_t>(1000000*m_dt));
81  }
82  prev_beep_start = bd._beep_start;
83  prev_beep_length = bd._beep_length;
84  }
85  quit_beep();
86 }
87 
89  : RTC::DataFlowComponentBase(manager),
90  // <rtc-template block="initializer">
91  m_beepCommandIn("beepCommand", m_beepCommand),
92  m_loop(0),
93  m_debugLevel(0)
94 {
95  pthread_create(&beep_thread, NULL, call_beep, (void*)NULL);
96  pthread_mutex_init( &beep_mutex, NULL );
97 }
98 
100 {
101  pthread_join(beep_thread, NULL );
102 }
103 
104 RTC::ReturnCode_t Beeper::onInitialize()
105 {
106  std::cerr << "[" << m_profile.instance_name << "] : onInitialize()" << std::endl;
107  // <rtc-template block="bind_config">
108  // Bind variables and configuration variable
109  bindParameter("debugLevel", m_debugLevel, "0");
110 
111  // </rtc-template>
112 
113  // Registration: InPort/OutPort/Service
114  // <rtc-template block="registration">
115  // Set InPort buffers
116  addInPort("beepCommand", m_beepCommandIn);
117 
118  // Set OutPort buffer
119 
120  // Set service consumers to Ports
121 
122  // Set CORBA Service Ports
123 
124  // </rtc-template>
125 
127  coil::stringTo(m_dt, prop["dt"].c_str());
128  pthread_mutex_lock( &beep_mutex );
129  is_initialized = true;
130  pthread_mutex_unlock( &beep_mutex );
131  std::cerr << "[" << m_profile.instance_name << "] : Beep thread dt = " << m_dt << "[s]" << std::endl;
132 
133  return RTC::RTC_OK;
134 }
135 
136 /*
137 RTC::ReturnCode_t Beeper::onFinalize()
138 {
139  return RTC::RTC_OK;
140 }
141 */
142 
143 /*
144 RTC::ReturnCode_t Beeper::onStartup(RTC::UniqueId ec_id)
145 {
146  return RTC::RTC_OK;
147 }
148 */
149 
150 /*
151 RTC::ReturnCode_t Beeper::onShutdown(RTC::UniqueId ec_id)
152 {
153  return RTC::RTC_OK;
154 }
155 */
156 
157 RTC::ReturnCode_t Beeper::onActivated(RTC::UniqueId ec_id)
158 {
159  std::cerr << "[" << m_profile.instance_name << "] : onActivated(" << ec_id << ")" << std::endl;
160  return RTC::RTC_OK;
161 }
162 
163 RTC::ReturnCode_t Beeper::onDeactivated(RTC::UniqueId ec_id)
164 {
165  std::cerr << "[" << m_profile.instance_name << "] : onDeactivated(" << ec_id << ")" << std::endl;
166  return RTC::RTC_OK;
167 }
168 
169 #define DEBUGP ((m_debugLevel==1 && m_loop%200==0) || m_debugLevel > 1 )
170 RTC::ReturnCode_t Beeper::onExecute(RTC::UniqueId ec_id)
171 {
172  // std::cout << m_profile.instance_name<< ": onExecute(" << ec_id << "), data = " << m_data.data << std::endl;
173  m_loop++;
174 
175  if (m_beepCommandIn.isNew()) {
176  // Read beepCommand from data port
178  BeepData bd;
179  bd._beep_start = (m_beepCommand.data[BEEP_INFO_START] == 1);
182  // Push beepCommand to buffer
183  size_t max_buffer_length = 10;
184  if (pthread_mutex_trylock( &beep_mutex ) == 0) {
185  beep_command_buffer.push_back(bd);
186  while (beep_command_buffer.size() > max_buffer_length) beep_command_buffer.pop_front();
187  pthread_mutex_unlock( &beep_mutex );
188  } else {
189  std::cerr << "[" << m_profile.instance_name<< "] Mutex trylock failed (loop=" << m_loop << ")" << std::endl;
190  }
191  // print
192  if (m_debugLevel > 0) {
193  if (bd._beep_start)
194  std::cerr << "[" << m_profile.instance_name<< "] isNew : beep start (freq=" << bd._beep_freq << ", length=" << bd._beep_length << ", loop=" << m_loop << ")" << std::endl;
195  else
196  std::cerr << "[" << m_profile.instance_name<< "] isNew : beep stop (loop=" << m_loop << ")" << std::endl;
197  }
198  }
199 // if (beep_start) {
200 // start_beep(beep_freq, beep_length, true);
201 // } else {
202 // stop_beep(true);
203 // }
204 
205  return RTC::RTC_OK;
206 }
207 
208 /*
209 RTC::ReturnCode_t Beeper::onAborting(RTC::UniqueId ec_id)
210 {
211  return RTC::RTC_OK;
212 }
213 */
214 
215 /*
216 RTC::ReturnCode_t Beeper::onError(RTC::UniqueId ec_id)
217 {
218  return RTC::RTC_OK;
219 }
220 */
221 
222 /*
223 RTC::ReturnCode_t Beeper::onReset(RTC::UniqueId ec_id)
224 {
225  return RTC::RTC_OK;
226 }
227 */
228 
229 /*
230 RTC::ReturnCode_t Beeper::onStateUpdate(RTC::UniqueId ec_id)
231 {
232  return RTC::RTC_OK;
233 }
234 */
235 
236 /*
237 RTC::ReturnCode_t Beeper::onRateChanged(RTC::UniqueId ec_id)
238 {
239  return RTC::RTC_OK;
240 }
241 */
242 
243 
244 extern "C"
245 {
246 
247  void BeeperInit(RTC::Manager* manager)
248  {
250  manager->registerFactory(profile,
251  RTC::Create<Beeper>,
252  RTC::Delete<Beeper>);
253  }
254 
255 };
256 
257 
ComponentProfile m_profile
png_infop png_charpp int png_charpp profile
pthread_t beep_thread
Definition: Beeper.h:135
#define max(a, b)
bool stringTo(To &val, const char *str)
virtual RTC::ReturnCode_t onActivated(RTC::UniqueId ec_id)
Definition: Beeper.cpp:157
unsigned int m_debugLevel
Definition: Beeper.h:134
virtual RTC::ReturnCode_t onDeactivated(RTC::UniqueId ec_id)
Definition: Beeper.cpp:163
InPort< TimedLongSeq > m_beepCommandIn
Definition: Beeper.h:108
void quit_beep()
Definition: beep.cpp:28
void init_beep()
Definition: beep.cpp:6
virtual ~Beeper()
Destructor.
Definition: Beeper.cpp:99
coil::Properties & getProperties()
pthread_mutex_t beep_mutex
Definition: Beeper.cpp:23
int _beep_length
Definition: Beeper.cpp:19
TimedLongSeq m_beepCommand
Definition: Beeper.h:104
int _beep_freq
Definition: Beeper.cpp:18
ExecutionContextHandle_t UniqueId
long long m_loop
Definition: Beeper.h:133
bool bindParameter(const char *param_name, VarType &var, const char *def_val, bool(*trans)(VarType &, const char *)=coil::stringTo)
void BeeperInit(RTC::Manager *manager)
Definition: Beeper.cpp:247
double m_dt
Definition: Beeper.cpp:24
Beeper component.
Beeper(RTC::Manager *manager)
Constructor.
Definition: Beeper.cpp:88
prop
virtual RTC::ReturnCode_t onInitialize()
Definition: Beeper.cpp:104
bool _beep_start
Definition: Beeper.cpp:17
virtual bool isNew()
static const char * beeper_spec[]
Definition: Beeper.cpp:29
bool is_initialized
Definition: Beeper.cpp:22
void stop_beep()
Definition: beep.cpp:22
HANDLE pthread_mutex_t
bool addInPort(const char *name, InPortBase &inport)
std::deque< BeepData > beep_command_buffer
Definition: Beeper.cpp:25
bool registerFactory(coil::Properties &profile, RtcNewFunc new_func, RtcDeleteFunc delete_func)
void start_beep(int freq, int length)
Definition: beep.cpp:16
void * call_beep(void *args)
Definition: Beeper.cpp:47
virtual RTC::ReturnCode_t onExecute(RTC::UniqueId ec_id)
Definition: Beeper.cpp:170
int usleep(useconds_t usec)


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Thu May 6 2021 02:41:49