From 746e81dcf3dc367ad558854af49b9819d23390fe Mon Sep 17 00:00:00 2001 From: Kenneth Yang Date: Tue, 30 Jul 2024 16:47:05 -0700 Subject: [PATCH 1/5] Removed MPM exception --- .../Probes/ManipulatorBehaviorController.cs | 107 +----------------- .../ManipulatorConnectionPanel.cs | 53 +-------- 2 files changed, 8 insertions(+), 152 deletions(-) diff --git a/Assets/Scripts/Pinpoint/Probes/ManipulatorBehaviorController.cs b/Assets/Scripts/Pinpoint/Probes/ManipulatorBehaviorController.cs index 37da33d6..9cbb5f8a 100644 --- a/Assets/Scripts/Pinpoint/Probes/ManipulatorBehaviorController.cs +++ b/Assets/Scripts/Pinpoint/Probes/ManipulatorBehaviorController.cs @@ -156,16 +156,16 @@ private void OnDisable() public void Initialize(string manipulatorID, bool calibrated) { - CommunicationManager.Instance.GetManipulators(reponse => + CommunicationManager.Instance.GetManipulators(response => { // Shortcut exit if we have an invalid manipulator ID - if (!reponse.Manipulators.Contains(manipulatorID)) + if (!response.Manipulators.Contains(manipulatorID)) return; // Set manipulator ID, number of axes, and dimensions ManipulatorID = manipulatorID; - NumAxes = reponse.NumAxes; - Dimensions = reponse.Dimensions; + NumAxes = response.NumAxes; + Dimensions = response.Dimensions; // Update transform and space UpdateSpaceAndTransform(); @@ -339,6 +339,7 @@ public void MoveByWorldSpaceDelta( ), newPos => { + print("New pos: " + newPos + "; Setting depth..."); // Process depth movement var targetDepth = newPos.w + manipulatorSpaceDepth; // Move the manipulator @@ -394,104 +395,6 @@ private void EchoPosition(Vector4 pos) if (!enabled && _probeController == null) return; - // Check for special Pathfinder mode - if (NumAxes == -1) - { - // Check if probe type changed - CommunicationManager.Instance.GetShankCount( - ManipulatorID, - shankCount => - { - // Use 2.4 if 4 shank, otherwise default to 1 - var probeType = - shankCount == 4 - ? ProbeProperties.ProbeType.Neuropixels24 - : ProbeProperties.ProbeType.Neuropixels1; - - // print("Read type: " + probeType + "; Current type: " + _probeManager.ProbeType); - // Check if change is needed - if (probeType != _probeManager.ProbeType) - { - // Unregister manipulator - _probeManager.SetIsEphysLinkControlled( - false, - ManipulatorID, - true, - () => - { - // Create new probe - CreatePathfinderProbe.Invoke(probeType); - - // Destroy current probe - DestroyThisProbe.Invoke(); - }, - Debug.LogError - ); - - // Exit early as this probe no longer exists - return; - } - - // Otherwise, update probe angles - CommunicationManager.Instance.GetAngles( - ManipulatorID, - angles => - { - _probeController.SetProbeAngles( - new Vector3(angles.x, 90 - angles.y, angles.z) - ); - - // If only the DV axis moved, then we drop on DV. Otherwise, we drop on depth. - if (Math.Abs(pos.z - _lastManipulatorPosition.z) > 0.0001) - IsSetToDropToSurfaceWithDepth = - Math.Abs(pos.x - _lastManipulatorPosition.x) > 0.0001 - || Math.Abs(pos.y - _lastManipulatorPosition.y) > 0.0001; - - // Copy in new 3-axis position into saved 4-axis position - _lastManipulatorPosition = new Vector4( - pos.x, - pos.y, - pos.z, - _lastManipulatorPosition.w - ); - - // Apply brain surface offset on correct axis - var brainSurfaceAdjustment = float.IsNaN(BrainSurfaceOffset) - ? 0 - : BrainSurfaceOffset; - if (IsSetToDropToSurfaceWithDepth) - _lastManipulatorPosition.w -= brainSurfaceAdjustment; - else - _lastManipulatorPosition.z -= brainSurfaceAdjustment; - - // Convert Pathfinder space coordinates into active atlas space - var convertedPos = _probeController.Insertion.World2T_Vector( - CoordinateSpace.Space2World_Vector(_lastManipulatorPosition) - ); - - // Copy and add 4th axis back in - _probeController.SetProbePosition( - new Vector4( - convertedPos.x, - convertedPos.y, - convertedPos.z, - _lastManipulatorPosition.w - ) - ); - - // Log and continue echoing - LogAndContinue(); - }, - Debug.LogError - ); - }, - Debug.LogError - ); - - // Exit early as we've handled Pathfinder - return; - } - // Calculate last used direction for dropping to brain surface (between depth and DV) var dvDelta = Math.Abs(pos.z - _lastManipulatorPosition.z); var depthDelta = Math.Abs(pos.w - _lastManipulatorPosition.w); diff --git a/Assets/Scripts/Pinpoint/UI/EphysLinkSettings/ManipulatorConnectionPanel.cs b/Assets/Scripts/Pinpoint/UI/EphysLinkSettings/ManipulatorConnectionPanel.cs index 654adccd..a88b5310 100644 --- a/Assets/Scripts/Pinpoint/UI/EphysLinkSettings/ManipulatorConnectionPanel.cs +++ b/Assets/Scripts/Pinpoint/UI/EphysLinkSettings/ManipulatorConnectionPanel.cs @@ -2,7 +2,6 @@ using System.Linq; using EphysLink; using TMPro; -using TrajectoryPlanner; using UnityEngine; using UnityEngine.UI; @@ -90,52 +89,6 @@ public void Initialize(EphysLinkSettings settingsMenu, string manipulatorID, int // Initialize components _manipulatorIdText.text = manipulatorID; - // Spawn and setup Pathfinder manipulators - if (_numAxes == -1) - { - // Hide all parts - _handednessGroup.SetActive(false); - _probeConnectionGroup.SetActive(false); - _probePropertiesSection.SetActive(false); - - CommunicationManager.Instance.GetShankCount( - manipulatorID, - shankCount => - { - // Use 2.4 if 4 shank, otherwise default to 1 - var probeType = - shankCount == 4 - ? ProbeProperties.ProbeType.Neuropixels24 - : ProbeProperties.ProbeType.Neuropixels1; - - CreatePathfinderProbe(probeType); - return; - - void CreatePathfinderProbe(ProbeProperties.ProbeType newProbeType) - { - // Create new probe - var trajectoryPlannerManager = - FindObjectOfType(); - var newProbe = trajectoryPlannerManager.AddNewProbe(newProbeType); - - // Configure probe and link to Ephys Link - newProbe.ManipulatorBehaviorController.CreatePathfinderProbe = - CreatePathfinderProbe; - newProbe.ManipulatorBehaviorController.DestroyThisProbe = () => - trajectoryPlannerManager.DestroyProbe(newProbe); - newProbe.Color = Color.magenta; - newProbe.name = "nsp_" + manipulatorID; - newProbe.Saved = false; - newProbe.SetIsEphysLinkControlled(true, manipulatorID); - } - }, - Debug.LogError - ); - - // Exit (don't need to do anything else for Pathfinder) - return; - } - // Restore or setup normal manipulator UpdateLinkableProbeOptions(); @@ -151,7 +104,7 @@ void CreatePathfinderProbe(ProbeProperties.ProbeType newProbeType) _handednessGroup.SetActive(true); } - // Apply handedness from memory or default to right handed, also pass along manipulator type + // Apply handedness from memory or default to right-handed, also pass along manipulator type if (_attachedProbe) { _handednessDropdown.value = _attachedProbe @@ -181,7 +134,7 @@ void CreatePathfinderProbe(ProbeProperties.ProbeType newProbeType) /// /// Handle changing manipulator's registered handedness on UI change. /// - /// Selected index of the handedness options (0 = left handed, 1 = right handed) + /// Selected index of the handedness options (0 = left-handed, 1 = right-handed) public void OnManipulatorHandednessValueChanged(int value) { // Set handedness on attached probe if it exists @@ -355,7 +308,7 @@ public void UpdateDuraDropDirection(int value) value == 1; } - public void SetDuraDropInteractable(bool interactable) + private void SetDuraDropInteractable(bool interactable) { _duraDropDirectionDropdown.interactable = interactable; } From daa0e8b06d37ef37839e7ff0fb8f2e12f20aaef4 Mon Sep 17 00:00:00 2001 From: Kenneth Yang Date: Wed, 31 Jul 2024 11:24:49 -0700 Subject: [PATCH 2/5] Re-enable copilot --- Assets/Prefabs/UI/SettingsMenu/Menus/EphysLinkMenu.prefab | 2 +- .../Scripts/Pinpoint/UI/EphysLinkSettings/EphysLinkSettings.cs | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/Assets/Prefabs/UI/SettingsMenu/Menus/EphysLinkMenu.prefab b/Assets/Prefabs/UI/SettingsMenu/Menus/EphysLinkMenu.prefab index 778578a3..3210c87c 100644 --- a/Assets/Prefabs/UI/SettingsMenu/Menus/EphysLinkMenu.prefab +++ b/Assets/Prefabs/UI/SettingsMenu/Menus/EphysLinkMenu.prefab @@ -4330,7 +4330,7 @@ MonoBehaviour: m_PressedTrigger: Pressed m_SelectedTrigger: Selected m_DisabledTrigger: Disabled - m_Interactable: 0 + m_Interactable: 1 m_TargetGraphic: {fileID: 6487466997665187127} toggleTransition: 1 graphic: {fileID: 2477337667047670143} diff --git a/Assets/Scripts/Pinpoint/UI/EphysLinkSettings/EphysLinkSettings.cs b/Assets/Scripts/Pinpoint/UI/EphysLinkSettings/EphysLinkSettings.cs index d8b5debf..05c7c530 100644 --- a/Assets/Scripts/Pinpoint/UI/EphysLinkSettings/EphysLinkSettings.cs +++ b/Assets/Scripts/Pinpoint/UI/EphysLinkSettings/EphysLinkSettings.cs @@ -392,9 +392,6 @@ public void ToggleCopilotPanel(bool isEnabled) public void InvokeShouldUpdateProbesListEvent() { ShouldUpdateProbesListEvent.Invoke(); - - // Enable/Disable Copilot toggle based on if there are any probes that can be controlled by it. - _copilotToggle.interactable = LinkedProbes.Count > 0; } #endregion From 649680b0067c793658ac967fa5539d14d636114e Mon Sep 17 00:00:00 2001 From: Kenneth Yang Date: Wed, 31 Jul 2024 11:33:22 -0700 Subject: [PATCH 3/5] Removed references to Pathfinder probe and axis count --- .../CoordinateSystems/PathfinderSpace.cs | 37 ------------------- .../CoordinateSystems/PathfinderSpace.cs.meta | 11 ------ .../Probes/ManipulatorBehaviorController.cs | 14 +------ .../Scripts/Pinpoint/Probes/ProbeManager.cs | 1 - .../UI/EphysLinkSettings/EphysLinkSettings.cs | 3 -- 5 files changed, 2 insertions(+), 64 deletions(-) delete mode 100644 Assets/Scripts/Pinpoint/CoordinateSystems/PathfinderSpace.cs delete mode 100644 Assets/Scripts/Pinpoint/CoordinateSystems/PathfinderSpace.cs.meta diff --git a/Assets/Scripts/Pinpoint/CoordinateSystems/PathfinderSpace.cs b/Assets/Scripts/Pinpoint/CoordinateSystems/PathfinderSpace.cs deleted file mode 100644 index ec02f4d3..00000000 --- a/Assets/Scripts/Pinpoint/CoordinateSystems/PathfinderSpace.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System; -using BrainAtlas.CoordinateSystems; -using UnityEngine; - -namespace Pinpoint.CoordinateSystems -{ - public class PathfinderSpace : CoordinateSpace - { - #region Properties - - public override string Name => "Pathfinder"; - public override Vector3 Dimensions => Vector3.one * 15f; - - #endregion - - - public override Vector3 Space2World(Vector3 coordSpace, bool useReference = true) - { - return new Vector3(coordSpace.x, coordSpace.z, coordSpace.y); - } - - public override Vector3 World2Space(Vector3 coordWorld, bool useReference = true) - { - return new Vector3(coordWorld.x, coordWorld.z, coordWorld.y); - } - - public override Vector3 Space2World_Vector(Vector3 vecSpace) - { - return Space2World(vecSpace); - } - - public override Vector3 World2Space_Vector(Vector3 vecWorld) - { - return World2Space(vecWorld); - } - } -} \ No newline at end of file diff --git a/Assets/Scripts/Pinpoint/CoordinateSystems/PathfinderSpace.cs.meta b/Assets/Scripts/Pinpoint/CoordinateSystems/PathfinderSpace.cs.meta deleted file mode 100644 index 582e72b0..00000000 --- a/Assets/Scripts/Pinpoint/CoordinateSystems/PathfinderSpace.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 7f80a87226e389a41aa0a48d15ea25e7 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Pinpoint/Probes/ManipulatorBehaviorController.cs b/Assets/Scripts/Pinpoint/Probes/ManipulatorBehaviorController.cs index 9cbb5f8a..2cb97051 100644 --- a/Assets/Scripts/Pinpoint/Probes/ManipulatorBehaviorController.cs +++ b/Assets/Scripts/Pinpoint/Probes/ManipulatorBehaviorController.cs @@ -191,20 +191,9 @@ void StartEchoing() }); } - public void Deinitialize() - { - // Destroy Pathfinder probe. - if (NumAxes == -1) - DestroyThisProbe.Invoke(); - } - private void UpdateSpaceAndTransform() { - CoordinateSpace = NumAxes switch - { - -1 => new PathfinderSpace(), - _ => new ManipulatorSpace(Dimensions) - }; + CoordinateSpace = new ManipulatorSpace(Dimensions); CoordinateTransform = NumAxes switch { 4 @@ -392,6 +381,7 @@ Action onErrorCallBack private void EchoPosition(Vector4 pos) { + // Exit if disabled and there is no probe controller. if (!enabled && _probeController == null) return; diff --git a/Assets/Scripts/Pinpoint/Probes/ProbeManager.cs b/Assets/Scripts/Pinpoint/Probes/ProbeManager.cs index ce731711..ea4cde71 100644 --- a/Assets/Scripts/Pinpoint/Probes/ProbeManager.cs +++ b/Assets/Scripts/Pinpoint/Probes/ProbeManager.cs @@ -1068,7 +1068,6 @@ public void SetIsEphysLinkControlled( } else { - ManipulatorBehaviorController.Deinitialize(); IsEphysLinkControlled = false; onSuccess?.Invoke(); } diff --git a/Assets/Scripts/Pinpoint/UI/EphysLinkSettings/EphysLinkSettings.cs b/Assets/Scripts/Pinpoint/UI/EphysLinkSettings/EphysLinkSettings.cs index 05c7c530..234ce5e0 100644 --- a/Assets/Scripts/Pinpoint/UI/EphysLinkSettings/EphysLinkSettings.cs +++ b/Assets/Scripts/Pinpoint/UI/EphysLinkSettings/EphysLinkSettings.cs @@ -433,9 +433,6 @@ var probeManager in ProbeManager.Instances.Where(probeManager => false, probeManager.ManipulatorBehaviorController.ManipulatorID ); - - // FIXME: This is done because of race condition with closing out server. Should be fixed with non-registration setup. - probeManager.ManipulatorBehaviorController.Deinitialize(); } CommunicationManager.Instance.DisconnectFromServer(() => From 25664b7cdd12241e66021c264119fbba69006648 Mon Sep 17 00:00:00 2001 From: Kenneth Yang Date: Wed, 31 Jul 2024 12:57:38 -0700 Subject: [PATCH 4/5] Fix 3lhm brain surface offset --- .../Probes/ManipulatorBehaviorController.cs | 47 +++++++++++++++---- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/Assets/Scripts/Pinpoint/Probes/ManipulatorBehaviorController.cs b/Assets/Scripts/Pinpoint/Probes/ManipulatorBehaviorController.cs index 2cb97051..d9b3efa7 100644 --- a/Assets/Scripts/Pinpoint/Probes/ManipulatorBehaviorController.cs +++ b/Assets/Scripts/Pinpoint/Probes/ManipulatorBehaviorController.cs @@ -392,35 +392,65 @@ private void EchoPosition(Vector4 pos) IsSetToDropToSurfaceWithDepth = depthDelta > dvDelta; _lastManipulatorPosition = pos; - // Apply zero coordinate offset + // Apply zero coordinate offset. var zeroCoordinateAdjustedManipulatorPosition = pos - ZeroCoordinateOffset; - // Convert to coordinate space + // Convert to coordinate space. var manipulatorSpacePosition = CoordinateTransform.T2U( zeroCoordinateAdjustedManipulatorPosition ); - // Brain surface adjustment + // Brain surface adjustment. var brainSurfaceAdjustment = float.IsNaN(BrainSurfaceOffset) ? 0 : BrainSurfaceOffset; if (IsSetToDropToSurfaceWithDepth) - zeroCoordinateAdjustedManipulatorPosition.w += brainSurfaceAdjustment; + { + // Apply depth adjustment to manipulator position for non-3 axis manipulators. + if (CoordinateTransform.Prefix != "3lhm") + zeroCoordinateAdjustedManipulatorPosition.w += brainSurfaceAdjustment; + } else manipulatorSpacePosition.y -= brainSurfaceAdjustment; - // Convert to world space + print( + zeroCoordinateAdjustedManipulatorPosition + + "\t" + + manipulatorSpacePosition + + "\t" + + brainSurfaceAdjustment + ); + + // Convert to world space. var zeroCoordinateAdjustedWorldPosition = CoordinateSpace.Space2World( manipulatorSpacePosition ); - // Set probe position (change axes to match probe) + // Set probe position (change axes to match probe). var transformedApmldv = BrainAtlasManager.World2T_Vector( zeroCoordinateAdjustedWorldPosition ); - // Split between 3 and 4 axis assignments + // Set probe position. + // For 3-axis manipulators, use depth to adjust brain offset if applying offset on depth. if (CoordinateTransform.Prefix == "3lhm") - _probeController.SetProbePosition(transformedApmldv); + { + if (IsSetToDropToSurfaceWithDepth) + { + _probeController.SetProbePosition( + new Vector4( + transformedApmldv.x, + transformedApmldv.y, + transformedApmldv.z, + brainSurfaceAdjustment + ) + ); + } + else + { + _probeController.SetProbePosition(transformedApmldv); + } + } else + { _probeController.SetProbePosition( new Vector4( transformedApmldv.x, @@ -429,6 +459,7 @@ private void EchoPosition(Vector4 pos) zeroCoordinateAdjustedManipulatorPosition.w ) ); + } // Log and continue echoing LogAndContinue(); From 837ffce9598dff6542c0fefa884610bd4b3bcc27 Mon Sep 17 00:00:00 2001 From: Kenneth Yang Date: Wed, 31 Jul 2024 13:27:55 -0700 Subject: [PATCH 5/5] Cleanup --- .../Probes/ManipulatorBehaviorController.cs | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/Assets/Scripts/Pinpoint/Probes/ManipulatorBehaviorController.cs b/Assets/Scripts/Pinpoint/Probes/ManipulatorBehaviorController.cs index d9b3efa7..694351a7 100644 --- a/Assets/Scripts/Pinpoint/Probes/ManipulatorBehaviorController.cs +++ b/Assets/Scripts/Pinpoint/Probes/ManipulatorBehaviorController.cs @@ -409,15 +409,9 @@ private void EchoPosition(Vector4 pos) zeroCoordinateAdjustedManipulatorPosition.w += brainSurfaceAdjustment; } else + { manipulatorSpacePosition.y -= brainSurfaceAdjustment; - - print( - zeroCoordinateAdjustedManipulatorPosition - + "\t" - + manipulatorSpacePosition - + "\t" - + brainSurfaceAdjustment - ); + } // Convert to world space. var zeroCoordinateAdjustedWorldPosition = CoordinateSpace.Space2World( @@ -434,7 +428,6 @@ private void EchoPosition(Vector4 pos) if (CoordinateTransform.Prefix == "3lhm") { if (IsSetToDropToSurfaceWithDepth) - { _probeController.SetProbePosition( new Vector4( transformedApmldv.x, @@ -443,11 +436,8 @@ private void EchoPosition(Vector4 pos) brainSurfaceAdjustment ) ); - } else - { _probeController.SetProbePosition(transformedApmldv); - } } else { @@ -475,7 +465,6 @@ void LogAndContinue() || Mathf.Abs(positionDifference.z) > 0.0001 || Mathf.Abs(positionDifference.w) > 0.0001 ) - { // Log every 4 hz if (Time.time - _lastLoggedTime >= 0.25) { @@ -511,7 +500,6 @@ void LogAndContinue() // Update last logged position _lastLoggedManipulatorPosition = pos; } - } // Continue echoing position CommunicationManager.Instance.GetPosition(ManipulatorID, EchoPosition);