Ask Your Question
0

Subscribe cmd_vel with Arduino

asked 2017-04-04 11:36:12 -0500

mattMGN gravatar image

updated 2017-04-04 11:59:29 -0500

NEngelhard gravatar image

Hello,

I would want to send PWM signal with an arduino in order to control motors. But, as a beginner, I am not exactly sure of what to do. I need first to publish "cmd_vel". Then I need to write a node in Arduino which subscribe to "cmd_vel", right ?

If so, I am not confident with the code I wrote :

  include "ros.h"

  include "geometry_msgs/Twist.h"

 ros::NodeHandle  nh;

 void velCallback(const geometry_msgs::Twist& vel) {   
     geometry_msgs::Twist new_vel = vel;   
     // HERE I WANT TO USE vel TO DEFINE PWM  }

 void setup() {   
     nh.initNode();  
     ros::Subscriber<geometry_msgs::Twist>  ("cmd_vel", &velCallback); 
}

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

It is the right way to do ? And is that code correct ?

PS : Sorry but I don't how to properly insert cpp code citation

Regards,

Matt

edit retag flag offensive close merge delete

Comments

looks ok. How are the motors connected? What kind of motors do you use? Which driver hardware do you use?

NEngelhard gravatar image NEngelhard  ( 2017-04-04 12:00:41 -0500 )edit

Do you have feedback? PWM isn't velocity control.

tonybaltovski gravatar image tonybaltovski  ( 2017-04-04 12:10:50 -0500 )edit

I don't want to make feedback control yet. I just want to use cmd_vel, like analogWrite(cmd_vel.linear.x). But currently when I run roscore, compile this arduino script, and run "rosrun rosserial_python serial_node.py /dev/ttyACM0". I just get this message : Connecting to /dev/ttyACM0 at 57600 baud

mattMGN gravatar image mattMGN  ( 2017-04-05 02:31:22 -0500 )edit

Someone know why I am stuck to 'Connecting to /dev/ttyACM0 at 57600 baud' ? And why i can't see my node working with 'rostopic list' ?

mattMGN gravatar image mattMGN  ( 2017-04-05 10:25:43 -0500 )edit
1

Firstly , you should update the question with what you tried and any new error message you have. Secondly, can you check if the serial node is working and sending out data?

DavidN gravatar image DavidN  ( 2017-04-05 21:30:11 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2017-04-06 10:23:10 -0500

mattMGN gravatar image

updated 2018-03-08 11:00:04 -0500

ahendrix gravatar image

I finally find the right syntax to define a publisher

#include "ros.h"

#include "geometry_msgs/Twist.h"

float x; 

ros::NodeHandle nh;

void velCallback(  const geometry_msgs::Twist& vel)
{
     x = vel.linear.x - 1.0; // I CAN USE VEL AS I WANT
}

ros::Subscriber<geometry_msgs::Twist> sub("cmd_vel" , velCallback);

void setup() {
     nh.initNode();
     nh.subscribe(sub);
}

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

Thanks for support

PS : I am still don't knwo how to properly insert cpp code citation

edit flag offensive delete link more

Comments

Hello man, have you figured it out ? I'm having the issue,and would like to know more about

hamzh.albar@gmail.com gravatar image hamzh.albar@gmail.com  ( 2018-07-22 01:23:43 -0500 )edit

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: 2017-04-04 11:36:12 -0500

Seen: 1,862 times

Last updated: Mar 08 '18