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
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...
void an_decoder_initialise(an_decoder_t *an_decoder)
packet_period_t packet_periods[MAXIMUM_PACKET_PERIODS]
void CloseComport()
#define an_decoder_increment(an_decoder, bytes_received)
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)
int OpenComport(char *, int)
std::set< packet_id_e > supportedPackets_
Set of packets containing all packet_id&#39;s we support.
static int CalculateRequiredBaud(KvhPacketRequest &)
Helper functions for configuring the hardware.
an_packet_t * an_packet_decode(an_decoder_t *an_decoder)
an_packet_t * encode_baud_rates_packet(baud_rates_packet_t *baud_rates_packet)
#define an_packet_size(packet)
#define MAXIMUM_PACKET_PERIODS
std::map< packet_id_e, int > packetSize_
Map relating packet id&#39;s to the associated struct size. Used for baudrate calculation.
static int FindCurrentBaudRate(std::string, int)
This function tries each possible setting of baudrates until it either finds one that is receiving pa...
int PollComport(unsigned char *, int)
std::vector< std::pair< packet_id_e, uint16_t > > KvhPacketRequest
#define an_decoder_size(an_decoder)
static int CreatePacketPeriodsPacket(KvhPacketRequest &_packetsRequested, packet_periods_packet_t &_packetPeriods)
void an_packet_encode(an_packet_t *an_packet)
#define an_packet_pointer(packet)
static int CreateOdometerOptionsPacket(odometer_configuration_packet_t &, bool _permanent=true, float _odom_pulse_to_meters=0.000583, bool _odom_auto_cal=true)
void an_packet_free(an_packet_t **an_packet)
int SendBuf(unsigned char *, int)
#define an_decoder_pointer(an_decoder)


kvh_geo_fog_3d_driver
Author(s): Trevor Bostic , Zach LaCelle
autogenerated on Mon Feb 28 2022 22:39:53