Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Dec 7, 2012 #1
    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
     
  2. jcsd
  3. Dec 7, 2012 #2

    berkeman

    User Avatar

    Staff: Mentor

    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?
     
  4. Dec 7, 2012 #3
    yeah, there are errors and read lines under defined powers
     
  5. Dec 7, 2012 #4

    berkeman

    User Avatar

    Staff: Mentor

    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?
     
  6. Dec 7, 2012 #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
     
  7. Dec 7, 2012 #6
    I do not know how I can remove the read line under the power A and b


    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
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Please I need a Help with matlab code (Controlling the LEGO NXT Using MatLab)
  1. I need help in Matlab (Replies: 0)

Loading...