nmealib 0.0.4
NMEA 0183/NMEA 2000 parsing library
Loading...
Searching...
No Matches
PGN127257.cpp
Go to the documentation of this file.
2
4
5namespace nmealib {
6namespace nmea2000 {
7
8std::unique_ptr<PGN127257> PGN127257::create(std::unique_ptr<Message2000> baseMessage) {
9 if (baseMessage->getCanFrameLength() != 8) {
10 std::string context = "PGN127257::create()";
11 NMEALIB_RETURN_ERROR(InvalidCanFrameException(context, "CAN frame must be 8 bytes for PGN127257"));
12 }
13
14 const uint8_t sequenceId = baseMessage->getCanFrame()[0];
15 const SignedAngle yaw = SignedAngle::fromRaw(static_cast<uint16_t>(baseMessage->getCanFrame()[1]) |
16 (static_cast<uint16_t>(baseMessage->getCanFrame()[2]) << 8));
17 const SignedAngle pitch = SignedAngle::fromRaw(static_cast<uint16_t>(baseMessage->getCanFrame()[3]) |
18 (static_cast<uint16_t>(baseMessage->getCanFrame()[4]) << 8));
19 const SignedAngle roll = SignedAngle::fromRaw(static_cast<uint16_t>(baseMessage->getCanFrame()[5]) |
20 (static_cast<uint16_t>(baseMessage->getCanFrame()[6]) << 8));
21 const Byte reserved = Byte::fromRaw(baseMessage->getCanFrame()[7]);
22
23 return std::unique_ptr<PGN127257>(new PGN127257(std::move(*baseMessage),
24 sequenceId,
25 yaw,
26 pitch,
27 roll,
28 reserved));
29}
30
31PGN127257::PGN127257(Message2000 baseMessage,
32 uint8_t sequenceId,
33 SignedAngle yaw,
34 SignedAngle pitch,
35 SignedAngle roll,
36 Byte reserved) noexcept :
37 Message2000(std::move(baseMessage)),
38 sequenceId_(sequenceId),
39 yaw_(yaw),
40 pitch_(pitch),
41 roll_(roll),
42 reserved_(reserved) {}
43
44PGN127257::PGN127257(uint8_t sequenceId,
45 SignedAngle yaw,
46 SignedAngle pitch,
47 SignedAngle roll,
48 Byte reserved) noexcept :
49 Message2000(*Message2000::create(rawPayload(sequenceId,
50 yaw,
51 pitch,
52 roll,
53 reserved))),
54 sequenceId_(sequenceId),
55 yaw_(yaw),
56 pitch_(pitch),
57 roll_(roll),
58 reserved_(reserved) {}
59
60std::unique_ptr<Message> PGN127257::clone() const {
61 return std::unique_ptr<PGN127257>(new PGN127257(*this));
62}
63
64std::string PGN127257::getStringContent(bool verbose) const noexcept {
65 std::ostringstream oss;
66
67 if (verbose) {
68 oss << this->toString(true);
69 oss << "\n";
70 oss << "Fields:\n";
71 oss << "\tSequence ID: " << static_cast<int>(sequenceId_) << "\n";
72 oss << "\tYaw: " << yaw_.toString() << "rad, " << getYawDegrees() << "°\n";
73 oss << "\tPitch: " << pitch_.toString() << "rad, " << getPitchDegrees() << "°\n";
74 oss << "\tRoll: " << roll_.toString() << "rad, " << getRollDegrees() << "°\n";
75 } else {
76 oss << this->toString(false);
77 oss << "SeqID=" << static_cast<int>(sequenceId_)
78 << " Yaw=" << getYawDegrees() << "°"
79 << " Pitch=" << getPitchDegrees() << "°"
80 << " Roll=" << getRollDegrees() << "°";
81 }
82
83 return oss.str();
84}
85
86std::string PGN127257::rawPayload(uint8_t sequenceId,
87 SignedAngle yaw,
88 SignedAngle pitch,
89 SignedAngle roll,
90 Byte reserved) {
91 std::vector<uint8_t> canFrame(8, 0);
92 canFrame[0] = sequenceId;
93 canFrame[1] = static_cast<uint8_t>(yaw.getRaw() & 0xFFU);
94 canFrame[2] = static_cast<uint8_t>((yaw.getRaw() >> 8) & 0xFFU);
95 canFrame[3] = static_cast<uint8_t>(pitch.getRaw() & 0xFFU);
96 canFrame[4] = static_cast<uint8_t>((pitch.getRaw() >> 8) & 0xFFU);
97 canFrame[5] = static_cast<uint8_t>(roll.getRaw() & 0xFFU);
98 canFrame[6] = static_cast<uint8_t>((roll.getRaw() >> 8) & 0xFFU);
99 canFrame[7] = reserved.getRaw();
100
101 const uint32_t canId = (127257U << 8U);
102 std::ostringstream oss;
103 oss << std::hex << std::uppercase << std::setfill('0');
104 oss << std::setw(8) << canId << ":";
105 for (uint8_t b : canFrame) {
106 oss << std::setw(2) << static_cast<int>(b);
107 }
108 return oss.str();
109}
110
111uint8_t PGN127257::getSequenceId() const noexcept {
112 return sequenceId_;
113}
114
116 return yaw_;
117}
118
120 return pitch_;
121}
122
124 return roll_;
125}
126
127Byte PGN127257::getReserved() const noexcept {
128 return reserved_;
129}
130
131float PGN127257::getYawDegrees() const noexcept {
132 return yaw_.getValue() * 180.0f / static_cast<float>(M_PI);
133}
134
135float PGN127257::getPitchDegrees() const noexcept {
136 return pitch_.getValue() * 180.0f / static_cast<float>(M_PI);
137}
138
139float PGN127257::getRollDegrees() const noexcept {
140 return roll_.getValue() * 180.0f / static_cast<float>(M_PI);
141}
142
143bool PGN127257::operator==(const PGN127257& other) const noexcept {
144 return Message2000::operator==(other) &&
145 sequenceId_ == other.sequenceId_ &&
146 yaw_ == other.yaw_ &&
147 pitch_ == other.pitch_ &&
148 roll_ == other.roll_ &&
149 reserved_ == other.reserved_;
150}
151
152} // namespace nmea2000
153} // namespace nmealib
RawType getRaw() const noexcept
Converts physical value to raw integer representation.
Definition dataTypes.h:128
static constexpr DataType fromRaw(RawType raw) noexcept
Constructs from a raw value.
Definition dataTypes.h:97
constexpr TargetType getValue() const noexcept
Returns the normalized physical value.
Definition dataTypes.h:113
Represents a generic NMEA 2000 message encapsulating a CAN frame.
Definition nmea2000.h:62
static std::unique_ptr< Message2000 > create(std::string raw, TimePoint ts=std::chrono::system_clock::now())
Protected factory — parses "CANID:data" (and variant formats) into a Message2000.
Definition nmea2000.cpp:109
virtual bool operator==(const Message2000 &other) const noexcept
Definition nmea2000.cpp:401
Strongly-typed class representing PGN 127257 - Attitude.
Definition PGN127257.h:26
Byte getReserved() const noexcept
float getPitchDegrees() const noexcept
std::unique_ptr< nmealib::Message > clone() const override
Creates a polymorphic deep copy of this Message2000.
Definition PGN127257.cpp:60
float getRollDegrees() const noexcept
PGN127257(uint8_t sequenceId, SignedAngle yaw, SignedAngle pitch, SignedAngle roll, Byte reserved=Byte::fromValue(0U)) noexcept
Definition PGN127257.cpp:44
std::string getStringContent(bool verbose) const noexcept override
Returns a human-readable string representation of the message.
Definition PGN127257.cpp:64
SignedAngle getRoll() const noexcept
float getYawDegrees() const noexcept
SignedAngle getPitch() const noexcept
uint8_t getSequenceId() const noexcept
bool operator==(const PGN127257 &other) const noexcept
SignedAngle getYaw() const noexcept
#define NMEALIB_RETURN_ERROR(exceptionExpr)
DataType< SignedAngleTraits > SignedAngle
Custom type representing signed angles in radians.
Definition dataTypes.h:356
DataType< ByteTraits > Byte
Custom type representing 8 bits of data (0-255).
Definition dataTypes.h:330