IMU Data Reading
The following page will help you read real-time data from an Inertial Measurement Unit (IMU) using the STM32CubeIDE.
Background Knowledge
Before the 2021 competition season, the team figured it would be incredibly convenient to see the data read from our sensors in real-time. In our case, we want to be able to read values from our onboard IMU. To read real-time data from the IMU on board our drone - BMX160 in our case - we found the easiest way is using a built-in function in the CubeIDE itself.
Other Attempts
Several other attempts at reading the data included communication over UART and using a signal analyzer. Neither produced optimal results.
Before we Start
Make sure that the values you want to trace/track are declared globally before you run the program (i.e. declare them outside the main loop ).
Reading Output
Using the Serial Wire Viewer (SWV) tool built into the CubeIDE, we can track variables that change with each iteration of our program, allowing us to read real-time data. To begin, open the “debug configurations” menu by going to run->debug configurations.
Once in the menu, navigate to the “Debugger” section and scroll down until you see an option to enable SWV. Set the clock to match the core you’re using (I used 48MHz, but I am not sure how this changes when using different values), and you should be able to leave the port value alone.
Click debug. Once the debug starts running, terminate it. Open SWV by going to window->show view->Other. Once here, open “SWV Trace log” and “SWV Data Trace.”
You should now see the Trace log and Data Trace menus on your screen. Navigate to the Trace Log menu, and select “Configure trace.” The comparator section of the menu below is what we will use to track our variables. Enable whichever comparator you like, and write the name/address of the variables you would like to track - note you can set up as many as four comparators for tracking!
Click ok. Select the “trace” button right next to configure (it almost looks like a red ribbon). Now you can run/resume the debug configuration. Navigate to SWV Data Trace, and should you be good to go! Here you can see your variables update with each iteration of the while loop!
Common Errors
You might get an error such as the following if you ran/configured your comparators to track specific variables before:
Click ok and select the trace button right next to configure, as you did before.
Misunderstanding the units we use is a common error. See below:
Forgetting to declare make the variables you want to track globally:
Using types greater than four bytes:
Limitations
Availability: The debugger can only track four variables at a time, making it hard to track/manage everything in the IMUData_t struct.
Display size: The debugger can only track variables of 1-4 bytes in size. Given each variable in IMUData_t is of type double (8 bytes), we have to use float, effectively removing several digits of precision. While this isn’t a huge issue, we might want to find a better way for more precise measurements.
Related Articles/Useful Guides
A significant portion of this document was possible due to a post available on STM’s website. The post details how to use SWV for debugging and tracking variables: https://wiki.st.com/stm32mpu/wiki/How_to_debug_with_Serial_Wire_Viewer_tracing_on_STM32MP15