PID Control of Robotics Connection Serializer Stinger

Description

In this project, I controlled the Serializer Stinger using PID control theory. Components for this project are following:

  • Mbed Serial Adapter
  • Serializer Stinger
  • Battery for Stinger( 12V - 2A )
  • SD card

The purpose of this project is learning and implement PID on a mobile robot base. The mobile robot base will move in a circular repetitive motion. Serializer Stinger is a good robot to start with because it has embedded PID control algorithm. This tutorial assume the views have a basic understanding of programming on mbed and PID control theory. /media/uploads/dustsnow/serializer_stinger_360x480.jpg /media/uploads/dustsnow/breadboard_640x480.jpg

Wiring Diagram

MBedSerial Adapter
P28Rx
P27Tx
VCCVCC
GNDGND

Connect serial adapter and Serializer Stinger with serial cable and null modem if necessary.

Serializer Stinger

Serializer Stinger is a well developed system. The brain is a board having two H-bridge. a micro-controller, Control of the robot is by sending commands through several kinds of protocol. In this project, I use RS232 serial to do this.

Several common commands are introduced here. For all commands, refer to the user manual. The syntax of command is a command name followed by parameters. Every command must be terminated with "Carriage Return" character(<CR>).

1. fw<CR>

check the firmware version.

2. blink 1:60<CR>

blink led1 at frequency 60.

3. vel<CR>

query the velocity of two wheels.

4. vpid<CR>

Query the velocity PID constants.

5. vpid 10:0:5:10<CR>

Set velocity PID constants

6. mogo 1:30 2:45<CR>

Set the velocity of motor 1 by 30 ticks/seconds and motor 2 by 45 ticks/seconds

7. digo 1:1000:25 2:1000:25<CR>

Move distance 1000 ticks at velocity 25

Equipped with these commands, we're able to control Stinger. Here is a snippet code to send command to Stinger

main.cpp

#include "mbed.h"

Serial uart(p28,p27);

int main() {
    while (1) {
        wait(1);
        uart.baud(19200);//The default baud rate of Stinger is 19200
        uart.printf("blink 1:0\r");//blink led1 at frequency 0, which means off
    }
}

Tunning Code Setup

Using mbed to send velocity command to Stinger and read velocity of each motor from Stinger. Using these data plots the system response in Matlab. The partial code for mbed:

main.cpp

    uart.printf("mogo 1:20 2:20\r");//let motor run at velocity 20
    t.start();//start the timer
    while(1){
        uart.printf("vel\r");// Send velocity command
        //Get response from Stinger
        i = 0;
        while( (tmp = uart.getc()) != '>' ){
            if( tmp != 13 && tmp != 10 ){
                buf[i++] = tmp;
            }
        }         
        double jj1 = (buf[0]-'0')*10+(buf[1]-'0');// cast char to int 
        double jj2 = (buf[3]-'0')*10+(buf[4]-'0');// cast char to int
        pc.printf("20 %f %f %f ", jj1,jj2, t.read());// send data to host computer
        // The number of data to collect.
        if(j < 100){
            j++;
        }else{
            break;
        }            
    }
    t.stop();

The matlab code to get the data from serial port

serial.m

	s = serial('COM5');
	set(s,'InputBufferSize',10240);
	set(s,'BaudRate',9600);
	if strcmp(s.Status, 'closed')
	    fopen(s);
	end
	j = fscanf(s,'%f %f %f %f',[4,2000]);
	fclose(s);
	delete(s);
	clear s;

Code to plot data in Matlab

plot.m

	data = transpose(j);
	[row col] = size(data);
	target = data(:,1);
	wheel1 = data(:,2);
	wheel2 = data(:,3);
	time = data(:,4);
	plot(time,target,'r');hold on;
	plot(time,wheel1,'b');
	plot(time,wheel2,'g');
	axis auto;
	hold off;

Tuning PID constant

The major job in this project is tuning velocity PID constants. Using feedback loop to control the system is the core of PID control. First of all, let's see what effect each one has on the system:

ResponseRise TimeOvershootSettling Time
KpDecreaseIncreaseNone
KiDecreaseIncreaseIncrease
KdNoneDecreaseDecrease

Using this table as a guide.

There're several common methods to tuning. The one I used here is called Ziegler-Nichols. Comparing with other tuning method, this method is simple to do for beginners.

The first step is adjust Kp value until the system oscillate. For example: /media/uploads/dustsnow/zn-kp-oscillate.png

From this plot, we need to measure the oscillation period ( T ).

Then calculate Kp, Ki and Kd according to the table:

ControllerKpKiKd
P0.5*Kp00
PI0.45*KpKp/0.85*T0
PID0.6*KpKp/(0.5*T)Kp*T*0.12

Since we are implementing PID, we using the third row to get PID constants. Applying these constants to Stinger using "vpid " command.

The new running plot looks a lot better:

/media/uploads/dustsnow/zn-result.png

Normally, the result won't be this nice. These values might need more tuning. If that's the case, the table at the beginning of this section come to serve. Adjusting based on ones' need. There's no universal perfect PID constants.

Running Demo

Reference


Please log in to post comments.