mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-04-14 10:07:39 +08:00
mavlink simple analyzer remove <limits> usage
- <limits> isn't available in the NuttX c++ standard library
This commit is contained in:
parent
572d1cefe8
commit
ce5fbc7751
@ -138,9 +138,7 @@ bool MavlinkStreamHighLatency2::send(const hrt_abstime t)
|
||||
updated |= write_wind_estimate(&msg);
|
||||
|
||||
if (updated) {
|
||||
uint32_t timestamp;
|
||||
convert_limit_safe(t / 1000, timestamp);
|
||||
msg.timestamp = timestamp;
|
||||
msg.timestamp = t / 1000;
|
||||
|
||||
msg.type = _mavlink->get_system_type();
|
||||
msg.autopilot = MAV_AUTOPILOT_PX4;
|
||||
@ -317,13 +315,10 @@ bool MavlinkStreamHighLatency2::write_global_position(mavlink_high_latency2_t *m
|
||||
const bool updated = _global_pos_sub->update(&_global_pos_time, &global_pos);
|
||||
|
||||
if (_global_pos_time > 0) {
|
||||
int32_t latitude, longitude;
|
||||
convert_limit_safe(global_pos.lat * 1e7, latitude);
|
||||
convert_limit_safe(global_pos.lon * 1e7, longitude);
|
||||
msg->latitude = latitude;
|
||||
msg->longitude = longitude;
|
||||
msg->latitude = global_pos.lat * 1e7;
|
||||
msg->longitude = global_pos.lon * 1e7;
|
||||
|
||||
int16_t altitude;
|
||||
int16_t altitude = 0;
|
||||
|
||||
if (global_pos.alt > 0) {
|
||||
convert_limit_safe(global_pos.alt + 0.5f, altitude);
|
||||
|
||||
@ -146,3 +146,29 @@ void SimpleAnalyzer::int_round(float &x) const
|
||||
x += 0.5f;
|
||||
}
|
||||
}
|
||||
|
||||
void convert_limit_safe(float in, uint16_t &out)
|
||||
{
|
||||
if (in > UINT16_MAX) {
|
||||
out = UINT16_MAX;
|
||||
|
||||
} else if (in < 0) {
|
||||
out = 0;
|
||||
|
||||
} else {
|
||||
out = static_cast<uint16_t>(in);
|
||||
}
|
||||
}
|
||||
|
||||
void convert_limit_safe(float in, int16_t &out)
|
||||
{
|
||||
if (in > INT16_MAX) {
|
||||
out = INT16_MAX;
|
||||
|
||||
} else if (in < INT16_MIN) {
|
||||
out = INT16_MIN;
|
||||
|
||||
} else {
|
||||
out = static_cast<int16_t>(in);
|
||||
}
|
||||
}
|
||||
|
||||
@ -39,7 +39,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <limits>
|
||||
#include <float.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/**
|
||||
* SimpleAnalyzer
|
||||
@ -115,14 +116,22 @@ public:
|
||||
* @param[out] ret: The scaled and rounded value of the current analyzer result.
|
||||
* @parma[in] scalingfactor: The factor which is used to scale the result.
|
||||
*/
|
||||
template <typename T>
|
||||
void get_scaled(T &ret, float scalingfactor) const
|
||||
void get_scaled(uint8_t &ret, float scalingfactor) const
|
||||
{
|
||||
float avg = get_scaled(scalingfactor);
|
||||
int_round(avg);
|
||||
check_limits(avg, std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
|
||||
check_limits(avg, 0, UINT8_MAX);
|
||||
|
||||
ret = static_cast<T>(avg);
|
||||
ret = static_cast<uint8_t>(avg);
|
||||
}
|
||||
|
||||
void get_scaled(int8_t &ret, float scalingfactor) const
|
||||
{
|
||||
float avg = get_scaled(scalingfactor);
|
||||
int_round(avg);
|
||||
check_limits(avg, INT8_MIN, INT8_MAX);
|
||||
|
||||
ret = static_cast<int8_t>(avg);
|
||||
}
|
||||
|
||||
private:
|
||||
@ -135,16 +144,5 @@ private:
|
||||
void int_round(float &x) const;
|
||||
};
|
||||
|
||||
template<typename Tin, typename Tout>
|
||||
void convert_limit_safe(Tin in, Tout &out)
|
||||
{
|
||||
if (in > std::numeric_limits<Tout>::max()) {
|
||||
out = std::numeric_limits<Tout>::max();
|
||||
|
||||
} else if (in < std::numeric_limits<Tout>::min()) {
|
||||
out = std::numeric_limits<Tout>::min();
|
||||
|
||||
} else {
|
||||
out = static_cast<Tout>(in);
|
||||
}
|
||||
}
|
||||
void convert_limit_safe(float in, uint16_t &out);
|
||||
void convert_limit_safe(float in, int16_t &out);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user