Skip to content

Commit faa0bce

Browse files
authored
705 reduce ephys link log and log copilot actions (#708)
* Only log Ephys Link where there is movement * Log to file * Log reset zero coordinate * Add copilot logs * Upgrade Ephys Link and add stop logs
1 parent 910c8bd commit faa0bce

File tree

8 files changed

+289
-42
lines changed

8 files changed

+289
-42
lines changed

Assets/Scripts/Log/OutputLog.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ public class OutputLog : MonoBehaviour
2525
private const int MAX_LOG_LINES = 60;
2626
private bool _warned;
2727

28+
#if !UNITY_WEBGL
29+
private string logPath =
30+
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/Pinpoint";
31+
private DateTime logStartTime = DateTime.Now;
32+
#endif
33+
2834
private void Awake()
2935
{
3036
if (Instance != null)
@@ -35,6 +41,10 @@ private void Awake()
3541

3642
#if UNITY_EDITOR
3743
_lastLogTime = float.MinValue;
44+
#endif
45+
#if !UNITY_WEBGL
46+
// Create log directory if it doesn't exist.
47+
System.IO.Directory.CreateDirectory(logPath);
3848
#endif
3949
}
4050

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

56-
Instance._log.Add(string.Join(',', data));
67+
Instance._log.Add(logLine);
5768
Instance.UpdateLogText();
69+
70+
#if !UNITY_WEBGL
71+
// Log to file.
72+
var fileName =
73+
Instance.logPath
74+
+ "/log_"
75+
+ Instance.logStartTime.ToString("yyyy-MM-dd_HH-mm-ss")
76+
+ ".csv";
77+
System.IO.File.AppendAllText(fileName, logLine + "\n");
78+
#endif
5879
}
5980

6081
public void UpdateLogText()

Assets/Scripts/Pinpoint/Probes/ManipulatorBehaviorController.cs

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public bool IsRightHanded
9999
#region Private internal fields
100100

101101
private Vector4 _lastManipulatorPosition = Vector4.zero;
102+
private Vector4 _lastLoggedManipulatorPosition = Vector4.zero;
102103
private Vector4 _zeroCoordinateOffset = Vector4.zero;
103104
private float _brainSurfaceOffset;
104105
private bool _isSetToDropToSurfaceWithDepth = true;
@@ -542,30 +543,50 @@ private void EchoPosition(Vector4 pos)
542543

