Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: move position correction code to lib #8

Merged
merged 20 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
87d41c1
chore: set cpp17 version in CMakeLists
eduardacoppo Jan 23, 2025
ae7baa6
chore: move position correction code to lib
eduardacoppo Jan 23, 2025
f7a3fde
chore: make speed threshold configurable
eduardacoppo Jan 23, 2025
f770128
test: add tests and LOG_ERROR messages
eduardacoppo Jan 23, 2025
cbf14f3
fix: do not throw error if no correction
eduardacoppo Jan 27, 2025
defb0b6
chore: use getRad() when checking for nan
eduardacoppo Jan 28, 2025
4c8e3b4
chore: change variable name to match convention
eduardacoppo Jan 28, 2025
9d1e750
fix: ref position relative to the vessel's center
eduardacoppo Jan 28, 2025
8752944
chore: give speed thresh a clearer name
eduardacoppo Jan 29, 2025
abb2680
fix: remove static_cast for ref pos elements
eduardacoppo Jan 29, 2025
22bb4d3
fix: fix passing arguments,
eduardacoppo Jan 29, 2025
aa9795f
chore: change log type to debug instead of error
eduardacoppo Jan 29, 2025
d33ceec
chore: remove unnecessary suffix in sensor2vessel_pos
eduardacoppo Jan 30, 2025
bb37d99
fix/chore: fix passing arguments and refactor code
eduardacoppo Jan 30, 2025
926f3f7
fix: write more robust tests and add tests
eduardacoppo Feb 4, 2025
6bfcd04
chore: use snake_case for variables
eduardacoppo Feb 5, 2025
dd6fb6d
chore: propagate status
eduardacoppo Feb 5, 2025
c144c10
fix/chore: fix nomenclature and move transform code
eduardacoppo Feb 5, 2025
899444b
test: add checks for timestamp in the tests
eduardacoppo Feb 5, 2025
3541563
chore: minor fixes
eduardacoppo Feb 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# CMakeLists.txt has to be located in the project folder and cmake has to be
# executed from 'project/build' with 'cmake ../'.
cmake_minimum_required(VERSION 2.6)
SET (CMAKE_CXX_STANDARD 17)
find_package(Rock)
rock_init(nmea0183 0.1)
rock_standard_layout()
124 changes: 120 additions & 4 deletions src/AIS.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <base-logging/Logging.hpp>
#include <marnav/ais/ais.hpp>
#include <marnav/nmea/vdm.hpp>
#include <nmea0183/AIS.hpp>
Expand All @@ -8,6 +9,7 @@ using namespace marnav;
using namespace nmea0183;

double constexpr KNOTS_TO_MS = 0.514444;
double constexpr MIN_SPEED_FOR_VALID_COURSE = 0.2;

