device_configuration.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (Apache 2.0)
3  *
4  * Copyright (c) 2019, The MITRE Corporation.
5  * All rights reserved.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * https://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * Sections of this project contains content developed by The MITRE Corporation.
20  * If this code is used in a deployment or embedded within another project,
21  * it is requested that you send an email to opensource@mitre.org in order to
22  * let us know where this software is being used.
23  *********************************************************************/
24 
26 
27 // RS 232
28 #include "rs232.h"
29 
30 namespace kvh
31 {
32 
54  {
55  if (_packetsRequested.size() > MAXIMUM_PACKET_PERIODS)
56  {
57  return -1;
58  }
59  // Make permanent in case it has a hot reset, otherwise an error is likely
60  _packetPeriods.permanent = 1;
61  // Clear all exisiting packet periods and replace with new ones
62  _packetPeriods.clear_existing_packets = 1;
63 
64  std::set<packet_id_e> packetIdList; // To hold list of already added packet id's
65  // \todo Figure out what to do with duplicated and unsupported variables
66  int duplicated = 0;
67  int unsupported = 0;
68  int currentPacketPeriod = 0;
69 
70  for (int i = 0; i < _packetsRequested.size(); i++)
71  {
72  std::pair<packet_id_e, int> packet = _packetsRequested[i];
73 
74  if (supportedPackets_.count(packet.first) == 0)
75  {
76  unsupported += 1;
77  continue;
78  }
79 
80  if (packetIdList.count(packet.first) > 0)
81  {
82  duplicated += 1; // Found duplicate, increase counter
83  continue;
84  }
85 
86  // packet_period_t period = {packet id, period}
87  packet_period_t period;
88  period.packet_id = _packetsRequested[i].first;
89  period.period = 1000 / _packetsRequested[i].second; // Using formula for rate to period derived above
90  _packetPeriods.packet_periods[currentPacketPeriod] = period;
91 
92  // Record we have seen this packet id to look for duplicates
93  packetIdList.insert(packet.first);
94  currentPacketPeriod++;
95  }
96 
97  return unsupported + duplicated;
98  }
99 
117  filter_options_packet_t &_filterOptions,
118  bool _permanent,
119  uint8_t _vehicle_type,
120  bool _internal_gnss_enabled,
121  bool _atmospheric_altitude_enabled,
122  bool _velocity_heading_enabled,
123  bool _reversing_detection_enabled,
124  bool _motion_analysis_enabled)
125  {
126  if (_vehicle_type > 13)
127  {
128  return -1;
129  }
130 
131  _filterOptions.permanent = _permanent;
132  _filterOptions.vehicle_type = _vehicle_type;
133  _filterOptions.internal_gnss_enabled = _internal_gnss_enabled; // Set if we want to test using gps or not
134  _filterOptions.reserved = 0; // Need to set to 0, have found problems with other packets not doing this
135  _filterOptions.atmospheric_altitude_enabled = _atmospheric_altitude_enabled;
136  _filterOptions.velocity_heading_enabled = _velocity_heading_enabled;
137  _filterOptions.reversing_detection_enabled = _reversing_detection_enabled;
138  _filterOptions.motion_analysis_enabled = _motion_analysis_enabled;
139 
140  return 0;
141  }
142 
155  odometer_configuration_packet_t &_odometerOptions,
156  bool _permanent,
157  float _odom_pulse_to_meters,
158  bool _odom_auto_cal)
159  {
160  _odometerOptions.permanent = _permanent;
161  _odometerOptions.automatic_calibration = _odom_auto_cal;
162  _odometerOptions.pulse_length = _odom_pulse_to_meters;
163 
164  return 0;
165  }
166 
186  int KvhDeviceConfig::SetBaudRate(std::string _port, int _curBaudRate, int _primaryBaudRate, int _gpioBaudRate, int _auxBaudRate)
187  {
188  // Create the baud rate packet that we want to send
189  baud_rates_packet_t baudRatePacket;
190  baudRatePacket.permanent = 1;
191  baudRatePacket.primary_baud_rate = _primaryBaudRate;
192  baudRatePacket.gpio_1_2_baud_rate = _gpioBaudRate;
193  baudRatePacket.auxiliary_baud_rate = _auxBaudRate;
194  baudRatePacket.reserved = 0;
195 
196  an_packet_t *requestPacket = encode_baud_rates_packet(&baudRatePacket);
197 
198  char portArr[4096];
199  strncpy(portArr, _port.c_str(), 4096);
200  if (OpenComport(portArr, _curBaudRate) != 0)
201  {
202  return -1;
203  }
204 
205  an_packet_encode(requestPacket);
206  // Send AN packet via serial port
207  if (!SendBuf(an_packet_pointer(requestPacket), an_packet_size(requestPacket)))
208  {
209  return -2;
210  }
211  an_packet_free(&requestPacket);
212  requestPacket = nullptr;
213 
214  CloseComport();
215 
216  return 0;
217  }
218 
228  int KvhDeviceConfig::FindCurrentBaudRate(std::string _port, int _startingBaud)
229  {
230  std::vector<int> baudRates = {
231  1200, 1800, 2400, 4800, 9600,
232  19200, 57600, 115200, 230400,
233  460800, 500000, 576000, 921600, 1000000};
234 
235  char portArr[4096];
236  strncpy(portArr, _port.c_str(), 4096);
237 
238  for (int baudRate : baudRates)
239  {
240  // Skip over baud rates lower than what we want to start at
241  if (baudRate < _startingBaud)
242  {
243  continue;
244  }
245 
246  printf("Opening with port: %s, baudrate: %d...\n", portArr, baudRate);
247  if (OpenComport(portArr, baudRate) != 0)
248  {
249  continue; // Try next baud rate
250  }
251 
252  an_decoder_t anDecoder;
253  an_packet_t *anPacket;
254  an_decoder_initialise(&anDecoder);
255 
256  // Check for data over the next second
257  for (int i = 0; i < 20; i++)
258  {
259  int bytesRec = 0;
260 
261  // Check if new packets have been sent
262  if ((bytesRec = PollComport(an_decoder_pointer(&anDecoder), an_decoder_size(&anDecoder))) > 0)
263  {
264  an_decoder_increment(&anDecoder, bytesRec);
265  // See if we can decode a packet
266  while ((anPacket = an_packet_decode(&anDecoder)) != NULL)
267  {
268  // If so, we have found the correct baud rate
269  printf("Found a packet\n");
270  CloseComport();
271  an_packet_free(&anPacket);
272  printf("Found connection at %d baud\n", baudRate);
273  return baudRate;
274  }
275  }
276  usleep(10000);
277  }
278  CloseComport();
279  }
280  return -1;
281  }
282 
300  {
301  std::set<packet_id_e> packetIdList;
302  int dataThroughput = 0;
303 
304  for (int i = 0; i < _packetsRequested.size(); i++)
305  {
306  std::pair<packet_id_e, uint16_t> packet = _packetsRequested[i];
307 
308  // Check for duplicate packets or unsupported packet id's
309  if (packetSize_.count(packet.first) == 0)
310  {
311  return -1;
312  }
313  else if (packetIdList.count(packet.first) > 0)
314  {
315  return -2;
316  }
317 
318  // Max allowed hz is 1000
319  if (packet.second > 1000)
320  {
321  return -3;
322  }
323 
324  // Increase required baudrate by (struct_size + 5) * rate
325  dataThroughput += (packetSize_[packet.first] + 5) * packet.second;
326  packetIdList.insert(packet.first);
327  }
328 
329  return dataThroughput * 11; // Minimum baud = Data throughput * 11
330  }
331 
332 } // namespace kvh
an_packet_size
#define an_packet_size(packet)
Definition: an_packet_protocol.h:42
kvh::KvhDeviceConfig::CreatePacketPeriodsPacket
static int CreatePacketPeriodsPacket(KvhPacketRequest &_packetsRequested, packet_periods_packet_t &_packetPeriods)
Definition: device_configuration.cpp:75
encode_baud_rates_packet
an_packet_t * encode_baud_rates_packet(baud_rates_packet_t *baud_rates_packet)
filter_options_packet_t::internal_gnss_enabled
uint8_t internal_gnss_enabled
Definition: spatial_packets.h:825
odometer_configuration_packet_t::automatic_calibration
uint8_t automatic_calibration
Definition: spatial_packets.h:892
an_packet_t
Definition: an_packet_protocol.h:61
packet_periods_packet_t
Definition: spatial_packets.h:779
baud_rates_packet_t::permanent
uint8_t permanent
Definition: spatial_packets.h:788
filter_options_packet_t::motion_analysis_enabled
uint8_t motion_analysis_enabled
Definition: spatial_packets.h:830
filter_options_packet_t::reversing_detection_enabled
uint8_t reversing_detection_enabled
Definition: spatial_packets.h:829
baud_rates_packet_t
Definition: spatial_packets.h:786
filter_options_packet_t::velocity_heading_enabled
uint8_t velocity_heading_enabled
Definition: spatial_packets.h:828
an_decoder_size
#define an_decoder_size(an_decoder)
Definition: an_packet_protocol.h:46
rs232.h
PollComport
int PollComport(unsigned char *, int)
kvh::KvhPacketRequest
std::vector< std::pair< packet_id_e, uint16_t > > KvhPacketRequest
Definition: kvh_geo_fog_3d_packet_storage.hpp:65
packet_period_t
Definition: spatial_packets.h:773
kvh_geo_fog_3d_device_configuration.hpp
Helper functions for configuring the hardware.
packet_periods_packet_t::permanent
uint8_t permanent
Definition: spatial_packets.h:781
kvh
Definition: kvh_geo_fog_3d_device_configuration.hpp:44
an_decoder_initialise
void an_decoder_initialise(an_decoder_t *an_decoder)
packet_periods_packet_t::packet_periods
packet_period_t packet_periods[MAXIMUM_PACKET_PERIODS]
Definition: spatial_packets.h:783
an_decoder_pointer
#define an_decoder_pointer(an_decoder)
Definition: an_packet_protocol.h:45
filter_options_packet_t::vehicle_type
uint8_t vehicle_type
Definition: spatial_packets.h:824
baud_rates_packet_t::gpio_1_2_baud_rate
uint32_t gpio_1_2_baud_rate
Definition: spatial_packets.h:790
filter_options_packet_t
Definition: spatial_packets.h:821
odometer_configuration_packet_t::pulse_length
float pulse_length
Definition: spatial_packets.h:893
OpenComport
int OpenComport(char *, int)
an_decoder_t
Definition: an_packet_protocol.h:54
kvh::KvhDeviceConfig::CreateFilterOptionsPacket
static int CreateFilterOptionsPacket(filter_options_packet_t &, bool _permanent=true, uint8_t _vehicle_type=vehicle_type_car, bool _internal_gnss_enabled=true, bool _atmospheric_altitude_enabled=true, bool _velocity_heading_enabled=true, bool _reversing_detection_enabled=true, bool _motion_analysis_enabled=true)
Definition: device_configuration.cpp:138
odometer_configuration_packet_t::permanent
uint8_t permanent
Definition: spatial_packets.h:891
kvh::KvhDeviceConfig::CalculateRequiredBaud
static int CalculateRequiredBaud(KvhPacketRequest &)
Definition: device_configuration.cpp:321
packet_period_t::period
uint32_t period
Definition: spatial_packets.h:776
kvh::KvhDeviceConfig::CreateOdometerOptionsPacket
static int CreateOdometerOptionsPacket(odometer_configuration_packet_t &, bool _permanent=true, float _odom_pulse_to_meters=0.000583, bool _odom_auto_cal=true)
Definition: device_configuration.cpp:176
baud_rates_packet_t::reserved
uint32_t reserved
Definition: spatial_packets.h:792
an_packet_pointer
#define an_packet_pointer(packet)
Definition: an_packet_protocol.h:41
an_decoder_increment
#define an_decoder_increment(an_decoder, bytes_received)
Definition: an_packet_protocol.h:47
kvh::KvhDeviceConfig::SetBaudRate
static int SetBaudRate(std::string _port, int _curBaudRate, int _primaryBaudRate, int _gpioBaudRate=115200, int _auxBaudRate=115200)
This function can be used to set the buad rate independent of the other functions of the driver....
Definition: device_configuration.cpp:208
an_packet_free
void an_packet_free(an_packet_t **an_packet)
SendBuf
int SendBuf(unsigned char *, int)
kvh::KvhDeviceConfig::FindCurrentBaudRate
static int FindCurrentBaudRate(std::string, int)
This function tries each possible setting of baudrates until it either finds one that is receiving pa...
Definition: device_configuration.cpp:250
baud_rates_packet_t::primary_baud_rate
uint32_t primary_baud_rate
Definition: spatial_packets.h:789
CloseComport
void CloseComport()
filter_options_packet_t::reserved
uint8_t reserved
Definition: spatial_packets.h:826
filter_options_packet_t::atmospheric_altitude_enabled
uint8_t atmospheric_altitude_enabled
Definition: spatial_packets.h:827
odometer_configuration_packet_t
Definition: spatial_packets.h:889
packet_period_t::packet_id
uint8_t packet_id
Definition: spatial_packets.h:775
an_packet_decode
an_packet_t * an_packet_decode(an_decoder_t *an_decoder)
kvh::packetSize_
std::map< packet_id_e, int > packetSize_
Map relating packet id's to the associated struct size. Used for baudrate calculation.
Definition: kvh_global_vars.cpp:56
packet_periods_packet_t::clear_existing_packets
uint8_t clear_existing_packets
Definition: spatial_packets.h:782
filter_options_packet_t::permanent
uint8_t permanent
Definition: spatial_packets.h:823
MAXIMUM_PACKET_PERIODS
#define MAXIMUM_PACKET_PERIODS
Definition: spatial_packets.h:38
kvh::supportedPackets_
std::set< packet_id_e > supportedPackets_
Set of packets containing all packet_id's we support.
Definition: kvh_global_vars.cpp:37
baud_rates_packet_t::auxiliary_baud_rate
uint32_t auxiliary_baud_rate
Definition: spatial_packets.h:791
an_packet_encode
void an_packet_encode(an_packet_t *an_packet)


kvh_geo_fog_3d_driver
Author(s): Trevor Bostic , Zach LaCelle
autogenerated on Wed Mar 2 2022 00:28:57