Getting Started SPIN 3 SerialPlot - owntech-foundation/Tutorials GitHub Wiki

Objective

The goal of this tutorial is to monitor a dummy variable via the serial port, using SerialPlot. We will start from the Serial Monitor Tutorial.

Required hardware

  • SPIN v_0_9
  • STLinkV3
  • PC 64-bits (windows or linux)

Required software

  • Git
  • Visual Studio Code with PlatformIO (see Blinky tutorial)

We will use SerialPlot to monitor variables. Install SerialPlot by going to https://hackaday.io/project/5334-serialplot-realtime-plotting-software. You will arrive at the page below.

serialPlot page

Scroll down to find the download link.

Download links

Download and install the software according to your system. In Windows, during installation, select "Do not add serialplot to the system PATH". In Linux, allow the appImage to be executed as a code.

image

Launch SerialPlot. Here is how it looks like when open.

serialplot_at_open

Create the project

  1. We will create the project by copying the Serial Monitor tutorial in a new branch, that will be called serial_plot. In the bottom menu, click on the New Terminal icon new_terminal_icon. This will open a new terminal into which you can write the following commands to create a new git branch.
git status
git add --all
git commit -m "Current work on the serial monitor branch"
git branch serial_plot
git checkout serial_plot

This will save your work (or commit it) into the previous branch and create a new one. Since you are no longer working on your main branch, you are free to add and remove content to your new branches.

  1. In the bottom menu, check that you are now in the serial_plot branch.

measurement_branch_view_1

Step-by-step implementation

  1. Define the variables

We will define a dummy variable that will be updated via the Serial.

In src/main.cpp, in the section USER VARIABLE DECLARATIONS, define the dummy variable counter.

//--------------USER VARIABLES DECLARATIONS----------------------

static uint32_t counter = 0; //counter variable

  1. Configure the hardware peripherals

In src/main.cpp, in the setup_hardware(), do not modify anything.

  1. Configure the software scheduling

In src/main.cpp, in the setup_software(), do not modify anything.

  1. Loop communication task

In the communication task, you will create the possibility of increasing or decreasing the counter dummy variable via the Serial Interface.

In src/main.cpp, in the function loop_communication_task(), add the following code.

void loop_communication_task()
{
    while(1) {
        received_serial_char = console_getchar();
        switch (received_serial_char) {
            case 'h':
                //----------SERIAL INTERFACE MENU-----------------------
	        printk(" _____________________________________\n");
                printk("|     ------- MENU ---------          |\n");
                printk("|     press i : idle mode             |\n");
                printk("|     press s : serial mode           |\n");
                printk("|     press u : counter UP            |\n");
                printk("|     press d : counter DOWN          |\n");
                printk("|_____________________________________|\n\n");
                //------------------------------------------------------
                break;
            case 'i':
                printk("idle mode\n");
                mode = IDLEMODE;
                break;
            case 's':
                printk("serial mode\n");
                mode = SERIALMODE;
                break;
            case 'u':
                printk("counter UP!\n");
                counter++;
                break;
            case 'd':
                printk("counter DOWN!\n");
                counter--;
                break;
            default:
                break;
        }
    }
}
  1. Loop application task

The application task will print the value in the counter dummy variable whenever the SERIALMODE is ON.

In src/main.cpp, in the function loop_application_task(), add the following code.

void loop_application_task()
{
    while(1){

        if(mode==IDLEMODE) {
            hwConfig.setLedOff();

        }else if(mode==SERIALMODE) {
            hwConfig.setLedOn();
            printk("%d\n", counter);
        }
        
        k_msleep(100);
    
    }
}
  1. Loop control task

In src/main.cpp, in the loop_control_task() function, do not modify anything. This control task will remain empty for this tutorial.

  1. Connect hardware (see Blinky tutorial)
  • Connect the USB power supply cable. The LED2 of the SPIN should be ON.
  • Connect the micro-JTAG connector of the SPIN to the PC thanks to the STLinkV3.

Hardware connection

  1. Build and Upload (build_icon+ flash_icon).

  2. In the bottom toolbar, click on the Serial Monitor icon serial_icon. Select it and press the h key. Press the s key: you should see the current value of counter on the Serial Monitor.

Expected outputs

  • Press the i key to switch to IDLEMODE: stop printing counter and turns the LED1 OFF.
  • Press the s key to switch to SERIALMODE: prints counter and turns the LED1 ON.
  • Press the u key to increase the counter.
  • Press the d key to decrease the counter.

Counter mode via serial Monitor

SerialPlot Visualization

We will now visualize the value of counter using SerialPlot, instead of the Serial Monitor.

  1. First kill the Serial Monitor by clicking on the trash button on the right hand side of the window as shown in the image below.

Kill serial monitor

  1. Launch SerialPlot. In SerialPlot, in the Port tab, choose the STLinkV3 and set the baud to 115200. Click on the Open button.

:warning: The Port might have a different name depending on your operating system.

serialPlot Open connection

  1. In SerialPlot, in the Data Format tab, use the following configuration.

Data Format setup

  1. In SerialPlot, in the Plot tab, use the following configuration.

plot_tab

  1. In SerialPlot, in the Commands tab, use the following configuration.

config_tuto_serialplot1

  1. In SerialPlot click on the Send button corresponding to the s command to switch to SERIALMODE and turn the LED1 ON. You should see the value of counter on your SerialPlot window. You can increase and decrease the counter, by clicking on the Send button corresponding to the u and d commands. SerialPlot will automatically update the size of the window as you change the value of the variable.

config_tuto_serialplot2

  1. In SerialPlot, click on the Send button corresponding to the i command to switch to IDLEMODE and turn the LED1 OFF. SerialPlot stops displaying the value of counter.

That’s it!

Contributors

  • 2021.11.04: Romain Delpoux, Loïc Quéval, Adrien Prévost
  • 2021.11.07: Luiz Villa, Antoine Boche
  • 2022.01.24: Luiz Villa, Loïc Quéval
  • 2022.02.01: Luiz Villa
  • 2022.02.22: Luiz Villa
  • 2022.03.13: Luiz Villa
  • 2022.06.23: Loïc Quéval, Romain Delpoux