Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only send start_pose on first mission in queue #1623

Merged
merged 2 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/api.test/Mocks/IsarServiceMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Api.Test.Mocks
{
public class MockIsarService : IIsarService
{
public async Task<IsarMission> StartMission(Robot robot, MissionRun mission)
public async Task<IsarMission> StartMission(Robot robot, MissionRun mission, bool isFirstMissionInQueue = false)
{
await Task.Run(() => Thread.Sleep(1));
var isarServiceMissionResponse = new IsarMission(
Expand Down
1 change: 1 addition & 0 deletions backend/api/EventHandlers/IsarConnectionEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ private async void OnTimeoutEvent(IsarRobotHeartbeatMessage robotHeartbeatMessag
{
await RobotService.UpdateRobotIsarConnected(robot.Id, false);
await RobotService.UpdateCurrentMissionId(robot.Id, null);
await RobotService.UpdateCurrentArea(robot.Id, null);
}
catch (RobotNotFoundException) { return; }
}
Expand Down
11 changes: 7 additions & 4 deletions backend/api/EventHandlers/MissionEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ private async void OnMissionRunCreated(object? sender, MissionRunCreatedEventArg
return;
}

bool isFirstMissionInQueue = false;

if (!await LocalizationService.RobotIsLocalized(missionRun.Robot.Id))
{
isFirstMissionInQueue = true;
if (missionRun.Robot.RobotCapabilities != null && !missionRun.Robot.RobotCapabilities.Contains(RobotCapabilitiesEnum.localize))
{
await RobotService.UpdateCurrentArea(missionRun.Robot.Id, missionRun.Area);
Expand Down Expand Up @@ -122,7 +124,7 @@ or IsarCommunicationException
await CancelReturnToHomeOnNewMissionSchedule(missionRun);

_startMissionSemaphore.WaitOne();
try { await MissionScheduling.StartNextMissionRunIfSystemIsAvailable(missionRun.Robot.Id); }
try { await MissionScheduling.StartNextMissionRunIfSystemIsAvailable(missionRun.Robot.Id, isFirstMissionInQueue: isFirstMissionInQueue); }
catch (MissionRunNotFoundException) { return; }
finally { _startMissionSemaphore.Release(); }
}
Expand All @@ -139,7 +141,7 @@ private async void OnRobotAvailable(object? sender, RobotAvailableEventArgs e)

if (robot.CurrentMissionId != null)
{
var stuckMission = await MissionService.ReadById(robot.CurrentMissionId);
var stuckMission = await MissionService.ReadById(robot.CurrentMissionId!);
if (stuckMission == null)
{
_logger.LogError("MissionRun with ID: {MissionId} was not found in the database", robot.CurrentMissionId);
Expand All @@ -153,8 +155,9 @@ private async void OnRobotAvailable(object? sender, RobotAvailableEventArgs e)
}
}

bool isFirstMissionInQueue = robot.CurrentArea == null;
_startMissionSemaphore.WaitOne();
try { await MissionScheduling.StartNextMissionRunIfSystemIsAvailable(robot.Id); }
try { await MissionScheduling.StartNextMissionRunIfSystemIsAvailable(robot.Id, isFirstMissionInQueue: isFirstMissionInQueue); }
catch (MissionRunNotFoundException) { return; }
finally { _startMissionSemaphore.Release(); }
}
Expand Down Expand Up @@ -267,7 +270,7 @@ private async void OnEmergencyButtonDepressedForRobot(object? sender, EmergencyB
catch (RobotNotFoundException) { return; }

_startMissionSemaphore.WaitOne();
try { await MissionScheduling.StartNextMissionRunIfSystemIsAvailable(robot.Id); }
try { await MissionScheduling.StartNextMissionRunIfSystemIsAvailable(robot.Id, isFirstMissionInQueue: robot.CurrentArea == null); }
catch (MissionRunNotFoundException) { return; }
finally { _startMissionSemaphore.Release(); }
}
Expand Down
14 changes: 14 additions & 0 deletions backend/api/EventHandlers/MqttEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ private async void OnIsarStatus(object? sender, MqttReceivedArgs mqttArgs)
_logger.LogInformation("Updated status for robot {Name} to {Status}", robot.Name, isarStatus.Status);

if (isarStatus.Status == RobotStatus.Available) missionSchedulingService.TriggerRobotAvailable(new RobotAvailableEventArgs(robot.Id));
else if (isarStatus.Status == RobotStatus.Offline) await robotService.UpdateCurrentArea(robot.Id, null);
}

private async void OnIsarRobotInfo(object? sender, MqttReceivedArgs mqttArgs)
Expand Down Expand Up @@ -290,6 +291,19 @@ private async void OnIsarMissionUpdate(object? sender, MqttReceivedArgs mqttArgs
}
}

if (flotillaMissionRun.IsReturnHomeMission() && (flotillaMissionRun.Status == MissionStatus.Cancelled || flotillaMissionRun.Status == MissionStatus.Failed))
{
try
{
await robotService.UpdateCurrentArea(robot.Id, null);
}
catch (RobotNotFoundException)
{
_logger.LogError("Could not find robot '{RobotName}' with ID '{Id}'", robot.Name, robot.Id);
return;
}
}

try { await robotService.UpdateCurrentMissionId(robot.Id, null); }
catch (RobotNotFoundException)
{
Expand Down
6 changes: 3 additions & 3 deletions backend/api/Services/IsarService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Api.Services
{
public interface IIsarService
{
public Task<IsarMission> StartMission(Robot robot, MissionRun missionRun);
public Task<IsarMission> StartMission(Robot robot, MissionRun missionRun, bool isFirstMissionInQueue = false);

public Task<IsarControlMissionResponse> StopMission(Robot robot);

Expand All @@ -23,15 +23,15 @@ public class IsarService(IDownstreamApi isarApi, ILogger<IsarService> logger) :
{
public const string ServiceName = "IsarApi";

public async Task<IsarMission> StartMission(Robot robot, MissionRun missionRun)
public async Task<IsarMission> StartMission(Robot robot, MissionRun missionRun, bool isFirstMissionInQueue = false)
{
var response = await CallApi(
HttpMethod.Post,
robot.IsarUri,
"schedule/start-mission",
new
{
mission_definition = new IsarMissionDefinition(missionRun)
mission_definition = new IsarMissionDefinition(missionRun, includeStartPose: isFirstMissionInQueue)
}
);

Expand Down
10 changes: 5 additions & 5 deletions backend/api/Services/MissionSchedulingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Api.Services
{
public interface IMissionSchedulingService
{
public Task StartNextMissionRunIfSystemIsAvailable(string robotId);
public Task StartNextMissionRunIfSystemIsAvailable(string robotId, bool isFirstMissionInQueue = false);

public Task<bool> OngoingMission(string robotId);

Expand All @@ -33,7 +33,7 @@ public interface IMissionSchedulingService
public class MissionSchedulingService(ILogger<MissionSchedulingService> logger, IMissionRunService missionRunService, IRobotService robotService,
IAreaService areaService, IIsarService isarService, ILocalizationService localizationService, IReturnToHomeService returnToHomeService, ISignalRService signalRService) : IMissionSchedulingService
{
public async Task StartNextMissionRunIfSystemIsAvailable(string robotId)
public async Task StartNextMissionRunIfSystemIsAvailable(string robotId, bool isFirstMissionInQueue = false)
{
logger.LogInformation("Starting next mission run if system is available for robot ID: {RobotId}", robotId);
var robot = await robotService.ReadById(robotId);
Expand Down Expand Up @@ -109,7 +109,7 @@ public async Task StartNextMissionRunIfSystemIsAvailable(string robotId)
if (missionRun == null) { return; }
}

try { await StartMissionRun(missionRun); }
try { await StartMissionRun(missionRun, isFirstMissionInQueue: isFirstMissionInQueue); }
catch (Exception ex) when (
ex is MissionException
or RobotNotFoundException
Expand Down Expand Up @@ -373,7 +373,7 @@ private async Task MoveInterruptedMissionsToQueue(IEnumerable<string> interrupte
}
}

private async Task StartMissionRun(MissionRun queuedMissionRun)
private async Task StartMissionRun(MissionRun queuedMissionRun, bool isFirstMissionInQueue = false)
{
string robotId = queuedMissionRun.Robot.Id;
string missionRunId = queuedMissionRun.Id;
Expand Down Expand Up @@ -409,7 +409,7 @@ private async Task StartMissionRun(MissionRun queuedMissionRun)
}

IsarMission isarMission;
try { isarMission = await isarService.StartMission(robot, missionRun); }
try { isarMission = await isarService.StartMission(robot, missionRun, isFirstMissionInQueue: isFirstMissionInQueue); }
catch (HttpRequestException e)
{
string errorMessage = $"Could not reach ISAR at {robot.IsarUri}";
Expand Down
4 changes: 2 additions & 2 deletions backend/api/Services/Models/IsarMissionDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ public IsarMissionDefinition(List<IsarTaskDefinition> tasks)
Tasks = tasks;
}

public IsarMissionDefinition(MissionRun missionRun)
public IsarMissionDefinition(MissionRun missionRun, bool includeStartPose = false)
{
Id = missionRun.IsarMissionId;
Name = missionRun.Name;
Tasks = missionRun.Tasks.Select(task => new IsarTaskDefinition(task, missionRun)).ToList();
StartPose = missionRun.Area.DefaultLocalizationPose != null ? new IsarPose(missionRun.Area.DefaultLocalizationPose.Pose) : null;
StartPose = includeStartPose && missionRun.Area.DefaultLocalizationPose != null ? new IsarPose(missionRun.Area.DefaultLocalizationPose.Pose) : null;
}
}

Expand Down
Loading