-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoombaInit.m
executable file
·73 lines (57 loc) · 1.82 KB
/
RoombaInit.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
function [serPort] = RoombaInit(my_COM);
%[serPort] = RoombaInit(my_COM)
% initializes serial port for use with Roomba
% COMMport is the number of the comm port
% ex. RoombaInit(1) sets port = 'COM1'
% note that it sets baudrate to a default of 57600
% can be changed (see SCI).
% An optional time delay can be added after all commands
% if your code crashes frequently. 15 ms is recommended by irobot
% By; Joel Esposito, US Naval Academy, 2011
global td
% td = 0.015;
td = 0.02;
% This code puts the robot in CONTROL(132) mode, which means does NOT stop
% when cliff sensors or wheel drops are true; can also run while plugged into charger
Contrl = 132;
% Esposito 9/2008
warning off
%% set up serial comms,
% output buffer must be big enough to take largest message size
comm = strcat('', num2str(my_COM));
a = instrfind('port',comm);
if ~isempty(a)
disp('That com port is in use. Closing it.')
fclose(a);
pause(1)
delete(a);
pause(1)
end
disp('Establishing connection to Roomba...');
% defaults at 57600, can change
serPort = serial(comm,'BaudRate', 115200);
set(serPort,'Terminator','LF')
set(serPort,'InputBufferSize',100)
set(serPort, 'Timeout', 1)
set(serPort, 'ByteOrder','bigEndian');
set(serPort, 'Tag', 'Roomba')
disp('Opening connection to Roomba...');
fopen(serPort);
%% Confirm two way connumication
disp('Setting Roomba to Control Mode...');
% Start! and see if its alive
Start=[128];
fwrite(serPort,Start);
pause(.1)
fwrite(serPort,Contrl);
pause(.1)
% light LEDS
fwrite(serPort,[139 25 0 128]);
% set song
fwrite(serPort, [140 1 1 48 20]);
pause(0.05)
% sing it
fwrite(serPort, [141 1])
disp('I am alive if my two outboard lights came on')
confirmation = (fread(serPort,4));
pause(.1)