-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduinoControls.cs
More file actions
140 lines (136 loc) · 5.41 KB
/
ArduinoControls.cs
File metadata and controls
140 lines (136 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using GXPEngine;
class ArduinoControls : GameObject
{
// Assigning starting variables
float yaw = 0.0f;
float roll = 0.0f;
float pitch = 0.0f;
bool leftButtonPressed;
bool rightButtonPressed;
float lastUpdateTime = 0.0f;
float updateInterval = 16.0f;
float previousYaw = 0.0f;
float startYaw = 0.0f;
SerialPort port;
public ArduinoControls()
{
port = new SerialPort();
port.PortName = "COM6";
port.BaudRate = 9600;
port.RtsEnable = true;
port.DtrEnable = true;
}
public void UseFile(Controller platform, Player player)
{
// Delay the file usage a little to prevent an exception and check if the file is being used
if (Time.time - lastUpdateTime > updateInterval && SerialPort.GetPortNames().Length != 0)
{
string[] incomingString = ReadFile();
// Incase data is not read properly it preserves the previous value instead
if (incomingString.Length == 5)
{
// I've adjusted the names of the variables to match the arduino when it is in a horizontal orientation
// Checking if values are empty to prevent exceptions
if (incomingString[0] != "" && incomingString[0] != null) { yaw = Convert.ToSingle(incomingString[0]); }
if (incomingString[1] != "" && incomingString[1] != null) { roll = Convert.ToSingle(incomingString[1]); }
if (incomingString[2] != "" && incomingString[2] != null) { pitch = Convert.ToSingle(incomingString[2]); }
if (incomingString[3] != "" && incomingString[3] != null)
{
if (Convert.ToSingle(incomingString[3]) < 0.5f) { leftButtonPressed = true; }
else { leftButtonPressed = false; }
}
if (incomingString[4] != "" && incomingString[4] != null)
{
if (Convert.ToSingle(incomingString[4]) < 0.5f) { rightButtonPressed = true; }
else { rightButtonPressed = false; }
}
// Setting starting yaw once. The assumption is that the arduino will be in a horizontal position
if (startYaw == 0.0f)
{
startYaw = yaw;
}
//Checking if the movement is natural by checking for big jumps in values.
if (previousYaw - yaw < 45 || yaw - previousYaw > 315)
{
// Rotating the object based on the given value and inverting the value depending on the orientation of the arduino
if (startYaw - yaw > 45) { platform.rotation = roll; }
else { platform.rotation = roll * -1; }
previousYaw = yaw;
//Console.WriteLine(platform.rotation);
}
player.Animate(0.06f);
if (leftButtonPressed)
{
if (player.x > player.boundary)
{
player.x -= player.speed;
}
player.SetCycle(6, 6);
player.Mirror(true, false);
}
else if (rightButtonPressed)
{
if (player.x < game.width - player.boundary)
{
player.x += player.speed;
}
player.SetCycle(6, 6);
player.Mirror(false, false);
}
else
{
player.SetCycle(0, 6);
}
player.Animate(0.05f);
}
lastUpdateTime = Time.time;
}
}
public bool ButtonPressedDown()
{
bool leftButtonWasPressed = leftButtonPressed;
bool rightButtonWasPressed = rightButtonPressed;
if (SerialPort.GetPortNames().Length != 0)
{
string[] incomingString = ReadFile();
// Incase data is not read properly it preserves the previous value instead
if (incomingString.Length == 5)
{
if (incomingString[3] != "" && incomingString[3] != null)
{
if (Convert.ToSingle(incomingString[3]) < 0.5f) { leftButtonPressed = true; }
else { leftButtonPressed = false; }
}
if (incomingString[4] != "" && incomingString[4] != null)
{
if (Convert.ToSingle(incomingString[4]) < 0.5f) { rightButtonPressed = true; }
else { rightButtonPressed = false; }
}
if ((leftButtonPressed && !leftButtonWasPressed) || (rightButtonPressed && !rightButtonWasPressed)) { return true; }
}
return false;
}
return false;
}
string[] ReadFile()
{
if (!port.IsOpen)
{
port.Open();
port.DiscardInBuffer();
port.DiscardOutBuffer();
string incomingData = port.ReadLine();
port.Close();
incomingData = incomingData.Trim();
string[] incomingString = incomingData.Split(new char[] { ',' });
return incomingString;
}
return null;
}
}