Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoooi0 committed Feb 1, 2021
2 parents 0ffa72f + 51de0ae commit 7f0c69b
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 98 deletions.
1 change: 0 additions & 1 deletion ADD_ME.cslist
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,3 @@ src\UI\UIGroup.cs
src\Utils\ComponentCache.cs
src\Utils\Extensions.cs
src\Utils\MathUtils.cs
src\Utils\PInvoke.cs
6 changes: 2 additions & 4 deletions ToySerialController.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@
<Compile Include="lib\vam-debug-utils\LineDrawer.cs" />
<Compile Include="src\Config\ConfigManager.cs" />
<Compile Include="src\Config\IConfigProvider.cs" />
<Compile Include="src\Device\AbstractGenericDevice.cs" />
<Compile Include="src\Device\AbstractGenericDevice.UI.cs" />
<Compile Include="src\Device\TCodeDevice.cs" />
<Compile Include="src\Device\TCodeDevice.UI.cs" />
<Compile Include="src\Device\IDevice.cs" />
<Compile Include="src\MotionSource\AbstractFemaleMotionSource.cs" />
<Compile Include="src\MotionSource\AbstractRefreshableMotionSource.cs" />
Expand All @@ -118,15 +118,13 @@
<Compile Include="src\UI\Control\UIHorizontalGroup.cs" />
<Compile Include="src\UI\IUIBuilder.cs" />
<Compile Include="src\UI\UIGroup.cs" />
<Compile Include="src\Device\TCodeDevice.cs" />
<Compile Include="src\Plugin.cs" />
<Compile Include="src\Plugin.UI.cs" />
<Compile Include="src\UI\IUIProvider.cs" />
<Compile Include="src\UI\UIBuilder.cs" />
<Compile Include="src\Utils\ComponentCache.cs" />
<Compile Include="src\Utils\Extensions.cs" />
<Compile Include="src\Utils\MathUtils.cs" />
<Compile Include="src\Utils\PInvoke.cs" />
</ItemGroup>
<ItemGroup>
<None Include="lib\vam-curve-editor\LICENSE" />
Expand Down
3 changes: 1 addition & 2 deletions meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@
"Custom\\Scripts\\Yoooi\\ToySerialController\\lib\\vam-debug-utils\\LineDrawer.cs",
"Custom\\Scripts\\Yoooi\\ToySerialController\\src\\Config\\ConfigManager.cs",
"Custom\\Scripts\\Yoooi\\ToySerialController\\src\\Config\\IConfigProvider.cs",
"Custom\\Scripts\\Yoooi\\ToySerialController\\src\\Device\\AbstractGenericDevice.cs",
"Custom\\Scripts\\Yoooi\\ToySerialController\\src\\Device\\AbstractGenericDevice.UI.cs",
"Custom\\Scripts\\Yoooi\\ToySerialController\\src\\Device\\IDevice.cs",
"Custom\\Scripts\\Yoooi\\ToySerialController\\src\\Device\\TCodeDevice.cs",
"Custom\\Scripts\\Yoooi\\ToySerialController\\src\\Device\\TCodeDevice.UI.cs",
"Custom\\Scripts\\Yoooi\\ToySerialController\\src\\MotionSource\\AbstractFemaleMotionSource.cs",
"Custom\\Scripts\\Yoooi\\ToySerialController\\src\\MotionSource\\AbstractRefreshableMotionSource.cs",
"Custom\\Scripts\\Yoooi\\ToySerialController\\src\\MotionSource\\AnimationMotionSource.cs",
Expand Down
15 changes: 11 additions & 4 deletions src/Config/ConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,25 @@ public static void SaveConfig(string path, IConfigProvider provider)
if (!CheckPath(ref path))
return;

var directory = FileManagerSecure.GetDirectoryName(path);
if (!FileManagerSecure.DirectoryExists(directory))
FileManagerSecure.CreateDirectory(directory);

var config = new JSONClass();
provider.StoreConfig(config);
Controller.SaveJSON(config, path);
Controller.SaveJSON(config, path, () => SuperController.LogMessage($"Saved config! \"{path}\""), null, null);

SuperController.LogMessage($"Saved config! \"{path}\"");
}

