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

Click For Summary

Discussion Overview

The discussion revolves around troubleshooting MATLAB code for controlling a LEGO NXT robot. Participants are seeking help with identifying mistakes in the code, which is intended to utilize various sensors and motors for tasks such as line following and sound production.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • Participants express confusion about specific errors in the MATLAB code and request clarification on what constitutes a "mistake."
  • One participant mentions experiencing errors related to undefined powers in the code.
  • Another participant asks for more explicit descriptions of the errors to provide better assistance.
  • A participant outlines the purpose of the code, which includes gathering input from various sensors and controlling motors.
  • There is a mention of initializing the workspace and establishing a connection with the NXT brick as part of the programming process.
  • One participant expresses uncertainty about how to resolve issues related to reading lines under defined powers.

Areas of Agreement / Disagreement

There is no consensus on the specific mistakes in the code, as participants have differing levels of understanding and clarity regarding the errors encountered. The discussion remains unresolved with multiple competing views on how to address the issues.

Contextual Notes

Participants have not provided specific error messages or detailed descriptions of the incorrect behavior of the code, leading to ambiguity in the troubleshooting process.

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