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

How can I control servo motor using ROS and python/c++?

asked 2023-07-04 08:22:20 -0500

JWShalom gravatar image

Hello,

I am relatively new to ROS2 and I have basic knowledge of Python and C++. I want to make a robotic arm using ROS2, but I do not know much about the hardware and the integration between hardware and software. I have several confusions and questions that I wanted to get some help. Please bear with my questions may not make sense, as I don't know.

  • Do I need an Arduino or Rasberry Pi to control the servo motor?
  • If, yes, then I heard that Arduino and Rasberry Pi have their own language. How can I use Python or C++ to control the servo motor instead of the default language?
  • In addition, if I can purely control the servo motor with just the Arduino or Rasberry Pi, how does the ROS2 come into play (if ROS2 was not needed to control the motor)?
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-07-06 10:09:39 -0500

CCM gravatar image

Hi

Commomly, servos are controlled by PWM. So you'll need a piece of hardware which offers such an interface (Pin). Rasperry Pi as well as Arduino will do (see links for more detailed explaination and exemplary tutorials on how to do this without ROS). On a Pi you can use Python. On an arduino you will basically write C++.

On a current Pi4 ROS2 should be able to install and run ROS natively, so you can just install it write a ROS node which controlls the servo directly. To interface ROS from lower level devices you can use micro-ROS if your plaform is supported (see link). If not supported like an arduino uno you will have to use a ROS node on a regular PC (or Pi...) and control the arduino with a common interface such as serial. The proper way to do so would be implementing a ROS2 control hardware interface such as done here but for a start it might be easier for you just to use the arduino firmware and a serial library (Serial lib (C++) or PySerial(Python)) in your ROS node e.g. if you just want a simple subscriber to servo position topics.

Finally, the question will be how do you want to control your Robot? Will there by any kind of other sensors and/or user input (e.g A gamepad, a camera with image processing, etc...)? Those will require other pieces of hard- and software, possibly on other computers connected to a network. Connecting them to your robot and keeping everything modular is where ROS will come into account. Otherwise it could also be that ROS would be overkill for your application.

From your questions it seems you will have quite a bit to look into. I hope the links provided will serve as a good start.

edit flag offensive delete link more

Comments

A great answer @CCM, I would just like to add on top of all this that if you are using a cheap servo motor(SG90s) and an arduino, you would also want to take a look at smoothing the servo jitter - this occurs mainly because the standard servo library for arduino does not use a hardware timer peripheral. There are many ways of fixing this, I would let the OP take a look at how they choose to implement. But it remains that in order to have smooth control of your servo, you would need to tackle this issue at some point.

sampreets3 gravatar image sampreets3  ( 2023-07-07 04:36:15 -0500 )edit

Question Tools

Stats

Asked: 2023-07-04 08:22:20 -0500

Seen: 914 times

Last updated: Jul 06 '23