1+ // Copyright 2024 Khalil Estell
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+ // http://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+ #include < cmath>
16+
17+ #include < libhal-sensor/imu/icm20948.hpp>
18+ #include < libhal-util/serial.hpp>
19+ #include < libhal-util/steady_clock.hpp>
20+
21+ #include < resource_list.hpp>
22+
23+ float compute_heading (float x, float y, float offset = 0.0 )
24+ {
25+ float angle = 360 - (atan2 (y, x) * (180.0 / std::numbers::pi));
26+ angle += offset; // Apply offset
27+ if (angle < 0 ) {
28+ angle += 360 ;
29+ } else if (angle >= 360 ) {
30+ angle -= 360 ;
31+ }
32+ return angle;
33+ }
34+
35+ void application (resource_list& p_map)
36+ {
37+ using namespace std ::chrono_literals;
38+ using namespace hal ::literals;
39+
40+ auto & clock = *p_map.clock .value ();
41+ auto & console = *p_map.console .value ();
42+ auto & i2c = *p_map.i2c .value ();
43+
44+ hal::print (console, " icm Application Starting...\n\n " );
45+ hal::delay (clock, 200ms);
46+ hal::sensor::icm20948 icm_device (i2c);
47+
48+ hal::delay (clock, 200ms);
49+ icm_device.init_mag ();
50+ hal::delay (clock, 100ms);
51+
52+ icm_device.auto_offsets ();
53+
54+ while (true ) {
55+ auto accel = icm_device.read_acceleration ();
56+ hal::delay (clock, 10ms);
57+
58+ auto gyro = icm_device.read_gyroscope ();
59+ hal::delay (clock, 10ms);
60+
61+ auto temp = icm_device.read_temperature ();
62+ hal::delay (clock, 10ms);
63+
64+ auto mag = icm_device.read_magnetometer ();
65+ hal::delay (clock, 10ms);
66+
67+ hal::print (console, " \n\n ================Reading IMU================\n " );
68+
69+ hal::print<128 >(console,
70+ " \n\n G-Accel Values: x = %fg, y = %fg, z = %fg" ,
71+ accel.x ,
72+ accel.y ,
73+ accel.z );
74+
75+ hal::print<128 >(console,
76+ " \n\n Gyro Values: x = %f, y = %f, z = %f" ,
77+ gyro.x ,
78+ gyro.y ,
79+ gyro.z );
80+
81+ hal::print<128 >(console, " \n\n Current Temperature: %f°C" , temp.temp );
82+
83+ hal::print<128 >(console,
84+ " \n\n Magnetometer Values: x = %f, y = %f, z = %f" ,
85+ mag.x ,
86+ mag.y ,
87+ mag.z );
88+
89+ float heading = compute_heading (mag.x , mag.y , 0.0 );
90+ hal::print<128 >(console, " \n\n Heading: %f°" , heading);
91+ hal::print (console, " \n\n ===========================================\n " );
92+ }
93+ }
0 commit comments