Smart Car Controller  1.0
Library to manage a two wheeled robotic vehicle (DC Motors)
Tuning PID Parameters

The next step is to use the Calibrate example sketch and the SerialStudio application (found at https://www.alex-spataru.com/serial-studio.html) to determine the motor control PID parameters. The IDE Serial Monitor can be used to view the raw numbers output from the library whilst tuning the PID loop; SerialStudio provides superior data visualization and an easier tuning process.

Calibrate allows the vehicle motors to be controlled through the Serial Monitor using a command line interface (CLI) to invoke test the functions (type ? for help text listing the functions). SerialStudio replaces the IDE Serial Monitor and adds real-time graphical plots of the PID control parameters.

Whilst setting parameters, it is a good idea to save to EEPROM using the CLI when a good set of Kp, Ki and Kd parameters are found. The saved parameters can be reloaded from EEPROM at any time, especially when changes make the PID tuning worse.

Setting up for PID Tuning

  1. Edit MD_SmartCar.h and change
    #define PID_TUNE 0
    to
    #define PID_TUNE 1
    This will enable the library to output PID control parameters as JSON data. Once tuning is completed, this defined value should be changed back to 0 to suppress output.
  2. Compile and download the Calibrate sketch with this setting turned on.
  3. Start the SerialStudio application and configure it as shown in the figure below
    • Set the Serial Parameters - COM port and baud rate - in the red highlight box.
    • Set the JSON file to 'Manual' and choose the PID.json file in the library src folder (orange highlight box). Check the Settings tab and set the Start and End sequences to '{' and '}'. This will make SerialStudio recognise the PID output data packets in the received Serial stream.
    • Connect the serial port to the vehicle (yellow highlight box).

SerialStudio has the same input functionality as the Serial Monitor. You can type commands to the vehicle application using the console input box (light blue highlight). The figure shows the result of the '?' command (help text).

PID Tuning

Once SerialStudio is set up, starting the motors from the console will automatically switch to a graphical dashboard view, shown below. This allows you to visualize the effects of changes to the speed setpoints and/or the PID parameters. You can switch between console and dashboard views using the menu options at the top of the window.

Parameters shown are Set Point (SP), Current Value (CV) and control Output (CO) for the Left and Right motors.

The default PID values in the SmartCar_HW header file should be a good starting point for the PID motor settings - expect to have a high Kp, no Ki and a relatively small Kd (this is really a PD controller). PID values can be set independently for Left and Right motors if required. In most cases the same PID setup for both motors should be adequate.

Once you are happy with the performance of the PID control loop, save the parameters to EEPROM.

Next: Testing with Remote Control