InertialSense.h
Go to the documentation of this file.
1 /*
2 MIT LICENSE
3 
4 Copyright (c) 2014-2020 Inertial Sense, Inc. - http://inertialsense.com
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
7 
8 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9 
10 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 */
12 
13 #ifndef __INTERTIALSENSE_H
14 #define __INTERTIALSENSE_H
15 
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <stddef.h>
19 #include <string>
20 #include <fstream>
21 #include <iostream>
22 #include <sstream>
23 #include <vector>
24 
25 #include "ISConstants.h"
26 #include "ISTcpClient.h"
27 #include "ISTcpServer.h"
28 #include "ISLogger.h"
29 #include "ISDisplay.h"
30 #include "ISUtilities.h"
31 #include "ISSerialPort.h"
32 #include "ISDataMappings.h"
33 #include "ISStream.h"
34 
35 // use of InertialSense class requires winsock
36 #if PLATFORM_IS_WINDOWS
37 
38 #pragma comment (lib, "Ws2_32.lib")
39 
40 #endif
41 
42 extern "C"
43 {
44  // [C COMM INSTRUCTION] Include data_sets.h and com_manager.h
45  #include "data_sets.h"
46  #include "com_manager.h"
47 
48  #include "serialPortPlatform.h"
49  #include "inertialSenseBootLoader.h"
50 }
51 
52 #include <functional>
53 
55 
56 typedef std::function<void(InertialSense* i, p_data_t* data, int pHandle)> pfnHandleBinaryData;
57 typedef void(*pfnStepLogFunction)(InertialSense* i, const p_data_t* data, int pHandle);
58 
59 using namespace std;
60 
66 {
67 public:
68  struct is_device_t
69  {
74  };
75 
77  {
78  // per device vars
79  vector<is_device_t> devices;
80 
81  // common vars
83  pfnHandleBinaryData binaryCallback[DID_COUNT];
86  char* clientBuffer;
89  };
90 
91  typedef struct
92  {
93  string port;
94  string error;
96 
102 
106  virtual ~InertialSense();
107 
108  /*
109  * Broadcast binary data
110  * @param dataId the data id (DID_* - see data_sets.h) to broadcast
111  * @param periodMultiple a scalar that the source period is multiplied by to give the output period in milliseconds, 0 for one time message, less than 0 to disable broadcast of the specified dataId
112  * @param callback optional callback for this dataId
113  * @return true if success, false if error - if callback is NULL and no global callback was passed to the constructor, this will return false
114  */
115  bool BroadcastBinaryData(uint32_t dataId, int periodMultiple, pfnHandleBinaryData callback = NULL);
116 
120  void Close();
121 
125  vector<string> GetPorts();
126 
130  bool IsOpen();
131 
139  bool Open(const char* port, int baudRate = IS_COM_BAUDRATE_DEFAULT, bool disableBroadcastsOnClose = false);
140 
145  size_t GetDeviceCount();
146 
151  bool Update();
152 
165  bool SetLoggerEnabled(
166  bool enable,
167  const string& path = cISLogger::g_emptyString,
168  cISLogger::eLogType logType = cISLogger::eLogType::LOGTYPE_DAT,
169  uint64_t rmcPreset = RMC_PRESET_PPD_BITS,
170  float maxDiskSpacePercent = 0.5f,
171  uint32_t maxFileSize = 1024 * 1024 * 5,
172  const string& subFolder = cISLogger::g_emptyString);
173 
178  void BroadcastBinaryDataRmcPreset(uint64_t rmcPreset=RMC_PRESET_INS_BITS, uint32_t rmcOptions=0);
179 
184  bool LoggerEnabled() { return m_logger.Enabled(); }
185 
191  bool OpenServerConnection(const string& connectionString);
192 
198  bool CreateHost(const string& ipAndPort);
199 
203  void CloseServerConnection();
204 
208  void StopBroadcasts(bool allPorts=true);
209 
217  void SendData(eDataIDs dataId, uint8_t* data, uint32_t length, uint32_t offset);
218 
226  void SendRawData(eDataIDs dataId, uint8_t* data, uint32_t length = 0, uint32_t offset = 0);
227 
233  const dev_info_t GetDeviceInfo(int pHandle = 0)
234  {
235  if ((size_t)pHandle >= m_comManagerState.devices.size())
236  {
237  pHandle = 0;
238  }
239  return m_comManagerState.devices[pHandle].devInfo;
240  }
241 
247  system_command_t GetSysCmd(int pHandle = 0)
248  {
249  if ((size_t)pHandle >= m_comManagerState.devices.size())
250  {
251  pHandle = 0;
252  }
253  return m_comManagerState.devices[pHandle].sysCmd;
254  }
255 
261  void SetSysCmd(const system_command_t& sysCmd, int pHandle = 0);
262 
268  nvm_flash_cfg_t GetFlashConfig(int pHandle = 0)
269  {
270  if ((size_t)pHandle >= m_comManagerState.devices.size())
271  {
272  pHandle = 0;
273  }
274  return m_comManagerState.devices[pHandle].flashConfig;
275  }
276 
282  void SetFlashConfig(const nvm_flash_cfg_t& flashConfig, int pHandle = 0);
283 
288  uint64_t GetClientServerByteCount() { return m_clientServerByteCount; }
289 
295  serial_port_t* GetSerialPort(int pHandle = 0)
296  {
297  if ((size_t)pHandle >= m_comManagerState.devices.size())
298  {
299  return NULL;
300  }
301  return &(m_comManagerState.devices[pHandle].serialPort);
302  }
303 
308  time_t GetTimeoutFlushLoggerSeconds() { return m_logger.GetTimeoutFlushSeconds(); }
309 
314  void SetTimeoutFlushLoggerSeconds(time_t timeoutFlushLoggerSeconds) { m_logger.SetTimeoutFlushSeconds(timeoutFlushLoggerSeconds); }
315 
326  static vector<bootloader_result_t> BootloadFile(const string& comPort, const string& fileName, const string& bootloaderFileName, int baudRate = IS_BAUD_RATE_BOOTLOADER, pfnBootloadProgress uploadProgress = NULLPTR, pfnBootloadProgress verifyProgress = NULLPTR, pfnBootloadStatus infoProgress = NULLPTR, bool updateBootloader = false);
327  static vector<bootloader_result_t> BootloadFile(const string& comPort, const string& fileName, int baudRate = IS_BAUD_RATE_BOOTLOADER, pfnBootloadProgress uploadProgress = NULLPTR, pfnBootloadProgress verifyProgress = NULLPTR, bool updateBootloader = false);
328 
329 protected:
330  bool OnPacketReceived(const uint8_t* data, uint32_t dataLength);
331  void OnClientConnecting(cISTcpServer* server) OVERRIDE;
332  void OnClientConnected(cISTcpServer* server, socket_t socket) OVERRIDE;
333  void OnClientConnectFailed(cISTcpServer* server) OVERRIDE;
334  void OnClientDisconnected(cISTcpServer* server, socket_t socket) OVERRIDE;
335 
336 private:
339  void* m_logThread;
340  cMutex m_logMutex;
341  map<int, vector<p_data_t>> m_logPackets;
344  char m_clientBuffer[512];
354  uint8_t m_gpCommBuffer[PKT_BUF_SIZE];
355 
356  // returns false if logger failed to open
357  bool EnableLogging(const string& path, cISLogger::eLogType logType, float maxDiskSpacePercent, uint32_t maxFileSize, const string& subFolder);
358  void DisableLogging();
359  bool HasReceivedResponseFromDevice(size_t index);
360  bool HasReceivedResponseFromAllDevices();
361  void RemoveDevice(size_t index);
362  bool OpenSerialPorts(const char* port, int baudRate);
363  static void LoggerThread(void* info);
364  static void StepLogger(InertialSense* i, const p_data_t* data, int pHandle);
365 };
366 
367 #endif
#define IS_COM_BAUDRATE_DEFAULT
Definition: ISComm.h:88
is_comm_instance_t m_gpComm
NMI_API SOCKET socket(uint16 u16Domain, uint8 u8Type, uint8 u8Flags)
Definition: socket.c:477
std::function< void(InertialSense *i, p_data_t *data, int pHandle)> pfnHandleBinaryData
Definition: InertialSense.h:54
uint64_t m_clientServerByteCount
const dev_info_t GetDeviceInfo(int pHandle=0)
#define OVERRIDE
Definition: ISConstants.h:423
uint64_t GetClientServerByteCount()
#define PKT_BUF_SIZE
Definition: ISComm.h:98
pfnHandleBinaryData binaryCallbackGlobal
Definition: InertialSense.h:82
nvm_flash_cfg_t flashConfig
Definition: InertialSense.h:73
#define DID_COUNT
Definition: data_sets.h:138
#define NULL
Definition: nm_bsp.h:52
uint32_t eDataIDs
Definition: data_sets.h:32
void SetTimeoutFlushLoggerSeconds(time_t timeoutFlushLoggerSeconds)
int m_clientBufferBytesToSend
#define RMC_PRESET_INS_BITS
Definition: data_sets.h:1284
system_command_t GetSysCmd(int pHandle=0)
#define NULLPTR
Definition: ISConstants.h:426
nvm_flash_cfg_t GetFlashConfig(int pHandle=0)
cISTcpServer m_tcpServer
void * m_logThread
com_manager_port_t * m_cmPorts
int(* pfnBootloadProgress)(const void *obj, float percent)
bool m_disableBroadcastsOnClose
dev_info_t devInfo
Definition: data_sets.h:3405
USBInterfaceDescriptor data
system_command_t sysCmd
Definition: InertialSense.h:72
time_t m_lastLogReInit
cISStream * m_clientStream
bool LoggerEnabled()
cISLogger m_logger
InertialSense::com_manager_cpp_state_t m_comManagerState
serial_port_t * GetSerialPort(int pHandle=0)
static const string g_emptyString
Definition: ISLogger.h:59
#define IS_BAUD_RATE_BOOTLOADER
void(* pfnBootloadStatus)(const void *obj, const char *infoString)
TFSIMD_FORCE_INLINE tfScalar length(const Quaternion &q)
#define RMC_PRESET_PPD_BITS
Definition: data_sets.h:1282
com_manager_init_t m_cmInit
map< int, vector< p_data_t > > m_logPackets
time_t GetTimeoutFlushLoggerSeconds()
cISSerialPort m_serialServer
void(* pfnStepLogFunction)(InertialSense *i, const p_data_t *data, int pHandle)
Definition: InertialSense.h:57
cISTcpClient m_tcpClient


inertial_sense_ros
Author(s):
autogenerated on Sat Sep 19 2020 03:19:04