1
1
#include < signal.h>
2
2
3
- #include < bitbots_localization/srv/reset_filter.hpp>
4
3
#include < bitbots_msgs/msg/audio.hpp>
5
4
#include < bitbots_msgs/msg/buttons.hpp>
6
5
#include < bitbots_msgs/srv/manual_penalize.hpp>
6
+ #include < bitbots_msgs/srv/set_teaching_mode.hpp>
7
+ #include < game_controller_hl_interfaces/msg/game_state.hpp>
7
8
#include < rclcpp/experimental/executors/events_executor/events_executor.hpp>
8
9
#include < rclcpp/rclcpp.hpp>
9
10
#include < std_msgs/msg/bool.hpp>
@@ -21,38 +22,25 @@ class ButtonNode : public rclcpp::Node {
21
22
this ->declare_parameter <bool >(" speak_active" , true );
22
23
this ->declare_parameter <double >(" short_time" , 2.0 );
23
24
this ->declare_parameter <bool >(" manual_penalty" , true );
24
- this ->declare_parameter <bool >(" in_game" , true );
25
25
this ->declare_parameter <double >(" debounce_time" , 0.1 );
26
26
this ->declare_parameter <bool >(" speak" , true );
27
27
28
28
this ->get_parameter (" speak_active" , speaking_active_);
29
29
this ->get_parameter (" short_time" , short_time_);
30
30
this ->get_parameter (" manual_penalty" , manual_penalty_mode_);
31
- this ->get_parameter (" in_game" , in_game_);
32
31
this ->get_parameter (" debounce_time" , debounce_time_);
33
32
this ->get_parameter (" speak" , speak_);
34
33
35
- // --- Class variables ---
36
- button1_ = false ;
37
- button2_ = false ;
38
- button3_ = false ;
39
- button1_time_ = 0.0 ;
40
- button2_time_ = 0.0 ;
41
- button3_time_ = 0.0 ;
42
-
43
34
// --- Initialize Topics ---
44
35
speak_pub_ = this ->create_publisher <bitbots_msgs::msg::Audio>(" /speak" , 1 );
45
- shoot_publisher_ = this ->create_publisher <std_msgs::msg::Bool>(" /shoot_button" , 1 );
46
36
47
37
if (manual_penalty_mode_) {
48
38
manual_penalize_client_ = this ->create_client <bitbots_msgs::srv::ManualPenalize>(" manual_penalize" );
49
39
manual_penalty_mode_ = manual_penalize_client_->wait_for_service (3s);
50
40
}
51
41
52
- if (!in_game_) {
53
- foot_zero_client_ = this ->create_client <std_srvs::srv::Empty>(" set_foot_zero" );
54
- foot_zero_available_ = foot_zero_client_->wait_for_service (3s);
55
- }
42
+ foot_zero_client_ = this ->create_client <std_srvs::srv::Empty>(" set_foot_zero" );
43
+ foot_zero_available_ = foot_zero_client_->wait_for_service (3s);
56
44
57
45
power_client_ = this ->create_client <std_srvs::srv::SetBool>(" /core/switch_power" );
58
46
while (!power_client_->wait_for_service (3s)) {
@@ -63,12 +51,16 @@ class ButtonNode : public rclcpp::Node {
63
51
RCLCPP_INFO (this ->get_logger (), " service switch_power not available, waiting again..." );
64
52
}
65
53
66
- localization_client_ = this ->create_client <bitbots_localization::srv::ResetFilter>(" reset_localization" );
67
- localization_available_ = localization_client_->wait_for_service (3s);
54
+ teaching_mode_client_ = this ->create_client <bitbots_msgs::srv::SetTeachingMode>(" set_teaching_mode" );
68
55
buttons_sub_ = this ->create_subscription <bitbots_msgs::msg::Buttons>(
69
56
" /buttons" , 1 , std::bind (&bitbots_buttons::ButtonNode::buttonCb, this , _1));
57
+ gamestate_sub_ = this ->create_subscription <game_controller_hl_interfaces::msg::GameState>(
58
+ " gamestate" , 1 , std::bind (&bitbots_buttons::ButtonNode::gamestateCb, this , _1));
70
59
}
71
60
61
+ // Sets the in_game_ variable to true, if a Gamestate message from the Gamecontroller arrives.
62
+ void gamestateCb (const game_controller_hl_interfaces::msg::GameState::SharedPtr msg) { in_game_ = true ; }
63
+
72
64
void buttonCb (const bitbots_msgs::msg::Buttons::SharedPtr msg) {
73
65
// button1 - red
74
66
// button2 - green
@@ -90,12 +82,11 @@ class ButtonNode : public rclcpp::Node {
90
82
if (current_time - button1_time_ > debounce_time_) {
91
83
if (current_time - button1_time_ < short_time_ || in_game_) {
92
84
// button 1 short
93
- speak (" 1 short" );
94
- setPower (false );
85
+ speak (" Red button pressed short. Turning motor power off. " );
86
+ setPower (false ); // this can not be reversed because the button cuts the power of himself
95
87
} else {
96
88
// button 1 long
97
- speak (" 1 long" );
98
- setPower (true );
89
+ speak (" Red button pressed long. No action implemented." );
99
90
}
100
91
}
101
92
button1_time_ = 0 ;
@@ -105,12 +96,12 @@ class ButtonNode : public rclcpp::Node {
105
96
double current_time = this ->get_clock ()->now ().seconds ();
106
97
if (current_time - button2_time_ > debounce_time_) {
107
98
if (current_time - button2_time_ < short_time_ || in_game_) {
108
- speak (" 2 short" );
99
+ speak (" Green button pressed short. Try deactivating Penalty mode " );
109
100
setPenalty (false );
110
- resetLocalization ();
111
101
} else {
112
- speak (" 2 long" );
113
- setPenalty (false );
102
+ speak (" Green button pressed long. Try deactivating teaching mode" );
103
+ // Turn teaching mode off
104
+ setTeachingMode (false );
114
105
}
115
106
}
116
107
button2_time_ = 0 ;
@@ -120,17 +111,30 @@ class ButtonNode : public rclcpp::Node {
120
111
double current_time = this ->get_clock ()->now ().seconds ();
121
112
if (current_time - button3_time_ > debounce_time_) {
122
113
if (current_time - button3_time_ < short_time_ || in_game_) {
123
- speak (" 3 short" );
114
+ speak (" Blue button pressed short. Try activating Penalty mode " );
124
115
setPenalty (true );
125
116
} else {
126
- speak (" 3 long" );
127
- setPenalty (true );
117
+ speak (" Blue button pressed long. Try switching teaching mode state" );
118
+ // Turn teaching mode on or switch between HOLD and TEACH
119
+ setTeachingMode (true );
128
120
}
129
121
}
130
122
button3_time_ = 0 ;
131
123
}
132
124
}
133
125
126
+ void setTeachingMode (bool state) {
127
+ auto request = std::make_shared<bitbots_msgs::srv::SetTeachingMode::Request>();
128
+ if (state) {
129
+ // switch teaching mode state to SWITCH
130
+ request->state = bitbots_msgs::srv::SetTeachingMode::Request::SWITCH;
131
+ } else {
132
+ // switch teaching mode state to OFF
133
+ request->state = bitbots_msgs::srv::SetTeachingMode::Request::OFF;
134
+ }
135
+ teaching_mode_client_->async_send_request (request);
136
+ }
137
+
134
138
void setPenalty (bool penalize) {
135
139
// Penalizes the robot, if it is not penalized and manual penalty mode is true.
136
140
if (manual_penalty_mode_) {
@@ -160,16 +164,6 @@ class ButtonNode : public rclcpp::Node {
160
164
power_client_->async_send_request (request);
161
165
}
162
166
163
- void resetLocalization () {
164
- if (localization_available_) {
165
- auto request = std::make_shared<bitbots_localization::srv::ResetFilter::Request>();
166
- request->init_mode = 0 ;
167
- localization_client_->async_send_request (request);
168
- } else {
169
- RCLCPP_WARN (this ->get_logger (), " service not available" );
170
- }
171
- }
172
-
173
167
private:
174
168
void speak (const std::string& text) {
175
169
/* *
@@ -186,26 +180,25 @@ class ButtonNode : public rclcpp::Node {
186
180
bool speaking_active_;
187
181
double short_time_;
188
182
bool manual_penalty_mode_;
189
- bool in_game_;
183
+ bool in_game_ = false ;
190
184
double debounce_time_;
191
185
bool speak_;
192
186
193
- bool button1_;
194
- bool button2_;
195
- bool button3_;
196
- bool localization_available_;
187
+ bool button1_ = false ;
188
+ bool button2_ = false ;
189
+ bool button3_ = false ;
197
190
bool foot_zero_available_;
198
- double button1_time_;
199
- double button2_time_;
200
- double button3_time_;
191
+ double button1_time_ = 0.0 ;
192
+ double button2_time_ = 0.0 ;
193
+ double button3_time_ = 0.0 ;
201
194
202
195
rclcpp::Publisher<bitbots_msgs::msg::Audio>::SharedPtr speak_pub_;
203
- rclcpp::Publisher<std_msgs::msg::Bool>::SharedPtr shoot_publisher_;
204
196
rclcpp::Client<bitbots_msgs::srv::ManualPenalize>::SharedPtr manual_penalize_client_;
197
+ rclcpp::Client<bitbots_msgs::srv::SetTeachingMode>::SharedPtr teaching_mode_client_;
205
198
rclcpp::Client<std_srvs::srv::Empty>::SharedPtr foot_zero_client_;
206
199
rclcpp::Client<std_srvs::srv::SetBool>::SharedPtr power_client_;
207
- rclcpp::Client<bitbots_localization::srv::ResetFilter>::SharedPtr localization_client_;
208
200
rclcpp::Subscription<bitbots_msgs::msg::Buttons>::SharedPtr buttons_sub_;
201
+ rclcpp::Subscription<game_controller_hl_interfaces::msg::GameState>::SharedPtr gamestate_sub_;
209
202
};
210
203
} // namespace bitbots_buttons
211
204
0 commit comments