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  deviceConfig_.CreatePacketPeriodsPacket(_packetsRequested, packetPeriods);
158 
160  // CALCULATING BUAD RATE
162 
163  // dataThroughput from above, 11 from their equation
164  int minBaudRequired = deviceConfig_.CalculateRequiredBaud(_packetsRequested);
165  if (debug_)
166  printf("Calculated required minimum baud rate: %d\n", minBaudRequired);
167 
168  // \todo Handle returned value appropriately
169  if (minBaudRequired < _initOptions.baudRate)
170  {
171  returnValue = 1;
172  }
173 
175  // SETTING UP KALMAN FILTER OPTIONS
177 
178  filter_options_packet_t filterOptions;
179  if (deviceConfig_.CreateFilterOptionsPacket(filterOptions, true, _initOptions.filterVehicleType,
180  _initOptions.gnssEnabled, _initOptions.atmosphericAltitudeEnabled, _initOptions.velocityHeadingEnabled,
181  _initOptions.reversingDetectionEnabled, _initOptions.motionAnalysisEnabled) != 0)
182  {
183  return -2;
184  }
185 
187  // CONNECTING TO KVH
189 
190  port_ = _port;
191  char portArr[4096];
192  strncpy(portArr, port_.c_str(), 4096);
193  if (debug_) printf("Baud: %d\n", _initOptions.baudRate);
194  if (OpenComport(portArr, _initOptions.baudRate) != 0)
195  {
196  if (debug_)
197  printf("Unable to establish connection.\n");
198  return -3;
199  }
200  // We are connected to the KVH!
201  connected_ = true;
202 
204  // SENDING CONFIGURATION PACKETS
206 
207  an_packet_t *requestPacket;
208  int packetError;
209 
210  if (debug_)
211  printf("Sending packet_periods.\n");
212 
213  requestPacket = encode_packet_periods_packet(&packetPeriods);
214  packetError = SendPacket(requestPacket);
215  an_packet_free(&requestPacket);
216  requestPacket = nullptr;
217  if (packetError)
218  {
219  return -4;
220  }
221 
222  if (debug_)
223  printf("Sending filter options packet.");
224 
225  requestPacket = encode_filter_options_packet(&filterOptions);
226  packetError = SendPacket(requestPacket);
227  requestPacket = nullptr;
228  if (packetError != 0)
229  {
230  return -5;
231  }
232 
234  // INITIALISE AN DECODER
236 
237  if (debug_)
238  printf("Initializing decoder.\n");
240 
241  return returnValue;
242 
243  } // END Init()
244 
263  {
264  an_packet_t *anPacket;
265  int bytesRec = 0;
266  int unexpectedPackets = 0;
267 
268  // Check if new packets have been sent
270  {
271  /* increment the decode buffer length by the number of bytes received */
272  an_decoder_increment(&anDecoder_, bytesRec);
273 
274  /* decode all the packets in the buffer */
275  while ((anPacket = an_packet_decode(&anDecoder_)) != NULL)
276  {
277  // If we get an acknowledgment packet from sending packets
278  // Acknowledgement packet is different than the others so we keep it seperate
279  if (anPacket->id == packet_id_acknowledge)
280  {
282  if (decode_acknowledge_packet(&ackP, anPacket) == 0)
283  {
284  if (debug_)
285  {
286  printf("*********************************\n"
287  "Acknowledging packet from packet id: %d\n Result of packet %d\n"
288  "********************************\n",
289  ackP.packet_id, ackP.acknowledge_result);
290  }
291  }
292  else
293  {
294  if (debug_)
295  printf("Unable to decode acknowledge packet properly.\n");
296  }
297  }
298  else
299  {
300  if (DecodePacket(anPacket) < 0)
301  unexpectedPackets++;
302  }
303 
304  /* Ensure that you free the an_packet when your done with it or you will leak memory */
305  an_packet_free(&anPacket);
306  }
307  }
308 
309  if (debug_) printf("Recieved %d unexpected packets during transmission.", unexpectedPackets);
310  } // END Once()
311 
321  {
322  return packetStorage_.PacketIsUpdated(_packetId);
323  }
324 
334  int Driver::SetPacketUpdated(packet_id_e _packetId, bool _updateStatus)
335  {
336  return packetStorage_.SetPacketUpdated(_packetId, _updateStatus);
337  }
338 
345  {
346  if (connected_)
347  CloseComport();
348  return 0;
349  } // END Cleanup()
350 
351 } // namespace kvh
KVH Geo Fog 3D driver class header.
std::string port_
Port to connect to the kvh through. Ex. "/dev/ttyUSB0".
an_packet_t * encode_filter_options_packet(filter_options_packet_t *filter_options_packet)
void an_decoder_initialise(an_decoder_t *an_decoder)
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)
an_decoder_t anDecoder_
Decoder for decoding incoming AN encoded packets.
int Once()
Do a single data read from the device.
KvhInitOptions defaultOptions_
If no init options are passed in, use this as the default.
int OpenComport(char *, int)
static int CalculateRequiredBaud(KvhPacketRequest &)
an_packet_t * an_packet_decode(an_decoder_t *an_decoder)
bool connected_
True if we&#39;re connected to the localization unit.
packet_id_e
#define an_packet_size(packet)
std::vector< packet_id_e > packetRequests_
an_packet_t * encode_packet_periods_packet(packet_periods_packet_t *packet_periods_packet)
~Driver()
Destructor. Will automatically cleanup the driver.
Definition: driver_main.cpp:71
int PollComport(unsigned char *, int)
std::vector< std::pair< packet_id_e, uint16_t > > KvhPacketRequest
#define an_decoder_size(an_decoder)
int Init(KvhPacketRequest &)
Correctly sets up a KvhPacketMap for the requested packets.
int Init(const std::string &_port, KvhPacketRequest &_packetsRequested)
Driver(bool _debug=false)
Initializes connected status, port to use, and if debug printing is turned on.
Definition: driver_main.cpp:61
bool PacketIsUpdated(packet_id_e)
Use this function to determine if new packet data has arrived since the last time you checked...
int SetPacketUpdated(packet_id_e, bool)
bool PacketIsUpdated(packet_id_e)
bool debug_
Set true to print debug statements.
KvhPacketStorage packetStorage_
Class responsible for handling packets and ensuring consistency.
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...
static int CreatePacketPeriodsPacket(KvhPacketRequest &_packetsRequested, packet_periods_packet_t &_packetPeriods)
void an_packet_encode(an_packet_t *an_packet)
#define an_packet_pointer(packet)
int DecodePacket(an_packet_t *)
int SendPacket(an_packet_t *)
Wrapper function for more easily sending an packets via serial port.
Definition: driver_main.cpp:86
void an_packet_free(an_packet_t **an_packet)
int SendBuf(unsigned char *, int)
int Cleanup()
Cleanup and close our connections.
#define an_decoder_pointer(an_decoder)
KvhDeviceConfig deviceConfig_
Class responsible for configuring kvh geo fog.
int decode_acknowledge_packet(acknowledge_packet_t *acknowledge_packet, an_packet_t *an_packet)


kvh_geo_fog_3d_driver
Author(s): Trevor Bostic , Zach LaCelle
autogenerated on Fri Jan 24 2020 03:18:17