0
votes

I get a float from raspberry pi (via struct module) and my sketch only shows the data on an LCD screen.

After 26 corrects loop, on the 27th, the Arduino crashes.

Can you tell me what's wrong with the 27th?

  • Changing delay from 20ms to 1s: NOK
  • Put the byte pointer out of the function: NOK
float f;

void getFloat(){
  byte *fdata = (byte *) &f;
  while(Serial.available() <= 4){}
  Serial.readBytes(fdata,4);
  }

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):

  lcd.setCursor(0, 0);
  lcd.print("Ready to receive");
  getFloat();
  AZ=f;
  getFloat();
  AL=f;
  lcd.setCursor(0, 0);
  lcd.print("Moving            ");
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print("AZ");
  lcd.print(String(AZ));
  lcd.setCursor(7, 1);
  lcd.print("; AL");
  lcd.print(String(AL));
  delay(1000);
  Serial.println("ok");
}

At the 27th, the arduino no longer acknoledge the data and the LCD shows : ==ving AZy ; AL0.00=

=== Resolution ===

Before, I have to send twice the floats via the next code to get the last if not, I get the previous data on my arduino but i think the limitation comes from there :

  def sendFloatToArduino(self,data):
    self.serial.write(struct.pack('<f', data))
    self.serial.flush()

  def pointer(self,AZ,AL):
#send the data
    print("AZ : "+str(AZ)+" ; AL : "+str(AL))
    self.sendFloatToArduino(AZ)
    self.sendFloatToArduino(AL)
    self.sendFloatToArduino(AZ)
    self.sendFloatToArduino(AL)
#wait for ack
    while (self.serialArduino.in_waiting==0):
      pass
    print(self.serialArduino.readline())

After deleting the double sending, everything is fine.

2
1.What do you mean by "NOK Put the byte pointer out of the function : NOK"? 2.Are you watching the output from raspberry pi? it may have some errorsI.Omar
No, I mean that it doesn't change anything.Fardouk
instead of String(AZ)I.Omar

2 Answers

0
votes

Thanks for your answers.

The problem is not about the parsing as you said but with the LCD display.

I've tried converting floats to array and geting only 16 characters:

  char str[16];
  AZ=getFloat();
  AL=getFloat();
  char buffer1[20];
  char buffer2[20];
  dtostrf(AZ,7,5,buffer1);
  dtostrf(AL,7,5,buffer2);
  for (int i=0;i<8;i++){
    if (i>1){
      str[i]=buffer1[i-2];
      str[i+8]=buffer2[i-2];
    }
  }
  Serial.println(str);

But I get this on serial output : AZ358.54AL48.544\x12.BBHE\xb3CGm=\r\n => fixed with \0 I think the problem is elswhere : I have to send twice the floats via the next code to get the last if not, I get the previous data on my arduino but i think the limitation comes from there :

  def sendFloatToArduino(self,data):
    self.serial.write(struct.pack('<f', data))
    self.serial.flush()

  def pointer(self,AZ,AL):
#send the data
    print("AZ : "+str(AZ)+" ; AL : "+str(AL))
    self.sendFloatToArduino(AZ)
    self.sendFloatToArduino(AL)
    self.sendFloatToArduino(AZ)
    self.sendFloatToArduino(AL)
#wait for ack
    while (self.serialArduino.in_waiting==0):
      pass
    print(self.serialArduino.readline())
0
votes

I can't figure out the problem, but you can try the following code. It's stronger I think.

float getFloat()
{
    float res;
    while (Serial.available() < sizeof(res))
        ;
    Serial.readBytes((char*)&res, sizeof(res));
    return res;
}

void loop()
{
    // ...
    AZ = getFloat();
    AL = getFloat();
    // ...
}