This commit is contained in:
2019-05-19 00:18:28 +02:00
parent aa1f537137
commit c91b885b17
8 changed files with 305 additions and 1 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
+50 -1
View File
@@ -10514,6 +10514,55 @@ Transform:
m_Father: {fileID: 920748677}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &73925798
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 5
m_Component:
- component: {fileID: 73925799}
- component: {fileID: 73925800}
m_Layer: 0
m_Name: SerialIn
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &73925799
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 73925798}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -43.539, y: -46.55305, z: -15.30658}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 5
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &73925800
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 73925798}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 7fa025fa8dcb3a249b67fc0e2fe67b44, type: 3}
m_Name:
m_EditorClassIdentifier:
bot: {fileID: 311513962}
walk: {fileID: 311513965}
a1: 0
a2: 0
a3: 0
b1: 0
b2: 0
b3: 0
enabled: 0
--- !u!1 &74230589
GameObject:
m_ObjectHideFlags: 0
@@ -48816,7 +48865,7 @@ MonoBehaviour:
speed2: 10
walkDirection: 0
centerRotation: 0
walkspeed: 2
walkspeed: 0
--- !u!1 &311874317
GameObject:
m_ObjectHideFlags: 0
+69
View File
@@ -0,0 +1,69 @@
using System;
using UnityEngine;
using System.Collections;
using System.IO.Ports;
using System.Linq;
public class Serial2 : MonoBehaviour{
public float x;
public float y;
public RotationControl bot;
~Serial2() {
serialPort.Close();
}
public bool enabled = false;
SerialPort serialPort;
public void Update()
{
if (serialPort == null)
{
serialPort = new SerialPort(@"\\.\" + "COM7", 115200);
try
{
serialPort.NewLine = "\n";
serialPort.Open();
serialPort.DiscardOutBuffer();
serialPort.DiscardInBuffer();
enabled = true;
}
catch
{
enabled = false;
}
}
if (enabled)
{
if (serialPort is SerialPort)
{
string received = serialPort.ReadLine();
string[] data = received.Split(',');
x = ConvertRadiansToDegrees(float.Parse(data.First()));
y = ConvertRadiansToDegrees(float.Parse(data.Last()));
bot.rotation = new Quaternion(-1* limit(y, -30, 30), 0, -1* limit(x, -30, 30), 1);
}
}
}
public float limit(float input, float min, float max)
{
return (input < max & input > min) ? input : input < min ? min : max;
}
public float ConvertRadiansToDegrees(float radians)
{
float degrees = (float) ((180 / Math.PI) * radians);
return (degrees);
}
}
+13
View File
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: ac6468bb4d2734947a923ea42d6d8192
timeCreated: 1557262171
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+160
View File
@@ -0,0 +1,160 @@
using System;
using UnityEngine;
using System.Collections;
using System.IO.Ports;
using System.Linq;
using UnityEngine.UI;
public class Serial3 : MonoBehaviour{
public RotationControl bot;
public Walk3 walk;
public float a1, a2, a3, b1, b2, b3;
~Serial3() {
serialPort.Close();
}
public bool enabled = false;
SerialPort serialPort;
public void Update()
{
if (serialPort == null)
{
serialPort = new SerialPort(@"\\.\" + "COM7", 115200);
try
{
serialPort.NewLine = "\r\n";
// serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
serialPort.Open();
serialPort.DiscardOutBuffer();
serialPort.DiscardInBuffer();
enabled = true;
}
catch
{
enabled = false;
}
}
if (enabled)
{
if (serialPort is SerialPort)
{
string received = serialPort.ReadLine();
string[] data = received.Split(',');
foreach (string d in data)
{
string[] ch = d.Split('_');
int channel = int.Parse(ch.First());
float value = float.Parse(ch.Last());
switch (channel)
{
case 0:
a1 = value - 127;
break;
case 1:
a2 = value - 127;
break;
case 2:
a3 = value - 127;
break;
case 3:
b1 = value - 127;
break;
case 4:
b2 = value - 127;
break;
case 5:
b3 = value - 127;
break;
}
bot.rotation = new Quaternion(limit(a2, -20, 20), -1 * limit(a3, -20, 20), -1 * limit(a1, -20, 20),
1);
float speed = (float) (Math.Sqrt(b1 * b1 + b2 * b2) / 127) * 3;
walk.walkspeed = speed < 0.5f ? 0 : speed;
float rotation = -1 * limit(b3/4, -5, 5);
walk.centerRotation = rotation < 0.5f && rotation > -0.5f ? 0 : rotation;
walk.walkDirection = (-1 * ConvertRadiansToDegrees((float) Math.Atan2(b2, b1)));
}
}
}
}
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort serialPort = (SerialPort)sender;
if (enabled)
{
if (serialPort is SerialPort)
{
string received = serialPort.ReadLine();
string[] data = received.Split(',');
foreach (string d in data)
{
// if (data == "\r")
// continue;
string[] ch = d.Split('_');
int channel = int.Parse(ch.First());
float value = float.Parse(ch.Last());
switch (channel)
{
case 0:
a1 = value;
break;
case 1:
a2 = value;
break;
case 2:
a3 = value;
break;
case 3:
b1 = value;
break;
case 4:
b2 = value;
break;
case 5:
b3 = value;
break;
}
}
// bot.rotation = new Quaternion(-1* limit(y, -30, 30), 0, -1* limit(x, -30, 30), 1);
}
}
}
public float limit(float input, float min, float max)
{
input = input / 6;
return (input < max & input > min) ? input : input < min ? min : max;
}
public float ConvertRadiansToDegrees(float radians)
{
float degrees = (float)((180 / Math.PI) * radians);
return (degrees);
}
}
+13
View File
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 7fa025fa8dcb3a249b67fc0e2fe67b44
timeCreated: 1558207463
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: