Hello, I am working on an antenna tracking project which uses a ground station to point some directional antennas at a transmitter which is attached to an unmanned aircraft. I have gotten nearly everything working correctly. I then have some function in my main loop which grabs telemetry data from a Holybro radio connected to a Hardware Serial1 on an Arduino Mega 2560. I am using the HRFMessage library to send and receive telemetry data between radios. I am successfully receiving data on the ground station however it is not consistently being received while the transmitter is a few feet away. I will usually get data between 2 and 15 times and then the if statement looking for new data is not entered for up to 20 seconds. This is a huge issue because the antennas need to constantly get updates of where the transmitter is. I suspect it is either a timing issue, however I have tried 100s of delays in the loop varying between 25 ms to 5 seconds. I also suspect that the length of the strings being sent or received is causing the buffer to either fill or get offset where the telemetry string is unreadable. I removed everything stepper and IMU related as the code was too long to post, thanks!
Transmitter
#include <SoftwareSerial.h>
#include <HRFMessage.h>
#include <TinyGPS.h>
#include <Wire.h>
#include <Adafruit_INA219.h>
Adafruit_INA219 ina219;
TinyGPS gps;
SoftwareSerial ss(10,14); // RX, TX
HRFMessage server(&Serial2);
static void print_float(float val, float invalid, int len, int prec);
static void print_int(unsigned long val, unsigned long invalid, int len);
static void print_date(TinyGPS &gps);
static void print_str(const char *str, int len);
void setup()
{
ss.begin(9600);
ina219.begin();
Serial.begin(57600); // Monitoring via USB
Serial2.begin(57600); // defaults to 9600
delay(500);
}
void loop()
{
bool newData = false;
unsigned long chars;
// unsigned short sentences, failed;
uint8_t buf[HRF_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
float flat, flon, Alt, fspeed;
float shuntvoltage = 0;
float busvoltage = 0;
float current_mA = 0;
float loadvoltage = 0;
float power_mW = 0;
shuntvoltage = ina219.getShuntVoltage_mV();
busvoltage = ina219.getBusVoltage_V();
current_mA = ina219.getCurrent_mA();
power_mW = ina219.getPower_mW();
loadvoltage = busvoltage + (shuntvoltage / 1000);
gps.f_get_position(&flat, &flon);
Alt = gps.f_altitude()+0;
fspeed = gps.f_speed_mph();
for (unsigned long start = millis(); millis() - start < 1000;)
{
while (ss.available())
{
char c = ss.read();
if (gps.encode(c)) // Did a new valid sentence come in?
newData = true;
}
}
char string1[5];
char string4[7];
char string5[8];
char string6[12];
char string7[12];
char string8[11];
char string9[9];
char total[64];
dtostrf(loadvoltage,2,2,string1);
dtostrf(current_mA,4,2,string4);
dtostrf(power_mW,5,2,string5);
dtostrf(flat,4,6,string6);
dtostrf(flon,4,6,string7);
dtostrf(fspeed,3,7,string8);
dtostrf(Alt,5,3,string9);
//Serial.println();
Serial.print(" Load Curr. Power Latitude Longitude Speed(mph) Altitude");
strcpy(total,string1);
strcat(total," ");
strcat(total,string4);
strcat(total," ");
strcat(total,string5);
strcat(total," ");
strcat(total,string6);
strcat(total," ");
strcat(total,string7);
strcat(total," ");
strcat(total,string8);
strcat(total," ");
strcat(total,string9);
server.send((uint8_t*)total,60);
Serial.println();
Serial.print("Info in total Array:");
Serial.print(" ");
Serial.print(total);
Serial.println();
}