driver_main.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 
34 //STD includes
35 #include <cstdio>
36 #include <string>
37 #include <cmath>
38 #include <functional>
39 #include <termios.h>
40 #include <typeinfo>
41 #include <set>
42 #include <iostream>
43 #include <fstream>
44 #include <unistd.h>
45 
46 // RS232
47 #include "rs232.h"
48 
49 // KVH Includes
51 
52 namespace kvh
53 {
54 
61  Driver::Driver(bool _debug) : connected_(false),
62  port_("/dev/ttyUSB0"),
63  debug_(_debug)
64  {
65  } // END Driver()
66 
72  {
73  Cleanup();
74  } // END ~Driver()
75 
77  // PRIVATE FUNCTIONS
79 
87  {
88  // Encode packet. Adds header including LRC and CRC
89  an_packet_encode(_anPacket);
90  // Send AN packet via serial port
91  if (SendBuf(an_packet_pointer(_anPacket), an_packet_size(_anPacket)))
92  {
93  packetRequests_.push_back(static_cast<packet_id_e>(_anPacket->id));
94  return 0;
95  }
96  else
97  {
98  return -1;
99  }
100  } // END SendPacket()
101 
103  // PUBLIC FUNCTIONS
105 
115  int Driver::Init(const std::string &_port, KvhPacketRequest &_packetsRequested)
116  {
117  return Driver::Init(_port, _packetsRequested, defaultOptions_);
118  }
119 
138  int Driver::Init(const std::string &_port, KvhPacketRequest &_packetsRequested, KvhInitOptions _initOptions)
139  {
140  int returnValue = 0;
141  int errorCode;
142  debug_ = _initOptions.debugOn;
143  if (debug_)
144  printf("Debug statements enabled.\n");
145 
146  if ((errorCode = packetStorage_.Init(_packetsRequested)) != 0)
147  {
148  if (debug_)
149  printf("Unable to intialize packet storage. Error code: %d", errorCode);
150  return -1;
151  }
153  // CREATE PACKET PERIODS PACKET
155 
156  packet_periods_packet_t packetPeriods;
157  if (deviceConfig_.CreatePacketPeriodsPacket(_packetsRequested, packetPeriods) < 0)
158  {
159  if (debug_)
160  printf("Unable to create packet periods packet properly.");
161  return -2;
162  }
163 
165  // CALCULATING BUAD RATE
167 
168  // dataThroughput from above, 11 from their equation
169  int minBaudRequired = deviceConfig_.CalculateRequiredBaud(_packetsRequested);
170  if (debug_)
171  printf("Calculated required minimum baud rate: %d\n", minBaudRequired);
172 
173  // \todo Handle returned value appropriately
174  if (minBaudRequired < _initOptions.baudRate)
175  {
176  returnValue = 1;
177  }
178 
180  // SETTING UP KALMAN FILTER OPTIONS
182 
183  filter_options_packet_t filterOptions;
184  if (deviceConfig_.CreateFilterOptionsPacket(filterOptions, true, _initOptions.filterVehicleType,
185  _initOptions.gnssEnabled, _initOptions.atmosphericAltitudeEnabled, _initOptions.velocityHeadingEnabled,
186  _initOptions.reversingDetectionEnabled, _initOptions.motionAnalysisEnabled) != 0)
187  {
188  return -3;
189  }
190 
192  // SETTING UP ODOMETER OPTIONS
194  odometer_configuration_packet_t odometerOptions;
195  if (deviceConfig_.CreateOdometerOptionsPacket(odometerOptions, true, static_cast<float>(_initOptions.odomPulseToMeters), false) != 0 )
196  {
197  return -2;
198  }
199 
201  // CONNECTING TO KVH
203 
204  port_ = _port;
205  char portArr[4096];
206  strncpy(portArr, port_.c_str(), 4096);
207  if (debug_) printf("Baud: %d\n", _initOptions.baudRate);
208  if (OpenComport(portArr, _initOptions.baudRate) != 0)
209  {
210  if (debug_)
211  printf("Unable to establish connection.\n");
212  return -4;
213  }
214  // We are connected to the KVH!
215  connected_ = true;
216 
218  // SENDING CONFIGURATION PACKETS
220 
221  an_packet_t *requestPacket;
222  int packetError;
223 
224  if (debug_)
225  printf("Sending packet_periods.\n");
226 
227  requestPacket = encode_packet_periods_packet(&packetPeriods);
228  packetError = SendPacket(requestPacket);
229  an_packet_free(&requestPacket);
230  requestPacket = nullptr;
231  if (packetError)
232  {
233  return -5;
234  }
235 
236  if (debug_)
237  printf("Sending filter options packet.");
238 
239  requestPacket = encode_filter_options_packet(&filterOptions);
240  packetError = SendPacket(requestPacket);
241  requestPacket = nullptr;
242  if (packetError != 0)
243  {
244  return -6;
245  }
246 
248  // INITIALISE AN DECODER
250 
251  if (debug_)
252  printf("Initializing decoder.\n");
254 
255  return returnValue;
256 
257  } // END Init()
258 
277  {
278  an_packet_t *anPacket;
279  int bytesRec = 0;
280  int unexpectedPackets = 0;
281 
282  // Check if new packets have been sent
284  {
285  /* increment the decode buffer length by the number of bytes received */
286  an_decoder_increment(&anDecoder_, bytesRec);
287 
288  /* decode all the packets in the buffer */
289  while ((anPacket = an_packet_decode(&anDecoder_)) != NULL)
290  {
291  // If we get an acknowledgment packet from sending packets
292  // Acknowledgement packet is different than the others so we keep it seperate
293  if (anPacket->id == packet_id_acknowledge)
294  {
296  if (decode_acknowledge_packet(&ackP, anPacket) == 0)
297  {
298  if (debug_)
299  {
300  printf("*********************************\n"
301  "Acknowledging packet from packet id: %d\n Result of packet %d\n"
302  "********************************\n",
303  ackP.packet_id, ackP.acknowledge_result);
304  }
305  }
306  else
307  {
308  if (debug_)
309  printf("Unable to decode acknowledge packet properly.\n");
310  }
311  }
312  else
313  {
314  if (DecodePacket(anPacket) < 0)
315  unexpectedPackets++;
316  }
317 
318  /* Ensure that you free the an_packet when your done with it or you will leak memory */
319  an_packet_free(&anPacket);
320  }
321  }
322 
323  if (debug_) printf("Recieved %d unexpected packets during transmission.\n", unexpectedPackets);
324  } // END Once()
325 
335  {
336  return packetStorage_.PacketIsUpdated(_packetId);
337  }
338 
348  int Driver::SetPacketUpdated(packet_id_e _packetId, bool _updateStatus)
349  {
350  return packetStorage_.SetPacketUpdated(_packetId, _updateStatus);
351  }
352 
359  {
360  return packetStorage_.AddPacket(_packetId);
361  }
362 
376  int Driver::RequestPacket(packet_id_e _requestedPacket)
377  {
378  an_packet_t* anPacket = encode_request_packet(_requestedPacket);
379  if (SendPacket(anPacket) != 0)
380  {
381  if (debug_)
382  printf("Unable to send packet request.\n");
383  return -1;
384  }
385 
386  return 0;
387  }
388 
395  {
396  if (connected_)
397  CloseComport();
398  return 0;
399  } // END Cleanup()
400 
401 } // 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
kvh::Driver::connected_
bool connected_
True if we're connected to the localization unit.
Definition: kvh_geo_fog_3d_driver.hpp:86
decode_acknowledge_packet
int decode_acknowledge_packet(acknowledge_packet_t *acknowledge_packet, an_packet_t *an_packet)
kvh::KvhInitOptions::debugOn
bool debugOn
Definition: kvh_geo_fog_3d_driver.hpp:63
kvh::KvhInitOptions::reversingDetectionEnabled
bool reversingDetectionEnabled
Definition: kvh_geo_fog_3d_driver.hpp:67
kvh::KvhInitOptions::gnssEnabled
bool gnssEnabled
Definition: kvh_geo_fog_3d_driver.hpp:60
kvh::Driver::RequestPacket
int RequestPacket(packet_id_e)
This function is used to request packets that you only want once or that cannot be requested through ...
Definition: driver_main.cpp:376
an_packet_t
Definition: an_packet_protocol.h:61
packet_periods_packet_t
Definition: spatial_packets.h:779
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
kvh::KvhInitOptions
Definition: kvh_geo_fog_3d_driver.hpp:58
kvh::KvhInitOptions::atmosphericAltitudeEnabled
bool atmosphericAltitudeEnabled
Definition: kvh_geo_fog_3d_driver.hpp:65
kvh::Driver::SendPacket
int SendPacket(an_packet_t *)
Wrapper function for more easily sending an packets via serial port.
Definition: driver_main.cpp:86
kvh::KvhPacketStorage::PacketIsUpdated
bool PacketIsUpdated(packet_id_e)
Definition: packet_storage.cpp:164
kvh
Definition: kvh_geo_fog_3d_device_configuration.hpp:44
encode_filter_options_packet
an_packet_t * encode_filter_options_packet(filter_options_packet_t *filter_options_packet)
an_decoder_initialise
void an_decoder_initialise(an_decoder_t *an_decoder)
kvh::KvhInitOptions::velocityHeadingEnabled
bool velocityHeadingEnabled
Definition: kvh_geo_fog_3d_driver.hpp:66
encode_request_packet
an_packet_t * encode_request_packet(uint8_t requested_packet_id)
an_decoder_pointer
#define an_decoder_pointer(an_decoder)
Definition: an_packet_protocol.h:45
kvh::KvhPacketStorage::Init
int Init(KvhPacketRequest &)
Correctly sets up a KvhPacketMap for the requested packets.
Definition: packet_storage.cpp:37
kvh::KvhInitOptions::odomPulseToMeters
double odomPulseToMeters
Definition: kvh_geo_fog_3d_driver.hpp:69
kvh::Driver::defaultOptions_
KvhInitOptions defaultOptions_
If no init options are passed in, use this as the default.
Definition: kvh_geo_fog_3d_driver.hpp:95
kvh::Driver::debug_
bool debug_
Set true to print debug statements.
Definition: kvh_geo_fog_3d_driver.hpp:91
kvh::Driver::packetStorage_
KvhPacketStorage packetStorage_
Class responsible for handling packets and ensuring consistency.
Definition: kvh_geo_fog_3d_driver.hpp:96
kvh::Driver::anDecoder_
an_decoder_t anDecoder_
Decoder for decoding incoming AN encoded packets.
Definition: kvh_geo_fog_3d_driver.hpp:90
filter_options_packet_t
Definition: spatial_packets.h:821
OpenComport
int OpenComport(char *, int)
encode_packet_periods_packet
an_packet_t * encode_packet_periods_packet(packet_periods_packet_t *packet_periods_packet)
kvh::Driver::AddPacket
int AddPacket(packet_id_e)
Definition: driver_main.cpp:358
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
kvh::KvhDeviceConfig::CalculateRequiredBaud
static int CalculateRequiredBaud(KvhPacketRequest &)
Definition: device_configuration.cpp:321
kvh::Driver::~Driver
~Driver()
Destructor. Will automatically cleanup the driver.
Definition: driver_main.cpp:71
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
an_packet_pointer
#define an_packet_pointer(packet)
Definition: an_packet_protocol.h:41
kvh::KvhInitOptions::filterVehicleType
uint8_t filterVehicleType
Definition: kvh_geo_fog_3d_driver.hpp:64
kvh::KvhPacketStorage::AddPacket
int AddPacket(packet_id_e)
Definition: packet_storage.cpp:72
kvh::Driver::packetRequests_
std::vector< packet_id_e > packetRequests_
Definition: kvh_geo_fog_3d_driver.hpp:92
an_decoder_increment
#define an_decoder_increment(an_decoder, bytes_received)
Definition: an_packet_protocol.h:47
kvh::Driver::PacketIsUpdated
bool PacketIsUpdated(packet_id_e)
Use this function to determine if new packet data has arrived since the last time you checked.
Definition: driver_main.cpp:334
an_packet_free
void an_packet_free(an_packet_t **an_packet)
an_packet_t::id
uint8_t id
Definition: an_packet_protocol.h:63
kvh::KvhInitOptions::baudRate
int baudRate
Definition: kvh_geo_fog_3d_driver.hpp:61
kvh::Driver::Driver
Driver(bool _debug=false)
Initializes connected status, port to use, and if debug printing is turned on.
Definition: driver_main.cpp:61
SendBuf
int SendBuf(unsigned char *, int)
CloseComport
void CloseComport()
odometer_configuration_packet_t
Definition: spatial_packets.h:889
packet_id_e
packet_id_e
Definition: spatial_packets.h:45
kvh::Driver::DecodePacket
int DecodePacket(an_packet_t *)
Definition: decode_packets.cpp:79
an_packet_decode
an_packet_t * an_packet_decode(an_decoder_t *an_decoder)
acknowledge_packet_t
Definition: spatial_packets.h:147
packet_id_acknowledge
@ packet_id_acknowledge
Definition: spatial_packets.h:47
acknowledge_packet_t::packet_id
uint8_t packet_id
Definition: spatial_packets.h:149
kvh::Driver::Once
int Once()
Do a single data read from the device.
Definition: driver_main.cpp:276
kvh_geo_fog_3d_driver.hpp
KVH Geo Fog 3D driver class header.
kvh::KvhPacketStorage::SetPacketUpdated
int SetPacketUpdated(packet_id_e, bool)
Definition: packet_storage.cpp:148
kvh::Driver::Cleanup
int Cleanup()
Cleanup and close our connections.
Definition: driver_main.cpp:394
kvh::Driver::SetPacketUpdated
int SetPacketUpdated(packet_id_e, bool)
Use this function to set that the packet has been updated (though the driver will usually do that its...
Definition: driver_main.cpp:348
kvh::KvhInitOptions::motionAnalysisEnabled
bool motionAnalysisEnabled
Definition: kvh_geo_fog_3d_driver.hpp:68
kvh::Driver::Init
int Init(const std::string &_port, KvhPacketRequest &_packetsRequested)
Definition: driver_main.cpp:115
acknowledge_packet_t::acknowledge_result
uint8_t acknowledge_result
Definition: spatial_packets.h:151
kvh::Driver::deviceConfig_
KvhDeviceConfig deviceConfig_
Class responsible for configuring kvh geo fog.
Definition: kvh_geo_fog_3d_driver.hpp:97
kvh::Driver::port_
std::string port_
Port to connect to the kvh through. Ex. "/dev/ttyUSB0".
Definition: kvh_geo_fog_3d_driver.hpp:87
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