# Quick Start - Chạy C# Example

## Bước 1: Cài đặt .NET SDK (nếu chưa có)

### Ubuntu/Debian:
```bash
# Cài đặt .NET 6.0 hoặc mới hơn
wget https://dot.net/v1/dotnet-install.sh
chmod +x dotnet-install.sh
./dotnet-install.sh --channel 6.0

# Thêm vào PATH
export PATH=$PATH:$HOME/.dotnet
export DOTNET_ROOT=$HOME/.dotnet

# Hoặc cài qua apt (nếu có)
sudo apt-get update
sudo apt-get install -y dotnet-sdk-6.0
```

### Kiểm tra:
```bash
dotnet --version
```

## Bước 2: Build C API Library

```bash
cd /home/robotics/AGV/Diff_Wheel_Prj/pnkx_nav_core
mkdir -p build
cd build
cmake ..
make nav_c_api
```

Library sẽ ở: `build/src/APIs/c_api/libnav_c_api.so`

## Bước 3: Chạy C# Example

### Cách 1: Dùng script tự động (Khuyến nghị)

```bash
cd /home/robotics/AGV/Diff_Wheel_Prj/pnkx_nav_core/examples
chmod +x run_example.sh
./run_example.sh
```

Script sẽ tự động:
- ✅ Build library nếu chưa có
- ✅ Tạo C# project
- ✅ Copy library và source code
- ✅ Set LD_LIBRARY_PATH cho tất cả dependencies
- ✅ Build và chạy example

### Cách 2: Chạy thủ công

```bash
# 1. Tạo project
cd /home/robotics/AGV/Diff_Wheel_Prj/pnkx_nav_core/examples
dotnet new console -n NavigationExample
cd NavigationExample

# 2. Copy source code
cp ../CSharpExample.cs Program.cs

# 3. Tạo .csproj
cat > NavigationExample.csproj << 'EOF'
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <RuntimeIdentifier>linux-x64</RuntimeIdentifier>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
  </PropertyGroup>
  <ItemGroup>
    <None Include="libnav_c_api.so">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>
</Project>
EOF

# 4. Copy library
cp ../../build/src/APIs/c_api/libnav_c_api.so .

# 5. Set library path (quan trọng!)
export LD_LIBRARY_PATH=../../build/src/APIs/c_api:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=../../build/src/Navigations/Libraries/robot_costmap_2d:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=../../build/src/Navigations/Libraries/geometry2/robot_tf3_geometry_msgs:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=../../build/src/Navigations/Libraries/geometry2/robot_tf3_sensor_msgs:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=../../build/src/Navigations/Packages/move_base:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=../../build/src/Navigations/Cores/move_base_core:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=../../build/src/Navigations/Libraries/tf3:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=../../build/src/Navigations/Libraries/robot_time:$LD_LIBRARY_PATH

# 6. Build và chạy
dotnet build
dotnet run
```

## Bước 4: Kiểm tra kết quả

Nếu chạy thành công, bạn sẽ thấy output như:
```
Robot pose: x=0.0, y=0.0, theta=0.0
State: PENDING, Feedback: ...
```

## Troubleshooting

### Lỗi: "dotnet: command not found"
→ Cài đặt .NET SDK (xem Bước 1)

### Lỗi: "Unable to load DLL 'libnav_c_api.so'"
```bash
# Kiểm tra library có tồn tại không
ls -la ../../build/src/APIs/c_api/libnav_c_api.so

# Set LD_LIBRARY_PATH (bao gồm tất cả dependencies)
export LD_LIBRARY_PATH=../../build/src/APIs/c_api:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=../../build/src/Navigations/Libraries/robot_costmap_2d:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=../../build/src/Navigations/Libraries/geometry2/robot_tf3_geometry_msgs:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=../../build/src/Navigations/Libraries/geometry2/robot_tf3_sensor_msgs:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=../../build/src/Navigations/Packages/move_base:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=../../build/src/Navigations/Cores/move_base_core:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=../../build/src/Navigations/Libraries/tf3:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=../../build/src/Navigations/Libraries/robot_time:$LD_LIBRARY_PATH

# Hoặc copy vào system lib
sudo cp libnav_c_api.so /usr/local/lib/
sudo ldconfig
```

### Lỗi: "Missing dependencies"
```bash
# Kiểm tra dependencies
ldd libnav_c_api.so

# Đảm bảo tất cả dependencies đều có sẵn
```

### Lỗi: "Failed to create navigation instance"
→ Đã được sửa! `navigation_create()` bây giờ tạo instance của `move_base::MoveBase`.
   Nếu vẫn gặp lỗi, kiểm tra:
   - Library đã được build đầy đủ: `make nav_c_api` trong build directory
   - Dependencies đã được build: `make move_base robot_costmap_2d`
   - LD_LIBRARY_PATH đã được set đúng

