Skip to content
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
23 changes: 22 additions & 1 deletion Assets/Scripts/Log/OutputLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public class OutputLog : MonoBehaviour
private const int MAX_LOG_LINES = 60;
private bool _warned;

#if !UNITY_WEBGL
private string logPath =
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/Pinpoint";
private DateTime logStartTime = DateTime.Now;
#endif

private void Awake()
{
if (Instance != null)
Expand All @@ -35,6 +41,10 @@ private void Awake()

#if UNITY_EDITOR
_lastLogTime = float.MinValue;
#endif
#if !UNITY_WEBGL
// Create log directory if it doesn't exist.
System.IO.Directory.CreateDirectory(logPath);
#endif
}

Expand All @@ -52,9 +62,20 @@ public static void Log(IEnumerable<string> data)
}
Instance._lastLogTime = Time.realtimeSinceStartup;
#endif
var logLine = string.Join(',', data);

Instance._log.Add(string.Join(',', data));
Instance._log.Add(logLine);
Instance.UpdateLogText();

#if !UNITY_WEBGL
// Log to file.
var fileName =
Instance.logPath
+ "/log_"
+ Instance.logStartTime.ToString("yyyy-MM-dd_HH-mm-ss")
+ ".csv";
System.IO.File.AppendAllText(fileName, logLine + "\n");
#endif
}

public void UpdateLogText()
Expand Down
65 changes: 43 additions & 22 deletions Assets/Scripts/Pinpoint/Probes/ManipulatorBehaviorController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public bool IsRightHanded
#region Private internal fields

private Vector4 _lastManipulatorPosition = Vector4.zero;
private Vector4 _lastLoggedManipulatorPosition = Vector4.zero;
private Vector4 _zeroCoordinateOffset = Vector4.zero;
private float _brainSurfaceOffset;
private bool _isSetToDropToSurfaceWithDepth = true;
Expand Down Expand Up @@ -542,30 +543,50 @@ private void EchoPosition(Vector4 pos)

void LogAndContinue()
{
// Log every 5 hz
if (Time.time - _lastLoggedTime >= 0.2)
// Don't log if the last position is the same.
var positionDifference = _lastLoggedManipulatorPosition - pos;
if (
Mathf.Abs(positionDifference.x) > 0.0001
|| Mathf.Abs(positionDifference.y) > 0.0001
|| Mathf.Abs(positionDifference.z) > 0.0001
|| Mathf.Abs(positionDifference.w) > 0.0001
)
{
_lastLoggedTime = Time.time;
var tipPos = _probeController.ProbeTipT.position;

// ["ephys_link", Real time stamp, Manipulator ID, X, Y, Z, W, Phi, Theta, Spin, TipX, TipY, TipZ]
string[] data =
// Log every 4 hz
if (Time.time - _lastLoggedTime >= 0.25)
{
"ephys_link",
Time.realtimeSinceStartup.ToString(CultureInfo.InvariantCulture),
ManipulatorID,
pos.x.ToString(CultureInfo.InvariantCulture),
pos.y.ToString(CultureInfo.InvariantCulture),
pos.z.ToString(CultureInfo.InvariantCulture),
pos.w.ToString(CultureInfo.InvariantCulture),
_probeController.Insertion.Yaw.ToString(CultureInfo.InvariantCulture),
_probeController.Insertion.Pitch.ToString(CultureInfo.InvariantCulture),
_probeController.Insertion.Roll.ToString(CultureInfo.InvariantCulture),
tipPos.x.ToString(CultureInfo.InvariantCulture),
tipPos.y.ToString(CultureInfo.InvariantCulture),
tipPos.z.ToString(CultureInfo.InvariantCulture)
};
OutputLog.Log(data);
_lastLoggedTime = Time.time;
var tipPos = _probeController.ProbeTipT.position;

// ["ephys_link", Real time stamp, Manipulator ID, X, Y, Z, W, Phi, Theta, Spin, TipX, TipY, TipZ]
OutputLog.Log(
new[]
{
"ephys_link",
DateTime.Now.ToString(CultureInfo.InvariantCulture),
ManipulatorID,
pos.x.ToString(CultureInfo.InvariantCulture),
pos.y.ToString(CultureInfo.InvariantCulture),
pos.z.ToString(CultureInfo.InvariantCulture),
pos.w.ToString(CultureInfo.InvariantCulture),
_probeController.Insertion.Yaw.ToString(
CultureInfo.InvariantCulture
),
_probeController.Insertion.Pitch.ToString(
CultureInfo.InvariantCulture
),
_probeController.Insertion.Roll.ToString(
CultureInfo.InvariantCulture
),
tipPos.x.ToString(CultureInfo.InvariantCulture),
tipPos.y.ToString(CultureInfo.InvariantCulture),
tipPos.z.ToString(CultureInfo.InvariantCulture)
}
);

// Update last logged position
_lastLoggedManipulatorPosition = pos;
}
}