543544
void LogAndContinue()
544545
{
545-
// Log every 5 hz
546-
if (Time.time - _lastLoggedTime >= 0.2)
546+
// Don't log if the last position is the same.
547+
var positionDifference = _lastLoggedManipulatorPosition - pos;
548+
if (
549+
Mathf.Abs(positionDifference.x) > 0.0001
550+
|| Mathf.Abs(positionDifference.y) > 0.0001
551+
|| Mathf.Abs(positionDifference.z) > 0.0001
552+
|| Mathf.Abs(positionDifference.w) > 0.0001
553+
)
547554
{
548-
_lastLoggedTime = Time.time;
549-
var tipPos = _probeController.ProbeTipT.position;
550-
551-
// ["ephys_link", Real time stamp, Manipulator ID, X, Y, Z, W, Phi, Theta, Spin, TipX, TipY, TipZ]
552-
string[] data =
555+
// Log every 4 hz
556+
if (Time.time - _lastLoggedTime >= 0.25)
553557
{
554-
"ephys_link",
555-
Time.realtimeSinceStartup.ToString(CultureInfo.InvariantCulture),
556-
ManipulatorID,
557-
pos.x.ToString(CultureInfo.InvariantCulture),
558-
pos.y.ToString(CultureInfo.InvariantCulture),
559-
pos.z.ToString(CultureInfo.InvariantCulture),
560-
pos.w.ToString(CultureInfo.InvariantCulture),
561-
_probeController.Insertion.Yaw.ToString(CultureInfo.InvariantCulture),
562-
_probeController.Insertion.Pitch.ToString(CultureInfo.InvariantCulture),
563-
_probeController.Insertion.Roll.ToString(CultureInfo.InvariantCulture),
564-
tipPos.x.ToString(CultureInfo.InvariantCulture),
565-
tipPos.y.ToString(CultureInfo.InvariantCulture),
566-
tipPos.z.ToString(CultureInfo.InvariantCulture)
567-
};
568-
OutputLog.Log(data);
558+
_lastLoggedTime = Time.time;
559+
var tipPos = _probeController.ProbeTipT.position;
560+
561+
// ["ephys_link", Real time stamp, Manipulator ID, X, Y, Z, W, Phi, Theta, Spin, TipX, TipY, TipZ]
562+
OutputLog.Log(
563+
new[]
564+
{
565+
"ephys_link",
566+
DateTime.Now.ToString(CultureInfo.InvariantCulture),
567+
ManipulatorID,
568+
pos.x.ToString(CultureInfo.InvariantCulture),
569+
pos.y.ToString(CultureInfo.InvariantCulture),
570+
pos.z.ToString(CultureInfo.InvariantCulture),
571+
pos.w.ToString(CultureInfo.InvariantCulture),
572+
_probeController.Insertion.Yaw.ToString(
573+
CultureInfo.InvariantCulture
574+
),
575+
_probeController.Insertion.Pitch.ToString(
576+
CultureInfo.InvariantCulture
577+
),
578+
_probeController.Insertion.Roll.ToString(
579+
CultureInfo.InvariantCulture
580+
),
581+
tipPos.x.ToString(CultureInfo.InvariantCulture),
582+
tipPos.y.ToString(CultureInfo.InvariantCulture),
583+
tipPos.z.ToString(CultureInfo.InvariantCulture)
584+
}
585+
);
586+
587+
// Update last logged position
588+
_lastLoggedManipulatorPosition = pos;
589+
}
569590
}
570591

571592
// Continue echoing position

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

