Skip to content

Commit 2b699b4

Browse files
authored
fix: Ensure single-precision float operations in ads7138, as5600, bldc_haptics, bldc_motor, encoder, filters, mt6701, and tla2528 components (#531)
* fix: Ensure single-precision float operations in `ads7138`, `as5600`, `bldc_haptics`, `bldc_motor`, `encoder`, `filters`, `mt6701`, and `tla2528` components * ensure float versions of functions are used
1 parent ad75c33 commit 2b699b4

File tree

21 files changed

+92
-89
lines changed

21 files changed

+92
-89
lines changed

components/ads7138/include/ads7138.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,7 @@ class Ads7138 : public BasePeripheral<> {
12401240
// can represent avdd_mv_ volts with 65536 values
12411241
// therefore, each value represents avdd_mv_ / 65536 volts.
12421242
// multiply by 1000 to get mV
1243-
return static_cast<float>(raw) * avdd_mv_ / 65536.0;
1243+
return static_cast<float>(raw) * avdd_mv_ / 65536.0f;
12441244
}
12451245

12461246
/**

components/as5600/include/as5600.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class As5600 : public BasePeripheral<> {
4444
static constexpr float COUNTS_PER_REVOLUTION_F =
4545
16384.0f; ///< Float number of counts per revolution for the magnetic encoder.
4646
static constexpr float COUNTS_TO_RADIANS =
47-
2.0f * M_PI /
47+
2.0f * (float)(M_PI) /
4848
COUNTS_PER_REVOLUTION_F; ///< Conversion factor to convert from count value to radians.
4949
static constexpr float COUNTS_TO_DEGREES =
5050
360.0f /

components/bldc_haptics/include/bldc_haptics.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ template <MotorConcept M> class BldcHaptics : public BaseComponent {
223223
// small for the P factor to work well.
224224
const float derivative_lower_strength = config.detent_strength * kd_factor_max_;
225225
const float derivative_upper_strength = config.detent_strength * kd_factor_min_;
226-
const float derivative_position_width_lower = 3.0f * M_PI / 180.0f; // radians(3);
227-
const float derivative_position_width_upper = 8.0f * M_PI / 180.0f; // radians(8);
226+
const float derivative_position_width_lower = 3.0f * (float)(M_PI) / 180.0f; // radians(3);
227+
const float derivative_position_width_upper = 8.0f * (float)(M_PI) / 180.0f; // radians(8);
228228
const float raw = derivative_lower_strength +
229229
(derivative_upper_strength - derivative_lower_strength) /
230230
(derivative_position_width_upper - derivative_position_width_lower) *

components/bldc_haptics/include/detent_config.hpp

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ struct DetentConfig {
1818
///< detents will be used.
1919
float detent_strength{1}; ///< Strength of the detents
2020
float end_strength{1}; ///< Strength of the end detents
21-
float snap_point{1.1}; ///< Position of the snap point, in radians, should be >= 0.5 for stability
21+
float snap_point{
22+
1.1f}; ///< Position of the snap point, in radians, should be >= 0.5 for stability
2223
float snap_point_bias{0}; ///< Bias for the snap point, in radians, should be >= 0 for stability
2324
float dead_zone_percent{0.2f}; ///< Percent of the dead zone to use for the detent
2425
float dead_zone_abs_max_radians{
25-
M_PI / 180.0f}; ///< Absolute maximum of the dead zone to use for the detent in radians
26+
(float)(M_PI) /
27+
180.0f}; ///< Absolute maximum of the dead zone to use for the detent in radians
2628
};
2729

2830
/// @brief Equality operator for DetentConfig
@@ -41,107 +43,107 @@ inline bool operator==(const DetentConfig &lhs, const DetentConfig &rhs) {
4143

4244
/// @brief Unbounded motion, no detents
4345
static const DetentConfig UNBOUNDED_NO_DETENTS = {
44-
.position_width = 10.0 * M_PI / 180.0,
46+
.position_width = 10.0f * (float)(M_PI) / 180.0f,
4547
.min_position = 0,
4648
.max_position = -1, // max < min indicates no bounds
4749
.detent_strength = 0,
4850
.end_strength = 1,
49-
.snap_point = 1.1,
51+
.snap_point = 1.1f,
5052
.snap_point_bias = 0,
5153
};
5254

5355
/// @brief Bounded motion, no detents
5456
static const DetentConfig BOUNDED_NO_DETENTS = {
55-
.position_width = 10.0 * M_PI / 180.0,
57+
.position_width = 10.0f * (float)(M_PI) / 180.0f,
5658
.min_position = 0,
5759
.max_position = 10,
5860
.detent_strength = 0,
5961
.end_strength = 1,
60-
.snap_point = 1.1,
62+
.snap_point = 1.1f,
6163
.snap_point_bias = 0,
6264
};
6365

6466
/// @brief Bounded motion with multiple revolutions, no detents, with end stops
6567
static const DetentConfig MULTI_REV_NO_DETENTS = {
66-
.position_width = 10.0 * M_PI / 180.0,
68+
.position_width = 10.0f * (float)(M_PI) / 180.0f,
6769
.min_position = 0,
6870
.max_position = 72,
6971
.detent_strength = 0,
7072
.end_strength = 1,
71-
.snap_point = 1.1,
73+
.snap_point = 1.1f,
7274
.snap_point_bias = 0,
7375
};
7476

7577
/// @brief On-off with strong detents
7678
static const DetentConfig ON_OFF_STRONG_DETENTS = {
77-
.position_width = 60.0 * M_PI / 180.0,
79+
.position_width = 60.0f * (float)(M_PI) / 180.0f,
7880
.min_position = 0,
7981
.max_position = 1,
8082
.detent_strength = 1,
8183
.end_strength = 1,
82-
.snap_point = 0.55, // Note the snap point is slightly past the midpoint (0.5); compare to
83-
// normal detents which use a snap point *past* the next value (i.e. > 1)
84+
.snap_point = 0.55f, // Note the snap point is slightly past the midpoint (0.5); compare to
85+
// normal detents which use a snap point *past* the next value (i.e. > 1)
8486
.snap_point_bias = 0,
8587
};
8688

8789
/// @brief Bounded motion with strong position detents spaced 9 degrees apart
8890
/// (coarse), with end stops
8991
static const DetentConfig COARSE_VALUES_STRONG_DETENTS = {
90-
.position_width = 8.225f * M_PI / 180.0,
92+
.position_width = 8.225f * (float)(M_PI) / 180.0f,
9193
.min_position = 0,
9294
.max_position = 31,
9395
.detent_strength = 2,
9496
.end_strength = 1,
95-
.snap_point = 1.1,
97+
.snap_point = 1.1f,
9698
.snap_point_bias = 0,
9799
};
98100

99101
/// @brief Bounded motion with no detents spaced 1 degree apart (fine), with end
100102
/// stops
101103
static const DetentConfig FINE_VALUES_NO_DETENTS = {
102-
.position_width = 1.0f * M_PI / 180.0,
104+
.position_width = 1.0f * (float)(M_PI) / 180.0f,
103105
.min_position = 0,
104106
.max_position = 255,
105107
.detent_strength = 0,
106108
.end_strength = 1,
107-
.snap_point = 1.1,
109+
.snap_point = 1.1f,
108110
.snap_point_bias = 0,
109111
};
110112

111113
/// @brief Bounded motion with position detents spaced 1 degree apart (fine),
112114
/// with end stops
113115
static const DetentConfig FINE_VALUES_WITH_DETENTS = {
114-
.position_width = 1.0f * M_PI / 180.0,
116+
.position_width = 1.0f * (float)(M_PI) / 180.0f,
115117
.min_position = 0,
116118
.max_position = 255,
117119
.detent_strength = 1,
118120
.end_strength = 1,
119-
.snap_point = 1.1,
121+
.snap_point = 1.1f,
120122
.snap_point_bias = 0,
121123
};
122124

123125
/// @brief Bounded motion with position detents, end stops, and explicit
124126
/// magnetic detents.
125127
static const DetentConfig MAGNETIC_DETENTS = {
126-
.position_width = 7.0 * M_PI / 180.0,
128+
.position_width = 7.0f * (float)(M_PI) / 180.0f,
127129
.min_position = 0,
128130
.max_position = 31,
129131
.detent_positions = {2, 10, 21, 22},
130-
.detent_strength = 2.5,
132+
.detent_strength = 2.5f,
131133
.end_strength = 1,
132-
.snap_point = 0.7,
134+
.snap_point = 0.7f,
133135
.snap_point_bias = 0,
134136
};
135137

136138
/// @brief Bounded motion for a return to center rotary encoder with positions
137139
static const DetentConfig RETURN_TO_CENTER_WITH_DETENTS = {
138-
.position_width = 60.0 * M_PI / 180.0,
140+
.position_width = 60.0f * (float)(M_PI) / 180.0f,
139141
.min_position = -6,
140142
.max_position = 6,
141143
.detent_strength = 1,
142144
.end_strength = 1,
143-
.snap_point = 0.55,
144-
.snap_point_bias = 0.4,
145+
.snap_point = 0.55f,
146+
.snap_point_bias = 0.4f,
145147
};
146148

147149
} // namespace espp::detail
@@ -155,23 +157,23 @@ template <> struct fmt::formatter<espp::detail::DetentConfig> {
155157

156158
template <typename FormatContext>
157159
auto format(espp::detail::DetentConfig const &detent_config, FormatContext &ctx) const {
158-
return fmt::format_to(ctx.out(),
159-
"DetentConfig:\n"
160-
"\tposition_width: {} radians ({} degrees)\n"
161-
"\tmin_position: {}\n"
162-
"\tmax_position: {}\n"
163-
"\trange of motion: {:.2f} to {:.2f} degrees\n"
164-
"\tdetent_positions: {}\n"
165-
"\tdetent_strength: {}\n"
166-
"\tend_strength: {}\n"
167-
"\tsnap_point: {}\n"
168-
"\tsnap_point_bias: {}",
169-
detent_config.position_width, detent_config.position_width * 180.0 / M_PI,
170-
detent_config.min_position, detent_config.max_position,
171-
detent_config.min_position * detent_config.position_width * 180.0 / M_PI,
172-
detent_config.max_position * detent_config.position_width * 180.0 / M_PI,
173-
detent_config.detent_positions, detent_config.detent_strength,
174-
detent_config.end_strength, detent_config.snap_point,
175-
detent_config.snap_point_bias);
160+
return fmt::format_to(
161+
ctx.out(),
162+
"DetentConfig:\n"
163+
"\tposition_width: {} radians ({} degrees)\n"
164+
"\tmin_position: {}\n"
165+
"\tmax_position: {}\n"
166+
"\trange of motion: {:.2f} to {:.2f} degrees\n"
167+
"\tdetent_positions: {}\n"
168+
"\tdetent_strength: {}\n"
169+
"\tend_strength: {}\n"
170+
"\tsnap_point: {}\n"
171+
"\tsnap_point_bias: {}",
172+
detent_config.position_width, detent_config.position_width * 180.0f / (float)(M_PI),
173+
detent_config.min_position, detent_config.max_position,
174+
detent_config.min_position * detent_config.position_width * 180.0f / (float)(M_PI),
175+
detent_config.max_position * detent_config.position_width * 180.0f / (float)(M_PI),
176+
detent_config.detent_positions, detent_config.detent_strength, detent_config.end_strength,
177+
detent_config.snap_point, detent_config.snap_point_bias);
176178
}
177179
};

components/bldc_motor/include/bldc_motor.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,15 +412,15 @@ class BldcMotor : public BaseComponent {
412412
Uout = 1.0f / (fast_inv_sqrt(ud * ud + uq * uq) * driver_->get_voltage_limit());
413413
// angle normalisation in between 0 and 2pi
414414
// only necessary if using fast_sin and fast_cos - approximation functions
415-
el_angle = normalize_angle(el_angle + atan2(uq, ud));
415+
el_angle = normalize_angle(el_angle + atan2f(uq, ud));
416416
} else { // only uq available - no need for atan2 and sqrt
417417
Uout = uq / driver_->get_voltage_limit();
418418
// angle normalisation in between 0 and 2pi
419419
// only necessary if using fast_sin and fast_cos - approximation functions
420-
el_angle = normalize_angle(el_angle + M_PI_2);
420+
el_angle = normalize_angle(el_angle + (float)(M_PI_2));
421421
}
422422
// find the sector we are in currently
423-
sector = floor(el_angle / _PI_3) + 1;
423+
sector = floorf(el_angle / _PI_3) + 1;
424424
// calculate the duty cycles
425425
float T1 = _SQRT3 * fast_sin(sector * _PI_3 - el_angle) * Uout;
426426
float T2 = _SQRT3 * fast_sin(el_angle - (sector - 1.0f) * _PI_3) * Uout;

components/bldc_motor/include/foc_utils.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ namespace espp {
66
/**
77
* @brief Conversion factor to go from rotations/minute to radians/second.
88
*/
9-
static constexpr float RPM_TO_RADS = (2.0f * M_PI) / 60.0f;
9+
static constexpr float RPM_TO_RADS = (2.0f * (float)(M_PI)) / 60.0f;
1010

1111
/**
1212
* @brief Conversion factor to go from radians/second to rotations/minute.
1313
*/
14-
static constexpr float RADS_TO_RPM = 60.0f / (2.0f * M_PI);
14+
static constexpr float RADS_TO_RPM = 60.0f / (2.0f * (float)(M_PI));
1515

1616
static constexpr float _2_SQRT3 = 1.15470053838f;
1717
static constexpr float _SQRT3 = 1.73205080757f;

components/bmi270/include/bmi270.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,9 @@ class Bmi270 : public espp::BasePeripheral<uint8_t, Interface == bmi270::Interfa
327327
orientation_ = orientation_filter_(dt, accel, gyro);
328328
// Calculate gravity vector from orientation
329329
gravity_vector_ = {
330-
static_cast<float>(sin(orientation_.pitch)),
331-
static_cast<float>(-sin(orientation_.roll) * cos(orientation_.pitch)),
332-
static_cast<float>(-cos(orientation_.roll) * cos(orientation_.pitch)),
330+
static_cast<float>(sinf(orientation_.pitch)),
331+
static_cast<float>(-sinf(orientation_.roll) * cosf(orientation_.pitch)),
332+
static_cast<float>(-cosf(orientation_.roll) * cosf(orientation_.pitch)),
333333
};
334334
}
335335