// Continue echoing position
Expand Down
124 changes: 123 additions & 1 deletion Assets/Scripts/Pinpoint/UI/EphysCopilot/DrivePanelHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using BrainAtlas;
using EphysLink;
using TMPro;
Expand Down Expand Up @@ -466,6 +468,18 @@ public void Drive()
switch (_driveStateManager.State)
{
case DriveState.DrivingToNearTarget:
// Log start of drive.
OutputLog.Log(
new[]
{
"Copilot",
DateTime.Now.ToString(CultureInfo.InvariantCulture),
"Drive",
_manipulatorId,
"Start Driving to Near Target @ " + _driveBaseSpeed,
}
);

// Update status text
_statusText.text =
"Driving to "
Expand All @@ -492,6 +506,18 @@ public void Drive()
CompleteAndAdvance();
break;
case DriveState.DrivingToPastTarget:
// Log driving to past target.
OutputLog.Log(
new[]
{
"Copilot",
DateTime.Now.ToString(CultureInfo.InvariantCulture),
"Drive",
_manipulatorId,
"Start Driving to Past Target: " + _pastTargetDepth,
}
);

// Update status text
_statusText.text =
"Driving to "
Expand All @@ -518,6 +544,18 @@ public void Drive()
CompleteAndAdvance();
break;
case DriveState.ReturningToTarget:
// Log returning to target.
OutputLog.Log(
new[]
{
"Copilot",
DateTime.Now.ToString(CultureInfo.InvariantCulture),
"Drive",
_manipulatorId,
"Start Returning to Target: " + _targetDepth,
}
);

// Update status text
_statusText.text = "Returning to target...";

Expand All @@ -532,7 +570,7 @@ public void Drive()
_targetDepth,
_nearTargetDriveSpeed
),
_ =>
finalDepth =>
{
_driveStateManager.CompleteMovement();

Expand All @@ -544,6 +582,18 @@ public void Drive()
_driveGroup.SetActive(true);
_driveButton.interactable = false;
_exitButton.SetActive(true);

// Log end of drive.
OutputLog.Log(
new[]
{
"Copilot",
DateTime.Now.ToString(CultureInfo.InvariantCulture),
"Drive",
_manipulatorId,
"End Drive: " + finalDepth,
}
);
},
Debug.LogError
);
Expand Down Expand Up @@ -590,6 +640,18 @@ public void Exit()
switch (_driveStateManager.State)
{
case DriveState.ExitingToNearTarget:
// Log start of exit.
OutputLog.Log(
new[]
{
"Copilot",
DateTime.Now.ToString(CultureInfo.InvariantCulture),
"Drive",
_manipulatorId,
"Start Exiting to Near Target: " + _nearTargetDepth,
}
);

// Update status text
_statusText.text = "Returning to surface...";

Expand All @@ -613,6 +675,18 @@ public void Exit()
CompleteAndAdvance();
break;
case DriveState.ExitingToDura:
// Log exiting to dura.
OutputLog.Log(
new[]
{
"Copilot",
DateTime.Now.ToString(CultureInfo.InvariantCulture),
"Drive",
_manipulatorId,
"Start Exiting to Dura: " + _duraDepth,
}
);

// Update status text
_statusText.text = "Returning to surface...";

Expand All @@ -638,6 +712,18 @@ public void Exit()
CompleteAndAdvance();
break;
case DriveState.ExitingToMargin:
// Log exiting to margin.
OutputLog.Log(
new[]
{
"Copilot",
DateTime.Now.ToString(CultureInfo.InvariantCulture),
"Drive",
_manipulatorId,
"Start Exiting to Margin: " + _exitMarginDepth,
}
);

// Update status text
_statusText.text = "Exiting Dura...";

Expand All @@ -663,6 +749,18 @@ public void Exit()
CompleteAndAdvance();
break;
case DriveState.ExitingToOutside:
// Log exiting to outside.
OutputLog.Log(
new[]
{
"Copilot",
DateTime.Now.ToString(CultureInfo.InvariantCulture),
"Drive",
_manipulatorId,
"Start Exiting to Outside",
}
);

// Update status text
_statusText.text = "Exiting Dura...";

Expand Down Expand Up @@ -734,6 +832,18 @@ void CompleteOutside()
_stopButton.SetActive(false);
_exitButton.SetActive(false);
_driveGroup.SetActive(true);

// Log end of exit.
OutputLog.Log(
new[]
{
"Copilot",
DateTime.Now.ToString(CultureInfo.InvariantCulture),
"Drive",
_manipulatorId,
"End Exiting to Outside",
}
);
}
}

Expand All @@ -746,6 +856,18 @@ public void Stop()
_manipulatorId,
() =>
{
// Log stop event.
OutputLog.Log(
new[]
{
"Copilot",
DateTime.Now.ToString(CultureInfo.InvariantCulture),
"Drive",
_manipulatorId,
"Stop",
}
);

// Show drive group and hide stop button.
_statusText.text = "Stopped";
_driveGroup.SetActive(true);
Expand Down
Loading