public static void LoadConfig(string path, IConfigProvider provider)
{
if (!CheckPath(ref path))
return;

var directory = FileManagerSecure.GetDirectoryName(path);
if (!FileManagerSecure.DirectoryExists(directory))
return;

var config = Controller.LoadJSON(path);
provider.RestoreConfig(config);

Expand All @@ -51,8 +58,8 @@ public static void LoadConfig(string path, IConfigProvider provider)

private static void OpenDialog(string defaultPath, string filter, bool textEntry, uFileBrowser.FileBrowserCallback callback)
{
if(!FileManagerSecure.DirectoryExists(defaultPath))
PInvoke.MakeSureDirectoryPathExists(defaultPath);
if (!FileManagerSecure.DirectoryExists(defaultPath))
FileManagerSecure.CreateDirectory(defaultPath);

Controller.mediaFileBrowserUI.fileRemovePrefix = null;
Controller.mediaFileBrowserUI.hideExtension = false;
Expand Down
5 changes: 2 additions & 3 deletions src/Device/TCodeDevice.UI.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using CurveEditor;
using CurveEditor.UI;
using SimpleJSON;
using System;
using System.Collections.Generic;
using ToySerialController.Config;
using ToySerialController.UI;
Expand Down Expand Up @@ -203,7 +202,7 @@ private void CreateR1AxisUI(IUIBuilder builder)
{
var group = new UIGroup(builder);
var visible = false;
R1AxisTitle = builder.CreateButton("Pitch | R1", () => group.SetVisible(visible = !visible), Color.magenta * 0.8f, Color.white, true);
R1AxisTitle = builder.CreateButton("Roll | R1", () => group.SetVisible(visible = !visible), Color.magenta * 0.8f, Color.white, true);
RangeMaxR1Slider = group.CreateSlider("Device:RangeMaxR1", "Range Max (+/- \u00b0)", 30, 1, 89, true, true, true, "F0");
OutputMaxR1Slider = group.CreateSlider("Device:OutputMaxR1", "Output Max (+/- %)", 0.5f, 0f, 0.5f, true, true, true, "P0");
OffsetR1Slider = group.CreateSlider("Device:OffsetR1", "Offset (%)", 0f, -0.25f, 0.25f, true, true, true, "P0");
Expand All @@ -218,7 +217,7 @@ private void CreateR2AxisUI(IUIBuilder builder)
{
var group = new UIGroup(builder);
var visible = false;
R2AxisTitle = builder.CreateButton("Roll | R2", () => group.SetVisible(visible = !visible), Color.yellow * 0.8f, Color.white, true);
R2AxisTitle = builder.CreateButton("Pitch | R2", () => group.SetVisible(visible = !visible), Color.yellow * 0.8f, Color.white, true);
RangeMaxR2Slider = group.CreateSlider("Device:RangeMaxR2", "Range Max (+/- \u00b0)", 30, 1, 89, true, true, true, "F0");
OutputMaxR2Slider = group.CreateSlider("Device:OutputMaxR2", "Output Max (+/- %)", 0.5f, 0.01f, 0.5f, true, true, true, "P0");
OffsetR2Slider = group.CreateSlider("Device:OffsetR2", "Offset (%)", 0f, -0.25f, 0.25f, true, true, true, "P0");
Expand Down
114 changes: 58 additions & 56 deletions src/Device/TCodeDevice.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using DebugUtils;
using System;
using System.IO.Ports;
using System.Text;
using ToySerialController.MotionSource;
Expand All @@ -12,9 +11,9 @@ public partial class TCodeDevice : IDevice
protected readonly float[] XTarget, RTarget, ETarget;
protected readonly float[] XCmd, RCmd, ECmd;

private float? _lastCollisionTime;
private bool _lastCollisionSmoothingEnabled;
private float _lastCollisionSmoothingStartTime, _lastCollisionSmoothingDuration;
private float? _lastNoCollisionTime;
private bool _lastNoCollisionSmoothingEnabled;
private float _lastNoCollisionSmoothingStartTime, _lastNoCollisionSmoothingDuration;

protected string DeviceReport { get; set; }
public string GetDeviceReport() => DeviceReport;
Expand All @@ -28,6 +27,8 @@ public TCodeDevice()
XCmd = new float[] { 0.5f, 0.5f, 0.5f };
RCmd = new float[] { 0.5f, 0.5f, 0.5f };
ECmd = new float[9];

_lastNoCollisionTime = Time.time;
}

public void Write(SerialPort serial)
Expand All @@ -51,14 +52,14 @@ public void Write(SerialPort serial)

var sb = new StringBuilder();
sb.Append(" Target Cmd Serial\n");
sb.Append("L0\t").AppendFormat("{0,5:0.00}", XTarget[0]).Append(",\t").AppendFormat("{0,5:0.00}", XCmd[0]).Append(",\t").Append(l0).AppendLine();
sb.Append("L1\t").AppendFormat("{0,5:0.00}", XTarget[1]).Append(",\t").AppendFormat("{0,5:0.00}", XCmd[1]).Append(",\t").Append(l1).AppendLine();
sb.Append("L2\t").AppendFormat("{0,5:0.00}", XTarget[2]).Append(",\t").AppendFormat("{0,5:0.00}", XCmd[2]).Append(",\t").Append(l2).AppendLine();
sb.Append("R0\t").AppendFormat("{0,5:0.00}", RTarget[0]).Append(",\t").AppendFormat("{0,5:0.00}", RCmd[0]).Append(",\t").Append(r0).AppendLine();
sb.Append("R1\t").AppendFormat("{0,5:0.00}", RTarget[1]).Append(",\t").AppendFormat("{0,5:0.00}", RCmd[1]).Append(",\t").Append(r1).AppendLine();
sb.Append("R2\t").AppendFormat("{0,5:0.00}", RTarget[2]).Append(",\t").AppendFormat("{0,5:0.00}", RCmd[2]).Append(",\t").Append(r2).AppendLine();
sb.Append("V0\t").AppendFormat("{0,5:0.00}", ETarget[0]).Append(",\t").AppendFormat("{0,5:0.00}", ECmd[0]).Append(",\t").Append(v0).AppendLine();
sb.Append("V1\t").AppendFormat("{0,5:0.00}", ETarget[1]).Append(",\t").AppendFormat("{0,5:0.00}", ECmd[1]).Append(",\t").Append(v1).AppendLine();
sb.Append("L0\t").AppendFormat("{0,5:0.00}", XTarget[0]).Append(",\t").AppendFormat("{0,5:0.00}", XCmd[0]).Append(",\t").AppendLine(l0);
sb.Append("L1\t").AppendFormat("{0,5:0.00}", XTarget[1]).Append(",\t").AppendFormat("{0,5:0.00}", XCmd[1]).Append(",\t").AppendLine(l1);
sb.Append("L2\t").AppendFormat("{0,5:0.00}", XTarget[2]).Append(",\t").AppendFormat("{0,5:0.00}", XCmd[2]).Append(",\t").AppendLine(l2);
sb.Append("R0\t").AppendFormat("{0,5:0.00}", RTarget[0]).Append(",\t").AppendFormat("{0,5:0.00}", RCmd[0]).Append(",\t").AppendLine(r0);
sb.Append("R1\t").AppendFormat("{0,5:0.00}", RTarget[1]).Append(",\t").AppendFormat("{0,5:0.00}", RCmd[1]).Append(",\t").AppendLine(r1);
sb.Append("R2\t").AppendFormat("{0,5:0.00}", RTarget[2]).Append(",\t").AppendFormat("{0,5:0.00}", RCmd[2]).Append(",\t").AppendLine(r2);
sb.Append("V0\t").AppendFormat("{0,5:0.00}", ETarget[0]).Append(",\t").AppendFormat("{0,5:0.00}", ECmd[0]).Append(",\t").AppendLine(v0);
sb.Append("V1\t").AppendFormat("{0,5:0.00}", ETarget[1]).Append(",\t").AppendFormat("{0,5:0.00}", ECmd[1]).Append(",\t").AppendLine(v1);
sb.Append("L3\t").AppendFormat("{0,5:0.00}", ETarget[2]).Append(",\t").AppendFormat("{0,5:0.00}", ECmd[2]).Append(",\t").Append(l3);
DeviceReport = sb.ToString();
}
Expand All @@ -85,47 +86,57 @@ public bool Update(IMotionSource motionSource)
for (var i = 0; i < 5; i++)
DebugDraw.DrawCircle(Vector3.Lerp(motionSource.ReferencePosition, referenceEnding, i / 4.0f), motionSource.ReferenceUp, Color.grey, radius);

if (diffPosition.magnitude > 0.00001f)
{
var t = Mathf.Clamp(Vector3.Dot(motionSource.TargetPosition - motionSource.ReferencePosition, motionSource.ReferenceUp), 0f, length);
var closestPoint = motionSource.ReferencePosition + motionSource.ReferenceUp * t;
var t = Mathf.Clamp(Vector3.Dot(motionSource.TargetPosition - motionSource.ReferencePosition, motionSource.ReferenceUp), 0f, length);
var closestPoint = motionSource.ReferencePosition + motionSource.ReferenceUp * t;

if (Vector3.Magnitude(closestPoint - motionSource.TargetPosition) > radius)
if (Vector3.Magnitude(closestPoint - motionSource.TargetPosition) <= radius)
{
if (diffPosition.magnitude > 0.0001f)
{
if (_lastCollisionTime == null)
_lastCollisionTime = Time.time;
return false;
XTarget[0] = 1 - Mathf.Clamp01((closestPoint - motionSource.ReferencePosition).magnitude / length);
if (aboveTarget)
XTarget[0] = XTarget[0] > 0 ? 1 : 0;

var diffOnPlane = Vector3.ProjectOnPlane(diffPosition, motionSource.ReferencePlaneNormal);
var yOffset = Vector3.Project(diffOnPlane, motionSource.ReferenceRight);
var zOffset = Vector3.Project(diffOnPlane, motionSource.ReferenceForward);
XTarget[1] = yOffset.magnitude * Mathf.Sign(Vector3.Dot(yOffset, motionSource.ReferenceRight));
XTarget[2] = zOffset.magnitude * Mathf.Sign(Vector3.Dot(zOffset, motionSource.ReferenceForward));
}
else
{
XTarget[0] = 1;
XTarget[1] = 0;
XTarget[2] = 0;
}

var correctedRight = Vector3.ProjectOnPlane(motionSource.TargetRight, motionSource.ReferenceUp);
if (Vector3.Dot(correctedRight, motionSource.ReferenceRight) < 0)
correctedRight -= 2 * Vector3.Project(correctedRight, motionSource.ReferenceRight);

XTarget[0] = 1 - Mathf.Clamp01((closestPoint - motionSource.ReferencePosition).magnitude / length);
if (aboveTarget)
XTarget[0] = XTarget[0] > 0 ? 1 : 0;
RTarget[0] = Vector3.SignedAngle(motionSource.ReferenceRight, correctedRight, motionSource.ReferenceUp) / 180;
RTarget[1] = -Vector3.SignedAngle(motionSource.ReferenceUp, Vector3.ProjectOnPlane(motionSource.TargetUp, motionSource.ReferenceForward), motionSource.ReferenceForward) / 90;
RTarget[2] = Vector3.SignedAngle(motionSource.ReferenceUp, Vector3.ProjectOnPlane(motionSource.TargetUp, motionSource.ReferenceRight), motionSource.ReferenceRight) / 90;

var diffOnPlane = Vector3.ProjectOnPlane(diffPosition, motionSource.ReferencePlaneNormal);
var yOffset = Vector3.Project(diffOnPlane, motionSource.ReferenceRight);
var zOffset = Vector3.Project(diffOnPlane, motionSource.ReferenceForward);
XTarget[1] = yOffset.magnitude * Mathf.Sign(Vector3.Dot(yOffset, motionSource.ReferenceRight));
XTarget[2] = zOffset.magnitude * Mathf.Sign(Vector3.Dot(zOffset, motionSource.ReferenceForward));
ETarget[0] = OutputV0CurveEditorSettings.Evaluate(XTarget, RTarget);
ETarget[1] = OutputV1CurveEditorSettings.Evaluate(XTarget, RTarget);
ETarget[2] = OutputL3CurveEditorSettings.Evaluate(XTarget, RTarget);

if (_lastNoCollisionTime != null)
{
_lastNoCollisionSmoothingEnabled = true;
_lastNoCollisionSmoothingStartTime = Time.time;
_lastNoCollisionSmoothingDuration = Mathf.Clamp(Time.time - _lastNoCollisionTime.Value, 0.5f, 2);
_lastNoCollisionTime = null;
}
}
else
{
SuperController.singleton.Message("diff");
XTarget[0] = 1;
XTarget[1] = 0;
XTarget[2] = 0;
}

var correctedRight = Vector3.ProjectOnPlane(motionSource.TargetRight, motionSource.ReferenceUp);
if (Vector3.Dot(correctedRight, motionSource.ReferenceRight) < 0)
correctedRight -= 2 * Vector3.Project(correctedRight, motionSource.ReferenceRight);

RTarget[0] = Vector3.SignedAngle(motionSource.ReferenceRight, correctedRight, motionSource.ReferenceUp) / 180;
RTarget[1] = Vector3.SignedAngle(motionSource.ReferenceUp, Vector3.ProjectOnPlane(motionSource.TargetUp, motionSource.ReferenceForward), motionSource.ReferenceForward) / 90;
RTarget[2] = Vector3.SignedAngle(motionSource.ReferenceUp, Vector3.ProjectOnPlane(motionSource.TargetUp, motionSource.ReferenceRight), motionSource.ReferenceRight) / 90;
if (_lastNoCollisionTime == null)
_lastNoCollisionTime = Time.time;

ETarget[0] = OutputV0CurveEditorSettings.Evaluate(XTarget, RTarget);
ETarget[1] = OutputV1CurveEditorSettings.Evaluate(XTarget, RTarget);
ETarget[2] = OutputL3CurveEditorSettings.Evaluate(XTarget, RTarget);
return false;
}

var l0t = (XTarget[0] - RangeMinL0Slider.val) / (RangeMaxL0Slider.val - RangeMinL0Slider.val);
var l1t = (XTarget[1] + RangeMaxL1Slider.val) / (2 * RangeMaxL1Slider.val);
Expand Down Expand Up @@ -184,21 +195,12 @@ public bool Update(IMotionSource motionSource)
if (EnableOverrideV1Toggle.val) v1CmdRaw = OverrideV1Slider.val;
if (EnableOverrideL3Toggle.val) l3CmdRaw = OverrideL3Slider.val;

if (_lastCollisionTime != null)
{
var noCollisionDuration = Time.time - _lastCollisionTime.Value;
_lastCollisionSmoothingDuration = Mathf.Clamp(noCollisionDuration, 0.5f, 2);
_lastCollisionSmoothingStartTime = Time.time;
_lastCollisionSmoothingEnabled = true;
_lastCollisionTime = null;
}

if (_lastCollisionSmoothingEnabled)
if (_lastNoCollisionSmoothingEnabled)
{
var lastCollisionSmoothingT = Mathf.Pow(2, 10 * ((Time.time - _lastCollisionSmoothingStartTime) / _lastCollisionSmoothingDuration - 1));
var lastCollisionSmoothingT = Mathf.Pow(2, 10 * ((Time.time - _lastNoCollisionSmoothingStartTime) / _lastNoCollisionSmoothingDuration - 1));
if (lastCollisionSmoothingT >= 1.0f)
{
_lastCollisionSmoothingEnabled = false;
_lastNoCollisionSmoothingEnabled = false;
}
else
{
Expand Down
1 change: 0 additions & 1 deletion src/MotionSource/AssetFemaleMotionSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using ToySerialController.Utils;
using UnityEngine;
using DebugUtils;
using Leap.Unity;

namespace ToySerialController.MotionSource
{
Expand Down
1 change: 0 additions & 1 deletion src/MotionSource/DildoFemaleMotionSource.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using SimpleJSON;
using System.Collections.Generic;
using System.Linq;
using ToySerialController.UI;
using ToySerialController.Utils;
Expand Down
1 change: 0 additions & 1 deletion src/MotionSource/RangeTestMotionSource.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using DebugUtils;
using SimpleJSON;
using System;
using System.Collections.Generic;
using ToySerialController.UI;
using UnityEngine;
Expand Down
3 changes: 0 additions & 3 deletions src/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
using ToySerialController.MotionSource;
using DebugUtils;
using ToySerialController.Utils;
using MVR.FileManagementSecure;
using Microsoft.Win32.SafeHandles;
using UnityEngine;

namespace ToySerialController
{
Expand Down
22 changes: 0 additions & 22 deletions src/Utils/PInvoke.cs

This file was deleted.

0 comments on commit 7f0c69b

Please sign in to comment.