JaiaBot 2.1.1
JaiaBot micro-AUV software
 
Loading...
Searching...
No Matches
xbee.h
Go to the documentation of this file.
1// Copyright 2011-2021:
2// GobySoft, LLC (2013-)
3// Massachusetts Institute of Technology (2007-2014)
4// Community contributors (see AUTHORS file)
5// File authors:
6// Toby Schneider <toby@gobysoft.org>
7// Ed Sanville <edsanville@gmail.com>
8//
9//
10// This file is part of the Goby Underwater Autonomy Project Libraries
11// ("The Goby Libraries").
12//
13// The Goby Libraries are free software: you can redistribute them and/or modify
14// them under the terms of the GNU Lesser General Public License as published by
15// the Free Software Foundation, either version 2.1 of the License, or
16// (at your option) any later version.
17//
18// The Goby Libraries are distributed in the hope that they will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU Lesser General Public License for more details.
22//
23// You should have received a copy of the GNU Lesser General Public License
24// along with Goby. If not, see <http://www.gnu.org/licenses/>.
25
26#ifndef XBEE_H
27#define XBEE_H
28
29#include <boost/asio.hpp> // for serial_port
30#include <map> // for map
31#include <queue>
32#include <string> // for string
33
34// basic file operations
35#include <fstream>
36#include <iostream>
37
38#include <functional>
39
40#include "xbee.pb.h"
41
42namespace jaiabot
43{
44namespace comms
45{
46typedef unsigned char byte;
47
48// SerialNumber is an immutable value that uniquely identifies an XBee modem
49// The XBee can only address packets to serial numbers, not to the user-configurable NodeId
50// However, the SerialNumber can be modified using ATUL (low 32-bit word) and ATUH (high 32-bit word)
51typedef uint64_t SerialNumber;
52
53// NodeId is a user-configurable id for an XBee modem, which corresponds to the modem_id from Goby
54typedef std::string NodeId;
55
56enum class NodeType
57{
58 HUB,
59 BOT
60};
61
62inline SerialNumber serial_from_node_data(NodeType type, int fleet_id, int bot_or_hub_id)
63{
64 // TES: testing with XBee on 1/27/25 seems to show that ATUH can't be any arbitrary value but 13A200 (the ATSH value for the modem I was testing on) seems to work fine
65 SerialNumber high_word = 0x13A200;
66 SerialNumber fleet64 = fleet_id & 0xFFFF, type64 = (type == NodeType::HUB) ? 0x0 : 0x1,
67 id64 = bot_or_hub_id & 0x7FFF;
68 SerialNumber low_word = ((fleet64 << 16) + (type64 << 15) + id64) & 0xFFFFFFFF;
69
70 return (high_word << 32) + low_word;
71}
72
73// Packet structure for queuing up packets for transmit
74struct Packet
75{
77 std::string data;
78
79 Packet() {}
80 Packet(const NodeId& dest, const std::string& data) : dest(dest), data(data) {}
81};
82
84{
85 public:
87 void startup(const std::string& port_name, const int baud_rate, const NodeId& my_node_id,
88 const uint16_t network_id, const std::string& xbee_info_location,
89 const bool& use_encryption, const std::string& encryption_password,
90 const std::string& mesh_unicast_retries, const std::string& unicast_mac_retries,
91 const std::string& network_delay_slots,
92 const std::string& broadcast_multi_transmits, int fleet, unsigned subnet_mask);
93 void shutdown();
94
95 void send_packet(const NodeId& dest, const std::string& s);
96 void send_test_links(const NodeId& dest, const NodeId& com_dest);
97
98 std::vector<std::string> get_packets();
99
100 void do_work();
101
102 static const NodeId broadcast;
103
105
106 // Adding a peer to the lookup table
107 void add_peer(const NodeId node_id, NodeType type, int bot_or_hub_id);
108
109 // Get Diagnostics
111
112 private:
113 static const SerialNumber broadcast_serial_number;
114
115 std::shared_ptr<boost::asio::io_context> io;
116 boost::asio::serial_port* port;
117 NodeId my_node_id;
118 SerialNumber my_serial_number;
119 byte frame_id;
120 std::string glog_group;
121 int fleet_id_{-1};
122 unsigned subnet_mask_{0xFFFF};
123
124 // Map of node_id onto serial number
125 std::map<NodeId, SerialNumber> node_id_to_serial_number_map;
126
127 std::vector<std::string> received_packets;
128
129 // Called during startup
130 void get_my_serial_number();
131 void get_maximum_payload_size();
132 void broadcast_node_id();
133
134 // Packet sending
135 void send_packet(const SerialNumber& dest, const std::string& data);
136
137 // Low level reads and writes
138 void write(const std::string& raw);
139 std::string read_until(const std::string& delimiter);
140 size_t bytes_available();
141 void read(void* ptr, const size_t n_bytes);
142 void async_read_with_timeout(std::string& buffer, const std::string& delimiter,
143 int timeout_seconds,
144 std::function<void(const std::string&)> handler);
145
146 // Convert string to hex
147 std::string convertToHex(const std::string& str);
148
149 // Command mode stuff
150 void enter_command_mode();
151 void assert_ok();
152 void exit_command_mode();
153
154 // Frame stuff
155 std::string read_frame();
156
157 // API mode stuff
158 SerialNumber read_frame_discover_node_response(const NodeId& node_id);
159 SerialNumber get_serial_number(const NodeId& node_id);
160 std::string api_transmit_request(const SerialNumber& dest, const byte frame_id, const byte* ptr,
161 const size_t length);
162 std::string api_explicit_transmit_request(const SerialNumber& dest,
163 const SerialNumber& com_dest, const byte frame_id);
164 // Processing frames
165 void process_frame();
166 void process_frame_if_available();
167 void process_frame_extended_transmit_status(const std::string& response_string);
168 void process_frame_at_command_response(const std::string& response_string);
169 void process_frame_receive_packet(const std::string& response_string);
170 void process_frame_node_identification_indicator(const std::string& response_string);
171 void process_frame_explicit_rx_indicator(const std::string& response_string);
172
173 // Query RSSI from Radio
174 void query_rssi();
175 // Query Received Error Count
176 void query_er();
177 // Query Received Good Count
178 void query_gd();
179 // Query Bytes Transmitted
180 void query_bc();
181 // Query Transmission Failure Count
182 void query_tr();
183
184 // Check if we received diagnostics
185 bool received_rssi_{false};
186 bool received_er_{false};
187 bool received_gd_{false};
188 bool received_bc_{false};
189 bool received_tr_{false};
190
191 // RSSI fields
192 uint16_t current_rssi_{0};
193 uint16_t history_rssi_{0};
194 int rssi_query_count_{1};
195 uint16_t max_rssi_{0};
196 uint16_t min_rssi_{150};
197 uint16_t average_rssi_{0};
198
199 // Bytes Transmitted
200 uint32_t bytes_transmitted_{0};
201
202 // Received Error Count
203 uint16_t received_error_count_{0};
204
205 // Received Good Count
206 uint16_t received_good_count_{0};
207
208 // Transmission Failure Count
209 uint16_t transmission_failure_count_{0};
210
211 std::string my_xbee_info_location_{""};
212};
213} // namespace comms
214} // namespace jaiabot
215
216#endif
static const NodeId broadcast
Definition xbee.h:102
uint16_t max_payload_size
Definition xbee.h:104
std::vector< std::string > get_packets()
void send_packet(const NodeId &dest, const std::string &s)
void send_test_links(const NodeId &dest, const NodeId &com_dest)
void startup(const std::string &port_name, const int baud_rate, const NodeId &my_node_id, const uint16_t network_id, const std::string &xbee_info_location, const bool &use_encryption, const std::string &encryption_password, const std::string &mesh_unicast_retries, const std::string &unicast_mac_retries, const std::string &network_delay_slots, const std::string &broadcast_multi_transmits, int fleet, unsigned subnet_mask)
void add_peer(const NodeId node_id, NodeType type, int bot_or_hub_id)
uint64_t SerialNumber
Definition xbee.h:51
SerialNumber serial_from_node_data(NodeType type, int fleet_id, int bot_or_hub_id)
Definition xbee.h:62
unsigned char byte
Definition xbee.h:46
std::string NodeId
Definition xbee.h:54
Packet(const NodeId &dest, const std::string &data)
Definition xbee.h:80
std::string data
Definition xbee.h:77