Ask Your Question
0

How to use rospublisher to publish a byte value

asked 2016-11-11 02:32:59 -0500

zyb1777 gravatar image

I did a s.bus reading on ardiuno, it can work perfectly when i link to ROS, but there is problem with my code:

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

#include <FUTABA_SBUS.h>
#include <Streaming.h>


int maximumRange = 400; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance
byte localdata[25] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
double Kp=0, Ki=0, Kd=1;
double Setpoint, Input, Output;
int i=0;

FUTABA_SBUS sBus;

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

void sbus_encoder()
{
   localdata[0] = sBus.sbusData[0];
   localdata[1] = (sBus.channels[0]  );
   localdata[2] = (sBus.channels[0]>>8 | sBus.channels[1]<<3);
   localdata[3] = (sBus.channels[1]>>5 | sBus.channels[2]<<6);
   localdata[4] = (sBus.channels[2]>>2);
   localdata[5] = (sBus.channels[2]>>10 | sBus.channels[3]<<1);
   localdata[6] = (sBus.channels[3]>>7 | sBus.channels[4]<<4);
   localdata[7] = (sBus.channels[4]>>4 | sBus.channels[5]<<7);
   localdata[8] = (sBus.channels[5]>>1);
   localdata[9] = (sBus.channels[5]>>9 | sBus.channels[6]<<2);
   localdata[10] = (sBus.channels[6]>>6 | sBus.channels[7]<<5);
   localdata[11] = (sBus.channels[7]>>3);
   localdata[12] = (sBus.channels[8]  );
   localdata[13] = (sBus.channels[8]>>8 | sBus.channels[9]<<3);
   localdata[14] = (sBus.channels[9]>>5 | sBus.channels[10]<<6);
   localdata[15] = (sBus.channels[10]>>2);
   localdata[16] = (sBus.channels[10]>>10 | sBus.channels[11]<<1);
   localdata[17] = (sBus.channels[11]>>7 | sBus.channels[12]<<4);
   localdata[18] = (sBus.channels[12]>>4 | sBus.channels[13]<<7);
   localdata[19] = (sBus.channels[13]>>1);
   localdata[20] = (sBus.channels[13]>>9 | sBus.channels[14]<<2);
   localdata[21] = (sBus.channels[14]>>6 | sBus.channels[15]<<5);
   localdata[22] = (sBus.channels[15]>>3);
   localdata[23] = sBus.sbusData[23]; //flag
   localdata[24] = 0x00;

   for(int i=0;i<25;i++)
      {
        //Serial1.write(localdata[i]);
          str_msg.data = localdata[i];
          chatter.publish(&str_msg);
      }
}



void setup(){
  sBus.begin();
  Serial.begin(115200);


   nh.initNode();
   nh.advertise(chatter);


   // sBus.channels[0]=990;
}

void loop(){

  sBus.FeedLine();
  sBus.PassthroughSet(0);
  if (sBus.toChannels == 1){
   //sBus.UpdateServos();
    sBus.UpdateChannels();
    sBus.toChannels = 0;
    if(sBus.channels[5]>1500)//limit
{
     if(sBus.channels[0]>1000)//throttle limit
     { sBus.channels[0]=1000;}
     if(sBus.channels[1]<940)//left yaw limit
     {sBus.channels[1]=940;}
      if(sBus.channels[1]>1040)//right yaw limit 
     {sBus.channels[1]=1040;}
     if(sBus.channels[2]<940)//pitch infront limit
     {sBus.channels[2]=940;}
      if(sBus.channels[2]>1040)//pitch backwards limit
     {sBus.channels[2]=1040;}
     if(sBus.channels[3]<940)//roll left limit
     {sBus.channels[3]=940;}
      if(sBus.channels[3]>1040)//roll right limit
     {sBus.channels[3]=1040;}     
    }



sbus_encoder();




  }

}

This is the error shown:

sbus_example.pde: In function ‘void sbus_encoder()’:
 sbus_example.pde:56:35: error: invalid conversion from ‘byte {aka unsigned char}’ to ‘std_msgs::String::_data_type {aka const char*}’ [-fpermissive]

How can i convert this code so that the publisher ... (more)

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2018-03-26 10:12:23 -0500

krl101 gravatar image

Well, i think your for loop does not make what you want it to do. With:

str_msg.data = localdata[i];

You will copy one byte (with the given index) into the hole string, which couses the given error, which will be published. Maybe you convert the hole byte-array into an string.

Or you can append one byte after the other to your std_msg.data - string

std_msg.data.append(localdata[i]); // someting like that

and publish the hole string (with your string termination) at once.

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

2 followers

Stats

Asked: 2016-11-11 02:32:59 -0500

Seen: 419 times

Last updated: Mar 26 '18