mavlink simple analyzer remove <limits> usage

- <limits> isn't available in the NuttX c++ standard library
This commit is contained in:
Daniel Agar
2019-01-21 09:27:10 -05:00
parent 572d1cefe8
commit ce5fbc7751
3 changed files with 46 additions and 27 deletions
@@ -138,9 +138,7 @@ bool MavlinkStreamHighLatency2::send(const hrt_abstime t)
updated |= write_wind_estimate(&msg); updated |= write_wind_estimate(&msg);
if (updated) { if (updated) {
uint32_t timestamp; msg.timestamp = t / 1000;
convert_limit_safe(t / 1000, timestamp);
msg.timestamp = timestamp;
msg.type = _mavlink->get_system_type(); msg.type = _mavlink->get_system_type();
msg.autopilot = MAV_AUTOPILOT_PX4; 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); const bool updated = _global_pos_sub->update(&_global_pos_time, &global_pos);
if (_global_pos_time > 0) { if (_global_pos_time > 0) {
int32_t latitude, longitude; msg->latitude = global_pos.lat * 1e7;
convert_limit_safe(global_pos.lat * 1e7, latitude); msg->longitude = global_pos.lon * 1e7;
convert_limit_safe(global_pos.lon * 1e7, longitude);
msg->latitude = latitude;
msg->longitude = longitude;
int16_t altitude; int16_t altitude = 0;
if (global_pos.alt > 0) { if (global_pos.alt > 0) {
convert_limit_safe(global_pos.alt + 0.5f, altitude); convert_limit_safe(global_pos.alt + 0.5f, altitude);
@@ -146,3 +146,29 @@ void SimpleAnalyzer::int_round(float &x) const
x += 0.5f; 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);
}
}
+16 -18
View File
@@ -39,7 +39,8 @@
#pragma once #pragma once
#include <limits> #include <float.h>
#include <stdint.h>
/** /**
* SimpleAnalyzer * SimpleAnalyzer
@@ -115,14 +116,22 @@ public:
* @param[out] ret: The scaled and rounded value of the current analyzer result. * @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. * @parma[in] scalingfactor: The factor which is used to scale the result.
*/ */
template <typename T> void get_scaled(uint8_t &ret, float scalingfactor) const
void get_scaled(T &ret, float scalingfactor) const
{ {
float avg = get_scaled(scalingfactor); float avg = get_scaled(scalingfactor);
int_round(avg); 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: private:
@@ -135,16 +144,5 @@ private:
void int_round(float &x) const; void int_round(float &x) const;
}; };
template<typename Tin, typename Tout> void convert_limit_safe(float in, uint16_t &out);
void convert_limit_safe(Tin in, Tout &out) void convert_limit_safe(float in, int16_t &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);
}
}