Code covered by the BSD License  

Highlights from
MPC555 Motor Control Function Blockset

MPC555 Motor Control Function Blockset

by

 

20 Mar 2006 (Updated )

Additional I/O blocks for MPC555 target - specifically targetting TPU functionality

pma_example(block)
function pma_example(block)

% Get a handle to a new figure
f = figure;


ha = axes('units', 'normalized', 'position', [0 0 1 1]);
uistack(ha, 'bottom');
X = imread('example_background.jpg');
hx = imagesc(X);
set(ha, 'handlevisibility', 'off', 'visible', 'off');

% Turn off the menubar on the figure
set(f, 'Menubar', 'none');
set(f, 'Name', 'PMA NUM_OF_TEETH Example');
set(f, 'NumberTitle', 'Off');
set(f, 'Resize', 'Off');
set(f, 'Color', 'w');

fposition = get(f, 'Position');
screensize = get(0, 'ScreenSize');
set(f, 'Position', [10,screensize(4)-670, 440, 640]);
fposition = get(f, 'Position');
axis;
a = gca;

set(a, 'DataAspectRatioMode', 'auto');
set(a, 'PlotBoxAspectRatio', [1 1 1]);
set(a, 'PlotBoxAspectRatioMode', 'manual');
set(a, 'Visible', 'off');
hold on;

e = uicontrol('Style', 'text', ...
    'Position', [10, 7, 80, 20], ...
    'BackgroundColor', 'w', ...
    'String', 'NUM_OF_TEETH:');
e = uicontrol('Style', 'edit', 'Position', [100, 10, 200, 20]);
b = uicontrol('Style', 'pushbutton', ...
    'String', 'Draw', ...
    'Position', [320, 10, 60, 20], ...
    'CallBack', {@i_draw_gear, e});

if nargin ==1
    b1 = uicontrol('Style', 'pushbutton', ...
        'String', 'Set', ...
        'Position', [390, 10, 30, 20], ...
        'CallBack', {@i_set_gear, e, block});

    set(e, 'String', get_param(block,'last_set_teeth'));
    i_draw_gear(0,0, e);
end

l = uicontrol('Style', 'text', ...
    'String', 'PMA:  Example usage of NUM_OF_TEETH', ...
    'FontWeight', 'bold', ...
    'FontSize', 12, ...
    'BackgroundColor', 'b', ...
    'ForegroundColor', 'y', ...
    'Position', [0, fposition(4) - 40, fposition(3), 40]);

end

function i_draw_gear(source, eventdata, e)
extra = get(e, 'String');
extra = str2num(extra);

fail = i_verify_extra(extra);
if fail
    return
end
hold off;
subplot('Position',[ 0 0.25 1 0.74]);
axis;
a = gca;
set(a, 'Position', [0 0.25 1 0.75]);
set(a, 'DataAspectRatioMode', 'auto');
set(a, 'PlotBoxAspectRatio', [1 1 1]);
set(a, 'PlotBoxAspectRatioMode', 'manual');
set(a, 'Visible', 'off');
hold on;

numeventeeth = max(extra) + 1 - length(extra);
draweventeeth(0, 0, 1.0, 0.3, numeventeeth, 'g', 'r', 8);
drawextrateeth(0, 0, 1.0, 0.3, numeventeeth, extra, 'b', 8);
drawcircle(0,0,1,100,'r',1);
drawcircle(0,0,0.3,100,'w',1);
drawnumbers(0, 0, 0.9, numeventeeth, extra)

subplot('Position', [ 0.1 0.1 0.8 0.15]);
a = gca;
set(a, 'Visible', 'off')
yaxis = i_generate_sequence(numeventeeth, extra);
xaxis = 0:0.25:numeventeeth-0.25;
bar(xaxis,yaxis);
axis([0 numeventeeth -0.5 1.5]);
end

function drawcircle(x1, y1, r, n, color, width)

theta = linspace(-pi, pi, n);
x = x1 + r * cos(theta);
y = y1 + r * sin(theta);

fill(x,y, color, 'LineWidth', width);

end

