10using System.Collections;
11using System.Collections.Generic;
14using System.Runtime.InteropServices;
15using System.Threading;
16using NaughtyAttributes;
37 [Tooltip(
"Port name with which the SerialPort object will be created.")]
40 [Tooltip(
"Baud rate that the serial device is using to transmit data.")]
43 [Tooltip(
"Reference to an scene object that will receive the events of connection, " +
44 "disconnection and the messages from the serial device.")]
47 [Tooltip(
"After an error in the serial communication, or an unsuccessful " +
48 "connect, how many milliseconds we should wait.")]
51 [Tooltip(
"Maximum number of unread data messages in the queue. " +
52 "New messages will be discarded.")]
91 if (userDefinedTearDownFunction !=
null)
92 userDefinedTearDownFunction();
135 var pos =
new Vector3(pose.x, pose.y, pose.z);
143 message = s ?? m?.ToString();
145 if (
string.IsNullOrEmpty(message))
159 return BitConverter.ToString(ba).Replace(
"-",
" ");
165 private void SendMoveCommand()
167 var header =
new byte[] {0xAA, 0xAA, 0x13};
168 var MoveCommand =
new byte[] {0x54, 0x03, 0x01};
169 var payload = ConcatArrays(MoveCommand, BitConverter.GetBytes(
x), BitConverter.GetBytes(
y),
170 BitConverter.GetBytes(
z), BitConverter.GetBytes(
r));
176 internal void SendMoveCommand(Vector3 v,
bool queue =
false)
178 SendMoveCommand(
new Vector4(v.x, v.y, v.z, 0), queue);
181 internal void SendMoveCommand(Vector4 v,
bool queue =
false)
184 var header =
new byte[] {0xAA, 0xAA, 0x13};
185 var MoveCommand =
new byte[] {0x54, (byte) (queue ? 0x03 : 0x01), 0x01};
186 var payload = ConcatArrays(MoveCommand, BitConverter.GetBytes(v.x), BitConverter.GetBytes(v.y),
187 BitConverter.GetBytes(v.z), BitConverter.GetBytes(v.w));
193 internal void SendRelativeMoveCommand(Vector3 v,
bool queue =
false)
196 var header =
new byte[] {0xAA, 0xAA, 0x13};
197 var MoveCommand =
new byte[] {0x54, (byte) (queue ? 0x03 : 0x01), 0x07};
198 var payload = ConcatArrays(MoveCommand, BitConverter.GetBytes(v.x), BitConverter.GetBytes(v.y),
199 BitConverter.GetBytes(v.z), BitConverter.GetBytes(0));
207 var result =
new T[list.Sum(a => a.Length)];
209 for (var
x = 0;
x < list.Length;
x++)
211 list[
x].CopyTo(result, offset);
212 offset += list[
x].Length;
221 foreach (var b
in payload) checksum += b;
223 return (
byte) (256 - checksum);
255 userDefinedTearDownFunction = userFunction;
259 private void SendGetPosePayload()
286 internal void SendGripperRelease()
292 internal void SendGripperGrip()
297 internal readonly List<Vector3> savedPos =
new List<Vector3>();
314 File.WriteAllText(
"DOBOT_POSITIONS.json", JsonConvert.SerializeObject(savedPos));
319 private void ReplayPosition()
321 StartCoroutine(replayer());
324 IEnumerator replayer()
326 foreach (var vector3
in savedPos)
328 SendMoveCommand(vector3,
true);
329 yield
return new WaitForSeconds(0.1f);
338 fixed (
byte* bufferPtr = buffer)
340 Buffer.MemoryCopy(bufferPtr, &result, 37, 37);
346 [StructLayout(LayoutKind.Sequential, Pack = 1)]
350 private readonly
byte Header1;
351 private readonly
byte Header2;
352 private readonly
byte payloadLength;
353 private readonly
byte ID;
354 private readonly
byte Ctrl;
UnityEngine.UI.Button Button
SerialThreadBytes serialThread
delegate void TearDownFunction()
void SetTearDownFunction(TearDownFunction userFunction)
static unsafe PoseReturn GetPoseFromArray(byte[] buffer)
GameObject messageListener
static string ByteArrayToString(byte[] ba)
const string SERIAL_DEVICE_CONNECTED
void SavePositionsToFile()
static byte CalculateChecksum(byte[] payload)
const string SERIAL_DEVICE_DISCONNECTED
static T[] ConcatArrays< T >(params T[][] list)
string ReadSerialMessage()
void ResetSavedPositions()
void SendSerialMessage(byte[] message)
fixed float jointAngle[4]