Skip to content

Commit 5c62f84

Browse files
committed
Code for 9 DoF to Unity guide
Adding code for the 9 DoF to Unity guide. Two sets of Arduino code (one for calibrated sensor, one for uncalibrated) and the C# code for Unity.
1 parent 05baa9d commit 5c62f84

File tree

4 files changed

+223
-0
lines changed

4 files changed

+223
-0
lines changed

Arduino_9DoF_to_Unity/.metro_m4.test.only

Whitespace-only changes.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
#include <Adafruit_LSM6DS3TRC.h>
6+
#include <Adafruit_AHRS.h>
7+
#include <Adafruit_Sensor_Calibration.h>
8+
#include <Adafruit_LIS3MDL.h>
9+
10+
Adafruit_Sensor *accelerometer, *gyroscope, *magnetometer;
11+
12+
Adafruit_LIS3MDL lis3mdl;
13+
Adafruit_LSM6DS3TRC lsm6ds;
14+
15+
//Adafruit_NXPSensorFusion filter; // slowest
16+
//Adafruit_Madgwick filter; // faster than NXP
17+
Adafruit_Mahony filter;
18+
19+
#if defined(ADAFRUIT_SENSOR_CALIBRATION_USE_EEPROM)
20+
Adafruit_Sensor_Calibration_EEPROM cal;
21+
#else
22+
Adafruit_Sensor_Calibration_SDFat cal;
23+
#endif
24+
25+
#define FILTER_UPDATE_RATE_HZ 100
26+
#define PRINT_EVERY_N_UPDATES 10
27+
28+
uint32_t timestamp;
29+
30+
void setup() {
31+
// put your setup code here, to run once:
32+
Serial.begin(9600);
33+
34+
if (!lsm6ds.begin_I2C()) {
35+
while (1) {
36+
delay(10);
37+
}
38+
}
39+
40+
accelerometer = lsm6ds.getAccelerometerSensor();
41+
gyroscope = lsm6ds.getGyroSensor();
42+
magnetometer = &lis3mdl;
43+
44+
lsm6ds.setAccelRange(LSM6DS_ACCEL_RANGE_2_G);
45+
lsm6ds.setGyroRange(LSM6DS_GYRO_RANGE_250_DPS);
46+
lis3mdl.setRange(LIS3MDL_RANGE_4_GAUSS);
47+
48+
lsm6ds.setAccelDataRate(LSM6DS_RATE_104_HZ);
49+
lsm6ds.setGyroDataRate(LSM6DS_RATE_104_HZ);
50+
lis3mdl.setDataRate(LIS3MDL_DATARATE_1000_HZ);
51+
lis3mdl.setPerformanceMode(LIS3MDL_MEDIUMMODE);
52+
lis3mdl.setOperationMode(LIS3MDL_CONTINUOUSMODE);
53+
54+
filter.begin(FILTER_UPDATE_RATE_HZ);
55+
56+
}
57+
58+
void loop() {
59+
60+
float roll, pitch, heading;
61+
float gx, gy, gz;
62+
static uint8_t counter = 0;
63+
64+
sensors_event_t accel, gyro, mag;
65+
accelerometer->getEvent(&accel);
66+
gyroscope->getEvent(&gyro);
67+
magnetometer->getEvent(&mag);
68+
69+
cal.calibrate(mag);
70+
cal.calibrate(accel);
71+
cal.calibrate(gyro);
72+
gx = gyro.gyro.x * SENSORS_RADS_TO_DPS;
73+
gy = gyro.gyro.y * SENSORS_RADS_TO_DPS;
74+
gz = gyro.gyro.z * SENSORS_RADS_TO_DPS;
75+
76+
filter.update(gx, gy, gz,
77+
accel.acceleration.x, accel.acceleration.y, accel.acceleration.z,
78+
mag.magnetic.x, mag.magnetic.y, mag.magnetic.z);
79+
if (counter++ <= PRINT_EVERY_N_UPDATES) {
80+
return;
81+
}
82+
counter = 0;
83+
84+
roll = filter.getRoll();
85+
pitch = filter.getPitch();
86+
heading = filter.getYaw();
87+
Serial.print("|");
88+
Serial.print(heading);
89+
Serial.print("|");
90+
Serial.print(pitch);
91+
Serial.print("|");
92+
Serial.print(roll);
93+
Serial.println();
94+
95+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
#include <Adafruit_LSM6DS3TRC.h>
6+
7+
int x;
8+
int y;
9+
int z;
10+
11+
Adafruit_LSM6DS3TRC lsm6ds3trc;
12+
13+
void setup() {
14+
// put your setup code here, to run once:
15+
Serial.begin(9600);
16+
17+
if (!lsm6ds.begin_I2C()) {
18+
while (1) {
19+
delay(10);
20+
}
21+
}
22+
lsm6ds.setAccelRange(LSM6DS_ACCEL_RANGE_2_G);
23+
lsm6ds.setGyroRange(LSM6DS_GYRO_RANGE_250_DPS);
24+
lis3mdl.setRange(LIS3MDL_RANGE_4_GAUSS);
25+
26+
lsm6ds.setAccelDataRate(LSM6DS_RATE_104_HZ);
27+
lsm6ds.setGyroDataRate(LSM6DS_RATE_104_HZ);
28+
lis3mdl.setDataRate(LIS3MDL_DATARATE_1000_HZ);
29+
lis3mdl.setPerformanceMode(LIS3MDL_MEDIUMMODE);
30+
lis3mdl.setOperationMode(LIS3MDL_CONTINUOUSMODE);
31+
32+
}
33+
34+
void loop() {
35+
// put your main code here, to run repeatedly:
36+
37+
sensors_event_t accel;
38+
sensors_event_t gyro;
39+
sensors_event_t temp;
40+
lsm6ds3trc.getEvent(&accel, &gyro, &temp);
41+
42+
x = map(accel.acceleration.x, -12, 11, 180, -180);
43+
y = map(accel.acceleration.y, -19, 20, -180, 180);
44+
z = map(accel.acceleration.z, -17, 15, 180, -180);
45+
46+
Serial.print("|");
47+
Serial.print(x);
48+
Serial.print("|");
49+
Serial.print(y);
50+
Serial.print("|");
51+
Serial.print(z);
52+
Serial.println();
53+
delay(50);
54+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
using System.Collections;
6+
using System.Collections.Generic;
7+
using System.IO.Ports;
8+
using UnityEngine;
9+
using UnityEngine.UI;
10+
11+
public class arduinoCtrl : MonoBehaviour
12+
{
13+
SerialPort stream = new SerialPort("COM52", 9600);
14+
//public float sensitivity;
15+
16+
public Transform t;
17+
18+
private float posX;
19+
private float posY;
20+
private float posZ;
21+
22+
private float sensX;
23+
private float sensY;
24+
private float sensZ;
25+
26+
public float Xcal;
27+
public float Ycal;
28+
public float Zcal;
29+
30+
void Start()
31+
{
32+
stream.Open();
33+
//stream.ReadTimeout = 20;
34+
}
35+
36+
void Update()
37+
{
38+
Vector3 lastData = Vector3.zero;
39+
40+
string UnSplitData = stream.ReadLine();
41+
print(UnSplitData);
42+
string[] SplitData = UnSplitData.Split('|');
43+
44+
float AccX = float.Parse(SplitData[1]);
45+
float AccY = float.Parse(SplitData[2]);
46+
float AccZ = float.Parse(SplitData[3]);
47+
48+
lastData = new Vector3(AccX, AccY, AccZ);
49+
50+
//Vector3 ParseAccelerometerData(string stream);
51+
/*string Temp = SplitData[4];
52+
string GyroX = (float.Parse(SplitData[5]) / 10000).ToString();
53+
string GyroY = (float.Parse(SplitData[6]) / 10000).ToString();
54+
string GyroZ = (float.Parse(SplitData[7]) / 10000).ToString();*/
55+
56+
/*Acceleration.text = "X: " + AccX + "\nY: " + AccY + "\nZ: " + AccZ;
57+
Gyroscope.text = "X: " + GyroX + "\nY: " + GyroY + "\nZ: " + GyroZ;
58+
Magnetometer.text = "X: " + MagX + "\nY: " + MagY + "\nZ: " + MagZ;
59+
Temperature.text = Temp + "`C";
60+
61+
posX += float.Parse(GyroX);
62+
posY += float.Parse(GyroY);
63+
posZ += float.Parse(GyroZ);
64+
65+
sensX += float.Parse(AccX);
66+
sensY += float.Parse(AccY);
67+
sensZ += float.Parse(AccZ);*/
68+
69+
//Vector3 accl = ParseAccelerometerData(stream);
70+
//Smoothly rotate to the new rotation position.
71+
t.transform.rotation = Quaternion.Slerp(t.transform.rotation, Quaternion.Euler(lastData), Time.deltaTime * 2f);
72+
73+
}
74+
}

0 commit comments

Comments
 (0)