Files
I150/srcs/RobotNet10/Commons/RobotNet10.NavigationTune/Controllers/VelocityControlController.cs
2026-07-03 16:37:12 +07:00

97 lines
3.1 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using RobotNet10.NavigationTune.Execution;
namespace RobotNet10.NavigationTune.Controllers;
/// <summary>
/// REST API controller for manual velocity control
/// </summary>
[ApiController]
[Route("api/[controller]")]
public class VelocityControlController(
IVelocityProvider? velocityProvider,
ILogger<VelocityControlController> logger) : ControllerBase
{
private readonly IVelocityProvider? _velocityProvider = velocityProvider;
private readonly ILogger<VelocityControlController> _logger = logger;
/// <summary>
/// Set manual velocity command
/// </summary>
[HttpPost("set-velocity")]
public ActionResult SetVelocity([FromBody] VelocityCommandRequest request)
{
try
{
if (_velocityProvider == null)
{
return BadRequest(new { error = "Velocity provider is not available. Robot connection required." });
}
_velocityProvider.SetVelocity(request.LinearVelocity, request.AngularVelocity);
return Ok(new { message = "Velocity command sent", linear = request.LinearVelocity, angular = request.AngularVelocity });
}
catch (Exception ex)
{
_logger.LogError(ex, "Error setting velocity");
return StatusCode(500, new { error = "Failed to set velocity", details = ex.Message });
}
}
/// <summary>
/// Stop all velocities (set to zero)
/// </summary>
[HttpPost("stop")]
public ActionResult Stop()
{
try
{
if (_velocityProvider == null)
{
return BadRequest(new { error = "Velocity provider is not available. Robot connection required." });
}
_velocityProvider.SetVelocity(0, 0);
return Ok(new { message = "All velocities stopped" });
}
catch (Exception ex)
{
_logger.LogError(ex, "Error stopping velocity");
return StatusCode(500, new { error = "Failed to stop velocity", details = ex.Message });
}
}
/// <summary>
/// Get current velocity
/// </summary>
[HttpGet("current")]
public ActionResult GetCurrentVelocity()
{
try
{
if (_velocityProvider == null)
{
return BadRequest(new { error = "Velocity provider is not available. Robot connection required." });
}
var (linear, angular) = _velocityProvider.GetActualVelocity();
return Ok(new { linear, angular });
}
catch (Exception ex)
{
_logger.LogError(ex, "Error getting current velocity");
return StatusCode(500, new { error = "Failed to get current velocity", details = ex.Message });
}
}
}
/// <summary>
/// Request model for velocity command
/// </summary>
public class VelocityCommandRequest
{
public double LinearVelocity { get; set; }
public double AngularVelocity { get; set; }
}