Thunderbots Project
Loading...
Searching...
No Matches
network.h
1#pragma once
2
3#include <mutex>
4#include <queue>
5
6#include "proto/ip_notification.pb.h"
7#include "proto/primitive.pb.h"
8#include "proto/robot_log_msg.pb.h"
9#include "proto/robot_status_msg.pb.h"
10#include "shared/constants.h"
11#include "shared/robot_constants.h"
12#include "software/embedded/services/network/proto_tracker.h"
13#include "software/networking/radio/threaded_proto_radio_listener.hpp"
14#include "software/networking/udp/threaded_proto_udp_listener.hpp"
15#include "software/networking/udp/threaded_proto_udp_sender.hpp"
16#include "software/time/duration.h"
17#include "software/time/timestamp.h"
18#include "software/world/robot_state.h"
19
21{
22 public:
38 NetworkService(const RobotId& robot_id, const std::string& ip_address,
39 unsigned short primitive_listener_port,
40 unsigned short robot_status_sender_port,
41 unsigned short full_system_to_robot_ip_notification_port,
42 unsigned short robot_to_full_system_ip_notification_port,
43 unsigned short robot_logs_port, const std::string& interface);
44
51 TbotsProto::Primitive poll(TbotsProto::RobotStatus& robot_status);
52
53 private:
66 bool shouldSendNewRobotStatus(const TbotsProto::RobotStatus& robot_status) const;
67
73 void logNewPrimitive(const TbotsProto::Primitive& new_primitive);
74
80 void updatePrimitiveLog(TbotsProto::RobotStatus& robot_status);
81
87 double getCurrentEpochTimeInSeconds();
88
94 void primitiveCallback(const TbotsProto::Primitive& input);
95
101 void onFullSystemIpNotification(const TbotsProto::IpNotification& ip_notification);
102
108 void sendRobotStatus(const TbotsProto::RobotStatus& robot_status);
109
110 // Constants
111 static constexpr unsigned int ROBOT_STATUS_BROADCAST_RATE_HZ = 30;
112 static constexpr double ROBOT_STATUS_TO_THUNDERLOOP_HZ_RATIO =
113 ROBOT_STATUS_BROADCAST_RATE_HZ / (THUNDERLOOP_HZ + 1.0);
114 static constexpr int IP_DISCOVERY_NOTIFICATION_RATE_HZ = 1 * THUNDERLOOP_HZ;
115
116 // increases size of deque when robot status messages are sent less frequently
117 static constexpr unsigned int PRIMITIVE_DEQUE_MAX_SIZE =
118 static_cast<unsigned int>(1500 / ROBOT_STATUS_BROADCAST_RATE_HZ);
119
120 // Variables
121
122 // Mutex protects the primitive message
123 std::mutex primitive_mutex;
124 TbotsProto::Primitive primitive_msg;
125
126 // Mutex protects the fullsystem IP address
127 std::mutex fullsystem_ip_mutex;
128 std::optional<std::string> fullsystem_ip;
129
130 // Mutex protects the robot status sender
131 std::mutex robot_status_sender_mutex;
132 std::unique_ptr<ThreadedProtoUdpSender<TbotsProto::RobotStatus>> robot_status_sender;
133
134 std::unique_ptr<ThreadedProtoUdpListener<TbotsProto::IpNotification>>
135 fullsystem_to_robot_ip_listener;
136 std::unique_ptr<ThreadedProtoUdpSender<TbotsProto::IpNotification>>
137 robot_to_fullsystem_ip_sender;
138 std::unique_ptr<ThreadedProtoUdpListener<TbotsProto::Primitive>>
139 udp_listener_primitive;
140 std::unique_ptr<ThreadedProtoRadioListener<TbotsProto::Primitive>>
141 radio_listener_primitive_set;
142 std::shared_ptr<ThreadedProtoUdpSender<TbotsProto::RobotLog>> robot_log_sender;
143
144 // The network interface to listen and send messages on
145 std::string interface;
146
147 // Port to send robot status messages
148 unsigned short robot_status_sender_port;
149
150 // Counters for tracking rate-limited events
151 unsigned int ip_notification_ticks = 0;
152 unsigned int network_ticks = 0;
153 unsigned int thunderloop_ticks = 0;
154
155 // ProtoTrackers for tracking recent primitive_set packet loss
156 ProtoTracker primitive_tracker;
157
158 // track last breakbeam state for sending RobotStatus outside of specified rate
159 bool last_breakbeam_state_sent = false;
160
161 struct RoundTripTime
162 {
163 // Primitive Sequence Number
164 uint64_t primitive_sequence_num = 0;
165 // Epoch time of primitive set sent time from Thunderscope in seconds
166 double thunderscope_sent_time_seconds = 0;
167 // System time for when primitive set was received by Thunderloop in seconds
168 double thunderloop_recieved_time_seconds = 0;
169 };
170
171 // Stores the most recent primitives for calculating round-trip time
172 std::deque<RoundTripTime> primitive_rtt;
173
174 // IP discovery message to send on the network
175 TbotsProto::IpNotification robot_ip_notification_msg;
176};
Definition network.h:21
TbotsProto::Primitive poll(TbotsProto::RobotStatus &robot_status)
Definition network.cpp:95
Definition proto_tracker.h:7