programas lego
TRANSCRIPT
-
7/27/2019 Programas Lego
1/3
Connecting to the NXT
Once you get everything installed and ready to go, start up MATLAB to connect to the NXT. The RWTHToolbox uses a "handle" created by the COM_OpenNXT function to communicate with the NXT. A handle isjust a variable containing information about the NXT, which port it is connected on, etc. All of the providedcode is set up to use a "default handle," which is specified with the COM_SetDefaultNXT function. Finally,when you're ready to disconnect the NXT, the COM_CloseNXT function closes the connection. Thus, yourMATLAB sessions should look like the following:
h = COM_OpenNXT(); Opens a new connection to an NXT on a USB port
COM_SetDefaultNXT(h); Sets the default handle to the one just created
... Do your work here
COM_CloseNXT(COM_GetDefaultNXT()); Close the connection to the NXT
You can easily verify that everything connected right by running the following command (after runningCOM_OpenNXT and COM_SetDefaultNXT):
NXT_PlayTone(440,100); Beep
------------------------------------------------------------------------------------------------------------------------------------------------
%% Programa 1%% Move o rob para frente e para trsfunction lab1
%% Move forwardm = NXTMotor( [MOTOR_B; MOTOR_C] );m.Power = 30;m.TachoLimit = 1080;m.SendToNXT()m.WaitFor()
%% Move backward
m.Power = -30;m.SendToNXT()m.WaitFor()
end
% ======================================================% ======================================================
%% Programa 2%% Realiza a rotao do rob em 180 grausfunction lab2
%% Rotatel_wheel = NXTMotor( MOTOR_B );r_wheel = NXTMotor( MOTOR_C );
l_wheel.Power = -30;r_wheel.Power = 30;
l_wheel.TachoLimit = 550;r_wheel.TachoLimit = 550;
l_wheel.SendToNXT()r_wheel.SendToNXT()
l_wheel.WaitFor()r_wheel.WaitFor()
end
-
7/27/2019 Programas Lego
2/3
% ======================================================% ======================================================
%% Programa 3%% Motitora o estado da posio do motorfunction lab3
%% Read motor states = NXT_GetOutputState( MOTOR_B );
disp( s.TachoCount )
end
% ======================================================% ======================================================
%% Programa 4%% Realiza a leitura do sensor de contatofunction lab4
%% Get sensor readingOpenSwitch( SENSOR_1 )
GetSwitch( SENSOR_1 )CloseSensor( SENSOR_1 )
end
% ======================================================% ======================================================
%% Programa 5%% Movimenta o rob at que o sensor de contato seja pressionadofunction lab5
%% NXTMotor
m = NXTMotor( [MOTOR_B; MOTOR_C] );m.SmoothStart = true;m.Power = 20;
%% Open sensorOpenSwitch( SENSOR_1 )
%% Start motorm.SendToNXT()
%% Read sensorwhile trueif GetSwitch( SENSOR_1 )
m.Stop('off');break;
endpause( 0.1 )
end
end
% ======================================================% ======================================================
%% Programa 6%% Realiza a leitura de cor atravs do sensor de corfunction lab6
%% Get color sensor readingOpenNXT2Color( SENSOR_1, 'COLOR' )pause( 0.1 )
-
7/27/2019 Programas Lego
3/3
c = GetNXT2Color( SENSOR_1 );disp( c )CloseSensor( SENSOR_1 )
end
% ======================================================% ======================================================
%% Programa 7%% Utiliza o sensor de cor para seguir uma linhafunction lab7
%% NXTMotorl_wheel = NXTMotor( MOTOR_B );r_wheel = NXTMotor( MOTOR_C );
l_wheel.SmoothStart = true;r_wheel.SmoothStart = true;
%% Open color sensorOpenNXT2Color( SENSOR_1, 'COLOR' )
pause( 0.1 )
%% Start right wheelr_wheel.Power = 20;r_wheel.SendToNXT()
%% Follow linewhile true
%% Right turnl_wheel.Power = 10;l_wheel.SendToNXT()% Wait until robot is over white area
while ~strcmp( GetNXT2Color( SENSOR_1 ), 'WHITE' )pause( 0.01 )
end
%% Left turnl_wheel.Power = 40;l_wheel.SendToNXT()% Wait until robot is over black linewhile ~strcmp( GetNXT2Color( SENSOR_1 ), 'BLACK' )pause( 0.01 )
end
end
% ======================================================% ======================================================
%% Programa 8%% Realiza a leitura do sensor ultrassnicofunction lab8
%% Get ultrasonic sensor readingsOpenUltrasonic( SENSOR_1 )pause( 0.1 )
for i=1:10disp( GetUltrasonic( SENSOR_1 ) )pause( 0.5 )
end
end