function draweventeeth(x1, y1, r, l, n, color1, color2, width)
theta0 = linspace(pi/2, -1.5 * pi, n+1);

x_base0 = x1 + r .* cos(theta0 - pi/(2*n));
y_base0 = y1 + r .* sin(theta0 - pi/(2*n));

x_base1 = x1 + r .* cos(theta0 + pi/(2*n));
y_base1 = y1 + r .* sin(theta0 + pi/(2*n));

x_tip = x1 + (r+l) .* cos(theta0);
y_tip = y1 + (r+l) .* sin(theta0);



for theta = 1:n
    if theta == 1
        color = color1;
    else
        color = color2;
    end
    fill([x_base0(theta), x_tip(theta), x_base1(theta)], ...
        [y_base0(theta), y_tip(theta), y_base1(theta)], ...
        color);
end
end

function drawextrateeth(x1, y1, r, l, n, teeth, color, width)

theta0 = linspace(pi/2, -1.5 * pi, (n*2)+1);

x_base0 = x1 + r .* cos(theta0 - pi/(2*n));
y_base0 = y1 + r .* sin(theta0 - pi/(2*n));

x_base1 = x1 + r .* cos(theta0 + pi/(2*n));
y_base1 = y1 + r .* sin(theta0 + pi/(2*n));

x_tip = x1 + (r+l) .* cos(theta0);
y_tip = y1 + (r+l) .* sin(theta0);


x_begin = x1 + r .* cos(theta0);
y_begin = y1 + r .* sin(theta0);

x_end = x1 + (r+l) .* cos(theta0);
y_end = y1 + (r+l) .* sin(theta0);

offset = 0:(length(teeth)-1);
teeth = teeth - offset;
for theta = teeth
        fill([x_base0(theta * 2), x_tip(theta * 2), x_base1(theta * 2)], ...
        [y_base0(theta * 2), y_tip(theta * 2), y_base1(theta * 2)], ...
        color);
end

end

function drawnumbers(x1, y1, r, n, teeth)

totalteeth = n + length(teeth);

theta0 = linspace(pi/2, -1.5 * pi, (n*2)+1);
x = x1 + r .* cos(theta0);
y = y1 + r .* sin(theta0);

allteeth = 0:totalteeth-1;
for pickout = teeth
    t1 = find(allteeth==pickout);
    allteeth(t1)=[];
end
for theta = 1:n
    text(x(theta*2-1)-0.05, y(theta*2-1), num2str(allteeth(theta)));
end

offset = 0:(length(teeth)-1);
teethoffset = teeth - offset;
for theta = 1:length(teeth)
    text(x(teethoffset(theta)*2)-0.05, y(teethoffset(theta)*2), num2str(teeth(theta)));
end

end

function fail = i_verify_extra(extra)
fail = 1;
if size(extra) < 1
    errordlg('The matrix must be of at least size 1');
    return;
end

if sort(extra) ~= extra
    errordlg('Invalid combination of additional teeth');
    return;
end

if sum( eq(diff(extra),1) ) ~= 0
    errordlg('Invalid combination of additional teeth');
    return;
end

if sum( lt(extra,0) ) ~= 0
    errordlg('Teeth positions cannot be negative!');
    return;
end
fail = 0;
end

function yaxis = i_generate_sequence(numeventeeth, extra)
yaxis = zeros(1, 4*numeventeeth);
for i = 1:4:4*numeventeeth
    yaxis(i)=1;
end

offset = 0:(length(extra)-1);
teethoffset = extra - offset;
for i = 1:length(extra)
    yaxis( teethoffset(i)*4 - 1 ) = 1;
end
end

function i_set_gear(source, eventdata, e, block)

extra = get(e, 'String');
extra = str2num(extra);

fail = i_verify_extra(extra);
if fail
    return
end
numeventeeth = max(extra) + 1 - length(extra);
yaxis = i_generate_sequence(numeventeeth, extra);

seqstring = ['boolean(', mat2str(yaxis), ')'];
set_param(block, 'gear_tooth_seq', seqstring);
set_param(block, 'last_set_teeth', mat2str(extra));
close(gcf);
end

Contact us