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