Sphero Simulink blocks

Specifically, the Setup block is needed to select a preexisting workspace Sphero object for execution. That preexisiting Sphero object is then used during the execution by the other blocks that specify the same name. If you want to use different Spheros in the same execution then each of them must have its own Setup block.

The Real Time Pacer block can be used to slow down a fast execution so it can track real time.

The Read Sensor block calls the MATLAB "readSensor" function, which returns the value of a specified sensor.

The Read Locator block calls the MATLAB "readLocator" function, which returns the current location and velocity components of the Sphero.

The bottom 4 blocks feature different mathematical models of the Sphero. The Kinematic models approximate the sphero kinematic using an unicycle model. The Grey-Box models also model the dynamics using a first order transfer function. In both cases the inputs can be either desired speed and angle or left and right wheel speed.

Also note that you can open the simulink example models (which will be shown shortly thereafter), by double clicking on the three blocks on the right.

A sphero object must be created in the workspace before executing the model

% Create a Sphero object (if it does not exist)if ~exist('sph','var'),
sph = sphero(); % Create a Sphero objectend% make sure the object is connected
connect(sph);
% ping it
result = ping(sph);
% interrupt the example if ping was not successfulif ~result,
disp('Example aborted due to unsuccessful ping');
return,
end% now we can actually execute the model: you can move the sphero around% to see the components of the acceleration along its 3 body axes changing
sim('accel_sim');
% and close the system when the simulation is terminated
close_system('accel_sim');
f1 = figure(1);
plot(yout(:, 1), yout(:, 2:4)); grid
title('Acceleration of Sphero along its X,Y and Z body axis')
xlabel('time (sec)');ylabel('acceleration');
legend('accelX','accelY','accelZ');

Open-loop example using Roll block

This example uses the Roll block to move the sphero, along a direction specified by a sinusoid, with a constant speed of 70/255. This should move the sphero along a path resembling an eight figure.

The Read Locator block is used to gather the Sphero's position and velocity.

A sphero object must be created in the workspace before starting

% Create a Sphero object (if it does not exist)if ~exist('sph','var'),
sph = sphero(); % Create a Sphero objectend% make sure the object is connected
connect(sph);
% ping it
result = ping(sph);
% interrupt the example if ping was not successfulif ~result,
disp('Example aborted due to unsuccessful ping');
return,
end% reset the calibration of the Sphero
calibrate(sph, 0);
% now we can actually execute the model
sim('roll_sim');
% and close the system when the execution is terminated
close_system('roll_sim');
f2 = figure(2);
plot(yout(:, 2), yout(:, 3), '*')
title({'x-y position of Sphero when running', 'open-loop example with Roll block'})

roll_sim/Real-Time Pacer: Average idle real time per major time step = 0.0640007 sec

Open-loop example using the Raw Motor Sphero Kinematic Model

This example uses a simple kinematic model of the Sphero (in which separate power commands are given to the left and right motors) instead of the Sphero itself.

sim('rawmotor_sim_kine');
% and close the system when the execution is terminated
close_system('rawmotor_sim_kine');
f3 = figure(3);
plot(yout(:, 2), yout(:, 3), '*')
title({'x-y position of Sphero when running', 'rawmotor kinematic model example'})

rawmotor_sim_kine/Real-Time Pacer: Average idle real time per major time step = 0.0834085 sec

Closed-loop example using Roll block on the Kinematic model

In this example, the "Outer Loop Control" block works as a controller. It commands the desired velocity (speed and angle) of the sphero, in order to minimize the difference between a reference point (which moves round a square) and the measured position and velocity.

The reference position and velocity are generated by the "Reference" block, depending on a set of points (arranged in a square) to be visited at a particular time. Both point positions and time are parameters of the block.

A simple kinematic model is used to double-check the control works fine.

sim('sph_control_sim_kine');
% and close the system when the simulation is terminated
close_system('sph_control_sim_kine');
f5 = figure(5);
plot(yout(:, 2), yout(:, 3), '*')
title({'x-y position of model when running', 'kinematic model control example'})

Closed-loop example using Roll block on the Grey Box model

In this example, the "Outer Loop Control" block works as a controller. It commands the desired velocity (speed and angle) of the sphero, in order to minimize the difference between a reference point (which moves round a square) and the measured position and velocity.

The reference position and velocity are generated by the "Reference" block, depending on a set of points (arranged in a square) to be visited at a particular time. Both point positions and time are parameters of the block.

The grey-box model is used in place of the real sphero, to validate the controller robustness.

sim('sph_control_sim_grey');
% and close the system when the simulation is terminated
close_system('sph_control_sim_grey');
f6 = figure(6);
plot(yout(:, 2), yout(:, 3), '*')
title({'x-y position of model when running', 'grey-box model control example'})

sph_control_sim_grey/Real-Time Pacer: Average idle real time per major time step = 0.0826057 sec

Closed-loop example using Roll block

In this example, the "Outer Loop Control" block works as a controller. It commands the desired velocity (speed and angle) of the sphero, in order to minimize the difference between a reference point (which moves round a square) and the measured position and velocity.

The reference position and velocity are generated by the "Reference" block, depending on a set of points (arranged in a square) to be visited at a particular time. Both point positions and time are parameters of the block.

The sphero position and velocity are retrieved by the "Sphero Read Locator" block.

% ping the sphero
result = ping(sph);
% interrupt the example if ping was not successfulif ~result,
disp('Example aborted due to unsuccessful ping');
return,
end% reset the calibration of the Sphero
calibrate(sph, 0);
% now we can actually execute
sim('sph_control_sim');
% and close the system when the execution is terminated
close_system('sph_control_sim');
f7 = figure(7);
plot(yout(:, 2), yout(:, 3), '*')
title({'x-y position of Sphero when running', 'closed loop control example'});