Advertisement
NoobsDeSroobs

Unreal Build Source

Feb 2nd, 2015
1,184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.40 KB | None | 0 0
  1. ///////Header//////////
  2.  
  3. // Fill out your copyright notice in the Description page of Project Settings.
  4.  
  5. #pragma once
  6.  
  7. #include "GameFramework/PlayerController.h"
  8. #include <rplidar.h> //RPLIDAR standard sdk, all-in-one header
  9. #include "LidarPlayerController.generated.h"
  10.  
  11.  
  12. #ifndef _countof
  13. #define _countof(_Array) (int)(sizeof(_Array) / sizeof(_Array[0]))
  14. #endif
  15.  
  16. //using namespace rp::standalone::rplidar;
  17.  
  18. /**
  19.  *
  20.  */
  21. UCLASS()
  22. class OCULUSTEST_API ALidarPlayerController : public APlayerController
  23. {
  24.     GENERATED_BODY()
  25.    
  26. private:
  27.     const char * opt_com_path = nullptr;
  28.     _u32         opt_com_baudrate = 115200;
  29.     u_result     op_result;
  30.     rp::standalone::rplidar::RPlidarDriver * drv;
  31.     bool checkRPLIDARHealth(rp::standalone::rplidar::RPlidarDriver * driver);
  32.  
  33. public:
  34.     ALidarPlayerController(const class FObjectInitializer& PCIP);
  35.     ~ALidarPlayerController();
  36.     FVector2D poll();
  37. };
  38.  
  39.  
  40. ///////CPP//////////
  41.  
  42. ALidarPlayerController::ALidarPlayerController(const class FObjectInitializer& PCIP) : Super(PCIP)
  43. {
  44.     opt_com_path = "\\\\.\\com3"; //Windows specific
  45.  
  46.     // create the driver instance
  47.     drv = rp::standalone::rplidar::RPlidarDriver::CreateDriver(rp::standalone::rplidar::RPlidarDriver::DRIVER_TYPE_SERIALPORT);
  48.  
  49.     if (!drv) {
  50.         if (GEngine)
  51.             GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, "insufficent memory, exit\n");
  52.         exit(-2);
  53.     }
  54.  
  55.  
  56.     // make connection...
  57.     if (IS_FAIL(drv->connect(opt_com_path, opt_com_baudrate))) {
  58.         if (GEngine)
  59.             GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, "Error, cannot bind to the specified serial port %s.\n");
  60.     }
  61.  
  62.     // check health...
  63.     if (!checkRPLIDARHealth(drv)) {
  64.         if (GEngine)
  65.             GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, "Lidar health warning.");
  66.     }
  67.  
  68.  
  69.     // start scan...
  70.     drv->startScan();
  71.  
  72.     if (GEngine)
  73.         GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, "Lidar loaded\n");
  74. };
  75. ALidarPlayerController::~ALidarPlayerController()
  76. {
  77.     rp::standalone::rplidar::RPlidarDriver::DisposeDriver(drv);
  78. };
  79.  
  80.  
  81. FVector2D ALidarPlayerController::poll()
  82. {
  83.     rplidar_response_measurement_node_t nodes[360];
  84.     size_t   count = _countof(nodes);
  85.     printf("%u\n", count);
  86.     op_result = drv->grabScanData(nodes, count);
  87.  
  88.     if (IS_OK(op_result)) {
  89.         drv->ascendScanData(nodes, count);
  90.         for (int pos = 0; pos < (int)count; ++pos) {
  91.             float angle = (nodes[pos].angle_q6_checkbit >> RPLIDAR_RESP_MEASUREMENT_ANGLE_SHIFT) / 64.0f;
  92.             float distance = nodes[pos].distance_q2 / 4.0f;
  93.         }
  94.     }
  95.     return FVector2D(0.0f, 0.0f);
  96. }
  97.  
  98. bool ALidarPlayerController::checkRPLIDARHealth(rp::standalone::rplidar::RPlidarDriver * driver)
  99. {
  100.  
  101.     u_result     op_result;
  102.     rplidar_response_device_health_t healthinfo;
  103.  
  104.  
  105.     op_result = drv->getHealth(healthinfo);
  106.     if (IS_OK(op_result)) { // the macro IS_OK is the preperred way to judge whether the operation is succeed.
  107.         if (GEngine)
  108.             GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, "RPLidar health status : %d\n");
  109.         if (healthinfo.status == RPLIDAR_STATUS_ERROR) {
  110.             if (GEngine)
  111.                 GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, "Error, rplidar internal error detected. Please reboot the device to retry.\n");
  112.             // enable the following code if you want rplidar to be reboot by software
  113.             // driver->reset();
  114.             return false;
  115.         } else {
  116.             return true;
  117.         }
  118.  
  119.     } else {
  120.         if (GEngine)
  121.             GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, "Error, cannot retrieve the lidar health code: %x\n");
  122.         return false;
  123.     }
  124. }
  125.  
  126.  
  127. ///////////Project.Build.cs///////////////////
  128.  
  129. // Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.
  130.  
  131. using UnrealBuildTool;
  132. using System.IO;
  133. public class OculusTest : ModuleRules
  134. {
  135.     private string ModulePath
  136.     {
  137.         get { return Path.GetDirectoryName(RulesCompiler.GetModuleFilename(this.GetType().Name)); }
  138.     }
  139.  
  140.     private string ThirdPartyPath
  141.     {
  142.         get { return Path.GetFullPath(Path.Combine( ModulePath, "../../ThirdParty/")); }
  143.     }
  144.  
  145.  
  146.     public OculusTest(TargetInfo Target)
  147.     {
  148.         PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" });
  149.         // Add static libs
  150.         PublicAdditionalLibraries.Add(Path.Combine(ThirdPartyPath, "RPLidar/Libraries", "rplidar_driver.lib"));
  151.         // Add a path with the .h that has the function definitions
  152.         PublicIncludePaths.Add(Path.Combine(ThirdPartyPath, "RPLidar", "Includes"));
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement