function omniVehicle
ser =[];
initSer
initTimer
function initSer
ser = serial('COM14', 'BaudRate', 9600, 'DataBits', 8, ...
'Parity', 'none', 'StopBits', 1, 'ReadAsyncMode', 'continuous', ...
'InputBufferSize', 64, 'OutputBufferSize', 64, ...
'Terminator' , 'CR/LF', 'BytesAvailableFcn', @bavCb);
fopen(ser);
end
function initTimer
delete(timerfind)
tmr = timer('TimerFcn', @tmrCb, 'ExecutionMode', 'fixedRate' , ...
'Period', 0.05);
start(tmr);
end
function bavCb(obj, ~)
dat = fgetl(obj);
disp(dat)
end
function tmrCb(~, ~)
joy1 = jst;
xVeloc = round(joy1(2) * 1500);
xVeloc(xVeloc > 1500) = 1500;
xVeloc(xVeloc < -1500) = -1500;
xVelocHex = dec2hex(typecast(int16(xVeloc), 'uint16'), 4);
yVeloc = -round(joy1(1) * 1500);
yVeloc(yVeloc > 1500) = 1500;
yVeloc(yVeloc < -1500) = -1500;
yVelocHex = dec2hex(typecast(int16(yVeloc), 'uint16'), 4);
rotVeloc = round(joy1(3) * 1500);
rotVeloc(rotVeloc > 1500) = 1500;
rotVeloc(rotVeloc < -1500) = -1500;
rotVelocHex = dec2hex(typecast(int16(rotVeloc), 'uint16'), 4);
fprintf(ser, [xVelocHex yVelocHex rotVelocHex]);
end
end