using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using RobotNet10.NavigationTune.Execution; namespace RobotNet10.NavigationTune.Controllers; /// /// REST API controller for manual velocity control /// [ApiController] [Route("api/[controller]")] public class VelocityControlController( IVelocityProvider? velocityProvider, ILogger logger) : ControllerBase { private readonly IVelocityProvider? _velocityProvider = velocityProvider; private readonly ILogger _logger = logger; /// /// Set manual velocity command /// [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 }); } } /// /// Stop all velocities (set to zero) /// [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 }); } } /// /// Get current velocity /// [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 }); } } } /// /// Request model for velocity command /// public class VelocityCommandRequest { public double LinearVelocity { get; set; } public double AngularVelocity { get; set; } }