components/encoder/include/abi_encoder.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ template <EncoderType T = EncoderType::ROTATIONAL> class AbiEncoder : public Bas
119119
* @return Number of radians, as a floating point number.
120120
*/
121121
float get_radians() requires(T == EncoderType::ROTATIONAL) {
122-
return get_revolutions() * 2.0f * M_PI;
122+
return get_revolutions() * 2.0f * (float)(M_PI);
123123
}
124124

125125
/**

components/file_system/src/file_system.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ std::string FileSystem::human_readable(size_t bytes) {
8181
float mantissa = bytes;
8282
for (; mantissa >= 1024.0f; mantissa /= 1024.0f, ++i) {
8383
}
84-
mantissa = std::ceilf(mantissa * 10.0f) / 10.0f;
84+
mantissa = ceilf(mantissa * 10.0f) / 10.0f;
8585
return i == 0 ? fmt::format("{}", bytes) : fmt::format("{:.3f}{}", mantissa, "BKMGTPE"[i]);
8686
}
8787

components/filters/example/main/filters_example.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ extern "C" void app_main(void) {
4848
float seconds = std::chrono::duration<float>(now - start).count();
4949
// use time to create a stairstep function
5050
constexpr float noise_scale = 0.2f;
51-
float input = floor(seconds) + get_random() * noise_scale;
51+
float input = floorf(seconds) + get_random() * noise_scale;
5252
fmt::print("{:.03f}, {:.03f}, {:.03f}, {:.03f}, "
5353
"{:.03f}, {:.03f}, {:.03f}, {:.03f}\n",
5454
seconds, input, slpf.update(input), lpf.update(input), bwf_df1_o1.update(input),

0 commit comments

Comments
 (0)