Robotics StackExchange | Archived questions

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

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.

Asked by JWShalom on 2023-07-04 08:22:20 UTC

Comments

Answers

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.

Asked by CCM on 2023-07-06 10:09:39 UTC

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.

Asked by sampreets3 on 2023-07-07 04:36:15 UTC