ME 405 Romi
Loading...
Searching...
No Matches
imu_task.py
Go to the documentation of this file.
1from pyb import UART
2from struct import pack
3
8class imu_task:
9
10
15 def __init__(self, imu, imu_flg, yaw, yaw_velocity):
16
17 self.imu=imu
18 self.imu_flg = imu_flg
19 self.state=0
20 self.count = 0
21 self.calibrated = 0
22 self.substate=0
23 self.accel=0
24 self.sys=0
25 self.gyro=0
26 self.mag=0
27 self.config_flag = 0
28 self.yaw = yaw
29 self.yaw_velocity = yaw_velocity
30
31
38 def run(self):
39 while (True):
40 #init
41 if self.state == 0:
42
43 self.imu.change_mode(0x00)
44 (mode,) = self.imu._read_reg(self.imu.reg.OPR_MODE)
45 print(f'mode: {hex(mode)}')
46 self.imu.write_calibration_coeff(pack('<h',-47), pack('<h',166),pack('<h',-34), pack('<h',-341),
47 pack('<h',481), pack('<h',480), pack('<h',-2), pack('<h',-3),
48 pack('<h',0),pack('<h',1000), pack('<h',480))
49 # fusion mode = 0C
50 # imu mode =08
51 self.imu.change_mode(0x08)
52 (mode,) = self.imu._read_reg(self.imu.reg.OPR_MODE)
53 print(f'mode: {hex(mode)}')
54 self.state = 1
55 yield 0
56
57
58 elif self.state == 1:
59
60 self.yaw.put(self.imu.get_Euler_x())
61 self.yaw_velocity.put(self.imu.get_Roll_Velocity())
62
63
64 #if self.imu_flg.get()==1:
65 # print("imu flag true")
66 # (self.sys,self.gyro,self.accel,self.mag) = self.imu.calibration_check()
67 # print("checked calibration")
68 # print(self.sys, self.gyro, self.accel, self.mag)
69
70 #print(f"yaw: {self.yaw}")
71 #print(f"yawvelo: {self.yaw_velocity}")
72
73
74
75
76
77
78
79 # if self.accel==3 and self.gyro==3 and self.mag==3:
80 # self.config_flag = 1
81 # self.imu.change_mode(0x00)
82
83 # if self.config_flag == 1:
84 # (self.acc_x, self.acc_y, self.acc_z,self.mag_x, self.mag_y, self.mag_z,self.gyr_x, self.gyr_y, self.gyr_z, self.acc_rad, self.mag_rad)=self.imu.get_calibration_coeff()
85 # print(self.acc_x, self.acc_y, self.acc_z,self.mag_x, self.mag_y, self.mag_z,self.gyr_x, self.gyr_y, self.gyr_z, self.acc_rad, self.mag_rad)
86
87 # (mode,) = self.imu._read_reg(self.imu.reg.OPR_MODE)
88 # print(f'fusion mode: {hex(mode)}')
89
90
91 # if self.substate==0:
92 # self.sys, self.gyry,self.acc, self.mag=self.imu.calibration_check()
93 # self.substate+=1
94
95 # yield 1
96 # elif self.substate==1:
97 # #self.mag=self.imu.calibration_check_mag()
98 # self.substate+=1
99 # yield 1
100 # elif self.substate==2:
101 # #self.acc=self.imu.calibration_check_accel()
102 # self.substate+=1
103 # yield 1
104 # elif self.substate==3:
105 # #self.gyr=self.imu.calibration_check_gyro()
106 # self.substate+=1
107 # yield 1
108 # elif self.substate==4:
109 # self.acc_coeff=self.imu.get_calibration_coeff_accel()
110 # self.substate+=1
111 # yield 1
112 # elif self.substate==5:
113 # self.gyr_coeff=self.imu.get_calibration_coeff_gyro()
114 # self.substate+=1
115 # yield 1
116 # elif self.substate==6:
117 # self.mag_coeff=self.imu.get_calibration_coeff_mag()
118 # self.substate+=1
119 # yield 1
120 # elif self.substate==7:
121 # print(f"calib check: {self.sys},{self.gyr},{self.acc},{self.mag}")
122 # yield 1
123 # print(f"calib coeff: {self.gyr_coeff},{self.acc_coeff},{self.mag_coeff}")
124 # self.imu_flg.put(0)
125 # self.substate=0
126 # yield 1
127 yield 1
128 # else:
129 # yield 1
130
131 # if gyr == 3:
132 # if self.calibrated == 1:
133 # print("gyro calibrated")
134 # else:
135 # self.calibrated = 1
136 # self.state = 2
137 # if mag == 3:
138 # if self.calibrated == 2:
139 # print("mag calibrated")
140 # self.calibrated = 2
141 # self.state = 2
142 # if acc == 3:
143 # if self.calibrated == 3:
144 # print("acc calibrated")
145 # self.calibrated = 3
146 # self.state = 2
147 # if sys == 3:
148 # if self.calibrated == 3:
149 # print("sys calibrated")
150 # self.calibrated = 4
151 # self.state = 2
152 # self.calibrated = True
153
154
155 # print(f"check calb: {sys},{mag},{acc},{gyr}")
156
157 # msg = "calib:{},{},{},{}".format(
158 # sys, gyro, accel, mag
159 # )
160
161 # print(msg)
162
163 # print(f'calib: {self.imu.calibration_check()}')
164 # print(f'calib coeff: {self.imu.get_calibration_coeff()}')
165 # # print(f'euler heading: {self.imu.get_Euler_Heading()}')
166 # print(f'euler yaw: {self.imu.get_Euler_Heading()}')
167 # #(mode,) = self.imu._read_reg(self.imu.reg.OPR_MODE)
168 # #print(f'fusion mode: {hex(mode)}')
169
170
171 elif self.state == 2:
172 self.count += 1
173 if self.count == 1000:
174 self.count = 0
175 self.state = 1
176 else:
177 raise ValueError("Not that many states")
178 yield self.state
Runs the IMU.
Definition imu_task.py:8
run(self)
Runs the IMU Tasks.
Definition imu_task.py:38
__init__(self, imu, imu_flg, yaw, yaw_velocity)
Initialization.
Definition imu_task.py:15