Pages: 1 [2]   Go Down
Author Topic: Problem communicating sensors data to PC  (Read 1690 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Mark, this is the code i'm using:

Code:
#include <ADXL345.h>
#include <HMC58X3.h>
#include <ITG3200.h>
#include <bma180.h>
#include <MS561101BA.h>
//#define DEBUG
#include "DebugUtils.h"
#include "FreeIMU.h"
#include "CommunicationUtils.h"
#include <Wire.h>
#include "I2Cdev.h"
#include "MPU6050.h"

int raw_values[11];
float ypr[3]; // yaw pitch roll
float ypr2[3]; // yaw pitch roll

String inputString, str = "";        
boolean stringComplete = false;  

float acc;
boolean paso;

FreeIMU my3IMU = FreeIMU();
FreeIMU my3IMU2 = FreeIMU();


void setup() {
  Serial.begin(115200);
  Serial3.begin(57600);
  delay(20);
  Serial3.println("$PMTK300,100,0,0,0,0*2C"); //configurar a 10hz
  Wire.begin();
  pinMode(2, INPUT);
  my3IMU.init(); // the parameter enable or disable fast mode
  my3IMU2.init(0x69, false);
  inputString.reserve(200);
}

void loop() {
  my3IMU.getYawPitchRoll(ypr);
  my3IMU2.getYawPitchRoll(ypr2);
  my3IMU.getRawValues(raw_values); // *******************************
  paso=digitalRead(2);
  acc=sqrt((float(raw_values[0])*float(raw_values[0]))+(float(raw_values[1])*float(raw_values[1]))+(float(raw_values[2])*float(raw_values[2])));
  
  Serial.print(ypr[0]);          //Yaw Imu 1
  Serial.print(',');
  Serial.print(ypr[1]);          //Pitch Imu 1
  Serial.print(',');
  Serial.print(paso);            //"Step detection"
  Serial.print(',');
  Serial.print(ypr2[1]);         //Pitch Imu 2
  Serial.print(',');
  Serial.print(acc);             //Aceleracion
  Serial.print(',');
  if (stringComplete) {
    Serial.print(inputString);
    inputString = "";
    stringComplete = false;
    Serial3.flush();
  }else {
  Serial.println("");
  }

}

void serialEvent3() {
  while (Serial3.available()) {
    char inChar = (char)Serial3.read();
    inputString += inChar;
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

And this is the result:

-39.35,-1.31,0,-12.44,16339.55,$GPGGA,123007.100,4027.1656,N,00343.5385,W,1,5,3.12,669.5,M,51.$GPGGA,123007.300,4027.1655,N,00343.5385,W,1,5,3.12,669.5,M,51.7,M,$GPGGA,123007.400,4027.1655,N,00343.5384,W,1,5,3.12,669.5,M,51.7,M,,$GPGGA,123007.500,4027.1655,N,00343.5384,W,1,5,3.12,669.5,M,51.7,M,,*4E
-39.34,-1.31,0,-12.45,16377.27,
-39.35,-1.31,0,-12.45,16410.62,
-39.34,-1.30,0,-12.46,16214.73,
-39.33,-1.30,0,-12.46,16358.73,$GPGGA,123007.600,4027.1655,N,00343.5383,W,1,5,3.12,669.5,M,51.7,M,,*4A
-39.34,-1.31,0,-12.48,16427.75,
-39.34,-1.31,0,-12.47,16282.03,
-39.34,-1.31,0,-12.48,16386.95,
-39.34,-1.31,0,-12.47,16314.30,
-39.34,-1.31,0,-12.47,16382.99,$GPGGA,123007.700,4027.1654,N,00343.5383,W,1,5,3.12,669.4,M,51.7,M,,*4B

i dont know if is clear enough, but if you see the first line of the results, it prints the gps sentence $GPGGA continuosly and overlaped with the others.
what i expect is just to see something like this:

127.61,72.31,0,18.29,16880.72,$GPGGA,120155.200,4027.1768,N,00343.5423,W,2,8,1.19,649.7,M,51.7,M,0000,0000*4E
127.51,72.36,0,18.21,16855.44,
127.40,72.42,0,18.13,16884.47,
127.31,72.42,0,18.05,16934.32,
127.20,72.49,0,17.97,17038.03,
127.11,72.48,0,17.89,16943.43,$GPGGA,120155.300,4027.1768,N,00343.5423,W,2,8,1.19,649.7,M,51.7,M,0000,0000*4F
127.01,72.54,0,17.81,16880.81,
126.91,72.60,0,17.74,16871.42,
126.80,72.66,0,17.67,16890.56,
126.70,72.72,0,17.59,16885.39,
126.61,72.78,0,17.50,16976.71,$GPGGA,120155.400,4027.1768,N,00343.5423,W,2,8,1.19,649.7,M,51.7,M,0000,0000*48
126.50,72.84,0,17.42,16892.15,
126.40,72.90,0,17.35,16916.25,
126.30,72.96,0,17.28,16900.62,
126.19,73.02,0,17.20,16872.01,
126.09,73.09,0,17.13,16861.00,$GPGGA,120155.500,4027.1768,N,00343.5423,W,2,8,1.19,649.7,M,51.7,M,0000,0000*49
125.98,73.15,0,17.04,16911.52,
125.88,73.21,0,16.97,16941.49,
125.78,73.27,0,16.90,16972.92,
125.68,73.33,0,16.82,16999.74,
125.59,73.33,0,16.74,16917.99,$GPGGA,120155.600,4027.1768,N,00343.5423,W,2,8,1.19,649.7,M,51.7,M,0000,0000*4A
125.48,73.39,0,16.65,16854.59,
125.39,73.44,0,16.58,16979.94,
125.31,73.43,0,16.51,16873.27,
125.23,73.41,0,16.43,16859.85,
125.14,73.46,0,16.36,16992.66,$GPGGA,120155.700,4027.1768,N,00343.5423,W,2,8,1.19,649.7,M,51.7,M,0000,0000*4B
« Last Edit: November 29, 2012, 07:37:30 am by cbarreto61 » Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 50
Posts: 2199
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think your problem is in this line     

inputString += inChar;

in serialEvent3().

You need to use an array of char and not a String!

String has a bug which makes a mess of the heap and then makes a mess of the ram!

Here is a bit of code which (I think - I can't test it) should fix things. It does build.

Code:
volatile char inputString[256]; // is 256 bytes big enough to hold the whole GPS response?
volatile int i = 0;
volatile boolean stringComplete = false;

void serialEvent3() {
  while (Serial3.available()) {
    char inChar = (char)Serial3.read();
    inputString[i] = inChar;
    i++;
    if (inChar == '\n') {
      stringComplete = true;
      inputString[i] ='/0';
    }
  }
}

You need to set i to 0 when you set stingComplete = false in loop().

As far as I could work serialEvent is, if not actually an ISR, it must be called from one. So any thing it changes needs to be volatile.

If this does not work try renaming serialEvent3() and then calling it from loop().

Mark
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25754
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
As far as I could work serialEvent is, if not actually an ISR, it must be called from one.
serialEvent is simply called inside "main" after "loop". (source hardware/arduino/cores/arduino/main)
It is most certainly not an ISR, and if your "loop()" never returns, it will never be called.
Variables do not need to be treated as volatile.
« Last Edit: November 29, 2012, 09:55:10 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 50
Posts: 2199
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
serialEvent is simply called inside "main" after "loop". (source hardware/arduino/cores/arduino/main)
It is most certainly not an ISR, and if your "loop()" never returns, it will never be called.
Variables do not need to be treated as volatile.

That's good to know.

Mark
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

With 1.0 and on, the Serial.flush() method blocks until all outgoing serial data has been shifted out (at least into the outgoing register). How does that help you? The whole idea behind interrupt driven serial output is to eliminate the blocking.

The ONLY time you need to wait for the outgoing buffer to be empty is before you put the Arduino to sleep. Are you planning to do that? If so, that'll really hurt your performance.

If not, get rid of the flush!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think your problem is in this line     

inputString += inChar;

in serialEvent3().

You need to use an array of char and not a String!

String has a bug which makes a mess of the heap and then makes a mess of the ram!

Here is a bit of code which (I think - I can't test it) should fix things. It does build.

Code:
volatile char inputString[256]; // is 256 bytes big enough to hold the whole GPS response?
volatile int i = 0;
volatile boolean stringComplete = false;

void serialEvent3() {
  while (Serial3.available()) {
    char inChar = (char)Serial3.read();
    inputString[i] = inChar;
    i++;
    if (inChar == '\n') {
      stringComplete = true;
      inputString[i] ='/0';
    }
  }
}

You need to set i to 0 when you set stingComplete = false in loop().

As far as I could work serialEvent is, if not actually an ISR, it must be called from one. So any thing it changes needs to be volatile.

If this does not work try renaming serialEvent3() and then calling it from loop().

Mark


Hi guys,

This solved my problem, i just had to make small changes, now it works as i wanted.
i removed the volatile declaration and, added one code line for emptying the char array, and one for reinitializing the i counter. Thats it!

Thanks for your help!

Code:
int raw_values[11];
float ypr[3]; // yaw pitch roll
float ypr2[3]; // yaw pitch roll

char inputString[256]; // is 256 bytes big enough to hold the whole GPS response?
int i = 0;
boolean stringComplete = false;

float acc;
boolean paso;

FreeIMU my3IMU = FreeIMU();
FreeIMU my3IMU2 = FreeIMU();


void setup() {
  Serial.begin(115200);
  Serial3.begin(57600); //Set Mega hardware port baud to match GPS default baud
  delay(20);
  Serial3.println("$PMTK300,100,0,0,0,0*2C"); //configurar a 10hz
  Wire.begin();
  pinMode(2, INPUT);
  my3IMU.init(); // the parameter enable or disable fast mode
  my3IMU2.init(0x69, false);
}

void loop() {
  my3IMU.getYawPitchRoll(ypr);
  my3IMU2.getYawPitchRoll(ypr2);
  my3IMU.getRawValues(raw_values); // *******************************
  paso=digitalRead(2);
  acc=sqrt((float(raw_values[0])*float(raw_values[0]))+(float(raw_values[1])*float(raw_values[1]))+(float(raw_values[2])*float(raw_values[2])));
 
  Serial.print(ypr[0]);          //Yaw Imu 1
  Serial.print(',');
  Serial.print(ypr[1]);          //Pitch Imu 1
  Serial.print(',');
  Serial.print(paso);            //"Step detection"
  Serial.print(',');
  Serial.print(ypr2[1]);         //Pitch Imu 2
  Serial.print(',');
  Serial.print(acc);             //Aceleracion
  Serial.print(',');
  if (stringComplete) {
    Serial.print(inputString);
    inputString[0] = '\0';
    i=0;
    stringComplete = false;
  }else {
  Serial.println("");
  }

}

void serialEvent3() {
  while (Serial3.available()) {
    char inChar = (char)Serial3.read();
    inputString[i] = inChar;
    i++;
    if (inChar == '\n') {
      stringComplete = true;
      inputString[i] ='\0';
    }
  }
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello again!

i come with a problem again,
after i get a 3D fix data with GPS the NMEA sentence shows more data, and when it happens the problem appears again! it looks like it is a problem with the lenght of what im sending through the serial port,
:S at this moment i dont have any idea,
 do you have any other suggestion?

thanks!!!
Logged

Pages: 1 [2]   Go Up
Jump to: