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

Some basic help

asked 2015-01-07 13:34:42 -0600

dshimano gravatar image

Hi, I'm using Ubuntu 14.04, Indigo.

I'm trying to get used to using ROS and robot programing. I have a code in python which will turn a servo, but I'm unsure how to "ROSatize" it. When I look online for codes written for ROS I usually see node handles, and values saved as geometry_msgs.msg Twist, publishers and subscribers. I think using the right variable (I think that's what they are) I hope to be able to do something like rosbag Twist to get the time history of the servo angle. Getting this right now will save me headache when I make a full blown robot. By writing this code for a servo, I hope to learn the basics of how these components function. I didn't find any tutorials that really explain these things.

Here's my code now:

# Servo Control
import time
def set(property, value):
    try:
        f = open("/sys/class/rpi-pwm/pwm0/" + property, 'w')
        f.write(value)
        f.close()   
    except:
        print("Error writing to: " + property + " value: " + value)


def setServo(angle):
    set("servo", str(angle))


set("delayed", "0")
set("mode", "servo")
set("servo_max", "180")
set("active", "1")

delay_period = 0.01



while True:
    angle = input('Enter Angle: ')
    setServo(angle)

This code uses some things like delayed, mode, and stuff. I just want to be able to run this using rosrun, send angles using something like rostopic pub servo std_msgs/UInt16 <angle>, and use rosbag on it.

Thanks

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2015-01-08 00:56:52 -0600

ahendrix gravatar image

When designing a ROS node, consider how it interacts with other ROS nodes.

Continuous inputs such as commands should be topics (subscribers) Configuration that doesn't change should be parameters Outputs should be topics (publishers)

You should also consider the type for each input, and make sure you choose a type which appropriately represents the data. For example, a std_msgs/UInt16 represents a single 16-bit number, while a geometry_msgs/Twist represents a motion in 3D - up/down speed, left/right speed, forward/back speed, and rotation about all three axes.

For controlling a simple servo, your input should be a single number, the desired angle. Since you're controlling a servo, we can name this topic "servo". Since this topic is a single number, you can use a type like std_msgs/UInt16 for it.

Your code will have a few major parts:

  • The setServo function (which you already have)
  • A message callback which extracts the value from the message and calls setServo
  • The ROS initialization (borrowed from the rospy subscriber tutorial)
  • A subscriber which subscribes to the servo topic, with a type of std_msgs/UInt16, and calls your callback whenever a message is received. (this can be mostly copied from the rospy subscriber tutorial, with a few modifications)

With that node running, you'll have a servo topic which you can use to control your servo through the publish command you mentioned.

You can also record the servo commands you send using rosbag: rosbag record servo Note that the rosbag command has two parts: the action (record) and the topics to record (servo).

edit flag offensive delete link more

Comments

Thanks! Is there a page that explains the input types? Like twist is for 3d motion?

dshimano gravatar image dshimano  ( 2015-01-08 11:47:49 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-01-07 13:34:42 -0600

Seen: 327 times

Last updated: Jan 08 '15