Skip to content

Commit f187d08

Browse files
authored
Merge pull request #2257 from adafruit/9dof_unity
Code for 9 DoF to Unity guide
2 parents 82d88bb + ea43136 commit f187d08

File tree

5 files changed

+185
-0
lines changed

5 files changed

+185
-0
lines changed

Arduino_9DoF_to_Unity/Arduino_Code/9dof_to_unity_calibrated/.feather_m4_express.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+
}

Arduino_9DoF_to_Unity/Arduino_Code/9dof_to_unity_uncalibrated/.feather_m4_express.test.only

Whitespace-only changes.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 lsm6ds;
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+
25+
lsm6ds.setAccelDataRate(LSM6DS_RATE_104_HZ);
26+
lsm6ds.setGyroDataRate(LSM6DS_RATE_104_HZ);
27+
28+
}
29+
30+
void loop() {
31+
// put your main code here, to run repeatedly:
32+
33+
sensors_event_t accel;
34+
sensors_event_t gyro;
35+
sensors_event_t temp;
36+
lsm6ds.getEvent(&accel, &gyro, &temp);
37+
38+
x = map(accel.acceleration.x, -12, 11, 180, -180);
39+
y = map(accel.acceleration.y, -19, 20, -180, 180);
40+
z = map(accel.acceleration.z, -17, 15, 180, -180);
41+
42+
Serial.print("|");
43+
Serial.print(x);
44+
Serial.print("|");
45+
Serial.print(y);
46+
Serial.print("|");
47+
Serial.print(z);
48+
Serial.println();
49+
delay(50);
50+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
// replace with your board's COM port
14+
SerialPort stream = new SerialPort("COM52", 9600);
15+
16+
public Transform t;
17+
18+
void Start()
19+
{
20+
stream.Open();
21+
}
22+
23+
void Update()
24+
{
25+
Vector3 lastData = Vector3.zero;
26+
27+
string UnSplitData = stream.ReadLine();
28+
print(UnSplitData);
29+
string[] SplitData = UnSplitData.Split('|');
30+
31+
float AccX = float.Parse(SplitData[1]);
32+
float AccY = float.Parse(SplitData[2]);
33+
float AccZ = float.Parse(SplitData[3]);
34+
35+
lastData = new Vector3(AccX, AccY, AccZ);
36+
37+
t.transform.rotation = Quaternion.Slerp(t.transform.rotation, Quaternion.Euler(lastData), Time.deltaTime * 2f);
38+
39+
}
40+
}

0 commit comments

Comments
 (0)