JaiaBot 1.10.0+14+g8dbf2589
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 "xbee.pb.h"
39
40namespace jaiabot
41{
42namespace comms
43{
44typedef unsigned char byte;
45
46// SerialNumber is an immutable value that uniquely identifies an XBee modem
47// The XBee can only address packets to serial numbers, not to the user-configurable NodeId
48typedef uint64_t SerialNumber;
49
50// NodeId is a user-configurable id for an XBee modem, which corresponds to the modem_id from Goby
51typedef std::string NodeId;
52
53// Packet structure for queuing up packets for transmit
54struct Packet
55{
57 std::string data;
58
59 Packet() {}
60 Packet(const NodeId& dest, const std::string& data) : dest(dest), data(data) {}
61};
62
64{
65 public:
67 void startup(const std::string& port_name, const int baud_rate, const NodeId& my_node_id,
68 const uint16_t network_id, const std::string& xbee_info_location,
69 const bool& use_encryption, const std::string& encryption_password);
70 void shutdown();
71
72 std::vector<NodeId> get_peers();
73
74 void send_packet(const NodeId& dest, const std::string& s);
75 void send_test_links(const NodeId& dest, const NodeId& com_dest);
76
77 std::vector<std::string> get_packets();
78
79 void do_work();
80
81 static const NodeId broadcast;
82
84
85 // Adding a peer to the lookup table
86 void add_peer(const NodeId node_id, const SerialNumber serial_number);
87
88 // Get Diagnostics
90
91 private:
92 static const SerialNumber broadcast_serial_number;
93
94 boost::asio::io_service* io;
95 boost::asio::serial_port* port;
96 NodeId my_node_id;
97 SerialNumber my_serial_number;
98 byte frame_id;
99 std::string glog_group;
100
101 // Map of node_id onto serial number
102 std::map<NodeId, SerialNumber> node_id_to_serial_number_map;
103 std::map<SerialNumber, NodeId> serial_number_to_node_id_map;
104
105 std::vector<std::string> received_packets;
106
107 // Called during startup
108 void get_my_serial_number();
109 void get_maximum_payload_size();
110 void broadcast_node_id();
111
112 // Packet sending
113 void send_packet(const SerialNumber& dest, const std::string& data);
114
115 // Low level reads and writes
116 void write(const std::string& raw);
117 std::string read_until(const std::string& delimiter);
118 size_t bytes_available();
119 void read(void* ptr, const size_t n_bytes);
120
121 // Command mode stuff
122 void enter_command_mode();
123 void assert_ok();
124 void exit_command_mode();
125
126 // Frame stuff
127 std::string read_frame();
128
129 // API mode stuff
130 SerialNumber read_frame_discover_node_response(const NodeId& node_id);
131 SerialNumber get_serial_number(const NodeId& node_id);
132 std::string api_transmit_request(const SerialNumber& dest, const byte frame_id, const byte* ptr,
133 const size_t length);
134 std::string api_explicit_transmit_request(const SerialNumber& dest,
135 const SerialNumber& com_dest, const byte frame_id);
136 // Processing frames
137 void process_frame();
138 void process_frame_if_available();
139 void process_frame_extended_transmit_status(const std::string& response_string);
140 void process_frame_at_command_response(const std::string& response_string);
141 void process_frame_receive_packet(const std::string& response_string);
142 void process_frame_node_identification_indicator(const std::string& response_string);
143 void process_frame_explicit_rx_indicator(const std::string& response_string);
144
145 // Query RSSI from Radio
146 void query_rssi();
147 // Query Received Error Count
148 void query_er();
149 // Query Received Good Count
150 void query_gd();
151 // Query Bytes Transmitted
152 void query_bc();
153 // Query Transmission Failure Count
154 void query_tr();
155
156 // Check if we received diagnostics
157 bool received_rssi_{false};
158 bool received_er_{false};
159 bool received_gd_{false};
160 bool received_bc_{false};
161 bool received_tr_{false};
162
163 // RSSI fields
164 uint16_t current_rssi_{0};
165 uint16_t history_rssi_{0};
166 int rssi_query_count_{1};
167 uint16_t max_rssi_{0};
168 uint16_t min_rssi_{150};
169 uint16_t average_rssi_{0};
170
171 // Bytes Transmitted
172 uint32_t bytes_transmitted_{0};
173
174 // Received Error Count
175 uint16_t received_error_count_{0};
176
177 // Received Good Count
178 uint16_t received_good_count_{0};
179
180 // Transmission Failure Count
181 uint16_t transmission_failure_count_{0};
182
183 std::string my_xbee_info_location_{""};
184};
185} // namespace comms
186} // namespace jaiabot
187
188#endif
void add_peer(const NodeId node_id, const SerialNumber serial_number)
static const NodeId broadcast
Definition xbee.h:81
uint16_t max_payload_size
Definition xbee.h:83
std::vector< std::string > get_packets()
void send_packet(const NodeId &dest, const std::string &s)
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)
void send_test_links(const NodeId &dest, const NodeId &com_dest)
std::vector< NodeId > get_peers()
uint64_t SerialNumber
Definition xbee.h:48
unsigned char byte
Definition xbee.h:44
std::string NodeId
Definition xbee.h:51
Packet(const NodeId &dest, const std::string &data)
Definition xbee.h:60
std::string data
Definition xbee.h:57