Skip to content

Commit cf004e0

Browse files
committed
update cmsis from 5.4.0 to 5.7.0, enable dsp libmath
add initial (wip) tf4micro-motion-kit sketch
1 parent 10f51fb commit cf004e0

File tree

6 files changed

+1097
-3
lines changed

6 files changed

+1097
-3
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/* Copyright 2021 Google LLC
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
https://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
==============================================================================*/
15+
16+
/*
17+
* Namespaced methods for providing IMU data
18+
* @author Rikard Lindstrom <rlindsrom@google.com>
19+
*/
20+
#include "data_provider.h"
21+
#include <Arduino_LSM9DS1.h> // change to Arduino_LSM6DS3.h for Nano 33 IoT or Uno WiFi Rev 2
22+
23+
namespace data_provider
24+
{
25+
#define DATA_PROVIDER_CALIBRATION_THRESHOLD 10.0
26+
#define DATA_PROVIDER_CALIBRATION_STEPS 40
27+
28+
/************************************************************************
29+
* Calibration vars
30+
************************************************************************/
31+
32+
float lastMagneticFieldReading[3] = {0.0, 0.0, 0.0};
33+
float calibratedMagneticFieldHeading[3] = {0.0, 0.0, 0.0};
34+
int calibrationStep = 0;
35+
bool calibrating = false;
36+
37+
/************************************************************************
38+
* "Public" functions
39+
************************************************************************/
40+
41+
// Calibrate the magnetometer
42+
void calibrate()
43+
{
44+
calibrating = true;
45+
calibrationStep = 0;
46+
}
47+
48+
bool dataAvailable()
49+
{
50+
// Skip magnetometer since it's running a lot slower and always wanted
51+
return IMU.accelerationAvailable() && IMU.gyroscopeAvailable();
52+
}
53+
54+
bool setup()
55+
{
56+
57+
if (!IMU.begin())
58+
{
59+
Serial.println("Failed to initialized IMU!");
60+
return false;
61+
}
62+
63+
// Experimental, enabling this will capture all readings
64+
// from the IMU sensors and should be more accurate. However,
65+
// it slows down the main loop by a lot when enabled.
66+
67+
// IMU.setContinuousMode();
68+
69+
Serial.println("IMU sample rates: ");
70+
Serial.print("Accelerometer sample rate = ");
71+
Serial.println(IMU.accelerationSampleRate());
72+
Serial.print("Gyroscope sample rate = ");
73+
Serial.println(IMU.gyroscopeSampleRate());
74+
Serial.print("Magnetometer sample rate = ");
75+
Serial.println(IMU.magneticFieldSampleRate());
76+
77+
return true;
78+
}
79+
80+
void update(float *buffer, bool useMagnetometer)
81+
{
82+
83+
if (!dataAvailable())
84+
{
85+
return;
86+
}
87+
88+
float ax, ay, az, gx, gy, gz;
89+
90+
// read the acceleration and gyroscope data
91+
IMU.readAcceleration(ax, ay, az);
92+
IMU.readGyroscope(gx, gy, gz);
93+
94+
// Accelorameter has a range of -4 – 4
95+
buffer[0] = ax / 4.0;
96+
buffer[1] = ay / 4.0;
97+
buffer[2] = az / 4.0;
98+
99+
// Gyroscope has a range of -2000 – 2000
100+
buffer[3] = gx / 2000.0;
101+
buffer[4] = gy / 2000.0;
102+
buffer[5] = gz / 2000.0;
103+
104+
if (useMagnetometer || calibrating)
105+
{
106+
float mx, my, mz;
107+
108+
// The Magnetometer sample rate is only 20hz, so we'll use previous values
109+
// if no new ones are available
110+
if (IMU.magneticFieldAvailable())
111+
{
112+
IMU.readMagneticField(mx, my, mz);
113+
lastMagneticFieldReading[0] = mx;
114+
lastMagneticFieldReading[1] = my;
115+
lastMagneticFieldReading[2] = mz;
116+
117+
if (calibrating)
118+
{
119+
// Running avarage
120+
calibratedMagneticFieldHeading[0] += mx;
121+
calibratedMagneticFieldHeading[1] += my;
122+
calibratedMagneticFieldHeading[2] += mz;
123+
calibratedMagneticFieldHeading[0] /= 2.0;
124+
calibratedMagneticFieldHeading[1] /= 2.0;
125+
calibratedMagneticFieldHeading[2] /= 2.0;
126+
calibrationStep++;
127+
if (calibrationStep > DATA_PROVIDER_CALIBRATION_STEPS &&
128+
abs(calibratedMagneticFieldHeading[0] - mx) < DATA_PROVIDER_CALIBRATION_THRESHOLD &&
129+
abs(calibratedMagneticFieldHeading[1] - my) < DATA_PROVIDER_CALIBRATION_THRESHOLD &&
130+
abs(calibratedMagneticFieldHeading[2] - mz) < DATA_PROVIDER_CALIBRATION_THRESHOLD)
131+
{
132+
calibrating = false;
133+
data_provider_calibrationComplete();
134+
return;
135+
}
136+
}
137+
}
138+
else
139+
{
140+
mx = lastMagneticFieldReading[0];
141+
my = lastMagneticFieldReading[1];
142+
mz = lastMagneticFieldReading[2];
143+
}
144+
145+
if (calibrating)
146+
{
147+
return;
148+
}
149+
150+
mx -= calibratedMagneticFieldHeading[0];
151+
my -= calibratedMagneticFieldHeading[1];
152+
mz -= calibratedMagneticFieldHeading[2];
153+
154+
// Raw magnetometer data
155+
buffer[6] = mx / 50.0;
156+
buffer[7] = my / 50.0;
157+
buffer[8] = mz / 50.0;
158+
}
159+
}
160+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* Copyright 2021 Google LLC
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
https://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
==============================================================================*/
15+
16+
#ifndef DATA_PROVIDER_CPP
17+
#define DATA_PROVIDER_CPP
18+
19+
// Forward declare the function that will be called when data has been delivered to us.
20+
void data_provider_calibrationComplete();
21+
22+
namespace data_provider {
23+
bool setup();
24+
void update(float* buffer, bool useMagnetometer);
25+
void calibrate();
26+
bool dataAvailable();
27+
}
28+
29+
#endif

0 commit comments

Comments
 (0)