Skip to content

Commit a5d8345

Browse files
[pre-commit.ci lite] apply automatic fixes
1 parent c17b251 commit a5d8345

6 files changed

Lines changed: 170 additions & 156 deletions

File tree

src/software/embedded/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ cc_library(
2626
deps = [
2727
":primitive_executor",
2828
"//proto:tbots_cc_proto",
29+
"//software/embedded/services:imu",
2930
"//software/embedded/services:motor",
3031
"//software/embedded/services:power",
3132
"//software/embedded/services/network",
32-
"//software/embedded/services:imu",
3333
"//software/embedded/toml_config",
3434
"//software/logger:network_logger",
3535
"//software/tracy:tracy_constants",

src/software/embedded/services/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ cc_library(
3838
deps = [
3939
"//proto:tbots_cc_proto",
4040
"//shared:constants",
41-
"//software/geom:angular_velocity",
4241
"//software/geom:angular_acceleration",
42+
"//software/geom:angular_velocity",
4343
"//software/logger",
4444
"@eigen",
4545
],

src/software/embedded/services/imu.cpp

Lines changed: 62 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1+
#include "imu.h"
2+
13
#include <fcntl.h>
24
#include <linux/i2c-dev.h>
35
#include <linux/i2c.h>
46
#include <sys/ioctl.h>
5-
#include <climits> // For SHRT_MAX
67

7-
#include "imu.h"
8+
#include <climits> // For SHRT_MAX
9+
810
#include "shared/constants.h"
911
#include "software/logger/logger.h"
1012

1113
// these functions taken from
1214
// https://git.kernel.org/pub/scm/utils/i2c-tools/i2c-tools.git/tree/lib/smbus.c
1315
static __s32 i2c_smbus_access(int file, char read_write, __u8 command, int size,
14-
union i2c_smbus_data* data)
16+
union i2c_smbus_data* data)
1517
{
1618
struct i2c_smbus_ioctl_data args;
1719
__s32 err;
@@ -128,22 +130,27 @@ ImuService::ImuService() : initialized_(false)
128130
}
129131
}
130132

131-
std::optional<ImuData> ImuService::poll(){
132-
133-
std::optional<AngularVelocity> angular_velocity = pollAngularVelocity();
134-
std::optional<AngularAcceleration> angular_acceleration = pollAngularAcceleration(angular_velocity);
135-
std::optional<Eigen::Vector2d> imu_linear_acceleration = pollLinearAcceleration();
136-
137-
if (angular_velocity.has_value() || angular_acceleration.has_value() || imu_linear_acceleration.has_value()){
138-
std::optional<Eigen::Vector2d> linear_acceleration = transformLinearAcceleration(angular_velocity.value(), angular_acceleration.value(), imu_linear_acceleration.value());
139-
}
140-
else{
141-
std::optional<Eigen::Vector2d> linear_acceleration = std::nullopt;
142-
}
133+
std::optional<ImuData> ImuService::poll()
134+
{
135+
std::optional<AngularVelocity> angular_velocity = pollAngularVelocity();
136+
std::optional<AngularAcceleration> angular_acceleration =
137+
pollAngularAcceleration(angular_velocity);
138+
std::optional<Eigen::Vector2d> imu_linear_acceleration = pollLinearAcceleration();
143139

140+
if (angular_velocity.has_value() || angular_acceleration.has_value() ||
141+
imu_linear_acceleration.has_value())
142+
{
143+
std::optional<Eigen::Vector2d> linear_acceleration = transformLinearAcceleration(
144+
angular_velocity.value(), angular_acceleration.value(),
145+
imu_linear_acceleration.value());
146+
}
147+
else
148+
{
149+
std::optional<Eigen::Vector2d> linear_acceleration = std::nullopt;
150+
}
144151

145-
return ImuData{angular_velocity, angular_acceleration, linear_acceleration};
146152

153+
return ImuData{angular_velocity, angular_acceleration, linear_acceleration};
147154
}
148155
std::optional<int16_t> ImuService::readAndCombineByteData(uint8_t ls_reg, uint8_t ms_reg)
149156
{
@@ -157,7 +164,7 @@ std::optional<int16_t> ImuService::readAndCombineByteData(uint8_t ls_reg, uint8_
157164

158165
uint16_t combined = (static_cast<uint8_t>(most_significant) << 8) |
159166
static_cast<uint8_t>(least_significant);
160-
167+
161168
return static_cast<int16_t>(combined);
162169
}
163170

@@ -168,50 +175,54 @@ std::optional<AngularVelocity> ImuService::pollAngularVelocity()
168175
return std::nullopt;
169176
}
170177

171-
std::optional<int16_t> opt_full_word = readAndCombineByteData(GYRO_LEAST_SIG_REG, GYRO_MOST_SIG_REG);
172-
178+
std::optional<int16_t> opt_full_word =
179+
readAndCombineByteData(GYRO_LEAST_SIG_REG, GYRO_MOST_SIG_REG);
180+
173181
if (!opt_full_word.has_value())
174182
{
175183
return std::nullopt;
176184
}
177185

178186
int16_t full_word = opt_full_word.value();
179-
187+
180188
double degrees_per_sec = static_cast<double>(full_word) /
181189
static_cast<double>(SHRT_MAX) * IMU_FULL_SCALE_DPS;
182-
183-
return AngularVelocity::fromRadians((degrees_per_sec - degrees_error_)*M_PI/180);
190+
191+
return AngularVelocity::fromRadians((degrees_per_sec - degrees_error_) * M_PI / 180);
184192
}
185193

186194

187-
std::optional<AngularAcceleration> ImuService::pollAngularAcceleration(std::optional<AngularVelocity> curr_angular_velocity)
195+
std::optional<AngularAcceleration> ImuService::pollAngularAcceleration(
196+
std::optional<AngularVelocity> curr_angular_velocity)
188197
{
189198
if (!initialized_)
190199
{
191200
return std::nullopt;
192201
}
193-
194-
if(!prev_angular_velocity_.has_value()){
195-
prev_angular_velocity_ = pollAngularVelocity();
196-
prev_time_ = std::chrono::steady_clock::now();
197-
return std::nullopt;
198-
}
199202

200-
auto curr_time = std::chrono::steady_clock::now();
203+
if (!prev_angular_velocity_.has_value())
204+
{
205+
prev_angular_velocity_ = pollAngularVelocity();
206+
prev_time_ = std::chrono::steady_clock::now();
207+
return std::nullopt;
208+
}
209+
210+
auto curr_time = std::chrono::steady_clock::now();
201211

202-
double dt = std::chrono::duration<double>(curr_time - prev_time_).count();
203-
if (dt <= 0 || !curr_angular_velocity.has_value()) return std::nullopt;
212+
double dt = std::chrono::duration<double>(curr_time - prev_time_).count();
213+
if (dt <= 0 || !curr_angular_velocity.has_value())
214+
return std::nullopt;
204215

205-
double alpha = (curr_angular_velocity->toRadians() -
206-
prev_angular_velocity_->toRadians()) / dt;
216+
double alpha =
217+
(curr_angular_velocity->toRadians() - prev_angular_velocity_->toRadians()) / dt;
207218

208-
prev_angular_velocity_ = curr_angular_velocity;
209-
prev_time_ = curr_time;
219+
prev_angular_velocity_ = curr_angular_velocity;
220+
prev_time_ = curr_time;
210221

211222
return AngularAcceleration::fromRadians(alpha);
212223
}
213-
214-
224+
225+
215226

216227
std::optional<Eigen::Vector2d> ImuService::pollLinearAcceleration()
217228
{
@@ -220,8 +231,10 @@ std::optional<Eigen::Vector2d> ImuService::pollLinearAcceleration()
220231
return std::nullopt;
221232
}
222233

223-
std::optional<int16_t> opt_x = readAndCombineByteData(ACCEL_X_LEAST_SIG_REG, ACCEL_X_MOST_SIG_REG);
224-
std::optional<int16_t> opt_y = readAndCombineByteData(ACCEL_Y_LEAST_SIG_REG, ACCEL_Y_MOST_SIG_REG);
234+
std::optional<int16_t> opt_x =
235+
readAndCombineByteData(ACCEL_X_LEAST_SIG_REG, ACCEL_X_MOST_SIG_REG);
236+
std::optional<int16_t> opt_y =
237+
readAndCombineByteData(ACCEL_Y_LEAST_SIG_REG, ACCEL_Y_MOST_SIG_REG);
225238

226239
if (!opt_x.has_value() || !opt_y.has_value())
227240
{
@@ -231,19 +244,18 @@ std::optional<Eigen::Vector2d> ImuService::pollLinearAcceleration()
231244
int16_t raw_x = opt_x.value();
232245
int16_t raw_y = opt_y.value();
233246

234-
double a_x = (static_cast<double>(raw_x) / SHRT_MAX)
235-
* ACCELEROMETER_FULL_SCALE_G
236-
* ACCELERATION_DUE_TO_GRAVITY_METERS_PER_SECOND_SQUARED;
237-
238-
double a_y = (static_cast<double>(raw_y) / SHRT_MAX)
239-
* ACCELEROMETER_FULL_SCALE_G
240-
* ACCELERATION_DUE_TO_GRAVITY_METERS_PER_SECOND_SQUARED;
241-
247+
double a_x = (static_cast<double>(raw_x) / SHRT_MAX) * ACCELEROMETER_FULL_SCALE_G *
248+
ACCELERATION_DUE_TO_GRAVITY_METERS_PER_SECOND_SQUARED;
249+
250+
double a_y = (static_cast<double>(raw_y) / SHRT_MAX) * ACCELEROMETER_FULL_SCALE_G *
251+
ACCELERATION_DUE_TO_GRAVITY_METERS_PER_SECOND_SQUARED;
252+
242253
return Eigen::Vector2d(a_x, a_y);
243254
}
244255

245-
Eigen::Vector2d ImuService::transformLinearAcceleration(
246-
AngularVelocity omega, AngularAcceleration alpha, Eigen::Vector2d a_imu)
256+
Eigen::Vector2d ImuService::transformLinearAcceleration(AngularVelocity omega,
257+
AngularAcceleration alpha,
258+
Eigen::Vector2d a_imu)
247259
{
248260
Eigen::Vector2d r(IMU_OFFSET_X, IMU_OFFSET_Y);
249261

0 commit comments

Comments
 (0)