updated walk and controller input to joystick

This commit is contained in:
2022-12-04 23:11:05 +01:00
parent 40e71c5ce4
commit be04329d65
25 changed files with 677 additions and 941 deletions
+119
View File
@@ -0,0 +1,119 @@
using System;
using UnityEngine;
using System.Collections;
using System.IO.Ports;
using Random = System.Random;
public class Serial
{
private static Serial instance;
private int rnd = 0;
private Serial()
{
}
~Serial()
{
serialPort.Close();
serialPort.Dispose();
}
public static Serial Instance
{
get
{
if (instance == null)
{
instance = new Serial();
}
return instance;
}
}
public bool enabled = false;
public SerialPort serialPort;
public int[] values = new int[18];
public static string ByteArrayToString(byte[] ba)
{
return BitConverter.ToString(ba).Replace("-", " ");
}
public void send(int start, int a, int b, int c)
{
if (serialPort == null)
{
serialPort = new SerialPort(@"\\.\" + "COM4", 250000);
try
{
serialPort.Parity = Parity.Even;
serialPort.StopBits = StopBits.One;
serialPort.DataBits = 8;
serialPort.Open();
serialPort.DiscardOutBuffer();
serialPort.DiscardInBuffer();
System.Threading.Thread.Sleep(1000);
enabled = true;
}
catch
{
enabled = false;
}
}
if (enabled)
{
values[start] = a;
values[++start] = b;
values[++start] = c;
if (serialPort is SerialPort && start == 17)
{
// string send = "s";
byte[] send = new byte[18+1+2];
send[18] = 0x00;
send[19] = 0xfe;
send[20] = 0xff;
// send[21] = 0xff;
// if (rnd == 0)
// {
//rnd = 1;
for (int i = 0; i < 18; i++)
{
//string crc = ((i) * (values[i])) + "";
//send = send + (i).ToString() + "_" + (values[i]).ToString() + "," + crc + "=";
byte[] intBytes = BitConverter.GetBytes(values[i]);
if (BitConverter.IsLittleEndian)
Array.Reverse(intBytes);
send[i] = intBytes[3];
send[18] += send[i];
}
// Debug.Log(ByteArrayToString(send));
serialPort.Write(send, 0, 18+1+2);
serialPort.BaseStream.Flush();
// }
// else
// {
// rnd = 0;
// for (int i = 10; i < 18; i++)
// {
// string crc = ((i + 3) * (values[i])) + "";
//
// send = send + (i + 2).ToString() + "_" + (values[i]).ToString() + "," + crc + "=";
// }
//
// Debug.Log(send);
// serialPort.Write(send);
// }
}
}
}
}
+13
View File
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: c99b6d9374ff6d3419c7692fdf3c1791
timeCreated: 1542057315
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+70
View File
@@ -0,0 +1,70 @@
/*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:
+170
View File
@@ -0,0 +1,170 @@
//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 Walk walk;
// public float a1, a2, a3, b1, b2, b3;
// // ~Serial3()
// // {
// // serialPort.Close();
// // serialPort.Dispose();
// // }
// public bool enabled = false;
// public bool enabled_send = false;
// SerialPort serialPort;
// public void Update()
// {
// if (serialPort == null)
// {
// serialPort = new SerialPort(@"\\.\" + "COM7", 230400);
// try
// {
// serialPort.NewLine = "\r\n";
//// serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
// serialPort.Open();
// serialPort.DiscardOutBuffer();
// serialPort.DiscardInBuffer();
// enabled = true;
// }
// catch
// {
// enabled = false;
// }
// }
// if (enabled)
// {
// serialPort = Serial.Instance.serialPort;
// Serial.Instance.enabled = enabled_send;
// 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)));
// float rotationabs = Math.Abs(walk.centerRotation);
// float speed2 = ((6 / 3) * walk.walkspeed) + ((6/5) * rotationabs);
// speed2 = speed2 < 6 ? speed2 : 6;
// walk.speed2 = (int)(10 - speed2);
// }
// }
// }
// }
// 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: