I'm working on a robotics research project that involves programming a microcontroller. I'd like to be able to decouple testing the software from testing the hardware to the greatest extent possible. This is both to increase development speed and also so that I can more easily unit test/simulate the code before putting it on the robot. So for example, I might write a "MyRobot" library. Then I could include this library both in the embedded code and my non-embedded simulation/testing code. At runtime, I'd provide function pointers that would either read (in the embedded case) or simulate (in the simulation) sensor data and feed that into the library.
So it seems like all I would need to do is generate two libraries at compile time: one for the embedded code, and one for the non-embedded code.
My question is whether or not this is feasible/if there are better ways to do it/if there are any gotcha's I should watch out for.
Thanks in advance!
This is a common situation in embedded systems development, and your approach of creating two libraries is usually the recommended solution. It's considered a best practice to decouple the low-level hardware from the software in embedded systems firmware.
The library you mentioned is commonly known as a "Hardware Abstraction Layer", or HAL. The API (application programming interface) for the HAL can be provided in a single header file named something like hal.h. Every source module in your software that needs to access the hardware would have the following line at the top of the source file:
#include "hal.h"
Benefits of designing your system like this include:
For your two scenarios, as you suggested, you would create two versions of the HAL library: the standard version that contains the code that runs on your embedded hardware, and the simulation version that simulates the hardware for the purpose of testing your software in a controlled manner. You might name the standard library hal.lib (perhaps with a different extension depending on your development environment), and the simulation library hal_simulated.lib. Both would have the same interface, as described in hal.h. That is, both libraries would contain all functions declared in hal.h, such as void halInit(), int halReadProximitySensor(), etc.
Assuming your IDE supports Release and Debug configurations, you could create a third configuration for your software testing that is named SW_Test. This configuration would be a duplicate of your Debug configuration, except that the hal_simulated.lib would be linked into the project instead of the standard hal.lib.
See also
Hardware Abstraction (Wikipedia)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With