23#include "button_thread.h"
25#include <alcore/alerror.h>
26#include <almemoryfastaccess/almemoryfastaccess.h>
27#include <alproxies/alaudioplayerproxy.h>
28#include <alproxies/allauncherproxy.h>
29#include <alproxies/almemoryproxy.h>
30#include <alproxies/alsentinelproxy.h>
31#include <alproxies/dcmproxy.h>
32#include <interfaces/NaoSensorInterface.h>
33#include <interfaces/SwitchInterface.h>
36#include <boost/bind/bind.hpp>
42#define POWEROFF_PATH "/sbin/poweroff"
57:
Thread(
"NaoQiButtonThread",
Thread::OPMODE_WAITFORWAKEUP),
70 sound_longpling_ = sound_pling_ = -1;
71 sound_bumper_left_ = sound_bumper_right_ = -1;
72 last_shutdown_actcount = 0;
74 cfg_chest_triple_long_click_shutdown_ =
false;
76 cfg_chest_triple_long_click_shutdown_ =
77 config->
get_bool(
"/hardware/nao/chestbut_triple_long_click_shutdown");
83 AL::ALPtr<AL::ALLauncherProxy> launcher(
new AL::ALLauncherProxy(
naoqi_broker));
84 bool is_auplayer_available = launcher->isModulePresent(
"ALAudioPlayer");
85 bool is_alsentinel_available = launcher->isModulePresent(
"ALSentinel");
87 if (!is_auplayer_available) {
90 auplayer_ = AL::ALPtr<AL::ALAudioPlayerProxy>(
new AL::ALAudioPlayerProxy(
naoqi_broker));
91 sound_longpling_ = auplayer_->loadFile(RESDIR
"/sounds/longpling.wav");
92 sound_pling_ = auplayer_->loadFile(RESDIR
"/sounds/pling.wav");
93 sound_bumper_left_ = auplayer_->loadFile(RESDIR
"/sounds/metal_click_1_left.wav");
94 sound_bumper_right_ = auplayer_->loadFile(RESDIR
"/sounds/metal_click_1_right.wav");
97 if (is_alsentinel_available) {
99 AL::ALPtr<AL::ALSentinelProxy> alsentinel(
new AL::ALSentinelProxy(
naoqi_broker));
100 alsentinel->enableDefaultActionSimpleClick(
false);
101 alsentinel->enableDefaultActionDoubleClick(
false);
102 alsentinel->enableDefaultActionTripleClick(
false);
104 }
catch (AL::ALError &e) {
105 throw Exception(
"Checking module availability failed: %s", e.toString().c_str());
124 chestbut_remote_enabled_ =
false;
125 lfoot_bumper_remote_enabled_ = rfoot_bumper_remote_enabled_ =
false;
126 head_front_remote_enabled_ = head_middle_remote_enabled_ = head_rear_remote_enabled_ =
false;
145 lfoot_bumper_if_ = NULL;
146 rfoot_bumper_if_ = NULL;
147 head_front_if_ = NULL;
148 head_middle_if_ = NULL;
149 head_rear_if_ = NULL;
153 auplayer_->unloadFile(sound_longpling_);
154 auplayer_->unloadFile(sound_pling_);
155 auplayer_->unloadFile(sound_bumper_left_);
156 auplayer_->unloadFile(sound_bumper_right_);
170 process_pattern_button(chestbut_if_,
173 chestbut_remote_enabled_,
176 process_pattern_button(head_front_if_,
179 head_front_remote_enabled_);
180 process_pattern_button(head_middle_if_,
183 head_middle_remote_enabled_);
184 process_pattern_button(head_rear_if_,
187 head_rear_remote_enabled_);
189 process_bumpers(lfoot_bumper_if_,
193 lfoot_bumper_remote_enabled_,
196 process_bumpers(rfoot_bumper_if_,
200 rfoot_bumper_remote_enabled_,
201 sound_bumper_right_);
203 if (cfg_chest_triple_long_click_shutdown_ && chestbut_if_->
long_activations() == 3
208 auplayer_->playFile(RESDIR
"/sounds/naoshutdown.wav");
211 if (stat(POWEROFF_PATH, &s) == -1) {
214 if (s.st_mode & S_ISUID) {
215 int rv = system(POWEROFF_PATH);
216 if (rv == -1 || (WEXITSTATUS(rv) != 0)) {
231 unsigned int activations,
232 unsigned int short_act,
233 unsigned int long_act)
252 bool & remote_enabled,
257 process_messages(switch_if, remote_enabled, value);
258 value = std::max(value, sensor_value);
260 bool enabled =
false;
261 float history = switch_if->
history();
266 pattern_button_logic(value,
276 set_interface(switch_if, enabled, value, history, activations, short_act, long_act);
284 bool & remote_enabled,
288 process_messages(switch_if, remote_enabled, value);
289 value = std::max(std::max(value, left_value), right_value);
291 bool enabled =
false;
292 float history = switch_if->
history();
297 bumpers_logic(value, time_diff_sec, enabled, history, activations, sound_id);
299 set_interface(switch_if, enabled, value, history, activations, short_act, long_act);
303NaoQiButtonThread::process_messages(
SwitchInterface *switch_if,
bool &remote_enabled,
float &value)
307 if (msg->is_enabled()) {
308 value = std::min(0.5f, msg->value());
310 value = std::max(0.49f, msg->value());
316 remote_enabled =
true;
320 remote_enabled =
false;
332NaoQiButtonThread::pattern_button_logic(
float value,
336 unsigned int &activations,
337 unsigned int &short_act,
338 unsigned int &long_act,
343 if (history > 0.025 ) {
347 if (auplayer_ && (sound_long != -1))
348 auplayer_->play(sound_long);
351 if (auplayer_ && (sound_short != -1))
352 auplayer_->play(sound_short);
354 }
else if (history < -2.0 ) {
356 short_act = long_act = 0;
376 if (history < -120.) {
378 }
else if (history > 120.) {
384NaoQiButtonThread::bumpers_logic(
float value,
388 unsigned int &activations,
400 if (history <= 0. ) {
401 if (auplayer_ && (sound_id != -1))
402 auplayer_->play(sound_id);
415 if (history < -120.) {
417 }
else if (history > 120.) {
BlackBoard * blackboard
This is the BlackBoard instance you can use to interact with the BlackBoard.
virtual Interface * open_for_reading(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for reading.
virtual Interface * open_for_writing(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for writing.
virtual void close(Interface *interface)=0
Close interface.
Thread aspect to use blocked timing.
Clock * clock
By means of this member access to the clock is given.
Configuration * config
This is the Configuration member used to access the configuration.
virtual bool get_bool(const char *path)=0
Get value from configuration which is of type bool.
Base class for exceptions in Fawkes.
void copy_shared_to_buffer(unsigned int buffer)
Copy data from private memory to buffer.
void msgq_pop()
Erase first message from queue.
void resize_buffers(unsigned int num_buffers)
Resize buffer array.
void write()
Write from local copy into BlackBoard memory.
const char * id() const
Get identifier of interface.
bool msgq_empty()
Check if queue is empty.
void read()
Read from BlackBoard into local copy.
int compare_buffers(unsigned int buffer)
Compare buffer to private memory.
MessageType * msgq_first_safe(MessageType *&msg) noexcept
Get first message casted to the desired type without exceptions.
virtual void log_debug(const char *component, const char *format,...)=0
Log debug message.
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
virtual void log_error(const char *component, const char *format,...)=0
Log error message.
Logger * logger
This is the Logger member used to access the logger.
AL::ALPtr< AL::ALBroker > naoqi_broker
NaoQi broker.
NaoSensorInterface Fawkes BlackBoard Interface.
uint8_t head_touch_rear() const
Get head_touch_rear value.
uint8_t l_foot_bumper_l() const
Get l_foot_bumper_l value.
uint8_t chest_button() const
Get chest_button value.
uint8_t r_foot_bumper_l() const
Get r_foot_bumper_l value.
uint8_t l_foot_bumper_r() const
Get l_foot_bumper_r value.
uint8_t head_touch_middle() const
Get head_touch_middle value.
uint8_t head_touch_front() const
Get head_touch_front value.
uint8_t r_foot_bumper_r() const
Get r_foot_bumper_r value.
DisableSwitchMessage Fawkes BlackBoard Interface Message.
EnableSwitchMessage Fawkes BlackBoard Interface Message.
SetMessage Fawkes BlackBoard Interface Message.
SwitchInterface Fawkes BlackBoard Interface.
float history() const
Get history value.
uint32_t short_activations() const
Get short_activations value.
void set_long_activations(const uint32_t new_long_activations)
Set long_activations value.
void set_short_activations(const uint32_t new_short_activations)
Set short_activations value.
uint32_t long_activations() const
Get long_activations value.
void set_enabled(const bool new_enabled)
Set enabled value.
void set_activation_count(const uint32_t new_activation_count)
Set activation_count value.
void set_value(const float new_value)
Set value value.
void set_history(const float new_history)
Set history value.
uint32_t activation_count() const
Get activation_count value.
Thread class encapsulation of pthreads.
const char * name() const
Get name of thread.
void set_clock(Clock *clock)
Set clock for this instance.
Time & stamp()
Set this time to the current time.
Fawkes library namespace.
double time_diff_sec(const timeval &a, const timeval &b)
Calculate time difference of two time structs.