diff --git a/firmware/application/apps/ui_sonde.cpp b/firmware/application/apps/ui_sonde.cpp index c710fc97..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(); @@ -49,7 +54,11 @@ SondeView::SondeView(NavigationView& nav) { &check_crc, &text_signature, &text_serial, + &text_timestamp, &text_voltage, + &text_frame, + &text_temp, + &text_humid, &geopos, &button_see_map }); @@ -114,24 +123,45 @@ void SondeView::focus() { field_vga.focus(); } -void SondeView::on_packet(const sonde::Packet& packet) { +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()); - if (use_crc && !packet.crc_ok()) //euquiq: Reject bad packet if crc is on - return; + sonde_id = packet.serial_number(); //used also as tag on the geomap + text_serial.set(sonde_id); - 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"); + text_timestamp.set(to_string_timestamp(packet.received_at())); - 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); + 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); } } diff --git a/firmware/application/apps/ui_sonde.hpp b/firmware/application/apps/ui_sonde.hpp index 2b5cb0b6..d7da2443 100644 --- a/firmware/application/apps/ui_sonde.hpp +++ b/firmware/application/apps/ui_sonde.hpp @@ -69,17 +69,24 @@ private: 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 } }; @@ -97,37 +104,59 @@ private: }; Checkbox check_log { - { 22 * 8, 3 * 16 }, + { 23 * 8, 6 * 16 }, 3, "Log" }; Checkbox check_crc { - { 22 * 8, 5 * 16 }, + { 23 * 8, 8 * 16 }, 3, "CRC" }; Text text_signature { - { 10 * 8, 2 * 16, 10 * 8, 16 }, + { 9 * 8, 2 * 16, 10 * 8, 16 }, "..." }; + Text text_serial { - { 10 * 8, 3 * 16, 11 * 8, 16 }, + { 9 * 8, 3 * 16, 11 * 8, 16 }, "..." }; + + Text text_timestamp { + { 9 * 8, 4 * 16, 11 * 8, 16 }, + "..." + }; + Text text_voltage { - { 10 * 8, 4 * 16, 10 * 8, 16 }, + { 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, 7 * 16 }, + { 0, 12 * 16 }, GeoPos::alt_unit::METERS }; Button button_see_map { - { 8 * 8, 11 * 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 b4a3333f..442034e4 100644 --- a/firmware/application/string_format.cpp +++ b/firmware/application/string_format.cpp @@ -223,3 +223,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 58d08ebc..9b13c486 100644 --- a/firmware/common/sonde_packet.cpp +++ b/firmware/common/sonde_packet.cpp @@ -33,12 +33,12 @@ namespace sonde { //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) @@ -68,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(); } @@ -83,10 +79,10 @@ 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]; +{ //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++) @@ -161,6 +157,115 @@ uint32_t Packet::battery_voltage() const } } +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_) @@ -182,7 +287,7 @@ std::string Packet::type_string() const std::string Packet::serial_number() const { - if (type() == Type::Meteomodem_M10) + 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 @@ -196,7 +301,7 @@ std::string Packet::serial_number() const 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) + else if (type_ == Type::Vaisala_RS41_SG) { std::string serial_id = ""; uint8_t achar; @@ -210,13 +315,15 @@ std::string Packet::serial_number() const return serial_id; } else + { return "?"; + } } FormattedSymbols Packet::symbols_formatted() const { - if (type() == Type::Vaisala_RS41_SG) - { //euquiq: now we distinguish different types + 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; @@ -234,35 +341,37 @@ FormattedSymbols Packet::symbols_formatted() const bool Packet::crc_ok() const { - switch (type()) + switch (type_) { case Type::Meteomodem_M10: return crc_ok_M10(); case Type::Vaisala_RS41_SG: return crc_ok_RS41(); default: - return false; + return true; //euquiq: it was false, but if no crc routine, then no way to check } } -//from 0x008 to 0x037 (48 bytes reed-solomon error correction data) //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 +bool Packet::crc_ok_RS41() const //check CRC for the data blocks we need { if (!crc16rs41(block_status)) return false; - if (!crc16rs41(block_gpspos)) + if (!crc16rs41(block_gpspos)) + return false; + + if (!crc16rs41(block_meas)) return false; return true; } -//euquiq: Checks CRC16 on a RS41 field: +//Checks CRC16 on a RS41 field: bool Packet::crc16rs41(uint32_t field_start) const { int crc16poly = 0x1021; @@ -272,7 +381,7 @@ bool Packet::crc16rs41(uint32_t field_start) const uint8_t length = vaisala_descramble(pos); if (pos + length + 2 > packet_.size() / 8) - return false; //Packet too short! + return false; //Out of packet! for (b = 0; b < length; b++) { @@ -292,20 +401,21 @@ bool Packet::crc16rs41(uint32_t field_start) const rem &= 0xFFFF; } } - //euquiq: Check calculated CRC against packet's one + //Check calculated CRC against packet's one pos++; int crcok = vaisala_descramble(pos) | (vaisala_descramble(pos + 1) << 8); if (crcok != rem) return false; - else - return true; + 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++) { +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; @@ -324,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 65937a27..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,6 +97,7 @@ 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;