Skip to content

Commit 77020ba

Browse files
committed
feat: Enable stopping individual manipulators
1 parent e85ea4a commit 77020ba

File tree

4 files changed

+350
-190
lines changed

4 files changed

+350
-190
lines changed

Assets/Scripts/EphysLink/CommunicationManager.cs

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class CommunicationManager : MonoBehaviour
2525
public static readonly string EPHYS_LINK_MIN_VERSION_STRING =
2626
$"≥ v{string.Join(".", EPHYS_LINK_MIN_VERSION)}";
2727

28-
private const string UNKOWN_EVENT = "UNKNOWN_EVENT";
28+
private const string UNKOWN_EVENT = "{\"error\": \"Unknown event.\"}";
2929

3030
/// <summary>
3131
/// The current state of the connection to Ephys Link.
@@ -284,7 +284,7 @@ public void VerifyVersion(Action onSuccess, Action onFailure)
284284
.TakeWhile(numbers => numbers.Length > 0)
285285
.Select(nonEmpty => int.Parse(new string(nonEmpty)))
286286
.ToArray();
287-
print(versionNumbers[0]+"."+versionNumbers[1]+"."+versionNumbers[2]);
287+
print(versionNumbers[0] + "." + versionNumbers[1] + "." + versionNumbers[2]);
288288

289289
// Fail if major version mismatch (breaking changes).
290290
if (versionNumbers[0] != EPHYS_LINK_MIN_VERSION[0])
@@ -569,13 +569,57 @@ public void SetInsideBrain(
569569
.Emit("set_inside_brain", ToJson(request));
570570
}
571571

572+
/// <summary>
573+
/// Request a manipulator stops moving.
574+
/// </summary>
575+
/// <param name="manipulatorId"></param>
576+
/// <param name="onSuccessCallback"></param>
577+
/// <param name="onErrorCallback"></param>
578+
public void Stop(
579+
string manipulatorId,
580+
Action onSuccessCallback,
581+
Action<string> onErrorCallback
582+
)
583+
{
584+
_connectionManager
585+
.Socket.ExpectAcknowledgement<string>(data =>
586+
{
587+
if (DataKnownAndNotEmpty(data))
588+
{
589+
// Non-empty response means error.
590+
onErrorCallback?.Invoke(data);
591+
}
592+
else
593+
{
594+
// Empty response means success.
595+
onSuccessCallback?.Invoke();
596+
}
597+
})
598+
.Emit("stop", manipulatorId);
599+
}
600+
572601
/// <summary>
573602
/// Request all movement to stop.
574603
/// </summary>
575-
/// <param name="callback">Callback function to handle stop result</param>
576-
public void Stop(Action<string> callback)
604+
/// <param name="onSuccessCallback">Handle successful stop.</param>
605+
/// <param name="onErrorCallback">Handle failed stops.</param>
606+
public void StopAll(Action onSuccessCallback, Action<string> onErrorCallback)
577607
{
578-
_connectionManager.Socket.ExpectAcknowledgement(callback).Emit("stop");
608+
_connectionManager
609+
.Socket.ExpectAcknowledgement<string>(data =>
610+
{
611+
if (DataKnownAndNotEmpty(data))
612+
{
613+
// Non-empty response means error.
614+
onErrorCallback?.Invoke(data);
615+
}
616+
else
617+
{
618+
// Empty response means success.
619+
onSuccessCallback?.Invoke();
620+
}
621+
})
622+
.Emit("stop_all");
579623
}
580624

581625
#endregion
@@ -599,4 +643,4 @@ private static string ToJson<T>(T data)
599643

600644
#endregion
601645
}
602-
}
646+
}

Assets/Scripts/Pinpoint/UI/EphysCopilot/DrivePanelHandler.cs

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,10 @@ private Vector4 _outsidePosition
334334

335335
// Drive Speeds
336336
private float _driveBaseSpeed = DEPTH_DRIVE_BASE_SPEED;
337+
337338
private float _per1000Speed =>
338339
_driveBaseSpeed <= DEPTH_DRIVE_BASE_SPEED ? PER_1000_SPEED : PER_1000_SPEED_TEST;
340+
339341
private float _targetDriveSpeed => _driveBaseSpeed + _targetDriveDistance * _per1000Speed;
340342
private float _nearTargetDriveSpeed => _driveBaseSpeed * NEAR_TARGET_SPEED_MULTIPLIER;
341343
private float _exitDriveBaseSpeed => _driveBaseSpeed * EXIT_DRIVE_SPEED_MULTIPLIER;
@@ -735,44 +737,48 @@ void CompleteOutside()
735737
}
736738
}
737739

