I am using the ros_arduino_bridge to control a robot, by connecting the Arduino to a main pc with a usb cable. I was thinking of using the Arduino with main pc with a serial cable and then doing real time control of the Arduino by using the real time clock.

Is it possible to communicate with the Arduino in hard real time by using the real time clock and serial connections? I want to use Arduino board as a bridge between the main computer and the sensor and motors. And i want to control those sensors and motors in real time. All the high level processing tasks like computer vision and motion planning will be running in the main computer which then sends the commands to motors from the Arduino. So it is just acting like a bridge.

The reason I want hard real time is so that my robot can control its joints at very high and accurate speed so that the robot can do human level tasks like running, jumping, assembling some parts, balancing its body while moving(walking, running, jumping) at any speed (which requires the joints to be controlled at very high speed and accuracy) etc. I will be using gazebo simulator to test most of the tasks.

  • 1
    $\begingroup$ If your main computer is not running a RTOS, then I don't see the purpose of having a "hard real time" connection between the computer and arduino (and I'm not even sure what it would mean). A typical architecture would run the heavy tasks (vision, planning) on the main computer "as fast as possible" and then pass mid-level objectives to the arduino which handles the high rate/real time low level control. $\endgroup$ – combo Mar 18 '17 at 0:36
  • $\begingroup$ Sir i am using xenomial patched linux in my computer. I am also using ROS and Orocos. $\endgroup$ – user14148 Mar 18 '17 at 6:46

The short answer is no, you will not be able to create a distributed system that meets the criteria of 'hard real time' using the approach you describe.

The more nuanced answer is: It is theoretically possible with a heck of a lot of work. You will have the most problems in achieving hard real time from the USB communications, the ROS code, and using python. You will need understand the details of how USB communications work, how your USB bus is physically arranged, and use a real time capable driver like usb4rt. ROS is not real time capable, but once you have finished the difficult effort of achieving realtime in the USB communications, an orocos module to replace the functionality of ros_arduino_bridge should be easy for you. Python can be used in realtime if you are careful.

One small change you could make to simplify the amount of work would be to use direct RS232 serial communications instead of USB communications. I would expect realtime serial drivers to be easier to set up and work with.

Distributed real time systems are difficult to get right. If you are able to design your system such that there is only the main PC and no arduinos, you will simplify things and be able to make progress towards your goals much more quickly.

However, it's likely that you don't really need hard real time. After all, humans do all the tasks you want to do and we do not have hard real time processing. Many robotics algorithms have a basic assumption of a constant time period, and these are the ones that cause robots to work incorrectly when realtime processing is not maintained. It is possible to derive alternate algorithms that do the same thing, but without the constant time period assumption for most of them.


There are multiple different parts to your question.

  1. Can an Ardiuno be used "in in real-time"? Yes it can, it has hw timer interrupts, the tolerance of your oscillator will give the precision of the interrupt.

  2. Can you communicate with an Ardiuno in real-time?Yyes, you can. A synchroneus communication, e.g. SPI is realtime capable.

  3. Can you communicate with an Ardiuino over serial in real-time? The serial port is an async communication. If you implement the other protocol layers ontop if it with care (e.g. maximized message length), I assume it can be done.

  4. can you use USB to communicate with an Arduino in real-time? No. USB is not real-time capable.

All the high level processing tasks like computer vision and motion planning will be running in the main computer which then sends the commands to motors from the Arduino. So it is just acting like a bridge.

In this case, which entity performs the closed loop control? Your description suggests, that the arduino is. It is then not a simple bridge. However, sending commands to an arduino does not necesarily need real-time communication capabilites:

The PC computes the reference values for the control system, these are then transfered to the arduino over a non-realtime channel. Tha arduino reads this channel in a non-real time part (i.e. not in the interrupt) and the interrupt on the arduino runs the control loop making sure that the reference value is mainained. You only have to take care that the time required to execute the task in the interrupt is less then (and not equal) the interrupt cycle time. This way the processor can process the communication task "between" two interrupts. The question how much less dies it have to be depends on how often you get a command and how fast do you want to react to a new command.

If the communication is not part of a closed control loop (i.e. it only sends reference values for the outmost loop in a cascasded loop), it does not have to be real-time capable.


Are you willing to put an RTOS on the Arduino, or do you want to make your program deterministic with bare metal code? If you are willing to use an RTOS, there are several to choose from. For example, FreeRTOS and RTuinOS are available. Check out this Dr Dobbs article from a couple of years ago for some additional options: http://www.drdobbs.com/embedded-systems/arduino-real-time/240165395.

  • $\begingroup$ Well after using the RTOS is their something extra which i need to do, to accomplish the real time communication between arduino and pc? Can i use pyserial or pypy for this purpose? $\endgroup$ – user14148 Mar 18 '17 at 6:50
  • $\begingroup$ That question is beyond me - I would use FreeRTOS and program in the C language. Perhaps others on here can help with integrating Python in an RTOS. $\endgroup$ – SteveO Mar 18 '17 at 13:17
  • $\begingroup$ so after i use freeRTOS on arduino, send the data to pc and than read them in python using pyserial, will it be real time? I mean i will use c in arduino for RTOS and python and serial library just in the main computer to interact with the serial port. Will it be real time sir. if yes, my problem is solved. i have already patched xenomai with my pc kernel. $\endgroup$ – user14148 Mar 18 '17 at 20:13
  • $\begingroup$ Well, it will be possible for it to be real-time in the Arduino side. You need to assess whether or not you can get all the data transferred and interpreted within the timing of your deterministic loop rate. $\endgroup$ – SteveO Mar 18 '17 at 22:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy