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

In summary: COM_CloseNXT(handle);COM_CloseNXTallclcclearhandle = 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,
  • #1
thekey
14
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
  • #2
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?
 
  • #3
yeah, there are errors and read lines under defined powers
 
  • #4
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?
 
  • #5
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
 
  • #6
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
 

1. What is MatLab?

MatLab is a high-level programming language and interactive environment designed for scientific computing. It is commonly used in engineering, mathematics, and other scientific fields for data analysis, visualization, and development of algorithms.

2. What is the LEGO NXT?

The LEGO NXT is a programmable robotics kit that allows users to build and control their own robots. It includes a microcontroller, sensors, motors, and other components that can be programmed to perform various tasks.

3. Can I use MatLab to control the LEGO NXT?

Yes, MatLab has a built-in support for the LEGO NXT through its Robotics System Toolbox. This allows users to write and execute code to control the robot's movements, react to sensor data, and more.

4. What is the benefit of using MatLab for controlling the LEGO NXT?

Using MatLab for controlling the LEGO NXT offers several benefits. It provides a user-friendly and intuitive interface for programming the robot, as well as powerful mathematical and analytical tools for data analysis and algorithm development. Additionally, the Robotics System Toolbox allows for easy integration with other MatLab functions and toolboxes.

5. Where can I find resources for learning how to control the LEGO NXT with MatLab?

There are several resources available for learning how to control the LEGO NXT using MatLab. These include tutorials and documentation provided by MathWorks, the company behind MatLab, as well as online forums and communities where users can share tips and ask for help. Additionally, there are books and online courses available for more comprehensive learning.

Back
Top