Lines changed: 123 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
using System;
12
using System.Collections.Generic;
3+
using System.Globalization;
24
using BrainAtlas;
35
using EphysLink;
46
using TMPro;
@@ -466,6 +468,18 @@ public void Drive()
466468
switch (_driveStateManager.State)
467469
{
468470
case DriveState.DrivingToNearTarget:
471+
// Log start of drive.
472+
OutputLog.Log(
473+
new[]
474+
{
475+
"Copilot",
476+
DateTime.Now.ToString(CultureInfo.InvariantCulture),
477+
"Drive",
478+
_manipulatorId,
479+
"Start Driving to Near Target @ " + _driveBaseSpeed,
480+
}
481+
);
482+
469483
// Update status text
470484
_statusText.text =
471485
"Driving to "
@@ -492,6 +506,18 @@ public void Drive()
492506
CompleteAndAdvance();
493507
break;
494508
case DriveState.DrivingToPastTarget:
509+
// Log driving to past target.
510+
OutputLog.Log(
511+
new[]
512+
{
513+
"Copilot",
514+
DateTime.Now.ToString(CultureInfo.InvariantCulture),
515+
"Drive",
516+
_manipulatorId,
517+
"Start Driving to Past Target: " + _pastTargetDepth,
518+
}
519+
);
520+
495521
// Update status text
496522
_statusText.text =
497523
"Driving to "
@@ -518,6 +544,18 @@ public void Drive()
518544
CompleteAndAdvance();
519545
break;
520546
case DriveState.ReturningToTarget:
547+
// Log returning to target.
548+
OutputLog.Log(
549+
new[]
550+
{
551+
"Copilot",
552+
DateTime.Now.ToString(CultureInfo.InvariantCulture),
553+
"Drive",
554+
_manipulatorId,
555+
"Start Returning to Target: " + _targetDepth,
556+
}
557+
);
558+
521559
// Update status text
522560
_statusText.text = "Returning to target...";
523561

@@ -532,7 +570,7 @@ public void Drive()
532570
_targetDepth,
533571
_nearTargetDriveSpeed
534572
),
535-
_ =>
573+
finalDepth =>
536574
{
537575
_driveStateManager.CompleteMovement();
538576

@@ -544,6 +582,18 @@ public void Drive()
544582
_driveGroup.SetActive(true);
545583
_driveButton.interactable = false;
546584
_exitButton.SetActive(true);
585+
586+
// Log end of drive.
587+
OutputLog.Log(
588+
new[]
589+
{
590+
"Copilot",
591+
DateTime.Now.ToString(CultureInfo.InvariantCulture),
592+
"Drive",
593+
_manipulatorId,
594+
"End Drive: " + finalDepth,
595+
}
596+
);
547597
},
548598
Debug.LogError
549599
);
@@ -590,6 +640,18 @@ public void Exit()
590640
switch (_driveStateManager.State)
591641
{
592642
case DriveState.ExitingToNearTarget:
643+
// Log start of exit.
644+
OutputLog.Log(
645+
new[]
646+
{
647+
"Copilot",
648+
DateTime.Now.ToString(CultureInfo.InvariantCulture),
649+
"Drive",
650+
_manipulatorId,
651+
"Start Exiting to Near Target: " + _nearTargetDepth,
652+
}
653+
);
654+
593655
// Update status text
594656
_statusText.text = "Returning to surface...";
595657

@@ -613,6 +675,18 @@ public void Exit()
613675
CompleteAndAdvance();
614676
break;
615677
case DriveState.ExitingToDura:
678+
// Log exiting to dura.
679+
OutputLog.Log(
680+
new[]
681+
{
682+
"Copilot",
683+
DateTime.Now.ToString(CultureInfo.InvariantCulture),
684+
"Drive",
685+
_manipulatorId,
686+
"Start Exiting to Dura: " + _duraDepth,
687+
}
688+
);
689+
616690
// Update status text
617691
_statusText.text = "Returning to surface...";
618692

@@ -638,6 +712,18 @@ public void Exit()
638712
CompleteAndAdvance();
639713
break;
640714
case DriveState.ExitingToMargin:
715+
// Log exiting to margin.
716+
OutputLog.Log(
717+
new[]
718+
{
719+
"Copilot",
720+
DateTime.Now.ToString(CultureInfo.InvariantCulture),
721+
"Drive",
722+
_manipulatorId,
723+
"Start Exiting to Margin: " + _exitMarginDepth,
724+
}
725+
);
726+
641727
// Update status text
642728
_statusText.text = "Exiting Dura...";
643729

@@ -663,6 +749,18 @@ public void Exit()
663749
CompleteAndAdvance();
664750
break;
665751
case DriveState.ExitingToOutside:
752+
// Log exiting to outside.
753+
OutputLog.Log(
754+
new[]
755+
{
756+
"Copilot",
757+
DateTime.Now.ToString(CultureInfo.InvariantCulture),
758+
"Drive",
759+
_manipulatorId,
760+
"Start Exiting to Outside",
761+
}
762+
);
763+
666764
// Update status text
667765
_statusText.text = "Exiting Dura...";
668766

@@ -734,6 +832,18 @@ void CompleteOutside()
734832
_stopButton.SetActive(false);
735833
_exitButton.SetActive(false);
736834
_driveGroup.SetActive(true);
835+
836+
// Log end of exit.
837+
OutputLog.Log(
838+
new[]
839+
{
840+
"Copilot",
841+
DateTime.Now.ToString(CultureInfo.InvariantCulture),
842+
"Drive",
843+
_manipulatorId,
844+
"End Exiting to Outside",
845+
}
846+
);
737847
}
738848
}
739849

@@ -746,6 +856,18 @@ public void Stop()
746856
_manipulatorId,
747857
() =>
748858
{
859+
// Log stop event.
860+
OutputLog.Log(
861+
new[]
862+
{
863+
"Copilot",
864+
DateTime.Now.ToString(CultureInfo.InvariantCulture),
865+
"Drive",
866+
_manipulatorId,
867+
"Stop",
868+
}
869+
);
870+
749871
// Show drive group and hide stop button.
750872
_statusText.text = "Stopped";
751873
_driveGroup.SetActive(true);

0 commit comments

Comments
 (0)