From 13abb620f6bd9ac8b9a4889f774d84969ccd2bf0 Mon Sep 17 00:00:00 2001 From: euquiq <31453004+euquiq@users.noreply.github.com> Date: Mon, 24 Aug 2020 17:31:27 -0300 Subject: [PATCH] Radiosonde-vaisala-add-temp-humidity Also added the fields "DateTime" which just shows the raw timestamp that portapack assigned the last packet received, in the format: YYYYMMDDHHMMSS ... And "Frame" which shows the packet # (or frame) for correlating with other software / verify that there are new packets being received. Also moved a string function for returning rounded-up decimals, originally inside the whipcalc tool app, into the string_format functions library, because I used that function on TEMP and HUMIDITY values inisde the radiosonde app. Finally, the whole UI has its widgets moved a bit, giving space for these new parameters. --- firmware/application/apps/ui_sonde.cpp | 70 +++-- firmware/application/apps/ui_sonde.hpp | 70 ++++- firmware/application/apps/ui_whipcalc.cpp | 13 - firmware/application/apps/ui_whipcalc.hpp | 1 - firmware/application/string_format.cpp | 10 + firmware/application/string_format.hpp | 2 +- firmware/common/sonde_packet.cpp | 361 +++++++++++++++++----- firmware/common/sonde_packet.hpp | 16 +- 8 files changed, 409 insertions(+), 134 deletions(-) diff --git a/firmware/application/apps/ui_sonde.cpp b/firmware/application/apps/ui_sonde.cpp index e61139ac..66e3c576 100644 --- a/firmware/application/apps/ui_sonde.cpp +++ b/firmware/application/apps/ui_sonde.cpp @@ -24,9 +24,14 @@ #include "baseband_api.hpp" #include "portapack.hpp" +#include +#include + using namespace portapack; #include "string_format.hpp" +#include "complex.hpp" + void SondeLogger::on_packet(const sonde::Packet& packet) { const auto formatted = packet.symbols_formatted(); @@ -46,9 +51,14 @@ SondeView::SondeView(NavigationView& nav) { &field_vga, &rssi, &check_log, + &check_crc, &text_signature, &text_serial, + &text_timestamp, &text_voltage, + &text_frame, + &text_temp, + &text_humid, &geopos, &button_see_map }); @@ -73,6 +83,10 @@ SondeView::SondeView(NavigationView& nav) { check_log.on_select = [this](Checkbox&, bool v) { logging = v; }; + + check_crc.on_select = [this](Checkbox&, bool v) { + use_crc = v; + }; radio::enable({ tuning_frequency(), @@ -109,26 +123,46 @@ void SondeView::focus() { field_vga.focus(); } -void SondeView::on_packet(const sonde::Packet& packet) { - //const auto hex_formatted = packet.symbols_formatted(); - - text_signature.set(packet.type_string()); - sonde_id = packet.serial_number(); //used also as tag on the geomap - text_serial.set(sonde_id); - text_voltage.set(unit_auto_scale(packet.battery_voltage(), 2, 3) + "V"); +void SondeView::on_packet(const sonde::Packet &packet) +{ + if (!use_crc || packet.crc_ok()) //euquiq: Reject bad packet if crc is on + { + text_signature.set(packet.type_string()); - gps_info = packet.get_GPS_data(); - - geopos.set_altitude(gps_info.alt); - geopos.set_lat(gps_info.lat); - geopos.set_lon(gps_info.lon); - - if (logger && logging) { - logger->on_packet(packet); + sonde_id = packet.serial_number(); //used also as tag on the geomap + text_serial.set(sonde_id); + + text_timestamp.set(to_string_timestamp(packet.received_at())); + + text_voltage.set(unit_auto_scale(packet.battery_voltage(), 2, 2) + "V"); + + text_frame.set(to_string_dec_uint(packet.frame(),0)); //euquiq: integrate frame #, temp & humid. + + temp_humid_info = packet.get_temp_humid(); + if (temp_humid_info.humid != 0) + { + double decimals = abs(get_decimals(temp_humid_info.humid, 10, true)); + //if (decimals < 0) + // decimals = -decimals; + text_humid.set(to_string_dec_int((int)temp_humid_info.humid) + "." + to_string_dec_uint(decimals, 1) + "%"); + } + + if (temp_humid_info.temp != 0) + { + double decimals = abs(get_decimals(temp_humid_info.temp, 10, true)); + // if (decimals < 0) + // decimals = -decimals; + text_temp.set(to_string_dec_int((int)temp_humid_info.temp) + "." + to_string_dec_uint(decimals, 1) + "C"); + } + + gps_info = packet.get_GPS_data(); + geopos.set_altitude(gps_info.alt); + geopos.set_lat(gps_info.lat); + geopos.set_lon(gps_info.lon); + + if (logger && logging) + logger->on_packet(packet); } - - /*if( packet.crc_ok() ) { - }*/ } void SondeView::set_target_frequency(const uint32_t new_value) { diff --git a/firmware/application/apps/ui_sonde.hpp b/firmware/application/apps/ui_sonde.hpp index 9e7743b1..d7da2443 100644 --- a/firmware/application/apps/ui_sonde.hpp +++ b/firmware/application/apps/ui_sonde.hpp @@ -67,18 +67,26 @@ private: std::unique_ptr logger { }; uint32_t target_frequency_ { 402700000 }; bool logging { false }; + bool use_crc { false }; sonde::GPS_data gps_info; + sonde::temp_humid temp_humid_info; std::string sonde_id; Labels labels { - { { 0 * 8, 2 * 16 }, "Signature:", Color::light_grey() }, - { { 3 * 8, 3 * 16 }, "Serial:", Color::light_grey() }, - { { 4 * 8, 4 * 16 }, "Vbatt:", Color::light_grey() } + { { 4 * 8, 2 * 16 }, "Type:", Color::light_grey() }, + { { 6 * 8, 3 * 16 }, "ID:", Color::light_grey() }, + { { 0 * 8, 4 * 16 }, "DateTime:", Color::light_grey() }, + + { { 3 * 8, 5 * 16 }, "Vbatt:", Color::light_grey() }, + { { 3 * 8, 6 * 16 }, "Frame:", Color::light_grey() }, + { { 4 * 8, 7 * 16 }, "Temp:", Color::light_grey() }, + { { 0 * 8, 8 * 16 }, "Humidity:", Color::light_grey() } }; FrequencyField field_frequency { { 0 * 8, 0 * 8 }, }; + RFAmpField field_rf_amp { { 13 * 8, 0 * 16 } }; @@ -96,31 +104,59 @@ private: }; Checkbox check_log { - { 22 * 8, 3 * 16 }, + { 23 * 8, 6 * 16 }, 3, "Log" }; - Text text_signature { - { 10 * 8, 2 * 16, 10 * 8, 16 }, - "..." - }; - Text text_serial { - { 10 * 8, 3 * 16, 11 * 8, 16 }, - "..." - }; - Text text_voltage { - { 10 * 8, 4 * 16, 10 * 8, 16 }, - "..." + Checkbox check_crc { + { 23 * 8, 8 * 16 }, + 3, + "CRC" }; + Text text_signature { + { 9 * 8, 2 * 16, 10 * 8, 16 }, + "..." + }; + + Text text_serial { + { 9 * 8, 3 * 16, 11 * 8, 16 }, + "..." + }; + + Text text_timestamp { + { 9 * 8, 4 * 16, 11 * 8, 16 }, + "..." + }; + + Text text_voltage { + { 9 * 8, 5 * 16, 10 * 8, 16 }, + "..." + }; + + Text text_frame { + { 9 * 8, 6 * 16, 10 * 8, 16 }, + "..." + }; + + Text text_temp { + { 9 * 8, 7 * 16, 10 * 8, 16 }, + "..." + }; + + Text text_humid { + { 9 * 8, 8 * 16, 10 * 8, 16 }, + "..." + }; + GeoPos geopos { - { 0, 6 * 16 }, + { 0, 12 * 16 }, GeoPos::alt_unit::METERS }; Button button_see_map { - { 8 * 8, 10 * 16, 14 * 8, 3 * 16 }, + { 8 * 8, 16 * 16, 14 * 8, 3 * 16 }, "See on map" }; diff --git a/firmware/application/apps/ui_whipcalc.cpp b/firmware/application/apps/ui_whipcalc.cpp index c99f21a7..70c01213 100644 --- a/firmware/application/apps/ui_whipcalc.cpp +++ b/firmware/application/apps/ui_whipcalc.cpp @@ -38,19 +38,6 @@ namespace ui field_frequency.focus(); } - double ui::WhipCalcView::get_decimals(double num, int16_t mult, bool round) - { - num -= int(num); //keep decimals only - num *= mult; //Shift decimals into integers - if (!round) - return num; - int16_t intnum = int(num); //Round it up if necessary - num -= intnum; //Get decimal part - if (num > .5) - intnum++; //Round up - return intnum; - } - void WhipCalcView::update_result() { double length, calclength, divider; diff --git a/firmware/application/apps/ui_whipcalc.hpp b/firmware/application/apps/ui_whipcalc.hpp index 7f1cefc0..8fca52a8 100644 --- a/firmware/application/apps/ui_whipcalc.hpp +++ b/firmware/application/apps/ui_whipcalc.hpp @@ -51,7 +51,6 @@ namespace ui }; std::vector antenna_db{}; - double get_decimals(double num, int16_t mult, bool round = false); void update_result(); uint16_t string_to_number(std::string); void txtline_process(std::string &); diff --git a/firmware/application/string_format.cpp b/firmware/application/string_format.cpp index fd7fb1a3..e8b3236b 100644 --- a/firmware/application/string_format.cpp +++ b/firmware/application/string_format.cpp @@ -224,3 +224,13 @@ std::string unit_auto_scale(double n, const uint32_t base_nano, uint32_t precisi return string; } + +double get_decimals(double num, int16_t mult, bool round) { + num -= int(num); //keep decimals only + num *= mult; //Shift decimals into integers + if (!round) return num; + int16_t intnum = int(num); //Round it up if necessary + num -= intnum; //Get decimal part + if (num > .5) intnum++; //Round up + return intnum; +} diff --git a/firmware/application/string_format.hpp b/firmware/application/string_format.hpp index c0164a0d..0555db8e 100644 --- a/firmware/application/string_format.hpp +++ b/firmware/application/string_format.hpp @@ -54,5 +54,5 @@ std::string to_string_timestamp(const rtc::RTC& value); std::string to_string_FAT_timestamp(const FATTimestamp& timestamp); std::string unit_auto_scale(double n, const uint32_t base_nano, uint32_t precision); - +double get_decimals(double num, int16_t mult, bool round = false); //euquiq added #endif/*__STRING_FORMAT_H__*/ diff --git a/firmware/common/sonde_packet.cpp b/firmware/common/sonde_packet.cpp index b1c22430..9b13c486 100644 --- a/firmware/common/sonde_packet.cpp +++ b/firmware/common/sonde_packet.cpp @@ -29,12 +29,16 @@ namespace sonde { //Defines for Vaisala RS41, from https://github.com/rs1729/RS/blob/master/rs41/rs41sg.c #define MASK_LEN 64 + +//Following values include the 4 bytes less shift, consumed in detecting the header on proc_sonde +#define block_status 0x35 //0x039 // 40 bytes +#define block_gpspos 0x10E //0x112 // 21 bytes +#define block_meas 0x61 //0x65 // 42 bytes #define pos_FrameNb 0x37 //0x03B // 2 byte #define pos_SondeID 0x39 //0x03D // 8 byte #define pos_Voltage 0x041 //0x045 // 3 bytes (but first one is the important one) voltage x 10 ie: 26 = 2.6v #define pos_CalData 0x04E //0x052 // 1 byte, counter 0x00..0x32 -#define pos_GPSweek 0x091 //0x095 // 2 byte -#define pos_GPSTOW 0x093 //0x097 // 4 byte +#define pos_temp 0x063 //0x067 // 3 bytes (uint24_t) #define pos_GPSecefX 0x110 //0x114 // 4 byte #define pos_GPSecefY 0x114 //0x118 // 4 byte (not actually used since Y and Z are following X, and grabbed in that same loop) #define pos_GPSecefZ 0x118 //0x11C // 4 byte (same as Y) @@ -64,10 +68,6 @@ size_t Packet::length() const { return decoder_.symbols_count(); } -bool Packet::is_valid() const { - return true; // TODO -} - Timestamp Packet::received_at() const { return packet_.timestamp(); } @@ -79,144 +79,343 @@ Packet::Type Packet::type() const { //euquiq here: //RS41SG 320 bits header, 320bytes frame (or more if it is an "extended frame") //The raw data is xor-scrambled with the values in the 64 bytes vaisala_mask (see.hpp) +//from 0x008 to 0x037 (48 bytes reed-solomon error correction data) - -uint8_t Packet::vaisala_descramble(const uint32_t pos) const { - //return reader_raw.read(pos * 8, 8) ^ vaisala_mask[pos & 63]; +uint8_t Packet::vaisala_descramble(const uint32_t pos) const +{ //vaisala_descramble(const uint32_t pos) const { // packet_[i]; its a bit; packet_.size the total (should be 2560 bits) uint8_t value = 0; - for (uint8_t i = 0; i < 8; i++) - value = (value << 1) | packet_[(pos * 8) + (7 -i)]; //get the byte from the bits collection + for (uint8_t i = 0; i < 8; i++) + value = (value << 1) | packet_[(pos * 8) + (7 - i)]; //get the byte from the bits collection //packetReader reader { packet_ }; //This works just as above. //value = reader.read(pos * 8,8); //shift pos because first 4 bytes are consumed by proc_sonde in finding the vaisala signature uint32_t mask_pos = pos + 4; - value = value ^ vaisala_mask[mask_pos % MASK_LEN]; //descramble with the xor pseudorandom table + value = value ^ vaisala_mask[mask_pos % MASK_LEN]; //descramble with the xor pseudorandom table return value; }; -GPS_data Packet::get_GPS_data() const { +GPS_data Packet::get_GPS_data() const +{ GPS_data result; - if ((type_ == Type::Meteomodem_M10) || (type_ == Type::Meteomodem_M2K2)) { + if ((type_ == Type::Meteomodem_M10) || (type_ == Type::Meteomodem_M2K2)) + { result.alt = (reader_bi_m.read(22 * 8, 32) / 1000) - 48; result.lat = reader_bi_m.read(14 * 8, 32) / ((1ULL << 32) / 360.0); result.lon = reader_bi_m.read(18 * 8, 32) / ((1ULL << 32) / 360.0); - - } else if (type_ == Type::Vaisala_RS41_SG) { + } + else if (type_ == Type::Vaisala_RS41_SG) + { uint8_t XYZ_bytes[4]; int32_t XYZ; // 32bit double_t X[3]; - for (int32_t k = 0; k < 3; k++) { //Get X,Y,Z ECEF position from GPS - for (int32_t i = 0; i < 4; i++) //each one is 4 bytes (32 bits) - XYZ_bytes[i] = vaisala_descramble(pos_GPSecefX + (4*k) + i); + for (int32_t k = 0; k < 3; k++) + { //Get X,Y,Z ECEF position from GPS + for (int32_t i = 0; i < 4; i++) //each one is 4 bytes (32 bits) + XYZ_bytes[i] = vaisala_descramble(pos_GPSecefX + (4 * k) + i); memcpy(&XYZ, XYZ_bytes, 4); X[k] = XYZ / 100.0; } double_t a = 6378137.0; double_t b = 6356752.31424518; - double_t e = sqrt( (a*a - b*b) / (a*a) ); - double_t ee = sqrt( (a*a - b*b) / (b*b) ); + double_t e = sqrt((a * a - b * b) / (a * a)); + double_t ee = sqrt((a * a - b * b) / (b * b)); - double_t lam = atan2( X[1] , X[0] ); - double_t p = sqrt( X[0]*X[0] + X[1]*X[1] ); - double_t t = atan2( X[2]*a , p*b ); - double_t phi = atan2( X[2] + ee*ee * b * sin(t)*sin(t)*sin(t) , - p - e*e * a * cos(t)*cos(t)*cos(t) ); + double_t lam = atan2(X[1], X[0]); + double_t p = sqrt(X[0] * X[0] + X[1] * X[1]); + double_t t = atan2(X[2] * a, p * b); + double_t phi = atan2(X[2] + ee * ee * b * sin(t) * sin(t) * sin(t), + p - e * e * a * cos(t) * cos(t) * cos(t)); - double_t R = a / sqrt( 1 - e*e*sin(phi)*sin(phi) ); + double_t R = a / sqrt(1 - e * e * sin(phi) * sin(phi)); result.alt = p / cos(phi) - R; - result.lat = phi*180/PI; - result.lon = lam*180/PI; - + result.lat = phi * 180 / PI; + result.lon = lam * 180 / PI; } return result; } -uint32_t Packet::battery_voltage() const { +uint32_t Packet::battery_voltage() const +{ if (type_ == Type::Meteomodem_M10) return (reader_bi_m.read(69 * 8, 8) + (reader_bi_m.read(70 * 8, 8) << 8)) * 1000 / 150; else if (type_ == Type::Meteomodem_M2K2) - return reader_bi_m.read(69 * 8, 8) * 66; // Actually 65.8 - else if (type_ == Type::Vaisala_RS41_SG) { - uint32_t voltage = vaisala_descramble(pos_Voltage) * 100; //byte 69 = voltage * 10 (check if this value needs to be multiplied) + return reader_bi_m.read(69 * 8, 8) * 66; // Actually 65.8 + else if (type_ == Type::Vaisala_RS41_SG) + { + uint32_t voltage = vaisala_descramble(pos_Voltage) * 100; //byte 69 = voltage * 10 (check if this value needs to be multiplied) return voltage; - } - else { - return 0; // Unknown - } -} - -std::string Packet::type_string() const { - switch (type_) { - case Type::Unknown: return "Unknown"; - case Type::Meteomodem_unknown: return "Meteomodem ???"; - case Type::Meteomodem_M10: return "Meteomodem M10"; - case Type::Meteomodem_M2K2: return "Meteomodem M2K2"; - case Type::Vaisala_RS41_SG: return "Vaisala RS41-SG"; - default: return "? 0x" + symbols_formatted().data.substr(0, 6); + } + else + { + return 0; // Unknown } } -std::string Packet::serial_number() const { - if (type() == Type::Meteomodem_M10) { +uint32_t Packet::frame() const +{ + if (type_ == Type::Vaisala_RS41_SG) + { + uint32_t frame_number = vaisala_descramble(pos_FrameNb) | (vaisala_descramble(pos_FrameNb + 1) << 8); + return frame_number; + } + else + { + return 0; // Unknown + } +} + +temp_humid Packet::get_temp_humid() const +{ + temp_humid result; + result.humid = 0; + result.temp = 0; + + if ( type_ == Type::Vaisala_RS41_SG && crc_ok_RS41() ) //Only process if packet is healthy + { + //memset(calfrchk, 0, 51); // is this necessary ? only if the sondeID changes (new sonde) + //original code from https://github.com/rs1729/RS/blob/master/rs41/rs41ptu.c + float Rf1, // ref-resistor f1 (750 Ohm) + Rf2, // ref-resistor f2 (1100 Ohm) + co1[3], // { -243.911 , 0.187654 , 8.2e-06 } + calT1[3], // calibration T1 + co2[3], // { -243.911 , 0.187654 , 8.2e-06 } + calT2[3], // calibration T2-Hum + calH[2]; // calibration Hum + + uint32_t meas[12], i; + + //-------------- get_CalData + + //-------------- populate calibytes (from getFrameConf) + + uint8_t calfr = vaisala_descramble(pos_CalData); //get subframe #slot + + for (i = 0; i < 16; i++) //Load subrfame calibration page (16 bytes) into #slot + calibytes[calfr * 16 + i] = vaisala_descramble(pos_CalData + 1 + i); //pos = pos_CalData + 1 + i ; vaisala_descramble(pos) + + calfrchk[calfr] = 1; //flag this #slot as populated + + memcpy(&Rf1, calibytes + 61, 4); // 0x03*0x10+13 + memcpy(&Rf2, calibytes + 65, 4); // 0x04*0x10+ 1 + + memcpy(co1 + 0, calibytes + 77, 4); // 0x04*0x10+13 + memcpy(co1 + 1, calibytes + 81, 4); // 0x05*0x10+ 1 + memcpy(co1 + 2, calibytes + 85, 4); // 0x05*0x10+ 5 + + memcpy(calT1 + 0, calibytes + 89, 4); // 0x05*0x10+ 9 + memcpy(calT1 + 1, calibytes + 93, 4); // 0x05*0x10+13 + memcpy(calT1 + 2, calibytes + 97, 4); // 0x06*0x10+ 1 + + memcpy(calH + 0, calibytes + 117, 4); // 0x07*0x10+ 5 + memcpy(calH + 1, calibytes + 121, 4); // 0x07*0x10+ 9 + + memcpy(co2 + 0, calibytes + 293, 4); // 0x12*0x10+ 5 + memcpy(co2 + 1, calibytes + 297, 4); // 0x12*0x10+ 9 + memcpy(co2 + 2, calibytes + 301, 4); // 0x12*0x10+13 + + memcpy(calT2 + 0, calibytes + 305, 4); // 0x13*0x10+ 1 + memcpy(calT2 + 1, calibytes + 309, 4); // 0x13*0x10+ 5 + memcpy(calT2 + 2, calibytes + 313, 4); // 0x13*0x10+ 9 + //--------------------------------------- + for (i = 0; i < 12; i++) + meas[i] = vaisala_descramble(pos_temp + (3 * i)) | + (vaisala_descramble(pos_temp + (3 * i) + 1) << 8) | + (vaisala_descramble(pos_temp + (3 * i) + 2) << 16); + + //----Check if necessary calibytes are already present for calculation + + if (calfrchk[0x03] && calfrchk[0x04] && calfrchk[0x04] && calfrchk[0x05] && calfrchk[0x05] && calfrchk[0x06]) //Calibites OK for Temperature + { + //----------get_Tc------------------------ + float *p = co1; + float *c = calT1; + float g = (float)(meas[2] - meas[1]) / (Rf2 - Rf1), // gain + Rb = (meas[1] * Rf2 - meas[2] * Rf1) / (float)(meas[2] - meas[1]), // ofs + Rc = meas[0] / g - Rb, + R = Rc * c[0], + T = (p[0] + p[1] * R + p[2] * R * R + c[1]) * (1.0 + c[2]); + result.temp = T; + } + + if (calfrchk[0x07]) + { + //----------get_RH------------------------ + float a0 = 7.5; // empirical + float a1 = 350.0 / calH[0]; // empirical + float fh = (meas[3] - meas[4]) / (float)(meas[5] - meas[4]); + float rh = 100.0 * (a1 * fh - a0); + float T0 = 0.0, T1 = -25.0; // T/C + rh += T0 - result.temp / 5.5; // empir. temperature compensation + if (result.temp < T1) + rh *= 1.0 + (T1 - result.temp) / 90.0; // empir. temperature compensation + if (rh < 0.0) + rh = 0.0; + if (rh > 100.0) + rh = 100.0; + if (result.temp < -273.0) + rh = -1.0; + result.humid = rh; + } + } + return result; +} + +std::string Packet::type_string() const +{ + switch (type_) + { + case Type::Unknown: + return "Unknown"; + case Type::Meteomodem_unknown: + return "Meteomodem ???"; + case Type::Meteomodem_M10: + return "Meteomodem M10"; + case Type::Meteomodem_M2K2: + return "Meteomodem M2K2"; + case Type::Vaisala_RS41_SG: + return "Vaisala RS41-SG"; + default: + return "? 0x" + symbols_formatted().data.substr(0, 6); + } +} + +std::string Packet::serial_number() const +{ + if (type_ == Type::Meteomodem_M10) + { // See https://github.com/rs1729/RS/blob/master/m10/m10x.c line 606 // Starting at byte #93: 00000000 11111111 22222222 33333333 44444444 // CCCC AAAABBBB // 44444444 33333333 // DDDEEEEE EEEEEEEE - + return to_string_hex(reader_bi_m.read(93 * 8 + 16, 4), 1) + - to_string_dec_uint(reader_bi_m.read(93 * 8 + 20, 4), 2, '0') + " " + - to_string_hex(reader_bi_m.read(93 * 8 + 4, 4), 1) + " " + - to_string_dec_uint(reader_bi_m.read(93 * 8 + 24, 3), 1) + - to_string_dec_uint(reader_bi_m.read(93 * 8 + 27, 13), 4, '0'); - - } else if(type() == Type::Vaisala_RS41_SG) { + to_string_dec_uint(reader_bi_m.read(93 * 8 + 20, 4), 2, '0') + " " + + to_string_hex(reader_bi_m.read(93 * 8 + 4, 4), 1) + " " + + to_string_dec_uint(reader_bi_m.read(93 * 8 + 24, 3), 1) + + to_string_dec_uint(reader_bi_m.read(93 * 8 + 27, 13), 4, '0'); + } + else if (type_ == Type::Vaisala_RS41_SG) + { std::string serial_id = ""; uint8_t achar; - for (uint8_t i=0; i<8; i++) { //euquiq: Serial ID is 8 bytes long, each byte a char + for (uint8_t i = 0; i < 8; i++) + { //euquiq: Serial ID is 8 bytes long, each byte a char achar = vaisala_descramble(pos_SondeID + i); - if (achar < 32 || achar > 126) return "?"; //Maybe there are ids with less than 8 bytes and this is not OK. + if (achar < 32 || achar > 126) + return "?"; //Maybe there are ids with less than 8 bytes and this is not OK. serial_id += (char)achar; } return serial_id; - } else + } + else + { return "?"; + } } -FormattedSymbols Packet::symbols_formatted() const { - if (type() == Type::Vaisala_RS41_SG) { //Euquiq: now we distinguish different types - uint32_t bytes = packet_.size() / 8; //Need the byte amount, which if full, it SHOULD be 320 size() should return 2560 +FormattedSymbols Packet::symbols_formatted() const +{ + if (type_ == Type::Vaisala_RS41_SG) + { //Euquiq: now we distinguish different types + uint32_t bytes = packet_.size() / 8; //Need the byte amount, which if full, it SHOULD be 320 size() should return 2560 std::string hex_data; std::string hex_error; hex_data.reserve(bytes * 2); //2 hexa chars per byte - hex_error.reserve(1); - for (uint32_t i=0; i < bytes; i++) //log will show the packet starting on the last 4 bytes from signature 93DF1A60 - hex_data += to_string_hex(vaisala_descramble(i),2); - return { hex_data, hex_error }; - - } else { + hex_error.reserve(1); + for (uint32_t i = 0; i < bytes; i++) //log will show the packet starting on the last 4 bytes from signature 93DF1A60 + hex_data += to_string_hex(vaisala_descramble(i), 2); + return {hex_data, hex_error}; + } + else + { return format_symbols(decoder_); } } -bool Packet::crc_ok() const { - switch(type()) { - case Type::Meteomodem_M10: return crc_ok_M10(); - default: return false; +bool Packet::crc_ok() const +{ + switch (type_) + { + case Type::Meteomodem_M10: + return crc_ok_M10(); + case Type::Vaisala_RS41_SG: + return crc_ok_RS41(); + default: + return true; //euquiq: it was false, but if no crc routine, then no way to check } } -bool Packet::crc_ok_M10() const { - uint16_t cs { 0 }; - uint32_t c0, c1, t, t6, t7, s,b ; - - for (size_t i = 0; i < packet_.size(); i++) { +//each data block has a 2 byte header, data, and 2 byte tail: +// 1st byte: block ID +// 2nd byte: data length (without header or tail) +// +// 2 bytes CRC16 over the data. +bool Packet::crc_ok_RS41() const //check CRC for the data blocks we need +{ + if (!crc16rs41(block_status)) + return false; + + if (!crc16rs41(block_gpspos)) + return false; + + if (!crc16rs41(block_meas)) + return false; + + return true; +} + +//Checks CRC16 on a RS41 field: +bool Packet::crc16rs41(uint32_t field_start) const +{ + int crc16poly = 0x1021; + int rem = 0xFFFF, b, j; + int xbyte; + uint32_t pos = field_start + 1; + uint8_t length = vaisala_descramble(pos); + + if (pos + length + 2 > packet_.size() / 8) + return false; //Out of packet! + + for (b = 0; b < length; b++) + { + pos++; + xbyte = vaisala_descramble(pos); + rem = rem ^ (xbyte << 8); + for (j = 0; j < 8; j++) + { + if (rem & 0x8000) + { + rem = (rem << 1) ^ crc16poly; + } + else + { + rem = (rem << 1); + } + rem &= 0xFFFF; + } + } + //Check calculated CRC against packet's one + pos++; + int crcok = vaisala_descramble(pos) | (vaisala_descramble(pos + 1) << 8); + if (crcok != rem) + return false; + return true; +} + +bool Packet::crc_ok_M10() const +{ + uint16_t cs{0}; + uint32_t c0, c1, t, t6, t7, s, b; + + for (size_t i = 0; i < packet_.size(); i++) + { b = packet_[i]; c1 = cs & 0xFF; @@ -235,9 +434,9 @@ bool Packet::crc_ok_M10() const { c0 = b ^ t ^ s; - cs = ((c1<<8) | c0) & 0xFFFF; - } - + cs = ((c1 << 8) | c0) & 0xFFFF; + } + return ((cs & 0xFFFF) == ((packet_[0x63] << 8) | (packet_[0x63 + 1]))); } diff --git a/firmware/common/sonde_packet.hpp b/firmware/common/sonde_packet.hpp index 746d42e3..54227db5 100644 --- a/firmware/common/sonde_packet.hpp +++ b/firmware/common/sonde_packet.hpp @@ -32,12 +32,20 @@ namespace sonde { + static uint8_t calibytes[51*16]; //need these vars to survive + static uint8_t calfrchk[51]; //so subframes are preserved while populated + struct GPS_data { uint32_t alt { 0 }; float lat { 0 }; float lon { 0 }; }; + struct temp_humid { + float temp { 0 }; + float humid { 0 }; + }; + class Packet { public: enum class Type : uint32_t { @@ -52,8 +60,6 @@ public: size_t length() const; - bool is_valid() const; - Timestamp received_at() const; Type type() const; @@ -61,8 +67,9 @@ public: std::string serial_number() const; uint32_t battery_voltage() const; - GPS_data get_GPS_data() const; + uint32_t frame() const; + temp_humid get_temp_humid() const; FormattedSymbols symbols_formatted() const; @@ -90,7 +97,10 @@ private: Type type_; using packetReader = FieldReader; //baseband::Packet instead of BiphaseMDecoder + bool crc_ok_M10() const; + bool crc_ok_RS41() const; + bool crc16rs41(uint32_t field_start) const; }; } /* namespace sonde */