Skip to content

Commit

Permalink
refactored pos to euler for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
wormyrocks committed Oct 12, 2017
1 parent 97138a6 commit 53a372a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
8 changes: 4 additions & 4 deletions Assets/JoyconDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ public class JoyconDemo : MonoBehaviour {
public float[] stick;
public Vector3 gyro;
public Vector3 accel;
public Vector3 pos;
public Vector3 euler;

void Start ()
{
gyro = new Vector3(0, 0, 0);
accel = new Vector3(0, 0, 0);
pos = new Vector3(0, 0, 0);
euler = new Vector3(0, 0, 0);
// get the public Joycon object attached to the JoyconManager in scene
j = JoyconManager.Instance.j;
}
Expand Down Expand Up @@ -72,8 +72,8 @@ void Update () {

// GetVector function is currently experimental! If you want to use raw IMU data without
// having to trawl through my crappy broken sensor fusion, make sure EnableLocalize is false in JoyconManager.
pos = j.GetVector();
gameObject.transform.eulerAngles = pos;
euler = j.GetVector();
gameObject.transform.eulerAngles = euler;

}
}
Expand Down
28 changes: 14 additions & 14 deletions Assets/JoyconLib_scripts/Joycon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public enum Button : int
private float yaw;
private float gyr_z_prev;
private bool do_localize;
private Vector3 pos;
private Vector3 euler;
private float filterweight;

private const uint report_len = 49;
Expand Down Expand Up @@ -241,9 +241,9 @@ public Vector3 GetVector(int type = 0)
case 2:
return (new Vector3(gyr_est.x, gyr_est.z, -gyr_est.y)) * 90f;
case 3:
return (new Vector3(pos.x, pos.z, -pos.y)) * 90f;
return (new Vector3(euler.x, euler.z, -euler.y)) * 90f;
default:
return (new Vector3(pos.x * (isLeft ? -1f : 1f), yaw, -pos.y * (isLeft ? -1f : 1f))) * 90f;
return (new Vector3(euler.x * (isLeft ? -1f : 1f), yaw, -euler.y * (isLeft ? -1f : 1f))) * 90f;
}
}
public int Attach(byte leds_ = 0x0, bool imu = true, float alpha = 10f, bool localize = false)
Expand Down Expand Up @@ -517,22 +517,22 @@ private int ProcessIMU(byte[] report_buf)
acc_g = acc_g.normalized;
if (first_imu_packet)
{
pos = acc_g;
euler = acc_g;
yaw = 0;
first_imu_packet = false;
}
else
{
yaw += ((gyr_g.z * Mathf.Cos(pos.y * Mathf.PI / 2) - gyr_g.y * Mathf.Sin(pos.y * Mathf.PI / 2)) * 0.005f * dt);
if (Mathf.Abs(pos.x) < 0.1f)
yaw += ((gyr_g.z * Mathf.Cos(euler.y * Mathf.PI / 2) - gyr_g.y * Mathf.Sin(euler.y * Mathf.PI / 2)) * 0.005f * dt);
if (Mathf.Abs(euler.x) < 0.1f)
{
gyr_est = pos;
gyr_est = euler;
}
else
{
// Euler: Ayz, Axz. In radians
Ayz = Mathf.Atan2(pos.y, pos.z) + gyr_g.y * .005f * dt;
Axz = Mathf.Atan2(pos.x, pos.z) + gyr_g.x * .005f * dt;
Ayz = Mathf.Atan2(euler.y, euler.z) + gyr_g.y * .005f * dt;
Axz = Mathf.Atan2(euler.x, euler.z) + gyr_g.x * .005f * dt;

int sign = (Mathf.Cos(Ayz) >= 0) ? 1 : -1;
gyr_est.x = Mathf.Sin(Axz) / Mathf.Sqrt(1 + Mathf.Pow(Mathf.Tan(Ayz), 2) * Mathf.Pow(Mathf.Cos(Axz), 2));
Expand All @@ -541,9 +541,9 @@ private int ProcessIMU(byte[] report_buf)

gyr_est = gyr_est.normalized;
}
pos = (acc_g + gyr_est * filterweight) / (1 + filterweight);
euler = (acc_g + gyr_est * filterweight) / (1 + filterweight);
}
pos = pos.normalized;
euler = euler.normalized;

dt = 1;
}
Expand All @@ -560,9 +560,9 @@ public void Begin()
}
public void Recenter()
{
pos[0] = 0;
pos[1] = 0;
pos[2] = 0;
euler[0] = 0;
euler[1] = 0;
euler[2] = 0;
first_imu_packet = true;
}
private float[] CenterSticks(UInt16[] vals)
Expand Down
10 changes: 8 additions & 2 deletions Assets/Scenes/Scene1.unity
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 405bad29b2b998c408059182c189d9e9, type: 3}
m_Name:
m_EditorClassIdentifier:
stick: []
EnableIMU: 0
EnableLocalize: 1
LEDs: 255
--- !u!4 &175246628
Transform:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -176,9 +178,13 @@ MonoBehaviour:
m_GameObject: {fileID: 798593168}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 475ab625ec280bf409835dd3929597de, type: 3}
m_Script: {fileID: 11500000, guid: b9b5c154d920d754db92ed07a735dbc4, type: 3}
m_Name:
m_EditorClassIdentifier:
stick: []
gyro: {x: 0, y: 0, z: 0}
accel: {x: 0, y: 0, z: 0}
pos: {x: 0, y: 0, z: 0}
--- !u!23 &798593170
MeshRenderer:
m_ObjectHideFlags: 0
Expand Down

0 comments on commit 53a372a

Please sign in to comment.