Go Down

Topic: Lost of command on serial (Read 170 times) previous topic - next topic


Hello everyone,
I'm currently working on a drone program using a RPi and an arduino.
I want to get GPS data from arduino and store them in a database on RPi.
I wrote a python script but sometimes the arduino doesn't want to respond the serial command.
Any advice on this ?

Thanks in advance


Any advice on this ?
Fix the code. If you need help, POST YOUR CODE!


That's why I gave the github link but here it is...


That code has some fundamental flaws. GPS() and GPS2() both read from the GPS for a period of time. That's pointless, as the GPS will send data on its schedule, not when you are looking to read the data.

Read the data in loop() and pass it to the encode() method. Make GPS() and GPS2() use that data, when there is new data to use.

Similarly, get rid of the duplicate code in gyro() and gyro2().


Thx for the advices, I fixed this part.
I also changed the speed of serial trans. to 57600 and now I get much less packet loss.


Code: [Select]
  Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.requestFrom(MPU, 14, true); // request a total of 14 registers
  AcX = Wire.read() << 8 | Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
  AcY = Wire.read() << 8 | Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  AcZ = Wire.read() << 8 | Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
  Tmp = Wire.read() << 8 | Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
  GyX = Wire.read() << 8 | Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
  GyY = Wire.read() << 8 | Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
  GyZ = Wire.read() << 8 | Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)

Why not put this in a function?

Code: [Select]
  // For one second we parse GPS data and report some key values
  for (unsigned long start = millis(); millis() - start < 1000;)
    while (ss.available())
      char c = ss.read();
      //Serial.write(c); // uncomment this line if you want to see the GPS data flowing
      if (gps.encode(c)) // Did a new valid sentence come in?
        newData = true;

This is still absolute nonsense. Get rid of the STUPID for loop!

GPS() assumes that there is useful data to deal with. That may not be a valid assumption.

Ditto for GPS2().


Done ! I also improved the python script.

I'm trying to learn how to code that's why I'm searching part of code on some websites.

Thanks :)

Go Up