-
Notifications
You must be signed in to change notification settings - Fork 0
/
4g
85 lines (70 loc) · 2.83 KB
/
4g
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
#include <Wire.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Joystick.h>
Adafruit_MPU6050 mpu;
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
JOYSTICK_TYPE_JOYSTICK, 0, 0,
true, true, true, // X, Y, Z Axis
true, true, true, // Rx, Ry, Rz
false, false, // rudder, throttle
false, false, false); // accelerator, brake, steering
void setup() {
Serial.begin(115200);
while (!Serial) delay(10);
Wire.begin();
Serial.println("Initializing MPU6050...");
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip. Please check connections.");
while (1) {
delay(10);
}
}
Serial.println("MPU6050 Found!");
// Set accelerometer and gyro range
mpu.setAccelerometerRange(MPU6050_RANGE_4_G);
mpu.setGyroRange(MPU6050_RANGE_250_DEG);
// Initialize Joystick
Joystick.begin();
Joystick.setXAxisRange(-1023, 1023);
Joystick.setYAxisRange(-1023, 1023);
Joystick.setZAxisRange(-1023, 1023);
Joystick.setRxAxisRange(-1023, 1023);
Joystick.setRyAxisRange(-1023, 1023);
Joystick.setRzAxisRange(-1023, 1023);
}
void loop() {
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
// Print raw accelerometer and gyroscope values for debugging
Serial.print("Raw Accel X: "); Serial.print(a.acceleration.x);
Serial.print(" Y: "); Serial.print(a.acceleration.y);
Serial.print(" Z: "); Serial.print(a.acceleration.z);
Serial.print(" | Raw Gyro X: "); Serial.print(g.gyro.x);
Serial.print(" Y: "); Serial.print(g.gyro.y);
Serial.print(" Z: "); Serial.println(g.gyro.z);
// Map accelerometer values to joystick ranges
int16_t x = map(constrain(a.acceleration.x * 8192, -32768, 32767), -32768, 32767, -1023, 1023);
int16_t y = map(constrain(a.acceleration.y * 8192, -32768, 32767), -32768, 32767, -1023, 1023);
int16_t z = map(constrain(a.acceleration.z * 8192, -32768, 32767), -32768, 32767, -1023, 1023);
// Map gyroscope values to joystick ranges
int16_t rx = map(constrain(g.gyro.x * 131, -32768, 32767), -32768, 32767, -1023, 1023);
int16_t ry = map(constrain(g.gyro.y * 131, -32768, 32767), -32768, 32767, -1023, 1023);
int16_t rz = map(constrain(g.gyro.z * 131, -32768, 32767), -32768, 32767, -1023, 1023);
// Set joystick axes
Joystick.setXAxis(x);
Joystick.setYAxis(y);
Joystick.setZAxis(z);
Joystick.setRxAxis(rx);
Joystick.setRyAxis(ry);
Joystick.setRzAxis(rz);
Joystick.sendState();
// Print mapped values for debugging
Serial.print("Mapped Accel X: "); Serial.print(x);
Serial.print(" Y: "); Serial.print(y);
Serial.print(" Z: "); Serial.print(z);
Serial.print(" | Mapped Gyro X: "); Serial.print(rx);
Serial.print(" Y: "); Serial.print(ry);
Serial.print(" Z: "); Serial.println(rz);
delay(500);
}