0
$\begingroup$

Rosanswers logo

I'm working on my arduino mega 2560 trying to alocate a float values that a flex sensor gives to arduino, but when i try lo read the topic it don't runs, only gives to me this message when i run:

jose@ubuntu:~$ rosrun rosserial_python serial_node.py /dev/ttyACM1
[INFO] [WallTime: 1353426930.914839] ROS Serial Python Node
[INFO] [WallTime: 1353426930.917873] Connected on /dev/ttyACM1 at 57600 baud
[INFO] [WallTime: 1353426933.336893] Setup Publisher on angulos [std_msgs/Float32MultiArray]
[ERROR] [WallTime: 1353426945.920335] Lost sync with device, restarting...
[INFO] [WallTime: 1353426946.190745] Setup Publisher on angulos [std_msgs/Float32MultiArray]
[ERROR] [WallTime: 1353426960.920971] Lost sync with device, restarting...
[INFO] [WallTime: 1353426961.049990] Setup Publisher on angulos [std_msgs/Float32MultiArray]
[ERROR] [WallTime: 1353426975.923001] Lost sync with device, restarting...
[INFO] [WallTime: 1353426976.409013] Setup Publisher on angulos [std_msgs/Float32MultiArray]
[ERROR] [WallTime: 1353426990.927987] Lost sync with device, restarting...
[INFO] [WallTime: 1353426991.268148] Setup Publisher on angulos [std_msgs/Float32MultiArray]
[ERROR] [WallTime: 1353427005.932885] Lost sync with device, restarting...
[INFO] [WallTime: 1353427006.127405] Setup Publisher on angulos [std_msgs/Float32MultiArray]

code in arduino:

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

    ros::NodeHandle nh;

   std_msgs::Float32MultiArray angulos;
   ros::Publisher angulos_pub("angulos", &angulos);

    int findice=1;
    int fcorazon=2;
    int fanular=3;
    int fmenique=4;
    int fpulgar=0;
    int vind = 0;//valor que sensa el flex sensor valor en bits
    int vcor = 0;//valor que sensa el flex sensor valor en bits
    int vanu = 0;//valor que sensa el flex sensor valor en bits
    int vmen = 0;//valor que sensa el flex sensor valor en bits
    int vpul = 0;//valor que sensa el flex sensor valor en bits
    float radind;
    float radcor;
    float radanu;
    float radmen;
    
      void setup(){
      nh.initNode();
      
      angulos.layout.dim = (std_msgs::MultiArrayDimension *)
      malloc(sizeof(std_msgs::MultiArrayDimension) * 1);
      angulos.layout.dim[0].label = "angulos";
      angulos.layout.dim[0].size = 4;
      angulos.layout.dim[0].stride = 1*4;
      angulos.layout.dim_length = 1;
      angulos.layout.data_offset = 0;
      
      angulos.data = (float *)malloc(sizeof(float)*4);
      angulos.data_length = 4;
      nh.advertise(angulos_pub);
      
      
          
    }
     
    void loop(){
      
       vind =analogRead(findice);
       vcor =analogRead(fcorazon);
       vanu =analogRead(fanular);
       vmen =analogRead(fmenique);
       
       radind = ((445-(vind))/(50.7));
       radcor = ((449.85-(vcor))/(55.41));
       radanu = ((479.1-(vanu))/(56.56));
       radmen = ((414.95-(vmen))/(95.76));
       angulos.data[0]= radind;
       angulos.data[1]= radcor;
       angulos.data[2]= radanu;
       angulos.data[3]= radmen;

       angulos_pub.publish(&angulos);
       nh.spinOnce();
       delay(500);
       
    }

Originally posted by joseescobar60 on ROS Answers with karma: 172 on 2012-11-20

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

Your device is probably segfaulting and keeps rebooting. The reason probably is that you are writing to unallocated memory when accessing the data slot of your message. You need to set the data slot to a pointer to allocated memory before you write the message and publish it. Have a look at this page for more information on how to use arrays in rosserial.


Originally posted by Lorenz with karma: 22731 on 2012-11-20

This answer was ACCEPTED on the original site

Post score: 0

$\endgroup$

Your Answer

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