AIS::AIS(Driver& driver)
: mDriver(driver)
Expand Down Expand Up @@ -108,13 +110,18 @@ ais_base::VesselInformation AIS::getVesselInformation(ais::message_05 const& mes
string call_sign = message.get_callsign();
first_not_space = call_sign.find_last_not_of(" ");
info.call_sign = call_sign.substr(0, first_not_space + 1);
info.length = message.get_to_bow() + message.get_to_stern();
info.width = message.get_to_port() + message.get_to_starboard();
auto get_to_stern = message.get_to_stern();
auto get_to_starboard = message.get_to_starboard();
float length = message.get_to_bow() + get_to_stern;
float width = message.get_to_port() + get_to_starboard;
info.length = length;
info.width = width;
info.draft = static_cast<float>(message.get_draught()) / 10;
info.ship_type = static_cast<ais_base::ShipType>(message.get_shiptype());
info.epfd_fix = static_cast<ais_base::EPFDFixType>(message.get_epfd_fix());
info.reference_position =
Eigen::Vector3d(message.get_to_stern(), message.get_to_starboard(), 0);
info.reference_position = Eigen::Vector3d(get_to_stern - (length / 2.0),
get_to_starboard - (width / 2.0),
0);

info.ensureEnumsValid();
return info;
Expand All @@ -129,3 +136,112 @@ ais_base::VoyageInformation AIS::getVoyageInformation(ais::message_05 const& mes
info.destination = message.get_destination();
return info;
}

std::pair<Eigen::Quaterniond, ais_base::PositionCorrectionStatus> vesselToWorldOrientation(
base::Angle const& yaw,
base::Angle const& course_over_ground,
double speed_over_ground)
{
if (!std::isnan(yaw.getRad())) {
return {
Eigen::Quaterniond(Eigen::AngleAxisd(yaw.getRad(), Eigen::Vector3d::UnitZ())),
ais_base::PositionCorrectionStatus::POSITION_CENTERED_USING_HEADING};
}
else if (!std::isnan(course_over_ground.getRad()) &&
speed_over_ground >= MIN_SPEED_FOR_VALID_COURSE) {
return {Eigen::Quaterniond(Eigen::AngleAxisd(course_over_ground.getRad(),
Eigen::Vector3d::UnitZ())),
ais_base::PositionCorrectionStatus::POSITION_CENTERED_USING_COURSE};
}
else {
return {Eigen::Quaterniond::Identity(),
ais_base::PositionCorrectionStatus::POSITION_RAW};
}
}

base::Vector3d sensorToVesselInWorldPose(base::Vector3d const& sensor2vessel_pos,
Eigen::Quaterniond const& vessel2world_ori)
{
base::Vector3d sensor2vessel_in_world_pos;
sensor2vessel_in_world_pos = vessel2world_ori * sensor2vessel_pos;

return sensor2vessel_in_world_pos;
}

base::samples::RigidBodyState convertGPSToUTM(ais_base::Position const& position,
gps_base::UTMConverter const& utm_converter)
{
gps_base::Solution sensor2world_solution;
sensor2world_solution.latitude = position.latitude.getDeg();
sensor2world_solution.longitude = position.longitude.getDeg();

base::samples::RigidBodyState sensor2world_UTM;
sensor2world_UTM.position =
utm_converter.convertToUTM(sensor2world_solution).position;
sensor2world_UTM.time = base::Time::now();

return sensor2world_UTM;
}

std::pair<base::Angle, base::Angle> convertUTMToGPSInWorldFrame(
base::samples::RigidBodyState const& sensor2world_UTM,
base::Vector3d const& sensor2vessel_in_world_pos,
gps_base::UTMConverter const& utm_converter)
{
base::samples::RigidBodyState vessel2world_UTM;
vessel2world_UTM.position = sensor2world_UTM.position + sensor2vessel_in_world_pos;

gps_base::Solution vessel2world_GPS;
vessel2world_GPS = utm_converter.convertUTMToGPS(vessel2world_UTM);

return {base::Angle::fromDeg(vessel2world_GPS.latitude),
base::Angle::fromDeg(vessel2world_GPS.longitude)};
}

ais_base::Position AIS::applyPositionCorrection(
std::optional<ais_base::Position> position,
base::Vector3d const& sensor2vessel_pos,
gps_base::UTMConverter const& utm_converter)
{
if (!position.has_value()) {
std::string error_msg = "Position data is unavailable.";
LOG_ERROR_S << error_msg;
throw std::invalid_argument(error_msg);
}

auto position_value = position.value();

if (std::isnan(position_value.yaw.getRad()) &&
std::isnan(position_value.course_over_ground.getRad())) {
LOG_DEBUG_S << "Position can't be corrected because both 'yaw' "
"and 'course_over_ground' values are missing."
<< std::endl;
return position_value;
}

auto [vessel2world_ori, status] = vesselToWorldOrientation(position_value.yaw,
position_value.course_over_ground,
position_value.speed_over_ground);

if (status == ais_base::PositionCorrectionStatus::POSITION_RAW) {
LOG_DEBUG_S << "Position can't be corrected because 'yaw' value is missing and "
"'speed_over_ground' is below the threshold."
<< std::endl;
return position_value;
}

auto sensor2vessel_in_world_pos =
sensorToVesselInWorldPose(sensor2vessel_pos, vessel2world_ori);

auto [latitude, longitude] =
convertUTMToGPSInWorldFrame(convertGPSToUTM(position_value, utm_converter),
sensor2vessel_in_world_pos,
utm_converter);

ais_base::Position corrected_position;
corrected_position.latitude = latitude;
corrected_position.longitude = longitude;
corrected_position.correction_status = status;

return corrected_position;
}
22 changes: 22 additions & 0 deletions src/AIS.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
#include <marnav/ais/message_01.hpp>
#include <marnav/ais/message_05.hpp>

#include <base/samples/RigidBodyState.hpp>
#include <gps_base/BaseTypes.hpp>
#include <gps_base/UTMConverter.hpp>
#include <optional>

namespace nmea0183 {
class AIS {
uint32_t mDiscardedSentenceCount = 0;
Expand Down Expand Up @@ -44,6 +49,23 @@ namespace nmea0183 {
*/
uint32_t getDiscardedSentenceCount() const;

/**
* Applies position correction using the vessel reference position and the sensor
* offset
*
* @param position the AIS position to be corrected
* @param sensor2vessel_pos The position of the sensor relative to the
* vessel
* @param utm_converter The UTM converter
*
* @return The corrected AIS position with updated latitude, longitude, and
* correction status
*/
static ais_base::Position applyPositionCorrection(
std::optional<ais_base::Position> position,
base::Vector3d const& sensor2vessel_pos,
gps_base::UTMConverter const& utm_converter);

static ais_base::Position getPosition(marnav::ais::message_01 const& message);
static ais_base::VesselInformation getVesselInformation(
marnav::ais::message_05 const& message);
Expand Down
102 changes: 101 additions & 1 deletion test/test_AIS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ struct AISTest : public ::testing::Test, public iodrivers_base::Fixture<Driver>
uint8_t const* msg_u8 = reinterpret_cast<uint8_t const*>(msg.c_str());
pushDataToDriver(msg_u8, msg_u8 + msg.size());
}

gps_base::UTMConverter createUTMConverter()
{
gps_base::UTMConversionParameters parameters = {Eigen::Vector3d(1, 1, 0),
11,
true};
gps_base::UTMConverter converter(parameters);

return converter;
}
};

const std::vector<std::string> ais_strings = {
Expand Down Expand Up @@ -180,7 +190,7 @@ TEST_F(AISTest, it_converts_marnav_message05_into_a_VesselInformation)
ASSERT_EQ(ais_base::SHIP_TYPE_CARGO, info.ship_type);
ASSERT_EQ(15, info.length);
ASSERT_EQ(6, info.width);
ASSERT_EQ(base::Vector3d(10, 4, 0), info.reference_position);
ASSERT_EQ(base::Vector3d(2.5, 1, 0), info.reference_position);
ASSERT_EQ(ais_base::EPFD_COMBINED_GPS_GLONASS, info.epfd_fix);
ASSERT_NEAR(0.7, info.draft, 1e-2);
}
Expand Down Expand Up @@ -254,3 +264,93 @@ TEST_F(AISTest, it_converts_marnav_message05_into_a_VoyageInformation)
ASSERT_EQ(7890, info.imo);
ASSERT_EQ("DEST", info.destination);
}

TEST_F(AISTest, it_corrects_position_using_yaw)
{
ais::message_01 msg;
msg.set_latitude(geo::latitude(45));
msg.set_longitude(geo::longitude(-120));
msg.set_sog(0);
msg.set_hdg(90);
auto position = AIS::getPosition(msg);

base::Vector3d sensor2vessel_pos(100.0, 50.0, 0.0);
gps_base::UTMConverter utm_converter = createUTMConverter();

ais_base::Position corrected_position =
AIS::applyPositionCorrection(position, sensor2vessel_pos, utm_converter);

ASSERT_NEAR(corrected_position.latitude.getDeg(), 44.9991, 1e-4);
ASSERT_NEAR(corrected_position.longitude.getDeg(), -119.9993, 1e-4);
ASSERT_EQ(corrected_position.correction_status,
ais_base::PositionCorrectionStatus::POSITION_CENTERED_USING_HEADING);
}

TEST_F(AISTest, it_corrects_position_using_cog)
{
ais::message_01 msg;
msg.set_latitude(geo::latitude(45));
msg.set_longitude(geo::longitude(-120));
msg.set_sog(0.5);
msg.set_cog(90);
auto position = AIS::getPosition(msg);

base::Vector3d sensor2vessel_pos(100.0, 50.0, 0.0);
gps_base::UTMConverter utm_converter = createUTMConverter();

ais_base::Position corrected_position =
AIS::applyPositionCorrection(position, sensor2vessel_pos, utm_converter);

ASSERT_NEAR(corrected_position.latitude.getDeg(), 44.9991, 1e-4);
ASSERT_NEAR(corrected_position.longitude.getDeg(), -119.9993, 1e-4);
ASSERT_EQ(corrected_position.correction_status,
ais_base::PositionCorrectionStatus::POSITION_CENTERED_USING_COURSE);
}

TEST_F(AISTest, it_does_no_correction_if_both_yaw_and_cog_are_missing)
{
ais::message_01 msg;
msg.set_latitude(geo::latitude(45));
msg.set_longitude(geo::longitude(-120));
msg.set_sog(0);
auto position = AIS::getPosition(msg);

base::Vector3d sensor2vessel_pos(100.0, 50.0, 0.0);
gps_base::UTMConverter utm_converter = createUTMConverter();

ais_base::Position corrected_position =
AIS::applyPositionCorrection(position, sensor2vessel_pos, utm_converter);
ASSERT_EQ(corrected_position.correction_status,
ais_base::PositionCorrectionStatus::POSITION_RAW);
}

TEST_F(AISTest, it_does_no_correction_if_yaw_is_missing_and_sog_is_below_threshold)
{
ais::message_01 msg;
msg.set_latitude(geo::latitude(45));
msg.set_longitude(geo::longitude(-120));
msg.set_sog(0.1);
msg.set_cog(90);
auto position = AIS::getPosition(msg);

base::Vector3d sensor2vessel_pos(100.0, 50.0, 0.0);
gps_base::UTMConverter utm_converter = createUTMConverter();

ais_base::Position corrected_position =
AIS::applyPositionCorrection(position, sensor2vessel_pos, utm_converter);
ASSERT_EQ(corrected_position.correction_status,
ais_base::PositionCorrectionStatus::POSITION_RAW);
}

TEST_F(AISTest, it_throws_runtime_error_if_position_has_no_value)
{
base::Vector3d sensor2vessel_pos(100.0, 50.0, 0.0);
gps_base::UTMConverter utm_converter = createUTMConverter();

try {
AIS::applyPositionCorrection({}, sensor2vessel_pos, utm_converter);
}
catch (std::invalid_argument const& e) {
ASSERT_STREQ(e.what(), "Position data is unavailable.");
}
}