From 8a60c8872f4d989ee83aac95dc1056e8a964a878 Mon Sep 17 00:00:00 2001 From: Livox-SDK Date: Fri, 18 Jan 2019 23:10:45 +0800 Subject: [PATCH] [A]add smaple. --- CMakeLists.txt | 12 +++ README.md | 6 +- sample/hub/CMakeLists.txt | 8 ++ sample/hub/main.c | 207 ++++++++++++++++++++++++++++++++++++ sample/lidar/CMakeLists.txt | 8 ++ sample/lidar/main.c | 198 ++++++++++++++++++++++++++++++++++ 6 files changed, 436 insertions(+), 3 deletions(-) create mode 100755 CMakeLists.txt create mode 100644 sample/hub/CMakeLists.txt create mode 100644 sample/hub/main.c create mode 100644 sample/lidar/CMakeLists.txt create mode 100644 sample/lidar/main.c diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100755 index 0000000..848e1ee --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,12 @@ +cmake_minimum_required(VERSION 3.0) + +project(livox_sdk VERSION 0.9) + +set(CMAKE_CXX_STANDARD 98) + +message(STATUS "main project dir: " ${PROJECT_SOURCE_DIR}) + +add_subdirectory(sdk_core sdk_core) +add_subdirectory(sample/hub) +add_subdirectory(sample/lidar) + diff --git a/README.md b/README.md index 3bc7811..0563218 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Livox SDK consists of Livox SDK communication protocol, Livox SDK core, Livox SD # Livox SDK Communication Protocol -Livox SDK communication protocol opens to all users. It is the communication protocol between user programs and Livox products. The protocol consists of control commands and data format. Please refer to the [Livox SDK Communication Protocol][https://www.livoxtech.com/sdk/downloads] for detailed information. +Livox SDK communication protocol opens to all users. It is the communication protocol between user programs and Livox products. The protocol consists of control commands and data format. Please refer to the [Livox SDK Communication Protocol](https://www.livoxtech.com/sdk/downloads) for detailed information. # Livox SDK Core @@ -20,7 +20,7 @@ The Livox LiDAR sensors can be connected to host directly or through the Livox H # Livox SDK API -Livox SDK API provides a set of C style functions which can be conveniently integrated in C/C++ programs. Please refer to the [Livox SDK API Reference][https://www.livoxtech.com/sdk/downloads] for detailed information. +Livox SDK API provides a set of C style functions which can be conveniently integrated in C/C++ programs. Please refer to the [Livox SDK API Reference](https://www.livoxtech.com/sdk/downloads) for detailed information. ## Build Notes @@ -51,7 +51,7 @@ sudo apt install \ ###### Compile the Source Code -You can download the apr source code from [http://apr.apache.org/download.cgi][id] and boost source code from [http://sourceforge.net/projects/boost/files/boost/1.54.0/][id] .The following commands can be used to compile and install it. +You can download the apr source code from http://apr.apache.org/download.cgi and boost source code from http://sourceforge.net/projects/boost/files/boost/1.54.0/.The following commands can be used to compile and install it. ``` tar zxf apr-1.6.5.tar.gz && \ diff --git a/sample/hub/CMakeLists.txt b/sample/hub/CMakeLists.txt new file mode 100644 index 0000000..98fe928 --- /dev/null +++ b/sample/hub/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.0) + +set(DEMO_NAME hub_smaple) +add_executable(${DEMO_NAME} main.c) +target_link_libraries(${DEMO_NAME} + PRIVATE + ${PROJECT_NAME}_static + ) diff --git a/sample/hub/main.c b/sample/hub/main.c new file mode 100644 index 0000000..c40fddc --- /dev/null +++ b/sample/hub/main.c @@ -0,0 +1,207 @@ +// +// The MIT License (MIT) +// +// Copyright (c) 2019 Livox. All rights reserved. +// +// 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: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// 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. +// + +#include +#include +#include +#include +#include "livox_sdk.h" + +typedef enum { + kDeviceStateDisconnect = 0, + kDeviceStateConnect = 1, + kDeviceStateSampling = 2, +} DeviceState; + +typedef struct { + uint8_t handle; + DeviceState device_state; + DeviceInfo info; +} DeviceItem; + +DeviceItem devices[kMaxLidarCount]; + +#define BROADCAST_CODE_LIST_SIZE 1 +char *broadcast_code_list[BROADCAST_CODE_LIST_SIZE] = { + "00000000000001" +}; + +void GetLidarData(uint8_t handle, LivoxEthPacket *data, uint32_t data_num) { + static uint32_t receive_packet_count = 0; + if (data) { + ++receive_packet_count; + if (0 == (receive_packet_count % 10000)) { + printf("receive packet count %d %d\n", data->id, receive_packet_count); + } + printf("receive packet from %d \n", (uint16_t) HubGetLidarHandle(data->slot, data->id)); + } +} + +void OnSampleCallback(uint8_t status, uint8_t handle, uint8_t response, void *data) { + printf("OnSampleCallback statue %d handle %d response %d \n", status, handle, response); + if (status == kStatusSuccess) { + if (response != 0) { + devices[handle].device_state = kDeviceStateConnect; + } + } else if (status == kStatusTimeout) { + devices[handle].device_state = kDeviceStateConnect; + } +} + +void OnStopSampleCallback(uint8_t status, uint8_t handle, uint8_t response, void *data) { + +} + +void OnDeviceInformation(uint8_t status, uint8_t handle, DeviceInformationResponse *ack, void *data) { + if (status != kStatusSuccess) { + printf("Device Query Informations Failed %d\n", status); + } + if (ack) { + printf("firm ver: %d.%d.%d.%d\n", + ack->firmware_version[0], + ack->firmware_version[1], + ack->firmware_version[2], + ack->firmware_version[3]); + } +} + +void OnHubLidarInfo(uint8_t status, uint8_t handle, HubQueryLidarInformationResponse *response, void *client_data) { + if (status != kStatusSuccess) { + printf("Device Query Informations Failed %d\n", status); + } + if (response) { + int i = 0; + for (i = 0; i < response->count; ++i) { + printf("Hub Lidar Info broadcast code %s id %d slot %d \n ", + response->device_info_list[i].broadcast_code, + response->device_info_list[i].id, + response->device_info_list[i].slot); + } + } +} + +void OnDeviceChange(const DeviceInfo *info, DeviceEvent type) { + if (info == NULL) { + return; + } + printf("OnDeviceChange broadcast code %s update type %d\n", info->broadcast_code, type); + uint8_t handle = info->handle; + if (handle >= kMaxLidarCount) { + return; + } + if (type == kEventConnect) { + DeviceInfo *_devices = (DeviceInfo *) malloc(sizeof(DeviceInfo) * kMaxLidarCount); + + uint8_t count = kMaxLidarCount; + uint8_t status = GetConnectedDevices(_devices, &count); + if (status == kStatusSuccess) { + int i = 0; + for (i = 0; i < count; ++i) { + uint8_t handle = _devices[i].handle; + if (handle < kMaxLidarCount) { + devices[handle].handle = handle; + devices[handle].info = _devices[i]; + devices[handle].device_state = kDeviceStateConnect; + } + } + } + if (_devices) { + free(_devices); + } + if (devices[handle].info.type == kDeviceTypeHub) { + HubQueryLidarInformation(OnHubLidarInfo, NULL); + } + if (devices[handle].device_state == kDeviceStateDisconnect) { + devices[handle].device_state = kDeviceStateConnect; + devices[handle].info = *info; + } + } else if (type == kEventDisconnect) { + devices[handle].device_state = kDeviceStateDisconnect; + } else if (type == kEventStateChange) { + devices[handle].info = *info; + } + + if (devices[handle].device_state == kDeviceStateConnect) { + printf("Device State error_code %d\n", devices[handle].info.status.status_code); + printf("Device State working state %d\n", devices[handle].info.state); + printf("Device feature %d\n", devices[handle].info.feature); + if (devices[handle].info.state == kLidarStateNormal && devices[handle].info.status.status_code == 0) { + if (devices[handle].info.type == kDeviceTypeHub) { + HubStartSampling(OnSampleCallback, NULL); + } else { + LidarStartSampling(handle, OnSampleCallback, NULL); + } + devices[handle].device_state = kDeviceStateSampling; + } + } +} + +void OnDeviceBroadcast(const BroadcastDeviceInfo *info) { + if (info == NULL) { + return; + } + + printf("Receive Broadcast Code %s\n", info->broadcast_code); + bool found = false; + + int i = 0; + for (i = 0; i < BROADCAST_CODE_LIST_SIZE; ++i) { + if (strncmp(info->broadcast_code, broadcast_code_list[i], kBroadcastCodeSize) == 0) { + found = true; + break; + } + } + if (!found) { + return; + } + + bool result = false; + uint8_t handle = 0; + result = AddHubToConnect(info->broadcast_code, &handle); + if (result == kStatusSuccess && handle < kMaxLidarCount) { + SetDataCallback(handle, GetLidarData); + devices[handle].handle = handle; + devices[handle].device_state = kDeviceStateDisconnect; + } +} + +int main(int argc, const char *argv[]) { + if (!Init()) { + return -1; + } + + memset(devices, 0, sizeof(devices)); + SetBroadcastCallback(OnDeviceBroadcast); + SetDeviceStateUpdateCallback(OnDeviceChange); + + if (Start()) { + sleep(20); + HubStopSampling(OnStopSampleCallback, NULL); + printf("stop sample\n"); + sleep(10); + } + + Uninit(); + +} diff --git a/sample/lidar/CMakeLists.txt b/sample/lidar/CMakeLists.txt new file mode 100644 index 0000000..fee9342 --- /dev/null +++ b/sample/lidar/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.0) + +set(DEMO_NAME lidar_smaple) +add_executable(${DEMO_NAME} main.c) +target_link_libraries(${DEMO_NAME} + PRIVATE + ${PROJECT_NAME}_static + ) diff --git a/sample/lidar/main.c b/sample/lidar/main.c new file mode 100644 index 0000000..64e8849 --- /dev/null +++ b/sample/lidar/main.c @@ -0,0 +1,198 @@ +// +// The MIT License (MIT) +// +// Copyright (c) 2019 Livox. All rights reserved. +// +// 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: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// 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. +// + +#include +#include +#include +#include +#include "livox_sdk.h" + +typedef enum { + kDeviceStateDisconnect = 0, + kDeviceStateConnect = 1, + kDeviceStateSampling = 2, +} DeviceState; + +typedef struct { + uint8_t handle; + DeviceState device_state; + DeviceInfo info; +} DeviceItem; + +DeviceItem devices[kMaxLidarCount]; +uint32_t data_recveive_count[kMaxLidarCount]; + +#define BROADCAST_CODE_LIST_SIZE 3 +char *broadcast_code_list[BROADCAST_CODE_LIST_SIZE] = { + "00000000000002", + "00000000000003", + "00000000000004" +}; + +/** Receiving point cloud data from Livox LiDAR. */ +void GetLidarData(uint8_t handle, LivoxEthPacket *data, uint32_t data_num) { + if (data) { + data_recveive_count[handle] += data_num; + if (data_recveive_count[handle] % 10000 == 0) { + printf("receive packet count %d %d\n", handle, data_recveive_count[handle]); + } + } +} + +/** Callback function of starting sampling. */ +void OnSampleCallback(uint8_t status, uint8_t handle, uint8_t response, void *data) { + printf("OnSampleCallback statue %d handle %d response %d \n", status, handle, response); + if (status == kStatusSuccess) { + if (response != 0) { + devices[handle].device_state = kDeviceStateConnect; + } + } else if (status == kStatusTimeout) { + devices[handle].device_state = kDeviceStateConnect; + } +} + +/** Callback function of stopping sampling. */ +void OnStopSampleCallback(uint8_t status, uint8_t handle, uint8_t response, void *data) { +} + +/** Query the firmware version of Livox LiDAR. */ +void OnDeviceInformation(uint8_t status, uint8_t handle, DeviceInformationResponse *ack, void *data) { + if (status != kStatusSuccess) { + printf("Device Query Informations Failed %d\n", status); + } + if (ack) { + printf("firm ver: %d.%d.%d.%d\n", + ack->firmware_version[0], + ack->firmware_version[1], + ack->firmware_version[2], + ack->firmware_version[3]); + } +} + +/** Callback function of changing of device state. */ +void OnDeviceChange(const DeviceInfo *info, DeviceEvent type) { + if (info == NULL) { + return; + } + printf("OnDeviceChange broadcast code %s update type %d\n", info->broadcast_code, type); + uint8_t handle = info->handle; + if (handle >= kMaxLidarCount) { + return; + } + if (type == kEventConnect) { + QueryDeviceInformation(handle, OnDeviceInformation, NULL); + if (devices[handle].device_state == kDeviceStateDisconnect) { + devices[handle].device_state = kDeviceStateConnect; + devices[handle].info = *info; + } + } else if (type == kEventDisconnect) { + devices[handle].device_state = kDeviceStateDisconnect; + } else if (type == kEventStateChange) { + devices[handle].info = *info; + } + + if (devices[handle].device_state == kDeviceStateConnect) { + printf("Device State error_code %d\n", devices[handle].info.status.status_code); + printf("Device State working state %d\n", devices[handle].info.state); + printf("Device feature %d\n", devices[handle].info.feature); + if (devices[handle].info.state == kLidarStateNormal && devices[handle].info.status.status_code == 0) { + if (devices[handle].info.type == kDeviceTypeHub) { + HubStartSampling(OnSampleCallback, NULL); + } else { + LidarStartSampling(handle, OnSampleCallback, NULL); + } + devices[handle].device_state = kDeviceStateSampling; + } + } +} + +/** Callback function when broadcast message received. + * You need to add listening device broadcast code and set the point cloud data callback in this function. + */ +void OnDeviceBroadcast(const BroadcastDeviceInfo *info) { + if (info == NULL) { + return; + } + + printf("Receive Broadcast Code %s\n", info->broadcast_code); + bool found = false; + + int i = 0; + for (i = 0; i < BROADCAST_CODE_LIST_SIZE; ++i) { + if (strncmp(info->broadcast_code, broadcast_code_list[i], kBroadcastCodeSize) == 0) { + found = true; + break; + } + } + if (!found) { + return; + } + + bool result = false; + uint8_t handle = 0; + result = AddLidarToConnect(info->broadcast_code, &handle); + if (result == kStatusSuccess) { + /** Set the point cloud data for a specific Livox LiDAR. */ + SetDataCallback(handle, GetLidarData); + devices[handle].handle = handle; + devices[handle].device_state = kDeviceStateDisconnect; + } +} + +int main(int argc, const char *argv[]) { + /** Initialize Livox-SDK. */ + if (!Init()) { + return -1; + } + + memset(devices, 0, sizeof(devices)); + memset(data_recveive_count, 0, sizeof(data_recveive_count)); + +/** Set the callback function receiving broadcast message from Livox LiDAR. */ + SetBroadcastCallback(OnDeviceBroadcast); + +/** Set the callback function called when device state change, + * which means connection/disconnection and changing of LiDAR state. + */ + SetDeviceStateUpdateCallback(OnDeviceChange); + +/** Start the device discovering routine. */ + if (!Start()) { + Uninit(); + return -1; + } + + sleep(30); + + int i = 0; + for (i = 0; i < kMaxLidarCount; ++i) { + if (devices[i].device_state == kDeviceStateSampling) { +/** Stop the sampling of Livox LiDAR. */ + LidarStopSampling(devices[i].handle, OnStopSampleCallback, NULL); + } + } + +/** Uninitialize Livox-SDK. */ + Uninit(); +}