MATLAB Please I need a Help with matlab code (Controlling the LEGO NXT Using MatLab)

AI Thread Summary
The discussion revolves around troubleshooting a MATLAB code for controlling a LEGO NXT robot. The user seeks help identifying mistakes in their code, which is intended to allow the robot to follow a line using various sensors. Key issues include undefined variables, such as "Turn" and "powerC," and the need for clearer error reporting. Participants emphasize the importance of specifying the nature of the errors encountered, whether they are related to functionality or error messages. Suggestions include simplifying the code to isolate errors and ensuring proper initialization of the workspace and sensor connections. The user aims to implement feedback mechanisms effectively, such as using touch, light, and ultrasonic sensors, and seeks guidance on achieving smooth operation of the robot.
thekey
Messages
14
Reaction score
0
Hi




Controlling the LEGO NXT Using MatLab


What are my mistakes in this code ??!



COM_CloseNXT
all
clc
clear
handle = COM_OpenNXT();
COM_SetDefaultNXT(handle);
COM_CloseNXT(handle);
NXT_GetBatteryLevel(handle);
COM_CloseNXT(handle);
COM_CloseNXT
all
clc
clear
handle = COM_OpenNXT();
COM_SetDefaultNXT(handle);
OpenSwitch(SENSOR_1);
x = 1;
power=50;
foreverLoop= 1;
loop = 1;
Kp = 1000;
offset = 45;
Tp = 25;
error = 0;
powerA = 0;
powerB = 0;
degrees=360;
powerA = Tp + Turn;
powerB = Tp - Turn;
Kd = 1;
dervative = 0;
lastError = 0;
while
foreverloop
while
x == 1
if GetSwitch(SENSOR_1)
disp (
'Hello world')
x = 2;
end
end
COM_CloseNXT(handle);
COM_CloseNXT
all
clc
clear
handle = COM_OpenNXT();
COM_SetDefaultNXT(handle);
getlight (SENSOR_3)
motorA = NXTMotor (MOTOR_A,
'Power',power,'TachoLimit', degrees);
motorB = NXTMotor (MOTOR_B,
'Power',power,'TachoLimit', degrees);
motorA.Stop (
'off');
motorA.ResetPosition();
data1 = motorA.ReadFromNXT();
position1 = data1.Position;
motorB.SendToNXT();
motorB.WaitFor();
motorB.Stop (
'off');
data2 = motorA.ReadFromNXT();
position2 = data2.Position;
dsip(position1,position2);
COM_CloseNXT(handle);
COM_CloseNXT
all
clc
clear
handle = COM_OpenNXT();
COM_SetDefaultNXT(handle);
OpenSwitch(SENSOR_1);
OpenUltrasonic(SENSOR_2);
mA = NXTMotor (MOTOR_A,
'Power',power);
mA.Stop (
'off');
mA.ResetPosition();
mB=NXTMotor(MOTOR_B,
'Power',power);
mB.Stop(
'off');
mB.ResetPosition();
while
x == 2
if GetUltrasonic(SENSOR_2) <= 15
motorA.Stop (
'brake');
data2 = motorA.ReadFromNXT();
position2 = data2.Position;
disp (
'Pardon Me')
x = 1;
else
motorA.SendToNXT();
end
end
motorA.Stop (
'off');
COM_CloseNXT(handle);
integral=integral+error;
ForeverLoop=1;
OpenSwitch(SENSOR_3);
OpenLight(SENSOR_1,
'Active' )
Loop
forever;
Turn=(Kp*error);
LightValue = OpenLight;
error= LightValue - offset;
Turn= (Turn) / 1000;
powerA = ceil(Tp + Turn);
powerB = ceil(Tp - Turn);
motorA = NXTMotor (MOTOR_A,
'Power',powerA, 'TachoLimit',0);
motorB = NXTMotor (MOTOR_B,
'Power',powerC, 'TachoLimit',0);
motorA.SendToNXT();
motorB.SendToNXT();
derivative = error - lastError;
Turn = (Kp * error) + (Ki * integral) + (Kd * derivative);
lastError = error;
end
 
Physics news on Phys.org
thekey said:
Hi




Controlling the LEGO NXT Using MatLab


What are my mistakes in this code ??!



