Skip to content

Commit

Permalink
Proper encoding/decoding when some elements are not available
Browse files Browse the repository at this point in the history
  • Loading branch information
pjalocha committed Feb 19, 2022
1 parent 994144d commit 4a59b29
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 50 deletions.
6 changes: 3 additions & 3 deletions main/ctrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ static void ProcessCtrlC(void) // print system
Parameters.Write(CONS_UART_Write); // write the parameters to the console
// Parameters.WriteFile(stdout); // write the parameters to the stdout

Format_String(CONS_UART_Write, "Batt: ");
Format_String(CONS_UART_Write, "Batt:");
Format_UnsDec(CONS_UART_Write, (10*BatteryVoltage+128)>>8, 5, 4);
Format_String(CONS_UART_Write, "V ");
Format_SignDec(CONS_UART_Write, (600*BatteryVoltageRate+128)>>8, 3, 1);
Expand All @@ -269,13 +269,13 @@ static void ProcessCtrlC(void) // print system
Format_UnsDec(CONS_UART_Write, Batt, 4, 3);
Format_String(CONS_UART_Write, "V ");
Format_UnsDec(CONS_UART_Write, InpCurr, 4, 3);
Format_String(CONS_UART_Write, "/");
Format_String(CONS_UART_Write, "-");
Format_UnsDec(CONS_UART_Write, OutCurr, 4, 3);
Format_String(CONS_UART_Write, "A USB:");
Format_UnsDec(CONS_UART_Write, Vbus, 4, 3);
Format_String(CONS_UART_Write, "V ");
Format_UnsDec(CONS_UART_Write, VbusCurr, 4, 3);
Format_String(CONS_UART_Write, "A Charge:");
Format_String(CONS_UART_Write, "A CC:");
Format_UnsDec(CONS_UART_Write, ((InpCharge<<12)+562)/1125, 2, 1);
Format_String(CONS_UART_Write, "-");
Format_UnsDec(CONS_UART_Write, ((OutCharge<<12)+562)/1125, 2, 1);
Expand Down
6 changes: 6 additions & 0 deletions main/disp_oled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,9 @@ void OLED_DrawRF(u8g2_t *OLED, GPS_Position *GPS) // RF
#ifdef WITH_RFM95
Len+=Format_String(Line+Len, "RFM95");
#endif
#ifdef WITH_SX1262
Len+=Format_String(Line+Len, "SX1262");
#endif
#ifdef WITH_SX1272
Len+=Format_String(Line+Len, "SX1272");
#endif
Expand Down Expand Up @@ -666,6 +669,9 @@ void OLED_DrawSystem(u8g2_t *OLED, GPS_Position *GPS)
#ifdef WITH_RFM95
Len+=Format_String(Line+Len, "RFM95 v");
#endif
#ifdef WITH_SX1262
Len+=Format_String(Line+Len, "SX1262 v");
#endif
#ifdef WITH_SX1272
Len+=Format_String(Line+Len, "SX1272 v");
#endif
Expand Down
1 change: 1 addition & 0 deletions main/hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,7 @@ void vApplicationTickHook(void) // RTOS timer tick hook
// AXP192

#ifdef WITH_AXP
// SemaphoreHandle_t AXP_Mutex;
AXP192 AXP;
#endif

Expand Down
1 change: 1 addition & 0 deletions main/hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ extern BQ24295 BQ;

#ifdef WITH_AXP
#include "axp192.h"
// extern SemaphoreHandle_t AXP_Mutex;
extern AXP192 AXP;
#endif

Expand Down
6 changes: 3 additions & 3 deletions main/lorawan.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class LoRaWANnode
bool SaveReq:1; // Request to save the node state
} ;
} ;
uint8_t Spare; // 112 bytes up to this point
uint8_t RxSilent; // count non-receptions <- 112 bytes up to (and including) this point
uint32_t LastSaved; // [sec] when saved to EEPROM or other permament storage
uint8_t Dummy[8]; // just to fill up the space, could be used later
uint32_t CRC32; // 128 bytes up to here: fits into 1kbit EEPROM
Expand All @@ -72,7 +72,7 @@ class LoRaWANnode

void Reset(void)
{ State=0; DevNonce=0; JoinNonce=0;
LastTx=0; TxCount=0; LastRx=0; RxCount=0; Flags=0; LastSaved=0;
LastTx=0; TxCount=0; LastRx=0; RxCount=0; RxSilent=0; Flags=0; LastSaved=0;
setCRC(); }

