-
Notifications
You must be signed in to change notification settings - Fork 2
/
MotionSensor.cs
97 lines (72 loc) · 2.07 KB
/
MotionSensor.cs
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
using UnityEngine;
using System.Collections;
using System;
using System.IO.Ports;
public class MotionSensor : MonoBehaviour
{
SerialPort stream;
public GameObject target; // is the gameobject to
public float acc_normalizer_factor = 0.00025f;
public float gyro_normalizer_factor = 1.0f / 32768.0f; // 32768 is max value captured during test on imu
//float curr_angle_x = 0;
//float curr_angle_y = 0;
//float curr_angle_z = 0;
float curr_offset_x = 0;
float curr_offset_y = -2;
// Increase the speed/influence rotation
public float factor = 7;
public bool enableRotation;
public bool enableTranslation;
// SELECT YOUR COM PORT AND BAUDRATE
string port = "COM5";
int baudrate = 4800;
void Start()
{
Debug.Log("OK1");
stream = new SerialPort("\\\\.\\" + port, baudrate);
stream.Open();
Debug.Log("OK");
}
void Update()
{
/*if (curr_offset_x < -10)
{
curr_offset_x = -10;
}
if (curr_offset_x > 10)
{
curr_offset_x = 10;
}
if (curr_offset_y > 10)
{
curr_offset_y = 10;
}
if (curr_offset_y < 1)
{
curr_offset_y = -1;
}*/
if (Input.GetKeyDown("space"))
{
Debug.Log("OK");
curr_offset_x = 0;
curr_offset_y = -2;
}
string dataString = "null received";
dataString = stream.ReadLine();
char splitChar = ';';
string[] dataRaw = dataString.Split(splitChar);
float vx = Int32.Parse(dataRaw[2]);
float vy = Int32.Parse(dataRaw[3]);
curr_offset_x += (vx*acc_normalizer_factor);
curr_offset_y += (vy*acc_normalizer_factor);
//Debug.Log(curr_offset_x);
//Debug.Log(curr_offset_y);
if(enableTranslation) target.transform.position = new Vector3(-curr_offset_x, -curr_offset_y, -15f);
//if(enableRotation)target.transform.rotation = Quaternion.Euler(curr_angle_x * factor, -curr_angle_z * factor, curr_angle_y * factor);
}
void OnTriggerEnter(Collider col){
if(col.gameObject.tag == "Cube"){
Destroy(col.gameObject);
}
}
}