0
$\begingroup$

Rosanswers logo

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


Originally posted by Mantamanni on ROS Answers with karma: 11 on 2012-05-08

Post score: 1

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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.


Originally posted by Lorenz with karma: 22731 on 2012-05-08

This answer was ACCEPTED on the original site

Post score: 1

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.