ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

How can i run Dc motor & servo motor ..?(ROS,Arduino)

asked 2019-06-01 02:34:33 -0500

njoonz gravatar image

updated 2019-06-01 03:29:30 -0500

gvdhoorn gravatar image

Hi guys I'm making a autonomous RC car .(using ROS(odroid),arduino) But, I can't run dc motor and servo motor.

This is my arduino code.

const int motorpin=10;
const int servopin=11;

Servo servo;

class data {
  public :
  float servo_cmd
  float motor_cmd
};

data cmd_data;
ros::NodeHandle nh;

void servo_cb(const std_msgs::Float32& servo){
  servo.write(servo.data);
}


void motor_cb(const std_msgs::Float32& motor){
 analogWrite(motorpin,motor.data);
}

std_msgs::Float32 servo_cmd;
std_msgs::Float32 motor_cmd;
ros::Subscriber<std_msgs::Float32>ordtoard_servo("servo",servo_cb);
ros::Subscriber<std_msgs::Float32>ordtoard_motor("motor",motor_cb);

void setup() {
pinMode(motorpin,OUTPUT);
servo.attach(servopin);


nh.initNode();
  nh.subscribe(ordtoard_servo);
  nh.subscribe(ordtoard_motor);
}

void loop(){
}

why cannot i run this code..?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2019-06-01 03:31:23 -0500

gvdhoorn gravatar image

updated 2019-06-01 03:33:09 -0500

void loop(){
}

why cannot i run this code..?

The code probably runs just fine, but to handle incoming messages (ie: process event queues), rosserial needs you to call nh.spinOnce() periodically.

You're not doing that.

You'll want to:

  1. call nh.spinOnce() in loop(), and
  2. add a delay(..) in there to control how often loop() is called

Refer to the Hello World tutorial on the rosserial_arduino/Tutorials page.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-06-01 02:29:38 -0500

Seen: 370 times

Last updated: Jun 01 '19