質問編集履歴
1
こ
test
CHANGED
File without changes
|
test
CHANGED
@@ -8,6 +8,286 @@
|
|
8
8
|
|
9
9
|
|
10
10
|
|
11
|
+
arudinoのコード
|
12
|
+
|
13
|
+
```ここに言語を入力
|
14
|
+
|
15
|
+
#include <Wire.h>
|
16
|
+
|
17
|
+
#include <MadgwickAHRS.h>
|
18
|
+
|
19
|
+
Madgwick MadgwickFilter;
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
#define MPU6050_PWR_MGMT_1 0x6B
|
24
|
+
|
25
|
+
#define MPU_ADDRESS 0x68
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
void setup() {
|
32
|
+
|
33
|
+
Wire.begin();
|
34
|
+
|
35
|
+
Serial.begin(115200); //115200bps
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
Wire.beginTransmission(MPU_ADDRESS);
|
40
|
+
|
41
|
+
Wire.write(MPU6050_PWR_MGMT_1); //MPU6050_PWR_MGMT_1レジスタの設定
|
42
|
+
|
43
|
+
Wire.write(0x00);
|
44
|
+
|
45
|
+
Wire.endTransmission();
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
MadgwickFilter.begin(100); //100Hz
|
50
|
+
|
51
|
+
}
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
void loop() {
|
56
|
+
|
57
|
+
Wire.beginTransmission(0x68);
|
58
|
+
|
59
|
+
Wire.write(0x3B);
|
60
|
+
|
61
|
+
Wire.endTransmission(false);
|
62
|
+
|
63
|
+
Wire.requestFrom(0x68, 14, true);
|
64
|
+
|
65
|
+
while (Wire.available() < 14);
|
66
|
+
|
67
|
+
int16_t axRaw, ayRaw, azRaw, gxRaw, gyRaw, gzRaw, Temperature;
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
axRaw = Wire.read() << 8 | Wire.read();
|
72
|
+
|
73
|
+
ayRaw = Wire.read() << 8 | Wire.read();
|
74
|
+
|
75
|
+
azRaw = Wire.read() << 8 | Wire.read();
|
76
|
+
|
77
|
+
Temperature = Wire.read() << 8 | Wire.read();
|
78
|
+
|
79
|
+
gxRaw = Wire.read() << 8 | Wire.read();
|
80
|
+
|
81
|
+
gyRaw = Wire.read() << 8 | Wire.read();
|
82
|
+
|
83
|
+
gzRaw = Wire.read() << 8 | Wire.read();
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
// 加速度値を分解能で割って加速度(G)に変換する
|
88
|
+
|
89
|
+
float acc_x = axRaw / 16384.0; //FS_SEL_0 16,384 LSB / g
|
90
|
+
|
91
|
+
float acc_y = ayRaw / 16384.0;
|
92
|
+
|
93
|
+
float acc_z = azRaw / 16384.0;
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
// 角速度値を分解能で割って角速度(degrees per sec)に変換する
|
98
|
+
|
99
|
+
float gyro_x = gxRaw / 131.0; // (度/s)
|
100
|
+
|
101
|
+
float gyro_y = gyRaw / 131.0;
|
102
|
+
|
103
|
+
float gyro_z = gzRaw / 131.0;
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
//Madgwickフィルターを用いて、PRY(pitch, roll, yaw)を計算
|
112
|
+
|
113
|
+
MadgwickFilter.updateIMU(gyro_x, gyro_y, gyro_z, acc_x, acc_y, acc_z);
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
//PRYの計算結果を取得する
|
118
|
+
|
119
|
+
float roll_1 = MadgwickFilter.getRoll();
|
120
|
+
|
121
|
+
float pitch_1 = MadgwickFilter.getPitch();
|
122
|
+
|
123
|
+
float yaw = MadgwickFilter.getYaw();
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
int roll_int,pitch_int,bitshift,i=1;
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
roll_int = int(roll_1) << 8;
|
132
|
+
|
133
|
+
pitch_int = int(pitch_1 * -1);
|
134
|
+
|
135
|
+
bitshift = roll_int + pitch_int;
|
136
|
+
|
137
|
+
// Serial.print("roll:");Serial.print(roll); Serial.print(",");
|
138
|
+
|
139
|
+
// Serial.print("pitch:"); Serial.print(pitch); Serial.println(",");
|
140
|
+
|
141
|
+
Serial.println(bitshift);
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
```
|
146
|
+
|
147
|
+
pythonのコード
|
148
|
+
|
149
|
+
今回の質問と直接関係ない部分は省かせていただいております。
|
150
|
+
|
151
|
+
```ここに言語を入力
|
152
|
+
|
153
|
+
# coding: UTF-8
|
154
|
+
|
155
|
+
# ジョイスティックWHILL操作システム
|
156
|
+
|
157
|
+
import time
|
158
|
+
|
159
|
+
from pygame.time import wait
|
160
|
+
|
161
|
+
import serial
|
162
|
+
|
163
|
+
from whill import ComWHILL
|
164
|
+
|
165
|
+
import pygame
|
166
|
+
|
167
|
+
from pygame.locals import *
|
168
|
+
|
169
|
+
import sys
|
170
|
+
|
171
|
+
import copy
|
172
|
+
|
173
|
+
import math
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
# 定数定義
|
180
|
+
|
181
|
+
SCREEN_WIDTH = 800
|
182
|
+
|
183
|
+
SCREEN_HEIGHT = 600
|
184
|
+
|
185
|
+
LEVELSTEPNUM = 10
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
ser = serial.Serial("COM7", 115200) # Arduinoが接続されているCOMポートを指定
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
def main():
|
194
|
+
|
195
|
+
global startTime
|
196
|
+
|
197
|
+
pygame.init() # 初期化
|
198
|
+
|
199
|
+
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) # 画面を作る
|
200
|
+
|
201
|
+
pygame.display.set_caption("whill controller")
|
202
|
+
|
203
|
+
font = pygame.font.Font(None, 30)
|
204
|
+
|
205
|
+
clock = pygame.time.Clock()
|
206
|
+
|
207
|
+
FPS = 120
|
208
|
+
|
209
|
+
# メイン関数
|
210
|
+
|
211
|
+
while True:
|
212
|
+
|
213
|
+
# デバイスの入力
|
214
|
+
|
215
|
+
getDeviceData()
|
216
|
+
|
217
|
+
# キー入力で終了
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
for event in pygame.event.get():
|
222
|
+
|
223
|
+
if event.type == QUIT:
|
224
|
+
|
225
|
+
pygame.quit()
|
226
|
+
|
227
|
+
sys.exit()
|
228
|
+
|
229
|
+
if event.type == KEYDOWN:
|
230
|
+
|
231
|
+
if event.key == K_ESCAPE:
|
232
|
+
|
233
|
+
pygame.quit()
|
234
|
+
|
235
|
+
sys.exit()
|
236
|
+
|
237
|
+
pygame.display.update()
|
238
|
+
|
239
|
+
clock.tick(FPS)
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
# 傾き判別
|
246
|
+
|
247
|
+
def getDeviceData():
|
248
|
+
|
249
|
+
Speed, RotationSpeed = 0, 0
|
250
|
+
|
251
|
+
getData1 = str(ser.readline().rstrip().decode(
|
252
|
+
|
253
|
+
encoding='utf-8', errors='ignore'))
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
data = int(getData1)
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
Speed = (data & 65280) >> 8
|
262
|
+
|
263
|
+
RotationSpeed = data & 255
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
if Speed > 127:
|
268
|
+
|
269
|
+
Speed -= 255
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
if RotationSpeed > 127:
|
274
|
+
|
275
|
+
RotationSpeed -= 255
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
print(Speed, RotationSpeed)
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
if __name__ == "__main__":
|
284
|
+
|
285
|
+
main()
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
```
|
290
|
+
|
11
291
|
|
12
292
|
|
13
293
|

|