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

Revision history [back]

click to hide/show revision 1
initial version

The code above is meant to be compiled and loaded on a microcontroller with the Arduino IDE, so you do not need to use catkin_make.

I don't have a microcontroller near me currently so I can't test it. But I did look through it and try compiling it with the Arduino IDE. There were a lot of syntax errors, but with those fixed I don't see why it wouldn't work.

Also, I'm not sure if some of the syntax errors were caused by copying-pasting the code here, but when you test it your command line argument should be rostopic pub snacbotservo std_msgs/UInt16 1 for servo 1. (Don't forget to start rosserial, as indicated in the tutorial).

Your code with the errors fixed is below:

# include <ros.h>

# if defined(ARDUINO) && ARDUINO >= 100 
  #include "Arduino.h"
# else 
  #include <wprogram.h>
# endif

# include <Servo.h>
# include <std_msgs/UInt16.h>

ros::NodeHandle  nh;

Servo servos[4]; 
int pins[4] = {10,6,11,9}; 
int pos = 10;

void open_close(const std_msgs::UInt16& cmd_msg){   
  int i = cmd_msg.data; // set motor 0-3

  for (pos = 10; pos <= 80; pos += 1) {
    if (i==1 || i==2) {
      servos[i].write(180-pos);
      delay(10);
    }
    else {
      servos[i].write(pos);
      delay(10);
    }   }

  delay(1000);

  for (pos = 150; pos >= 10; pos -= 1) {
    if (i==1 || i==2) {
      servos[i].write(180-pos);
      delay(10);
    }
    else {
      servos[i].write(pos);
      delay(10);
    }   } }

ros::Subscriber<std_msgs::UInt16> sub("snacbotservo", open_close);

//lose

void setup() {   
  nh.initNode();   
  nh.subscribe(sub);   
  int i;   
  for (i=0; i<4; i++) {
    servos[i].attach(pins[i]);   
  } 
}

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