post #1 of 1
Thread Starter 
So the code is meant to have the robot drive in a circle, analyze the furthest distance in that circle, then drive towards that furthest distance then stop within 25 cm of it. The code drives it in the circle, but then doesn't work on the next part. If anyone knows how to fix it, it's probably simple.

Code:
COM_CloseNXT all
clear all
close all

%Initialize NXT connection

handle = COM_OpenNXT('bluetooth.ini', 'check');
COM_SetDefaultNXT(handle);

RunTime=20;
P=20;

%Declare port number for the ultrasound sensor.

port=SENSOR_4;

%Initialize ultrasound sensor.

OpenUltrasonic(port);

ResetMotorAngle(MOTOR_B);
ResetMotorAngle(MOTOR_C);

distance=[];
TachoAngle=[];

%[]; is an empty vector.

SetMotor(MOTOR_B);
SetPower(P);
SetAngleLimit(0);
SendMotorSettings();

% SetMotor(MOTOR_C);
% SetPower(-P);
% SetAngleLimit(0);
% SendMotorSettings();

tictic(1)

while toctoc(1)<RunTime

dist = GetUltrasonic(port);

if dist == 255
    dist = [];
else
    %Identify the current TachoCouhnt
    CurPos = GetMotorSettings(MOTOR_B);
    CurAngle=CurPos.TachoCount;
    TachoAngle=[TachoAngle,CurAngle];
end

distance=[distance,dist];

end

if toctoc(1) > RunTime
    
StopMotor('all', 'off');

end

pause(1)

%Determine the maximum distance.

[MaxDist,I]=max(distance);

MaxAngle=TachoAngle(I);

CurPos = GetMotorSettings(MOTOR_B);
%CurAngle=CurPos.TachoCount
%Motor angle to return to a position corresponding to the maximum distance.
BackAngle=CurPos.TachoCount-MaxAngle;

ResetMotorAngle(MOTOR_B);
ResetMotorAngle(MOTOR_C);

SetMotor(MOTOR_B);
SetPower(-P);
SetAngleLimit(abs(BackAngle));
SendMotorSettings();

WaitForMotor(MOTOR_B);

StopMotor('all', 'off');

Run = 20

tictic(2)

SetMotor(MOTOR_B);
    SyncToMotor(MOTOR_C);
    SetPower(30);
    SetAngleLimit(0);
    WaitForMotor(GetMotor);

while toctoc(2) < Run
dist = GetUltrasonic(port); 
if dist < 10
    StopMotor('all','off');
else
    SendMotorSettings();
end
end

CloseSensor(port);

COM_CloseNXT(handle);
HAFaestus
(18 items)
 
  
CPUMotherboardGraphicsRAM
FX-8120 ASUS Sabertooth 990FX ASUS DirectCU GTX 460 2 x 4GB Samsung 
Hard DriveHard DriveHard DriveOptical Drive
128GB M4 250GB Seagate 1TB WD Black LG 20X DualLayer 
OSMonitorKeyboardPower
Windows 7 Professional 64-bit HP w2338h Chicony KB-2961 SeaSonic X750 80+Gold 
CaseMouseMouse PadAudio
Cooler Master HAF 932 Black Interior Logitech G9x Razer Goliathus Extended Razer Carcharias 
AudioAudio
Denon AVR-1712 Klipsch Qunitet 
  hide details  
Reply
HAFaestus
(18 items)
 
  
CPUMotherboardGraphicsRAM
FX-8120 ASUS Sabertooth 990FX ASUS DirectCU GTX 460 2 x 4GB Samsung 
Hard DriveHard DriveHard DriveOptical Drive
128GB M4 250GB Seagate 1TB WD Black LG 20X DualLayer 
OSMonitorKeyboardPower
Windows 7 Professional 64-bit HP w2338h Chicony KB-2961 SeaSonic X750 80+Gold 
CaseMouseMouse PadAudio
Cooler Master HAF 932 Black Interior Logitech G9x Razer Goliathus Extended Razer Carcharias 
AudioAudio
Denon AVR-1712 Klipsch Qunitet 
  hide details  
Reply