COM_CloseNXT
all
clc
clear
handle = COM_OpenNXT();
COM_SetDefaultNXT(handle);
COM_CloseNXT(handle);
NXT_GetBatteryLevel(handle);
COM_CloseNXT(handle);
COM_CloseNXT
all
clc
clear
handle = COM_OpenNXT();
COM_SetDefaultNXT(handle);
OpenSwitch(SENSOR_1);
x = 1;
power=50;
foreverLoop= 1;
loop = 1;
Kp = 1000;
offset = 45;
Tp = 25;
error = 0;
powerA = 0;
powerB = 0;
degrees=360;
powerA = Tp + Turn;
powerB = Tp - Turn;
Kd = 1;
dervative = 0;
lastError = 0;
while
foreverloop
while
x == 1
if GetSwitch(SENSOR_1)
disp (
'Hello world')
x = 2;
end
end
COM_CloseNXT(handle);
COM_CloseNXT
all
clc
clear
handle = COM_OpenNXT();
COM_SetDefaultNXT(handle);
getlight (SENSOR_3)
motorA = NXTMotor (MOTOR_A,
'Power',power,'TachoLimit', degrees);
motorB = NXTMotor (MOTOR_B,
'Power',power,'TachoLimit', degrees);
motorA.Stop (
'off');
motorA.ResetPosition();
data1 = motorA.ReadFromNXT();
position1 = data1.Position;
motorB.SendToNXT();
motorB.WaitFor();
motorB.Stop (
'off');
data2 = motorA.ReadFromNXT();
position2 = data2.Position;
dsip(position1,position2);
COM_CloseNXT(handle);
COM_CloseNXT
all
clc
clear
handle = COM_OpenNXT();
COM_SetDefaultNXT(handle);
OpenSwitch(SENSOR_1);
OpenUltrasonic(SENSOR_2);
mA = NXTMotor (MOTOR_A,
'Power',power);
mA.Stop (
'off');
mA.ResetPosition();
mB=NXTMotor(MOTOR_B,
'Power',power);
mB.Stop(
'off');
mB.ResetPosition();
while
x == 2
if GetUltrasonic(SENSOR_2) <= 15
motorA.Stop (
'brake');
data2 = motorA.ReadFromNXT();
position2 = data2.Position;
disp (
'Pardon Me')
x = 1;
else
motorA.SendToNXT();
end
end
motorA.Stop (
'off');
COM_CloseNXT(handle);
integral=integral+error;
ForeverLoop=1;
OpenSwitch(SENSOR_3);
OpenLight(SENSOR_1,
'Active' )
Loop
forever;
Turn=(Kp*error);
LightValue = OpenLight;
error= LightValue - offset;
Turn= (Turn) / 1000;
powerA = ceil(Tp + Turn);
powerB = ceil(Tp - Turn);
motorA = NXTMotor (MOTOR_A,
'Power',powerA, 'TachoLimit',0);
motorB = NXTMotor (MOTOR_B,
'Power',powerC, 'TachoLimit',0);
motorA.SendToNXT();
motorB.SendToNXT();
derivative = error - lastError;
Turn = (Kp * error) + (Ki * integral) + (Kd * derivative);
lastError = error;
end

Welcome to the PF.

I think you need to explain what kind of "mistakes" or incorrect results you are seeing. Are there error messages? Or does the code just not act correctly. If it's not acting correctly, what is it supposed to do, and what is it doing wrong?
 
yeah, there are errors and read lines under defined powers
 
thekey said:
yeah, there are errors and read lines under defined powers

Sorry, what does that mean? I think you will need to be a lot more explicit about the errors before folks can help you out.

What is the simplest version of this program that runs? How much do you have to strip out before you don't get any errors?
 
I am supposed to program the Lego to follow the line smoothly
here are some information ..
--------------------------------PURPOSE: To utilize the LEGO NXT block as an interface to gather input from feedback mechanisms (touch sensor, light sensor, ultrasonic sensor, etc.), make sound, display a message and serve as a motor controller.
PART 1: Communication Test
For this part of the lab, we will connect to the NXT brick and have it make a beep. We will also have it relay the battery voltage in millivolts back to the command window MatLab.
To initialize the workspace, we must clear everything, including the connections that may still exist between the software and the NXT. To do this we enter the commands:
%initialize work space
COM_CloseNXT all
clc
clear

We then need to set up the connection between the NXT and MatLab:
%Open NXT connection
handle = COM_OpenNXT();
COM_SetDefaultNXT(handle);

The next step is to ask the user for the frequency of the sound and the duration of the sound.

We then must tell the NXT to play this sound:

NXT_PlayTone(freq,time,handle);

Using the value NXT_GetBatteryLevel(handle)we need to display the voltage of the NXT battery.Finally, at the end of every program, we need to close the connection with the NXT:

COM_CloseNXT(handle);

PART 2: Using feedback

first start by initializing the workspace
%initialize work space
 
I do not know how I can remove the read line under the power A and bclc
clear
handle = COM_OpenNXT();
COM_SetDefaultNXT(handle);
OpenSwitch(SENSOR_1);
x = 1;
power=50;
foreverLoop= 1;
loop = 1;
Kp = 1000;
offset = 45;
Tp = 25;
error = 0;
powerA = 0;
powerB = 0;

degrees=360;
powerA = Tp + Turn;
powerB = Tp - Turn;
Kd = 1;
dervative = 0;
lastError = 0;
while
foreverloop
while
x == 1
if GetSwitch(SENSOR_1)
disp (
'Hello world')
x = 2;
end
end
 
Back
Top