From 8630ef075343f7e0760b107ae9f655fe252f4894 Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 14 Nov 2018 22:22:38 +0100 Subject: [PATCH] v6 rotational walking :D --- .../Hexapod/v15/Server/sqlite3/storage.ide | 4 +- .../v15/Server/sqlite3/storage.ide-shm | 2 +- .../v15/Server/sqlite3/storage.ide-wal | 2 +- 2018-unity-sim/Assets/Leg.cs | 12 +- 2018-unity-sim/Assets/Main.unity | 4 +- 2018-unity-sim/Assets/Serial.cs | 13 ++- 2018-unity-sim/Assets/Walk.cs | 4 +- 2018-unity-sim/Assets/Walk2.cs | 103 ++++++++++++++++++ 2018-unity-sim/Assets/Walk2.cs.meta | 13 +++ 9 files changed, 145 insertions(+), 12 deletions(-) create mode 100644 2018-unity-sim/Assets/Walk2.cs create mode 100644 2018-unity-sim/Assets/Walk2.cs.meta diff --git a/2018-unity-sim/.vs/Hexapod/v15/Server/sqlite3/storage.ide b/2018-unity-sim/.vs/Hexapod/v15/Server/sqlite3/storage.ide index d35ce34..ff76b55 100644 --- a/2018-unity-sim/.vs/Hexapod/v15/Server/sqlite3/storage.ide +++ b/2018-unity-sim/.vs/Hexapod/v15/Server/sqlite3/storage.ide @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:9202b8dd6cfeae2178ab386ec0e86aad5dfeca5ff21214cee23f3cd8ebd79869 -size 1024000 +oid sha256:1fb1ba69420bf701266a582fef590a853b372d4d22a1e31ba047caec54371a0b +size 1028096 diff --git a/2018-unity-sim/.vs/Hexapod/v15/Server/sqlite3/storage.ide-shm b/2018-unity-sim/.vs/Hexapod/v15/Server/sqlite3/storage.ide-shm index 018b39c..18e2c1a 100644 --- a/2018-unity-sim/.vs/Hexapod/v15/Server/sqlite3/storage.ide-shm +++ b/2018-unity-sim/.vs/Hexapod/v15/Server/sqlite3/storage.ide-shm @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:fba003802c813b0a87d96cacf1c558f976e2d17d121e27e33b9207cdb44efd72 +oid sha256:353baed719f408b743f68e68adde7c2a391e680fe9b6bba231924b5b5d144a10 size 32768 diff --git a/2018-unity-sim/.vs/Hexapod/v15/Server/sqlite3/storage.ide-wal b/2018-unity-sim/.vs/Hexapod/v15/Server/sqlite3/storage.ide-wal index 02cfc59..0e092b0 100644 --- a/2018-unity-sim/.vs/Hexapod/v15/Server/sqlite3/storage.ide-wal +++ b/2018-unity-sim/.vs/Hexapod/v15/Server/sqlite3/storage.ide-wal @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:edf75adc1c1ee4aac6176c016911cc99350128df50dc38cfbb8202d3a6d03727 +oid sha256:e7d85fc1cd7e52ae5c3c70261ea42d55729b28400d99b590cb0b578189c04ea3 size 4140632 diff --git a/2018-unity-sim/Assets/Leg.cs b/2018-unity-sim/Assets/Leg.cs index b38c499..e6b4d47 100644 --- a/2018-unity-sim/Assets/Leg.cs +++ b/2018-unity-sim/Assets/Leg.cs @@ -24,7 +24,7 @@ public class Leg : MonoBehaviour { public double translateZ = 0; public GameObject pointShouldBe; - public Vector3 pos = new Vector3(0, 0, 0); + //public Vector3 pos = new Vector3(0, 0, 0); public int startServo = -1; @@ -99,6 +99,16 @@ public class Leg : MonoBehaviour { pointShouldBe.transform.localPosition = foot.pos; } + public Vector3 getWorldPos() + { + return point + new Vector3((float)translateX, (float)translateY, (float)translateZ); + } + + public void setWorldPos(Vector3 pos) + { + this.point = pos - new Vector3((float)translateX, (float)translateY, (float)translateZ); + } + public static float Remap(float value, float from1, float to1, float from2, float to2) { return (value - from1) / (to1 - from1) * (to2 - from2) + from2; diff --git a/2018-unity-sim/Assets/Main.unity b/2018-unity-sim/Assets/Main.unity index 3ab7da8..b30442b 100644 --- a/2018-unity-sim/Assets/Main.unity +++ b/2018-unity-sim/Assets/Main.unity @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:75c899ee15cdc09c6d7f8705def4a49207aac4f45c6394ed9434aaa3d1d53138 -size 9968810 +oid sha256:747794c65fedc51eaf1915d291c3e149b26158aa02c9491f0c1dca1c3c7a2c90 +size 9969205 diff --git a/2018-unity-sim/Assets/Serial.cs b/2018-unity-sim/Assets/Serial.cs index 5eb6ece..d082e17 100644 --- a/2018-unity-sim/Assets/Serial.cs +++ b/2018-unity-sim/Assets/Serial.cs @@ -35,9 +35,16 @@ public class Serial { { serialPort = new SerialPort(@"\\.\" + "COM10", 115200); - serialPort.Open(); - serialPort.DiscardOutBuffer(); - serialPort.DiscardInBuffer(); + try + { + serialPort.Open(); + serialPort.DiscardOutBuffer(); + serialPort.DiscardInBuffer(); + } + catch + { + enabled = false; + } } if (serialPort is SerialPort) diff --git a/2018-unity-sim/Assets/Walk.cs b/2018-unity-sim/Assets/Walk.cs index 4382d70..44d3dc8 100644 --- a/2018-unity-sim/Assets/Walk.cs +++ b/2018-unity-sim/Assets/Walk.cs @@ -47,8 +47,8 @@ public class Walk : MonoBehaviour new step(new Vector3(0, 0, 0), false), // rest - new step(new Vector3(0, 2, 0), true), - new step(new Vector3(-1, 0, 0), false), + new step(new Vector3(0, 2, 0), false), + new step(new Vector3(-1, 0, 0), true), }; public int st = 5; diff --git a/2018-unity-sim/Assets/Walk2.cs b/2018-unity-sim/Assets/Walk2.cs new file mode 100644 index 0000000..d275907 --- /dev/null +++ b/2018-unity-sim/Assets/Walk2.cs @@ -0,0 +1,103 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Walk2 : MonoBehaviour +{ + public List legs = new List(); + private List legsInitial = new List(); + + // Use this for initialization + void Start() + { + for (int index = 0; index < legs.Count; ++index) + { + legsInitial.Add(legs[index].GetComponent().point); + } + } + + struct step + { + public float rotation; + public float height; + public bool inAir; + + public step(float r, float h, bool i) + { + rotation = r; + height = h; + inAir = i; + } + } + + // 1, 3, 5 -= 3 + + int offset(int i, int offset, int max) + { + return i - offset < 0 ? (max - (offset - i)) : i - offset; + } + + step[] s = { + new step(-1, 0, false), + + new step(0, -2, false), + + new step(0, 0, false), // rest + + new step(1, 0, false), + new step(1, 0, false), + + new step(0, 0, false), // rest + + new step(0, 2, false), + new step(-1, 0, true), + }; + + public int st = 5; + public int speed2 = 30; + int speed = 10; + + public float rotation = 0; + + int size = 8; + + //public float walkspeed = 1; + + // Update is called once per frame + void FixedUpdate() + { + if (speed <= 0) + { + if (st >= size-1) + st = 0; + else + st++; + + for (int index = 0; index < legs.Count; ++index) + { + int t = st; + if (index == 1 || index == 3 || index == 5) + { + t = offset(st, 4, size); + } + + if (s[t].inAir == false) + { + Vector3 n = legs[index].GetComponent().getWorldPos(); + n = Quaternion.Euler(0, rotation * s[t].rotation, 0) * n; + n.y += s[t].height; + // rotate x,y around rotation. + legs[index].GetComponent().setWorldPos(n); + } + else + { + legs[index].GetComponent().point = legsInitial[index] + new Vector3(0, s[t].height, 0); + } + } + + speed = speed2; + } + else + speed--; + } +} diff --git a/2018-unity-sim/Assets/Walk2.cs.meta b/2018-unity-sim/Assets/Walk2.cs.meta new file mode 100644 index 0000000..19644ec --- /dev/null +++ b/2018-unity-sim/Assets/Walk2.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: 91e6f3895f276d94da1803128e6290e0 +timeCreated: 1542229569 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: