Ask Your Question
0

Rosserial_arduino Servo Code [closed]

asked 2016-07-28 15:39:55 -0600

Auraius gravatar image

updated 2016-07-29 12:46:48 -0600

This is based on this tutorial: http://wiki.ros.org/rosserial_arduino...

For some reason, I can't run catkin_ make so I can not test this out, but it is hevily based on the tutorial (but instead of one servo there is four and instead of controlling servo angle this code has the user chose which motor to move back and forth). Can someone confirm that it works or show why it would not work?

  1. roscore 2.rostopic pub snacbotservo std_msgs/UInt16 <motor#&gt; <="" p="">

    # 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); }
    

Edit 1: This is what shows up when I attempt to compile the Arduino sketch:

In file included from /home/hcrlab/Arduino/FixedSnacbotServo/FixedSnacbotServo.ino:1:0:
/home/hcrlab/Arduino/libraries/ros_lib/ros.h:38:29: fatal error: ros/node_handle.h: No such file or directory
 #include "ros/node_handle.h"
                             ^
compilation terminated.
exit status 1
Error compiling for board Arduino/Genuino Uno.

Edit 2:

Exporting to .
Traceback (most recent call last):
  File "/opt/ros/groovy/share/rosserial_arduino/make_libraries.py", line 87, in <module>
    shutil.copytree(rosserial_arduino_dir+"/src/ros_lib", path+"/ros_lib")
  File "/usr/lib/python2.7/shutil.py", line 175, in copytree
    os.makedirs(dst)
  File "/usr/lib/python2.7/os.py", line 157, in makedirs
    mkdir(name, mode)
OSError: [Errno 17] File exists: './ros_lib'
edit retag flag offensive reopen merge delete

Closed for the following reason duplicate question by Auraius
close date 2016-07-29 13:30:38.062222

Comments

I closed this because the problem was fixed?

Auraius gravatar imageAuraius ( 2016-07-29 13:57:51 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2016-07-29 10:26:39 -0600

Steven_Daniluk gravatar image

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); 
}
edit flag offensive delete link more

Comments

Thanks for your help- as you can see I am pretty new to this. The reason I wanted to run catkin_make was because it was stated in the setup instructions and I cannot currently compile the Arduino sketch. I have edited my post to show the error message. Again, any advice?

Auraius gravatar imageAuraius ( 2016-07-29 11:52:27 -0600 )edit
1

Did you follow step 2.2 from the Arduino Setup Tutorial? It looks like it can't find the ros_lib (all the ROS stuff). Step 2.2 will copy ros_lib into a folder in your sketchbook (folder with arduino code).

Steven_Daniluk gravatar imageSteven_Daniluk ( 2016-07-29 12:13:06 -0600 )edit

I actually just fixed the catkin_make issue by adding empty documents to 2 locations- is that OK? About step 2.2- ros_lib does show up in my IDE & its associated examples too (though I can't compile those either). However, when I run make_libraries I get an error that I have just edited in my post.

Auraius gravatar imageAuraius ( 2016-07-29 12:45:36 -0600 )edit

Again, thanks for your help.

Auraius gravatar imageAuraius ( 2016-07-29 12:45:54 -0600 )edit

The issue was that the workspace was for hydro and not for groovy so I just re-did that. Anyway, thanks for your help- I really appreciated it.

Auraius gravatar imageAuraius ( 2016-07-29 13:30:24 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-07-28 15:39:55 -0600

Seen: 471 times

Last updated: Jul 29 '16