void Reset(uint64_t MAC, uint8_t *AppKey=0)
Expand All @@ -82,7 +82,7 @@ class LoRaWANnode
Reset(); } // reset to not-joined state

void Disconnect(void)
{ State=0; }
{ State=0; RxSilent=0; }

uint32_t calcCRC(void) const
{ uint32_t Sum=0x87654321; // start the sum with some magic
Expand Down
5 changes: 4 additions & 1 deletion main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ void app_main(void)
CONS_UART_Init();
CONS_Mutex = xSemaphoreCreateMutex(); // semaphore for sharing the writing to the console
I2C_Mutex = xSemaphoreCreateMutex(); // semaphore for sharing the I2C bus
#ifdef WITH_AXP
// AXP_Mutex = xSemaphoreCreateMutex(); // semaphore for sharing the AXP power controller
#endif

NVS_Init(); // initialize Non-Volatile-Storage in Flash and read the tracker parameters

Expand Down Expand Up @@ -79,7 +82,7 @@ void app_main(void)
{ WANdev.Reset(getUniqueID(), Parameters.AppKey); // then reset LoRaWAN to this key
WANdev.WriteToNVS(); } // and save LoRaWAN config. to NVS
Parameters.clrAppKey(); }
WANdev.Disconnect(); // restart with network join-request/accept at each restart
// WANdev.Disconnect(); // restart with network join-request/accept at each restart
#endif

CONS_UART_SetBaudrate(Parameters.CONbaud);
Expand Down
11 changes: 7 additions & 4 deletions main/ogn1.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,17 @@ class OGN1_Packet // Packet structure for the OGN tracker
Out[Len++]='/'; Out[Len++]='0'+Status.FixQuality;
Out[Len++]='/'; Len+=Format_UnsDec(Out+Len, Status.SatSNR+8); Out[Len++]='d'; Out[Len++]='B';
Out[Len++]=' '; Len+=Format_SignDec(Out+Len, DecodeAltitude(), 1, 0, 1); Out[Len++]='m';
Out[Len++]=' '; Len+=Format_UnsDec(Out+Len, (((uint32_t)Status.Pressure<<3)+5)/10, 2, 1); Out[Len++]='h'; Out[Len++]='P'; Out[Len++]='a';
Out[Len++]=' '; Len+=Format_SignDec(Out+Len, DecodeTemperature(), 2, 1); Out[Len++]='d'; Out[Len++]='e'; Out[Len++]='g'; Out[Len++]='C';
Out[Len++]=' '; Len+=Format_SignDec(Out+Len, DecodeHumidity(), 2, 1); Out[Len++]='%';
if(Status.Pressure>0)
{ Out[Len++]=' '; Len+=Format_UnsDec(Out+Len, (((uint32_t)Status.Pressure<<3)+5)/10, 2, 1); Out[Len++]='h'; Out[Len++]='P'; Out[Len++]='a'; }
if(hasTemperature())
{ Out[Len++]=' '; Len+=Format_SignDec(Out+Len, DecodeTemperature(), 2, 1); Out[Len++]='d'; Out[Len++]='e'; Out[Len++]='g'; Out[Len++]='C'; }
if(hasHumidity())
{ Out[Len++]=' '; Len+=Format_SignDec(Out+Len, DecodeHumidity(), 2, 1); Out[Len++]='%'; }
Out[Len++]=' '; Len+=Format_SignDec(Out+Len, ((uint32_t)DecodeVoltage()*100+32)>>6, 3, 2); Out[Len++]='V';
Out[Len++]=' '; Len+=Format_UnsDec(Out+Len, Status.TxPower+4);
Out[Len++]='/'; Out[Len++]='-'; Len+=Format_UnsDec(Out+Len, 5*Status.RadioNoise, 2, 1); Out[Len++]='d'; Out[Len++]='B'; Out[Len++]='m';
Out[Len++]=' '; Len+=Format_UnsDec(Out+Len, (1<<Status.RxRate)-1); Out[Len++]='/'; Out[Len++]='m'; Out[Len++]='i'; Out[Len++]='n';
return Len; }
Out[Len]=0; return Len; }

