0
$\begingroup$

Rosanswers logo

This is based on this tutorial: http://wiki.ros.org/rosserial_arduino/Tutorials/Servo%20Controller

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#>

    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'

Originally posted by Auraius on ROS Answers with karma: 13 on 2016-07-28

Post score: 0


Original comments

Comment by Auraius on 2016-07-29:
I closed this because the problem was fixed?

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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

Originally posted by Steven_Daniluk with karma: 606 on 2016-07-29

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by Auraius on 2016-07-29:
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?

Comment by Steven_Daniluk on 2016-07-29:
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).

Comment by Auraius on 2016-07-29:
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.

Comment by Auraius on 2016-07-29:
Again, thanks for your help.

Comment by Auraius on 2016-07-29:
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.

$\endgroup$

Your Answer

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