Hello,
When trying to read a GPS using UBX with software serial and sensor data from an altimu 10 I'm noticing that sometimes it takes considerably more time to print to the serial port, does anyone know why this happens.
21 0 0 -4 -172 -36 -5 -79 -224 0
20 1 11 1 -167 -36 -4 -79 -2251 -92467000 387034642 103446 55171 -9 50 -124 2
20 0 0 -3 -171 -35 -4 -79 -223 0
20 1 0 3 -169 -39 -4 -80 -221 0
20 0 0 0 -165 -35 -4 -79 -223 13
41 0 0 -1 -168 -35 -5 -79 -223 14
37 0 0 1 -163 -35 -4 -79 -223 1
20 1 0 -1 -171 -35 -3 -80 -225 0
20 0 0 -1 -180 -38 -3 -79 -223 0
20 0 0 -3 -165 -35 -4 -79 -222 0
20 0 0 -4 -166 -33 -4 -80 -224 0
20 0 0 0 -170 -36 -2 -80 -222 0
20 0 11 -1 -173 -36 -5 -79 -2211 -92466994 387034637 103383 55108 -63 83 -120 4
20 0 0 -2 -170 -36 -4 -79 -221 0
20 1 0 -5 -173 -38 -4 -79 -221 0
20 0 0 -1 -176 -36 -4 -79 -222 0
20 0 0 -1 -166 -35 -4 -80 -222 13
39 0 0 1 -169 -35 -4 -79 -225 13
40 0 0 0 -166 -33 -4 -79 -222 1
20 2 0 -1 -174 -40 -4 -79 -222 0
20 0 0 -4 -175 -37 -5 -79 -222 0
20 0 0 0 -165 -36 -4 -78 -223 0
20 0 0 0 -169 -35 -4 -79 -223 0
20 0 12 -1 -168 -36 -3 -79 -2221 -92466993 387034637 103247 54972 -120 149 -101 3
20 0 0 -4 -172 -35 -3 -79 -224 0
20 1 0 0 -155 -35 -4 -79 -223 0
20 0 0 -2 -175 -37 -4 -79 -221 0
20 0 0 -1 -166 -39 -4 -79 -224 0
20 0 0 -1 -173 -37 -4 -79 -223 0
20 0 0 -1 -172 -35 -3 -79 -223 13
40 0 0 -1 -171 -37 -4 -79 -222 13
39 1 0 1 -166 -33 -5 -79 -221 0
20 0 0 4 -166 -39 -5 -79 -221 0
20 0 0 -1 -168 -35 -5 -79 -222 0
20 0 11 2 -167 -35 -3 -78 -2201 -92467001 387034646 103162 54887 -92 68 -87 3
20 0 0 -1 -167 -35 -5 -79 -225 0
You can see in the code above that in some cases it takes longer to print, the first number on the left is the las loop time, the number on its right is the time it took to read the magnetic data in the current looop and on its right the time it took to read the GPS on the loop, the last number on the right is the time it took to print the data, as you can see this number is sometimes significantly larger.
The full code:
////////////////////////////////////////////////////////////////////////////GPS
#include <SoftwareSerial1.h>
////////////////////////////////////////////////////////////////////////////ALTIMU
#include <Wire.h>
#include <L3G.h>
#include <LSM303.h>
#include <LPS.h>
////////////////////////////////////////////////////////////////////////////ALTIMU
L3G gyro;
LSM303 compass;
LPS ps;
unsigned long timer;
uint8_t counter = 0;
int pres;
int temp;
long a[3];
long g[3];
long m[3];
////////////////////////////////////////////////////////////////////////////GPS
SoftwareSerial1 GPS(3, 2); //rx|tx
uint8_t cc = 2;
uint8_t RATE[14] = {0xB5, 0x62, 0x06, 0x08, 0x06, 0x00, 0x64, 0x00, 0x01, 0x00, 0x01, 0x00, 0x7A, 0x12};
//38200
uint8_t UART[28] = {0xB5, 0x62, 0x06, 0x00, 0x14, 0x00, 0x01, 0x00, 0x00, 0x00, 0xD0, 0x08, 0x00, 0x00, 0x00, 0x96, 0x00, 0x00, 0x07, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x91, 0x84};
//9600
//uint8_t UART[28] = {0xB5, 0x62, 0x06, 0x00, 0x14, 0x00, 0x01, 0x00, 0x00, 0x00, 0xD0, 0x08, 0x00, 0x00, 0x80, 0x25, 0x00, 0x00, 0x07, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA0, 0xA9};
//uint8_t PVT[100];
uint8_t RQPVT[8] = { 0xB5, 0x62, 0x01, 0x07, 0x00, 0x00, 0x08, 0x19 };
long dtgps[8];
void setup()
{
Serial.begin(115200);
////////////////////////////////////////////////////////////////////////////ALTIMU
Wire.begin();
gyro.init();
gyro.enableDefault();
gyro.writeReg(L3G::CTRL_REG4, 0x20); // 2000 dps full scale
gyro.writeReg(L3G::CTRL_REG1, 0x0F); // normal power mode, all axes enabled, 100 Hz
compass.init();
compass.enableDefault();
switch (compass.getDeviceType())
{
case LSM303::device_D:
compass.writeReg(LSM303::CTRL2, 0x18); // 8 g full scale: AFS = 011
break;
case LSM303::device_DLHC:
compass.writeReg(LSM303::CTRL_REG4_A, 0x28); // 8 g full scale: FS = 10; high resolution output mode
break;
default: // DLM, DLH
compass.writeReg(LSM303::CTRL_REG4_A, 0x30); // 8 g full scale: FS = 11
}
////////////////////////////////////////////////////////////////////////////GPS
GPS.begin(9600);
GPS.write(RATE, 14);
delay(100);
GPS.write(UART, 28);
delay(100);
GPS.begin(38400);
timer = millis();
}
void readgps(long *dtgps)
{
GPS.write(RQPVT, 8);
uint8_t PVT[92];
int i = 0;
uint8_t cka;
uint8_t ckb;
while (GPS.available() > 0 || i < 91 )
{
PVT[i] = GPS.read();
i++;
}
Serial.println(i);
dtgps[1] = (long)PVT[24 + 9] << 24 | (long)PVT[24 + 8] << 16 | (long)PVT[24 + 7] << 8 | (long)PVT[24 + 6];
dtgps[2] = (long)PVT[28 + 9] << 24 | (long)PVT[28 + 8] << 16 | (long)PVT[28 + 7] << 8 | (long)PVT[28 + 6];
dtgps[3] = (long)PVT[32 + 9] << 24 | (long)PVT[32 + 8] << 16 | (long)PVT[32 + 7] << 8 | (long)PVT[32 + 6];
dtgps[4] = (long)PVT[36 + 9] << 24 | (long)PVT[36 + 8] << 16 | (long)PVT[36 + 7] << 8 | (long)PVT[36 + 6];
//hac=(uint32_t)PVT[40+9]<<24|(uint32_t)PVT[40+8]<<16|PVT[40+7]<<8|PVT[40+6];
//vac=(uint32_t)PVT[44+9]<<24|(uint32_t)PVT[44+8]<<16|PVT[44+7]<<8|PVT[44+6];
dtgps[5] = (long)PVT[48 + 9] << 24 | (long)PVT[48 + 8] << 16 | (long)PVT[48 + 7] << 8 | (long)PVT[48 + 6];
dtgps[6] = (long)PVT[52 + 9] << 24 | (long)PVT[52 + 8] << 16 | (long)PVT[52 + 7] << 8 | (long)PVT[52 + 6];
dtgps[7] = (long)PVT[56 + 9] << 24 | (long)PVT[56 + 8] << 16 | (long)PVT[56 + 7] << 8 | (long)PVT[56 + 6];
// velac=(uint32_t)PVT[68+9]<<24|(uint32_t)PVT[68+8]<<16|PVT[68+7]<<8|PVT[68+6];
//////////////////////////////////////////////////////////////////////CHKSM
cka = ckb = 0;
for (int a = 2; a < i - 2; a++) {
cka = cka + PVT[a];
ckb = ckb + cka;
}
if (cka == PVT[90] && ckb == PVT[91]) {
dtgps[0] = 1;
} else {
dtgps[0] = 0;
}
}
void loop() // run over and over again
{
if ((millis() - timer) >= 20) // Main loop runs at 50Hz
{
Serial.print(millis() - timer);
Serial.print('\t');
timer = millis();
//gyro.read();
//compass.readAcc();
a[0] = compass.a.x;
a[1] = compass.a.y;
a[2] = compass.a.z;
g[0] = gyro.g.x;
g[1] = gyro.g.y;
g[2] = gyro.g.z;
counter++;
cc++;
unsigned long t1 = millis();
if (counter > 10) // Read compass data at 10Hz... (5 loop runs)
{
// Serial.println(F("MAG"));
counter = 0;
compass.readMag();
m[0] = compass.m.x;
m[1] = compass.m.y;
m[2] = compass.m.z;
pres = ps.readPressureMillibars();
temp = ps.readTemperatureC();
}
Serial.print(millis() - t1);
Serial.print('\t');
////////////////////////////////////////////////////////////////////////////GPS
t1 = millis();
if (cc > 10) {
// Serial.println(F("GPS"));
readgps(dtgps);
cc = 0;
}
Serial.print(millis() - t1);
Serial.print('\t');
t1 = millis();
Serial.print(a[0]);
Serial.print('\t');
Serial.print(a[1]);
Serial.print('\t');
Serial.print(a[2]);
Serial.print('\t');
Serial.print(g[0]);
Serial.print('\t');
Serial.print(g[1]);
Serial.print('\t');
Serial.print(g[2]);
Serial.print('\t');
Serial.print(m[0]);
Serial.print('\t');
Serial.print(m[1]);
Serial.print('\t');
Serial.print(m[2]);
Serial.print('\t');
Serial.print(pres);
Serial.print('\t');
Serial.print(temp);
Serial.print('\t');
if (cc == 0) {
Serial.print(dtgps[0]);
Serial.print('\t');
Serial.print(dtgps[1]);
Serial.print('\t');
Serial.print(dtgps[2]);
Serial.print('\t');
Serial.print(dtgps[3]);
Serial.print('\t');
Serial.print(dtgps[4]);
Serial.print('\t');
Serial.print(dtgps[5]);
Serial.print('\t');
Serial.print(dtgps[6]);
Serial.print('\t');
Serial.print(dtgps[7]);
}
Serial.print('\t');
Serial.flush();
Serial.println(millis() - t1);
}
}
I have changed the Softserial library to have a 100 Byte Rx buffer as i'm trying to read 92 byte sentences.
Does anyone know any cause that can explain this behavior?
thank you in advance!
