0
$\begingroup$

Rosanswers logo

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


Originally posted by mattMGN on ROS Answers with karma: 78 on 2017-04-04

Post score: 0


Original comments

Comment by NEngelhard on 2017-04-04:
looks ok. How are the motors connected? What kind of motors do you use? Which driver hardware do you use?

Comment by tonybaltovski on 2017-04-04:
Do you have feedback? PWM isn't velocity control.

Comment by mattMGN on 2017-04-05:
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

Comment by mattMGN on 2017-04-05:
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' ?

Comment by DavidN on 2017-04-05:
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?

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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


Originally posted by mattMGN with karma: 78 on 2017-04-06

This answer was ACCEPTED on the original site

Post score: 0


Original comments

Comment by [email protected] on 2018-07-22:
Hello man, have you figured it out ? I'm having the issue,and would like to know more about

$\endgroup$

Your Answer

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