I am currently working on a motor control project (post about it coming soon ;)) which requires a lot of parameter tuning of controllers and filters. One way would be to recompile and flash the microcontroller for each new parameter, which takes time and is quite a pain. Hence I’m working with ChibiOS which has a built in shell through serial port or USB CDC I decided to write a simple code that would enable me to change variables during runtime. In the end it was not a lot of work and I am using it, not just for parameters but also for debugging.
1 Data types
usbcdcParameterStruct_t struct contains the variable name and a pointer to the variable. I am working only with float data types, you could of course use any other type. I put the following code block into the
usbcdc.h file of ChibiOS.
2 Shell code
The module that handles the shell needs the pointer to a list of all variables and a function to set this pointer:
There are two commands
set. Each has a callback function that handles the request. The
get one is easy. It writes a list of all variables stored at the location of the
mShellVars pointer with the corresponding value.
set function is also quite straightforward. It searches the list of variable for the matching name and then converts the string to a float to store it.
To use it you only have to declare which variables you’d like to access through the shell and call the
ch> set foo 1
foo = 1
This method enables you to tweak all the necessary variables during runtime. It requires some flash storage (variable names and code) but it is definitively it. All the code is available here: https://gist.github.com/noah95/149447ccaa8a84e3b515607aad11ea82