InertialSense.h
Go to the documentation of this file.
1 /*
2 MIT LICENSE
3 
4 Copyright (c) 2014-2021 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  {
75  };
76 
78  {
79  // per device vars
80  vector<is_device_t> devices;
81 
82  // common vars
84  pfnHandleBinaryData binaryCallback[256];
87  char* clientBuffer;
90  };
91 
92  typedef struct
93  {
94  string port;
95  string error;
97 
103 
107  virtual ~InertialSense();
108 
109  /*
110  * Broadcast binary data
111  * @param dataId the data id (DID_* - see data_sets.h) to broadcast
112  * @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
113  * @param callback optional callback for this dataId
114  * @return true if success, false if error - if callback is NULL and no global callback was passed to the constructor, this will return false
115  */
116  bool BroadcastBinaryData(uint32_t dataId, int periodMultiple, pfnHandleBinaryData callback = NULL);
117 
121  void Close();
122 
126  vector<string> GetPorts();
127 
131  bool IsOpen();
132 
140  bool Open(const char* port, int baudRate = IS_COM_BAUDRATE_DEFAULT, bool disableBroadcastsOnClose = false);
141 
146  size_t GetDeviceCount();
147 
152  bool Update();
153 
166  bool SetLoggerEnabled(
167  bool enable,
168  const string& path = cISLogger::g_emptyString,
169  cISLogger::eLogType logType = cISLogger::eLogType::LOGTYPE_DAT,
170  uint64_t rmcPreset = RMC_PRESET_PPD_BITS,
171  float maxDiskSpacePercent = 0.5f,
172  uint32_t maxFileSize = 1024 * 1024 * 5,
173  const string& subFolder = cISLogger::g_emptyString);
174 
179  void BroadcastBinaryDataRmcPreset(uint64_t rmcPreset=RMC_PRESET_INS_BITS, uint32_t rmcOptions=0);
180 
185  bool LoggerEnabled() { return m_logger.Enabled(); }
186 
192  bool OpenServerConnection(const string& connectionString);
193 
199  bool CreateHost(const string& ipAndPort);
200 
204  void CloseServerConnection();
205 
209  void StopBroadcasts(bool allPorts=true);
210 
218  void SendData(eDataIDs dataId, uint8_t* data, uint32_t length, uint32_t offset);
219 
227  void SendRawData(eDataIDs dataId, uint8_t* data, uint32_t length = 0, uint32_t offset = 0);
228 
234  const dev_info_t GetDeviceInfo(int pHandle = 0)
235  {
236  if ((size_t)pHandle >= m_comManagerState.devices.size())
237  {
238  pHandle = 0;
239  }
240  return m_comManagerState.devices[pHandle].devInfo;
241  }
242 
248  system_command_t GetSysCmd(int pHandle = 0)
249  {
250  if ((size_t)pHandle >= m_comManagerState.devices.size())
251  {
252  pHandle = 0;
253  }
254  return m_comManagerState.devices[pHandle].sysCmd;
255  }
256 
262  void SetSysCmd(const system_command_t& sysCmd, int pHandle = 0);
263 
269  nvm_flash_cfg_t GetFlashConfig(int pHandle = 0)
270  {
271  if ((size_t)pHandle >= m_comManagerState.devices.size())
272  {
273  pHandle = 0;
274  }
275  return m_comManagerState.devices[pHandle].flashConfig;
276  }
277 
284  {
285  if ((size_t)pHandle >= m_comManagerState.devices.size())
286  {
287  pHandle = 0;
288  }
289  return m_comManagerState.devices[pHandle].evbFlashCfg;
290  }
291 
297  void SetFlashConfig(const nvm_flash_cfg_t& flashConfig, int pHandle = 0);
298 
304  void SetEvbFlashConfig(const evb_flash_cfg_t& evbFlashCfg, int pHandle = 0);
305 
310  uint64_t GetClientServerByteCount() { return m_clientServerByteCount; }
311 
317  serial_port_t* GetSerialPort(int pHandle = 0)
318  {
319  if ((size_t)pHandle >= m_comManagerState.devices.size())
320  {
321  return NULL;
322  }
323  return &(m_comManagerState.devices[pHandle].serialPort);
324  }
325 
330  time_t GetTimeoutFlushLoggerSeconds() { return m_logger.GetTimeoutFlushSeconds(); }
331 
336  void SetTimeoutFlushLoggerSeconds(time_t timeoutFlushLoggerSeconds) { m_logger.SetTimeoutFlushSeconds(timeoutFlushLoggerSeconds); }
337 
348  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);
349  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);
350 
351 protected:
352  bool OnPacketReceived(const uint8_t* data, uint32_t dataLength);
353  void OnClientConnecting(cISTcpServer* server) OVERRIDE;
354  void OnClientConnected(cISTcpServer* server, socket_t socket) OVERRIDE;
355  void OnClientConnectFailed(cISTcpServer* server) OVERRIDE;
356  void OnClientDisconnected(cISTcpServer* server, socket_t socket) OVERRIDE;
357 
358 private:
361  void* m_logThread;
362  cMutex m_logMutex;
363  map<int, vector<p_data_t>> m_logPackets;
366  char m_clientBuffer[512];
376  uint8_t m_gpCommBuffer[PKT_BUF_SIZE];
377 
378  // returns false if logger failed to open
379  bool EnableLogging(const string& path, cISLogger::eLogType logType, float maxDiskSpacePercent, uint32_t maxFileSize, const string& subFolder);
380  void DisableLogging();
381  bool HasReceivedResponseFromDevice(size_t index);
382  bool HasReceivedResponseFromAllDevices();
383  void RemoveDevice(size_t index);
384  bool OpenSerialPorts(const char* port, int baudRate);
385  static void LoggerThread(void* info);
386  static void StepLogger(InertialSense* i, const p_data_t* data, int pHandle);
387 };
388 
389 #endif
#define IS_COM_BAUDRATE_DEFAULT
Definition: ISComm.h:88
evb_flash_cfg_t GetEvbFlashConfig(int pHandle=0)
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:83
nvm_flash_cfg_t flashConfig
Definition: InertialSense.h:73
#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:1342
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:3527
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)
evb_flash_cfg_t evbFlashCfg
Definition: InertialSense.h:74
TFSIMD_FORCE_INLINE tfScalar length(const Quaternion &q)
#define RMC_PRESET_PPD_BITS
Definition: data_sets.h:1340
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 Sun Feb 28 2021 03:17:57