uint8_t WriteDeviceStatus(char *Out)
{ return sprintf(Out, " h%02X v%02X %dsat/%d/%ddB %ldm %3.1fhPa %+4.1fdegC %3.1f%% %4.2fV %d/%+4.1fdBm %d/min",
Expand Down
28 changes: 24 additions & 4 deletions main/ogn2.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,29 @@ class OGN2_Packet // Packet structure for the OGN tracker
{ printf(" %02X", Byte()[Idx]); }
printf("\n"); }

int WriteStatus(char *Out)
{ uint8_t Len=0;
Out[Len++]=' '; Out[Len++]='h'; Len+=Format_Hex(Out+Len, (uint8_t)Status.Hardware);
Out[Len++]=' '; Out[Len++]='v'; Len+=Format_Hex(Out+Len, (uint8_t)Status.Firmware);
Out[Len++]=' '; Len+=Format_UnsDec(Out+Len, Status.Satellites); Out[Len++]='s'; Out[Len++]='a'; Out[Len++]='t';
Out[Len++]='/'; Out[Len++]='0'+Status.FixQuality;
Out[Len++]='/'; Len+=Format_UnsDec(Out+Len, Status.SatSNR+8); Out[Len++]='d'; Out[Len++]='B';
Out[Len++]=' '; Len+=Format_SignDec(Out+Len, DecodeAltitude(), 1, 0, 1); Out[Len++]='m';
if(Status.Pressure>0)
{ Out[Len++]=' '; Len+=Format_UnsDec(Out+Len, (((uint32_t)Status.Pressure<<3)+5)/10, 2, 1); Out[Len++]='h'; Out[Len++]='P'; Out[Len++]='a'; }
if(hasTemperature())
{ Out[Len++]=' '; Len+=Format_SignDec(Out+Len, DecodeTemperature(), 2, 1); Out[Len++]='d'; Out[Len++]='e'; Out[Len++]='g'; Out[Len++]='C'; }
if(hasHumidity())
{ Out[Len++]=' '; Len+=Format_SignDec(Out+Len, DecodeHumidity(), 2, 1); Out[Len++]='%'; }
Out[Len++]=' '; Len+=Format_SignDec(Out+Len, ((uint32_t)DecodeVoltage()*100+32)>>6, 3, 2); Out[Len++]='V';
Out[Len++]=' '; Len+=Format_UnsDec(Out+Len, Status.TxPower+4);
Out[Len++]='/'; Out[Len++]='-'; Len+=Format_UnsDec(Out+Len, 5*Status.RadioNoise, 2, 1); Out[Len++]='d'; Out[Len++]='B'; Out[Len++]='m';
Out[Len++]=' '; Len+=Format_UnsDec(Out+Len, (1<<Status.RxRate)-1); Out[Len++]='/'; Out[Len++]='m'; Out[Len++]='i'; Out[Len++]='n';
Out[Len]=0; return Len; }

int WriteDeviceStatus(char *Out)
{ return sprintf(Out, " h%02X v%02X %dsat/%d %ldm %3.1fhPa %+4.1fdegC %3.1f%% %4.2fV %d/%+4.1fdBm %d/min",
Status.Hardware, Status.Firmware, Status.Satellites, Status.FixQuality, (long int)DecodeAltitude(),
{ return sprintf(Out, " h%02X v%02X %dsat/%d/%ddB %ldm %3.1fhPa %+4.1fdegC %3.1f%% %4.2fV %d/%+4.1fdBm %d/min",
Status.Hardware, Status.Firmware, Status.Satellites, Status.FixQuality, 8+Status.SatSNR, (long int)DecodeAltitude(),
0.08*Status.Pressure, 0.1*DecodeTemperature(), 0.1*DecodeHumidity(),
(1.0/64)*DecodeVoltage(), Status.TxPower+4, -0.5*Status.RadioNoise, (1<<Status.RxRate)-1 );
}
Expand Down Expand Up @@ -162,8 +182,8 @@ class OGN2_Packet // Packet structure for the OGN tracker
void PrintDeviceStatus(void) const
{ printf("%c:%06lX R%c%c %02ds:",
'0'+Header.AddrType, (long int)Header.Address, '0'+Header.Relay, Header.Emergency?'E':' ', Status.Time);
printf(" h%02X v%02X %dsat/%d %ldm %3.1fhPa %+4.1fdegC %3.1f%% %4.2fV Tx:%ddBm Rx:%+4.1fdBm %d/min",
Status.Hardware, Status.Firmware, Status.Satellites, Status.FixQuality, (long int)DecodeAltitude(),
printf(" h%02X v%02X %dsat/%d/%ddB %ldm %3.1fhPa %+4.1fdegC %3.1f%% %4.2fV Tx:%ddBm Rx:%+4.1fdBm %d/min",
Status.Hardware, Status.Firmware, Status.Satellites, Status.FixQuality, 8+Status.SatSNR, (long int)DecodeAltitude(),
0.08*Status.Pressure, 0.1*DecodeTemperature(), 0.1*DecodeHumidity(),
(1.0/64)*DecodeVoltage(), Status.TxPower+4, -0.5*Status.RadioNoise, (1<<Status.RxRate)-1 );
printf("\n");
Expand Down
3 changes: 3 additions & 0 deletions main/parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,9 @@ uint16_t StratuxPort;
#endif
#ifdef WITH_SX1272
Len+=Format_String(Line+Len, " SX1272");
#endif
#ifdef WITH_SX1262
Len+=Format_String(Line+Len, " SX1262");
#endif
Line[Len++]='/';
Len+=Format_SignDec(Line+Len, (int16_t)TxPower);
Expand Down
49 changes: 29 additions & 20 deletions main/paw.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class PAW_Packet
{ union
{ uint32_t AddrWord;
struct
{ uint8_t Sync : 8; // the first (thus lowest) byte is the "sync" = '$' = 0x24
{ uint8_t Sync : 8; // the first (thus lowest) byte is the "sync" = '$' = 0x24 (or 0x48)
uint32_t Address:24; // 24-bit address: can be ICAO or internally produced
} ;
} ;
Expand All @@ -27,7 +27,7 @@ class PAW_Packet
{ uint16_t HeadWord;
struct
{ uint16_t Heading:9; // [deg]
int8_t Climb :7; // [64fpm]
// int8_t Climb :7; // [64fpm] proposed extension
} ;
} ;
union
Expand All @@ -41,16 +41,17 @@ class PAW_Packet
{ uint16_t SpeedWord;
struct
{ uint16_t Speed:10; // [kt]
uint8_t Time : 6; // [sec]
// uint8_t Time : 6; // [sec] proposed extension
} ;
} ;
union
{ uint8_t TypeByte;
struct
{ uint8_t AcftType:4; // [] lower nibble is the aircraft-type like for FLARM/OGN, upper nibble possibly retransmit flag
bool Relay :1; // relay flag (by ground station)
bool OGN :1; // for packets produced by OGN-Tracker
uint8_t AddrType:2; // address-type for OGN packets (if OGN==1)
uint8_t Relay :4; // to be worked out: 1 or 2 signal some kind of relay
// bool Relay :1; // relay flag (by ground station)
// bool OGN :1; // for packets produced by OGN-Tracker - proposed extension
// uint8_t AddrType:2; // address-type for OGN packets (if OGN==1) - proposed extension
} ;
} ;
uint8_t CRC; // internal CRC: a XOR of all bytes
Expand All @@ -65,16 +66,17 @@ class PAW_Packet
Byte[Idx]=0; }

uint8_t getAddrType(void) const // get (or guess) address-type
{ if(OGN) return AddrType; // if OGN-Tracker then AddrType is explicit
{ // if(OGN) return AddrType; // if OGN-Tracker then AddrType is explicit
if(AcftType==0xF) return 3; // if fixed object then OGN-type
if(Address<0xD00000) return 1; // ICAO-type
if(Address<0xE00000) return 2; // FLARM-type
return 3; }
return 3; } // OGN-type

bool isPos(void) const { return Sync==0x24; }
// uint32_t getAddress(void) const { return Address>>8; } // remove the sync '$'
// void setAddress(uint32_t Addr) { Address = (Addr<<8) | 0x24; } // set new address and set the '$' sync char

int Copy(const OGN1_Packet &Packet, bool Ext=0)
int Copy(const OGN1_Packet &Packet /* , bool Ext=0 */ ) // convert from an OGN packet
{ Clear();
Address = Packet.Header.Address; // [24-bit]
if(Packet.Header.NonPos) return 0; // encode only position packets
Expand All @@ -84,6 +86,7 @@ class PAW_Packet
Speed = (398*(int32_t)Packet.DecodeSpeed()+1024)>>11; // [0.1m/s] => [kts]
Latitude = 0.0001f/60*Packet.DecodeLatitude(); // [deg]
Longitude = 0.0001f/60*Packet.DecodeLongitude(); // [deg]
/*
if(Ext)
{ OGN=1; // extended data flag
AddrType = Packet.Header.AddrType; // [2-bit]
Expand All @@ -94,10 +97,11 @@ class PAW_Packet
if(ClimbRate>127) ClimbRate=127;
else if(ClimbRate<(-127)) ClimbRate=(-127);
Climb = ClimbRate; }
*/
SeqMsg = 0;
setCRC(); return 1; }

int WriteJSON(char *JSON) const
int WriteStxJSON(char *JSON) const
{ int Len=0;
Len+=Format_String(JSON+Len, "\"addr\":\"");
Len+=Format_Hex(JSON+Len, (uint8_t) (Address>>16));
Expand All @@ -121,7 +125,7 @@ class PAW_Packet
// Len+=Format_SignDec(JSON+Len, RxTime, 4, 3, 1);
Len+=sprintf(JSON+Len, ",\"lat_deg\":%8.7f,\"lon_deg\":%8.7f,\"alt_msl_m\":%d", Latitude, Longitude, Altitude);
Len+=sprintf(JSON+Len, ",\"track_deg\":%d,\"speed_mps\":%3.1f", Heading, 0.514*Speed);
if(OGN) Len+=sprintf(JSON+Len, ",\"climb_mps\":%3.1f", 0.32512*Climb);
// if(OGN) Len+=sprintf(JSON+Len, ",\"climb_mps\":%3.1f", 0.32512*Climb);
return Len; }

uint8_t Dump(char *Out)
Expand All @@ -136,7 +140,8 @@ class PAW_Packet
TypeByte, Address, Latitude, Longitude, Altitude, Heading, Speed, Seq, Msg[0], Msg[1], Msg[2]);
for(int Idx=0; Idx<3; Idx++)
{ printf("%c", Msg[Idx]<' '?'.':Msg[Idx]); }
printf("\n"); }
// printf(" %c%c%c\n", Relay?'R':'_', OGN?'O':'_', '0'+AddrType);
printf(" %04X %04X\n", HeadWord, SpeedWord); }

uint8_t Read(const char *Inp) // read packet from a hex dump
{ uint8_t Len;
Expand Down Expand Up @@ -201,9 +206,9 @@ class PAW_Packet

} ;

class PAW_RxPacket // Received PilotAware packet
class PAW_RxPacket: public PAW_Packet // Received PilotAware packet
{ public:
PAW_Packet Packet;
// PAW_Packet Packet;
// uint8_t CRC; // [] external CRC (we assume it is correct)
uint8_t CSNR; // [0.5dB] carrier Signal-to-Noise Ratio
uint8_t SNR; // [0.25dB]
Expand All @@ -214,14 +219,18 @@ class PAW_RxPacket // Received PilotAware packet
public:
void Print(void)
{ printf("PAW: %d.%03ds: %02X:%06X [%+09.5f, %+010.5f]deg %4dm, %03ddeg %3dkt #%02X %3.1f/%3.1fdB %+4.1fkHz\n",
Time, nsTime/1000000, Packet.TypeByte, Packet.Address, Packet.Latitude, Packet.Longitude, Packet.Altitude, Packet.Heading, Packet.Speed,
Packet.Seq, 0.25*SNR, 0.5*CSNR, 0.01*FreqOfs); }
Time, nsTime/1000000, TypeByte, Address, Latitude, Longitude, Altitude, Heading, Speed,
Seq, 0.25*SNR, 0.5*CSNR, 0.01*FreqOfs); }

int WriteJSON(char *JSON) const
{ int Len = Packet.WriteJSON(JSON);
uint32_t getSlotTime(void) const
{ if(nsTime>=300000000) return Time;
return Time-1; }

int WriteStxJSON(char *JSON) const
{ int Len = PAW_Packet::WriteStxJSON(JSON);
uint32_t PosTime=Time; if(nsTime<300000000) PosTime--;
if(Packet.OGN)
{ }
// if(OGN)
// { }
Len+=Format_String(JSON+Len, ",\"time\":");
Len+=Format_UnsDec(JSON+Len, PosTime);
int64_t RxTime=(int64_t)Time-PosTime; RxTime*=1000; RxTime+=nsTime/1000000;
Expand Down
4 changes: 4 additions & 0 deletions main/proc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,11 @@ static void ReadStatus(OGN_Packet &Packet)
#endif
#endif

#ifdef WITH_SX1262
if(Packet.Status.Pressure==0) Packet.clrTemperature();
#else
if(Packet.Status.Pressure==0) Packet.EncodeTemperature(TRX.chipTemp*10); // [0.1degC]
#endif
Packet.Status.RadioNoise = TRX.averRSSI; // [-0.5dBm] write radio noise to the status packet

uint8_t TxPower = Parameters.TxPower-4;
Expand Down
Loading

0 comments on commit 4a59b29

Please sign in to comment.