Ask Your Question
0

Implementing a subscriber with std_msgs::Int16

asked 2020-03-10 17:42:26 -0500

Dragonjinx gravatar image

Hello. I am new to ros and rosserial in general. After going through the beginner tutorials on rosserial-arduino, I decided to modify the blink example that comes with the ros library so that whenever I enter the following in the terminal:

rosoptic pub Led_Brigghtness std_msgs/Int16 x (where x is between 0 and 100)

it maps it into the range of arduino analog values and then changes the brightness of the led. However, I am having problems in declaring a suitable subscriber. I was previously about to create a custom message for this but after I discovered that std_msgs has int data types, I figured I could use those instead. I have the following code:

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

ros::NodeHandle  nh;
int Control_Pin = 3;
std_msgs::Int16 User_Input;

void messageCb( std_msgs::Int16 & Brightness){
  int brightness = map(User_Input.data, 0, 100, 0, 254);
  analogWrite(Control_Pin, brightness);   // blink the led
}

ros::Subscriber<std_msgs::Int16> sub("Led_Brightness", &User_Input);

void setup()
{ 
  pinMode(Control_Pin, OUTPUT);
  nh.initNode();
  nh.subscribe(sub);
}

void loop()
{  
  nh.spinOnce();
  delay(1);
}

When I try to compile the code, I get an error on the segment:

ros::Subscriber<std_msgs::Int16> sub("Led_Brightness", &User_Input);

that says:

no matching function for call to 'ros::Subscriber<std_msgs::Int16>::Subscriber(const char [15], std_msgs::Int16*)

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-07-19 10:39:33 -0500

Hi, you defined your function name as User_Input but you do not have a definition for that name. Instead you have messageCB.

you should correct your function names to be identical, one of them should change to match each other.

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

1 follower

Stats

Asked: 2020-03-10 17:42:26 -0500

Seen: 239 times

Last updated: Mar 10 '20