Ask Your Question
0

Something wrong with my arduino code? Lost sync with device, restarting...

asked 2012-05-08 06:30:57 -0500

Mantamanni gravatar image

updated 2012-05-08 07:36:31 -0500

Lorenz gravatar image

Hi guys. I'm using an arduino mega with the rosserial stack. I tried to write a code to read the voltage of a LI-FE accumulator. Rosserial should show me the measured voltage in a terminal. The last step of my project was to sum two characters, so that i can connect more accumulators and see which accumulator has which voltage. The crazy thing about that is that it worked as I used this order: "measured voltage, text" but when I change the order to "text, measured voltage" my terminal says: lost sync with device... after i typed "rosrun rosserial_python serial_node.py /dev/ttyACM0" and "rostopic echo chatter".

Here is the code that worked:

int spgn1 = A5;
int ledpin = 13;
int Spannung1=0;
double Spgn1=0.0;

#include <ros.h>
#include <std_msgs/String.h>

ros::NodeHandle  nh;

std_msgs::String str_msg;
ros::Publisher chatter("chatter", &str_msg);


char Text[17]=" Volt an Zelle 1";
char Zelle1[32]="";



void setup() {
  // put your setup code here, to run once:
  pinMode(ledpin, OUTPUT);
  nh.initNode();
  nh.advertise(chatter);

}

void loop() {

  Spannung1 = analogRead(spgn1);
  Spgn1 = ((0.005*Spannung1)-0.0703);
  dtostrf(Spgn1,10,2,Zelle1);
  strcat(Zelle1,Text);
  str_msg.data = Zelle1;
  chatter.publish( &str_msg );
  nh.spinOnce();
  delay(5000);

}



And here is the code that did not work:


int spgn1 = A5;
int ledpin = 13;
int Spannung1=0;
double Spgn1=0.0;

#include <ros.h>
#include <std_msgs/String.h>

ros::NodeHandle  nh;

std_msgs::String str_msg;
ros::Publisher chatter("chatter", &str_msg);


char Text[]="Zelle 1: ";
char Zelle1[32]="";



void setup() {
  // put your setup code here, to run once:
  pinMode(ledpin, OUTPUT);
  nh.initNode();
  nh.advertise(chatter);

}

void loop() {

  Spannung1 = analogRead(spgn1);
  Spgn1 = ((0.005*Spannung1)-0.0703);
  dtostrf(Spgn1,10,2,Zelle1);
  strcat(Text,Zelle1);
  str_msg.data = Text;
  chatter.publish( &str_msg );
  nh.spinOnce();
  delay(5000);


}

I hope you could understand what my problem is and i also hope that you can help me with that.

Thank you

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2012-05-08 07:47:09 -0500

Lorenz gravatar image

updated 2012-05-08 07:48:54 -0500

You are using strcat on a destination string that has length 10 (length of "Zelle 1: "), i.e. your destination of strcat just doesn't provide enough space and strcat might mess up your memory. This probably lets your micro controller die and the connection to it is lost.

Explicitly specify the length of the array Text and make sure that it is big enough to keep "Zelle 1: " and your voltage or use the other array you are defining.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2012-05-08 06:30:57 -0500

Seen: 1,168 times

Last updated: May 08 '12