forked from rockin-robot-challenge/at_home_rsbb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore_public_channel.h
158 lines (134 loc) · 5.79 KB
/
core_public_channel.h
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
/*
* Copyright 2014 Instituto de Sistemas e Robotica, Instituto Superior Tecnico
*
* This file is part of RoAH RSBB.
*
* RoAH RSBB is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RoAH RSBB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with RoAH RSBB. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __CORE_PUBLIC_CHANNEL_H__
#define __CORE_PUBLIC_CHANNEL_H__
#include "core_includes.h"
#include "core_shared_state.h"
class CorePublicChannel
: boost::noncopyable
, public roah_rsbb::RosPublicChannel
{
CoreSharedState& ss_;
Timer beacon_timer_;
void
transmit_beacon (const TimerEvent& = TimerEvent())
{
ROS_DEBUG ("Transmitting beacon");
roah_rsbb_msgs::RoahRsbbBeacon msg;
for (auto const& i : ss_.benchmarking_robots) {
roah_rsbb_msgs::BenchmarkingTeam* bt = msg.add_benchmarking_teams();
bt->set_team_name (i.first);
bt->set_robot_name (i.second.first);
bt->set_rsbb_port (i.second.second);
}
msg.mutable_devices_bell()->set_sec (ss_.last_devices_state->bell.sec);
msg.mutable_devices_bell()->set_nsec (ss_.last_devices_state->bell.nsec);
msg.set_devices_switch_1 (ss_.last_devices_state->switch_1 != 0);
msg.set_devices_switch_2 (ss_.last_devices_state->switch_2 != 0);
msg.set_devices_switch_3 (ss_.last_devices_state->switch_3 != 0);
msg.set_devices_dimmer (static_cast<uint32_t> (ss_.last_devices_state->dimmer));
msg.set_devices_blinds (static_cast<uint32_t> (ss_.last_devices_state->blinds));
msg.set_tablet_display_map (ss_.tablet_display_map);
if (ss_.last_tablet) {
(* (msg.mutable_tablet_call_time())) = ss_.last_tablet->last_call();
(* (msg.mutable_tablet_position_time())) = ss_.last_tablet->last_pos();
msg.set_tablet_position_x (ss_.last_tablet->x());
msg.set_tablet_position_y (ss_.last_tablet->y());
}
else {
msg.mutable_tablet_call_time()->set_sec (0);
msg.mutable_tablet_call_time()->set_nsec (0);
msg.mutable_tablet_position_time()->set_sec (0);
msg.mutable_tablet_position_time()->set_nsec (0);
msg.set_tablet_position_x (0);
msg.set_tablet_position_y (0);
}
send (msg);
}
void
setup_transmit_beacon (const TimerEvent&)
{
transmit_beacon ();
beacon_timer_.stop();
beacon_timer_ = ss_.nh.createTimer (Duration (1, 0), &CorePublicChannel::transmit_beacon, this);
ss_.status = "OK";
}
void
receive_rsbb_beacon (boost::asio::ip::udp::endpoint endpoint,
uint16_t comp_id,
uint16_t msg_type,
std::shared_ptr<const roah_rsbb_msgs::RoahRsbbBeacon> rsbb_beacon)
{
ROS_FATAL_STREAM ("Another RSBB running at " << endpoint.address().to_string()
<< ":" << endpoint.port());
abort_rsbb();
}
void
receive_robot_beacon (boost::asio::ip::udp::endpoint endpoint,
uint16_t comp_id,
uint16_t msg_type,
std::shared_ptr<const roah_rsbb_msgs::RobotBeacon> msg)
{
Time now = Time::now();
Time msg_time (msg->time().sec(), msg->time().nsec());
Duration skew = msg_time - now;
ROS_DEBUG_STREAM ("Received RobotBeacon from " << endpoint.address().to_string()
<< ":" << endpoint.port()
<< ", COMP_ID " << comp_id
<< ", MSG_TYPE " << msg_type
<< ", team_name: " << msg->team_name()
<< ", robot_name: " << msg->robot_name()
<< ", time: " << msg->time().sec() << "." << msg->time().nsec()
<< ", skew: " << skew);
ss_.active_robots.add (msg->team_name(), msg->robot_name(), skew, now);
}
void
receive_tablet_beacon (boost::asio::ip::udp::endpoint endpoint,
uint16_t comp_id,
uint16_t msg_type,
std::shared_ptr<const roah_rsbb_msgs::TabletBeacon> msg)
{
ROS_DEBUG_STREAM ("Received TabletBeacon from " << endpoint.address().to_string()
<< ":" << endpoint.port()
<< ", COMP_ID " << comp_id
<< ", MSG_TYPE " << msg_type);
ss_.last_tablet_time = Time::now();
ss_.last_tablet = msg;
}
public:
CorePublicChannel (CoreSharedState& ss)
: roah_rsbb::RosPublicChannel (param_direct<string> ("~rsbb_host", "10.255.255.255"),
param_direct<int> ("~rsbb_port", 6666))
, ss_ (ss)
, beacon_timer_ (ss_.nh.createTimer (Duration (5, 0), &CorePublicChannel::setup_transmit_beacon, this, true))
{
set_rsbb_beacon_callback (&CorePublicChannel::receive_rsbb_beacon, this);
set_robot_beacon_callback (&CorePublicChannel::receive_robot_beacon, this);
set_tablet_beacon_callback (&CorePublicChannel::receive_tablet_beacon, this);
ROS_INFO ("Listening only... beacon transmission will start in 5 seconds.");
}
~CorePublicChannel()
{
beacon_timer_.stop();
signal_rsbb_beacon_received().disconnect_all_slots();
signal_robot_beacon_received().disconnect_all_slots();
signal_tablet_beacon_received().disconnect_all_slots();
}
};
#endif