diff --git a/Quadruped.Driver/dynamixel_sdk/dynamixel.cs b/Quadruped.Driver/dynamixel_sdk/dynamixel.cs index 7049f2c..3493b6f 100644 --- a/Quadruped.Driver/dynamixel_sdk/dynamixel.cs +++ b/Quadruped.Driver/dynamixel_sdk/dynamixel.cs @@ -31,6 +31,7 @@ /* Author: Ryu Woon Jung (Leon) */ using System; +using System.IO; using System.Runtime.InteropServices; namespace Quadruped.Driver.dynamixel_sdk @@ -39,6 +40,39 @@ class dynamixel { const string dll_path = "dxl_lib.ds"; + static dynamixel() + { + var assemblyPath = AppDomain.CurrentDomain.BaseDirectory; + var architecture = RuntimeInformation.OSArchitecture; + if (architecture == Architecture.Arm || architecture == Architecture.Arm64) + { + const string newFileName = "libdxl_sbc_c.so"; + File.Copy(Path.Combine(assemblyPath, newFileName), Path.Combine(assemblyPath, dll_path), true); + } + else if (architecture == Architecture.X64) + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + const string newFileName = "dxl_x64_c.dll"; + File.Copy(Path.Combine(assemblyPath, newFileName), Path.Combine(assemblyPath, dll_path), true); + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + const string newFileName = "libdxl_x64_c.so"; + File.Copy(Path.Combine(assemblyPath, newFileName), Path.Combine(assemblyPath, dll_path), true); + } + else + { + throw new NotSupportedException("This OS is not supported!"); + } + } + else + { + throw new NotSupportedException("This architecture is not supported!"); + } + } + + #region PortHandler [DllImport(dll_path)] public static extern int portHandler(string port_name); diff --git a/Quadruped.NetCore.TestConsole/Program.cs b/Quadruped.NetCore.TestConsole/Program.cs index f1a1a4f..5342437 100644 --- a/Quadruped.NetCore.TestConsole/Program.cs +++ b/Quadruped.NetCore.TestConsole/Program.cs @@ -3,7 +3,6 @@ using System.IO; using System.Numerics; using System.Reflection; -using System.Runtime.InteropServices; using Newtonsoft.Json; using Quadruped.Driver; @@ -11,62 +10,9 @@ namespace Quadruped.NetCore.TestConsole { class Program { - private const string DxlLib = "dxl_lib.ds"; - - private static void SaveCorrectDxlLibrary() - { - // This is an extremly ugly way to load the correct library for dynamixel - // TODO: figure out a better way - Console.ForegroundColor = ConsoleColor.Yellow; - var architecture = RuntimeInformation.OSArchitecture; - Console.WriteLine($"Current architecture {architecture}"); - if (architecture == Architecture.Arm || architecture == Architecture.Arm64) - { - const string newFileName = "libdxl_sbc_c.so"; - Console.WriteLine($"Saving library as {newFileName}"); - File.Copy(newFileName, DxlLib, true); - } - else if (architecture == Architecture.X64) - { - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - Console.WriteLine($"Current OS is {OSPlatform.Windows}"); - const string newFileName = "dxl_x64_c.dll"; - Console.WriteLine($"Saving library as {newFileName}"); - File.Copy(newFileName, DxlLib, true); - } - else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - { - Console.WriteLine($"Current OS is {OSPlatform.Linux}"); - const string newFileName = "libdxl_x64_c.so"; - Console.WriteLine($"Saving library as {newFileName}"); - File.Copy(newFileName, DxlLib, true); - } - else - { - throw new NotSupportedException("This OS is not supported!"); - } - } - else - { - throw new NotSupportedException("This architecture is not supported!"); - } - Console.ResetColor(); - } - private static void DeleteDxlLibrary() - { - // This is an extremly ugly way to load the correct library for dynamixel - // TODO: figure out a better way - Console.ForegroundColor = ConsoleColor.Yellow; - Console.WriteLine($"Deleting {DxlLib}"); - File.Delete(DxlLib); - Console.ResetColor(); - } - static void Main(string[] args) { Directory.SetCurrentDirectory(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)); - SaveCorrectDxlLibrary(); Console.WriteLine("Starting"); using (var driver = new DynamixelDriver(args.Length > 0 ? args[0] : "COM4")) using (var quadruped = new QuadrupedIkDriver(driver)) @@ -132,7 +78,6 @@ static void Main(string[] args) quadruped.DisableMotors(); } } - DeleteDxlLibrary(); Console.WriteLine("Done"); } diff --git a/Quadruped.WebInterface/Program.cs b/Quadruped.WebInterface/Program.cs index aae70d0..ae9a10e 100644 --- a/Quadruped.WebInterface/Program.cs +++ b/Quadruped.WebInterface/Program.cs @@ -11,16 +11,6 @@ public class Program { public static void Main(string[] args) { - try - { - SaveCorrectDxlLibrary(); - } - catch (Exception ) - { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine("Failed to load library!!!"); - Console.ResetColor(); - } BuildWebHost(args).Run(); } @@ -36,47 +26,5 @@ public static IWebHost BuildWebHost(string[] args) => .UseUrls("http://0.0.0.0:50093/") .Build(); - private const string DxlLib = "dxl_lib.ds"; - - private static void SaveCorrectDxlLibrary() - { - // This is an extremly ugly way to load the correct library for dynamixel - // TODO: figure out a better way - Console.ForegroundColor = ConsoleColor.Yellow; - var architecture = RuntimeInformation.OSArchitecture; - Console.WriteLine($"Current architecture {architecture}"); - if (architecture == Architecture.Arm || architecture == Architecture.Arm64) - { - const string newFileName = "libdxl_sbc_c.so"; - Console.WriteLine($"Saving library as {newFileName}"); - File.Copy(newFileName, DxlLib, true); - } - else if (architecture == Architecture.X64) - { - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - Console.WriteLine($"Current OS is {OSPlatform.Windows}"); - const string newFileName = "dxl_x64_c.dll"; - Console.WriteLine($"Saving library as {newFileName}"); - File.Copy(newFileName, DxlLib, true); - } - else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - { - Console.WriteLine($"Current OS is {OSPlatform.Linux}"); - const string newFileName = "libdxl_x64_c.so"; - Console.WriteLine($"Saving library as {newFileName}"); - File.Copy(newFileName, DxlLib, true); - } - else - { - throw new NotSupportedException("This OS is not supported!"); - } - } - else - { - throw new NotSupportedException("This architecture is not supported!"); - } - Console.ResetColor(); - } } }