740+
/// <summary>
741+
/// Stop this drive.
742+
/// </summary>
738743
public void Stop()
739744
{
740-
CommunicationManager.Instance.Stop(stopError =>
741-
{
742-
if (!string.IsNullOrEmpty(stopError))
743-
return;
744-
745-
// Show drive group and hide stop button.
746-
_statusText.text = "Stopped";
747-
_driveGroup.SetActive(true);
748-
_exitButton.SetActive(true);
749-
_stopButton.SetActive(false);
750-
751-
// Disable drive button if at Dura and above, otherwise enable.
752-
switch (_driveStateManager.State)
745+
CommunicationManager.Instance.Stop(
746+
_manipulatorId,
747+
() =>
753748
{
754-
case DriveState.AtDura:
755-
case DriveState.ExitingToMargin:
756-
case DriveState.AtExitMargin:
757-
case DriveState.ExitingToOutside:
758-
case DriveState.Outside:
759-
_driveButton.interactable = false;
760-
break;
761-
case DriveState.DrivingToNearTarget:
762-
case DriveState.AtNearTarget:
763-
case DriveState.DrivingToPastTarget:
764-
case DriveState.AtPastTarget:
765-
case DriveState.ReturningToTarget:
766-
case DriveState.ExitingToDura:
767-
case DriveState.ExitingToNearTarget:
768-
case DriveState.AtTarget:
769-
_driveButton.interactable = true;
770-
break;
771-
default:
772-
Debug.LogError("Unexpected stop state: " + _driveStateManager.State);
773-
break;
774-
}
775-
});
749+
// Show drive group and hide stop button.
750+
_statusText.text = "Stopped";
751+
_driveGroup.SetActive(true);
752+
_exitButton.SetActive(true);
753+
_stopButton.SetActive(false);
754+
755+
// Disable drive button if at Dura and above, otherwise enable.
756+
switch (_driveStateManager.State)
757+
{
758+
case DriveState.AtDura:
759+
case DriveState.ExitingToMargin:
760+
case DriveState.AtExitMargin:
761+
case DriveState.ExitingToOutside:
762+
case DriveState.Outside:
763+
_driveButton.interactable = false;
764+
break;
765+
case DriveState.DrivingToNearTarget:
766+
case DriveState.AtNearTarget:
767+
case DriveState.DrivingToPastTarget:
768+
case DriveState.AtPastTarget:
769+
case DriveState.ReturningToTarget:
770+
case DriveState.ExitingToDura:
771+
case DriveState.ExitingToNearTarget:
772+
case DriveState.AtTarget:
773+
_driveButton.interactable = true;
774+
break;
775+
default:
776+
Debug.LogError("Unexpected stop state: " + _driveStateManager.State);
777+
break;
778+
}
779+
},
780+
Debug.LogError
781+
);
776782
}
777783

778784
#endregion

Assets/Scripts/Pinpoint/UI/EphysCopilot/InsertionSelectionPanelHandler.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public static readonly Dictionary<
109109
string,
110110
ProbeManager
111111
> ManipulatorIDToSelectedTargetProbeManager = new();
112+
112113
private static readonly UnityEvent _shouldUpdateTargetInsertionOptionsEvent = new();
113114

114115
#endregion
@@ -215,14 +216,15 @@ public void MoveOrStopProbeToInsertionTarget()
215216
if (_isMoving)
216217
// Movement in progress -> should stop movement
217218
{
218-
CommunicationManager.Instance.Stop(stopError =>
219-
{
220-
if (!string.IsNullOrEmpty(stopError))
221-
return;
222-
223-
_isMoving = false;
224-
_moveButtonText.text = MOVE_TO_TARGET_INSERTION_STR;
225-
});
219+
CommunicationManager.Instance.Stop(
220+
ProbeManager.ManipulatorBehaviorController.ManipulatorID,
221+
() =>
222+
{
223+
_isMoving = false;
224+
_moveButtonText.text = MOVE_TO_TARGET_INSERTION_STR;
225+
},
226+
Debug.LogError
227+
);
226228
}
227229
else
228230
{

0 commit comments

Comments
 (0)