Rosanswers logo

I intend to use STM32 for communicating with ROS , rosserial look promising. Since arduino, raspberrypi can do. Other should work also. I've seen Adding Support for New Hardware , IDEs , STM32 Microcontroller for communication with ROS

I can read/write function uart to STM32 which use GNU ARM toolchain compiler and library from ST with Eclipse. Also I can import package that publish and subscribe message into Eclipse. Those import package in Eclipse can RUN, DEBUG no problem so far..

But NO IDEA how to somehow mix 2 together , ros packge and stm32 source , (#include "ros/ros.h" and stuff into STM32 source) and build them to flash into STM32 microcontroller. I intend to create node in STM32 and subscribe sensor to PC or SBC run ROS or Subscribe some motor control or other control. Since it has more more memory and horse power than arduino. Also benefit of RTOS

Any help please?

Originally posted by nquantum on ROS Answers with karma: 46 on 2017-12-05

Post score: 0


1 Answer 1


Rosanswers logo

"Update: add example subscribe and publish to main.cpp"

During wait for answer I've search and try some. I think I got it working now. Maybe not perfect since I'm still learning C++

Here is my file: https://github.com/nquantum/stm32_rosserial image description

from install for arduino: http://wiki.ros.org/rosserial_arduino/Tutorials/Arduino%20IDE%20Setup

  1. I install rosserial libraries on PC-Linux sudo apt-get install ros-kinetic-rosserial sudo apt-get install ros-rosserial-arduino

  2. building Arduino library >> after this we get ros_lib for our microcontroller , can copy to linux or windows cd <some_empty_directory> rosrun rosserial_arduino make_libraries.py .

  3. get ros_lib ready to import to Arduino or other micro

  4. get working project & set preprocessor c , c++ > I use from cubeF4 example project uart_polling to make sure usart work

  5. copy ros_lib into project

  6. exclude from build , right click folder then untick exclude so it will compile this source in this folder

  7. include ros_lib in project property for c, c++

  8. rename main.c to main.cpp

  9. in main try #include <ros.h> compile error because it for arduino ros.h,ArduinoHardware.h, ArduinoTcpHardware.h

  10. replace ros.h and copy STM32Hardware.h

  11. adjust usart parameter in main.h to usartx, x = your number , I should move these parameter to STM32Hardware.h or from STM32Hardware.h to main.h to make it centralize. Maybe later...

test 1st terminal

  1. roscore

2nd terminal

  1. rosrun rosserial_python serial_node.py _port:=/dev/ttyACM0 _baud:=57600

3rd terminal

  1. rostopic list
  2. rostopic echo /chatter

Now you can see from those topic. Which I adjust from Hello World (example publisher) - Arduino : http://wiki.ros.org/rosserial_arduino/Tutorials/Hello%20World

Also reference from AVR and Adding Support for New Hardware http://wiki.ros.org/rosserial_client/Tutorials/Using%20rosserial%20with%20AVR%20and%20UART http://wiki.ros.org/rosserial_client/Tutorials/Adding%20Support%20for%20New%20Hardware

Another example: https://github.com/bosch-ros-pkg/stm32 https://github.com/spiralray/stm32f1_rosserial These two also interface stm32 to rosserial but they use library C++ of HAL. I see it not convenience for me , new in C++ and I like to use ST HAL which can easy import to Eclipse and generate from CubeMX.

Let me know if it work or recommendation. Since I now learn to make it work with FreeRTOS. I'm also new in ROS so still learn from tutorial.

Now is more happy coz since Arduino Uno have SRAM 2048 or 2K, simple Hello World already take 1358 or 66%. If I try more code it take 70% above then it unable to communicate with rosserial already. But now I use STM32F469 Discovery Board have SRAM 320K with Hello World take for 1.3K-1.5K I've plenty of room to play with plus power of 32bit and HAL library.

I use HAL_UART function in polling mode in client-side rosserial implementation. Maybe interupt mode should be better. I don't know if/how I can use interupt mode for the rosserial coz it seem like function read() and void write(uint8_t* data, int length) get call periodically. Shold I interupt read then place in buffer and read from those buffer? Anyway so far it work in polling..


"Lost sync with device, restarting"

  • can communicate but maybe set timeout for write/read too long so spinonce() timeout

"Unable to sync with device; possible link problem or link software version mismatch such as hydro rosserial_python with groovy Arduino"

  • from can not communicate to those com port maybe set wrong com port or read/write function not work or wrong baud rate host<>board

  • Since using polling , uart timeout too long and too short will not successful and get error in rosserial. In those website I overview they seem to use interupt with ring buffer I will try to improved my code later

STM32 Discovery board have virtual comport very convenience just use those com port then plug-in usb to host pc then communicate!

Originally posted by nquantum with karma: 46 on 2017-12-22

This answer was ACCEPTED on the original site

Post score: 1

Original comments

Comment by elgarbe on 2017-12-22:
What a great news!!!! you got it working!

I will try it this weekend with my STM32F4Discovery and let you know the result.

Thank for sharing!

Comment by elgarbe on 2017-12-24:
wich IDE are you using? I'm stuck on step 4, 6 and 7. Becouse I'm using SystemWorkbench and those options are not available. Can you explain a litle more that steps, please?


EDIT: I get it working, comipilng part. I get Unable to sync.... error.... I'm debuging now...

Comment by elgarbe on 2017-12-24:
It stuck on virtual int publish(int id, const Msg * msg) { if (id >= 100 && !configured_)

Id=125 and configured is false...

Comment by elgarbe on 2017-12-24:
IT'S WORKING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! What a Christmas present!

Please, keep us informed about your progress!

Comment by nquantum on 2017-12-24:
Congratulation! More room open now with STM32! I'm open for any suggest/improve. It only two source link to rosserial: ros.h and STM32Hardware.h Now I tend to move from poll to interupt with buffer, then DMA since lost sync come often use with FreeRTOS. I'm brand new so it will take sometime

Comment by nquantum on 2017-12-24:
Order new toy, SQ200 Logic Analyzer from IKALOGIC so let see what hidden bug here! :-p

Comment by nquantum on 2017-12-24:
For the ref. I use Eclipse. Step4 is to config preprocessor-compiler the define ,include folder in both c and c++ since we will compile both source. Step6,7 is for eclipse, when e create new folders in eclipse project, by default it excludes for build so we set it include.

Comment by nquantum on 2017-12-24:
"Unable to sync" Mostly mean well... can not sync or communicate due to uart can not transmit and/or receive or wrong baudrate/port invote to rosserial_python parameter. Make it computer can't "talk" to uc

Comment by nquantum on 2017-12-24:
t stuck on virtual int publish(int id, const Msg * msg) { if (id >= 100 && !configured_) Id=125 and configured is false... I think this relate to your IDE setting I've no idea how u solve.

Comment by elgarbe on 2017-12-24:
Are you using STM32F4Discovery? I'm creating a CubeMX project and follow your steps. So going to Interrupt or DMA could be easy. Then only problem is with Error_Handler code. I'm not using freeRTOS. I'm noob in C++ too.

Comment by nquantum on 2017-12-31:
make some adjust:

  • now change UART 3,6 port only in STM32Hardware.h , I adjusted stm32f4xx_hal_msp.c
  • good in polling mode next I will move to interupt mode
  • example hello world
  • code now generate from cubemx

Comment by nquantum on 2017-12-31:
@elegarbe , At beginning I use example uart from STM32F4 Cube and adjust that or those.. Now I push new code that all generate from CubeMX along with rosserial library and hello world in polling mode work well. Now if want to swich uart6 to uart3 by adjust 1file: STM32Hardware.h

Comment by nquantum on 2017-12-31:
@elegarbe What problem you face with Error_Handler code?

Comment by nquantum on 2018-01-02:
finish dma mode, check at dma branch on github. Choose between uart3 or 6 by comment out. Choose between polling, interrupt, dma by also comment out. Default at dma branch is dma mode rx and tx, and uart6

Comment by elgarbe on 2018-01-02:
Excelent! I follow your work, I can't test it right now, but I will be using it soon. Thank for Share!

Comment by nquantum on 2018-01-02:
Welcome. Now I try adapt these mode interface with FreeRtos. Seem I've lot to read. I don't know how I interrupt interact with FreeRtos. Since all mode seem to miss usart receive sometime. Sometime not work is not good.

Comment by nquantum on 2018-01-03:
Once I can pass this. Hope to interface stm32 wit SLAM do some navigate with PC or SBC..

Comment by elgarbe on 2018-01-04:
That would be great! I'm working with Intel Edison and flytOS...

Comment by elgarbe on 2018-03-17:
Had you made any progress? please, keep us informed!

Comment by winnie on 2018-08-01:
So,final you decided to use the arduino? Instead of stm32 board?

Comment by nquantum on 2018-08-01:
I still think stm32 better in long run. More ram, more process power, price almost same. So can do more. Arduino might be good for small test. But I run into limit 1-2node very fast. It seem to workable to run into stm32 already so I pause projects a while to build CNC for my nxt robot in future.

Comment by bryantan on 2019-04-10:
Hi, I'm also trying to link my STM32F4 discovery board to ros, but i'm getting the same error too (Unable to sync with device; possible link problem or link software version mismatch such as hydro rosserial_python with groovy Arduino). I've checked that the baud rate matches in my codes (i'm using 9600 and USART2), and i've also checked the oscillator HSE in my header files also match, so I'm really confused what else could cause this error. Anyone has any ideas?

Comment by marinePhD on 2019-04-10:
@bryantan I'm also trying to do something similar with the f7 discovery and I'm running into the main issue. I came across this post here look at Number 24. It didn't work for me but maybe it'll work for you.

Comment by elgarbe on 2019-04-10:
I've managed to make it work on a custom board with a F7 uC. What do you need to know? maybe open a new question and I can answer there.

Comment by marinePhD on 2019-04-10:
@elgarbe Yeah, I've been meaning to tag you in this as I saw you opened an issue here: https://github.com/yoneken/rosserial_stm32/issues/4. I opened a new topic here

Comment by bryantan on 2019-04-12:
@elgarbe i'm trying to use the same method as you did, creating a CubeMX project that enables the USART (in my case USART2). Do you think using the polling mode example instead will help?

And also, on top you posted you got the unable to sync error as well as this error message: It stuck on virtual int publish(int id, const Msg * msg) { if (id >= 100 && !configured_) Id=125 and configured is false... How did you resolve these errors?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.