View unanswered posts | View active topics It is currently Wed Oct 16, 2019 11:46 pm






Reply to topic  [ 5 posts ] 
New to RobotC 
Author Message
Rookie

Joined: Fri Feb 12, 2010 4:16 pm
Posts: 9
Post New to RobotC
Hi All, new to RobotC but enjoying it. I've written a program that uses the US and a motor.

The program is suppose to:

Record distance at 45 degrees to the left

Record distance at 90 degrees to the left

Return the sensor to the starting position

Record distance at 45 degrees to the right

Record distance at 90 degrees to the right

Return sensor to the starting postion - Fails to return to center

Check recorded readings from left sweep, find the largest distance and record position (45 or 90) and distance

Check recorded readings from right sweep, find the largest distance and record position (45 or 90) and distance

Compare left and right largest distances, select which has largest distance

Rinse and repeat.
_________________________________

If I step through the code, all works fine.

Running the code:

The sensor fails to return to the starting position

The global variables are not being updated until I press the stop button on the debugger form

The display on the NXT is not being updated
___________________________________

Thanks in advance for taking a look at my code and for feedback.
Code:
#pragma config(Sensor, S1,     Touch,               sensorTouch)
#pragma config(Sensor, S4,     Sonar,               sensorSONAR)
#pragma config(Motor,  motorA,          SDrive,        tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  motorB,          LDrive,        tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  motorC,          RDrive,        tmotorNormal, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

// Counters
int iCount;

// Sonar
int iSonarValue;

int iSonarSweepDir;
// 1 is Left, 0 is Right for sweep direction

// Sonar Readings
int iDistanceLeft[2];
int iDistanceRight[2];

// Headings and ranges with greatest safe travel distance
int iTravelLeft[2];
int iTravelRight[2];

const int iRANGESAFE = 50;
const int iSONARRANGE = 30;
const int iSonarSweep = 90;

// calculate amount of time to turn to get to desired heading
int TurnTime( int Heading){
  nxtDisplayString(1,"HTime: %3d",(90/Heading)*675);
  return (90/Heading)*675;
}

void SonarCenter(){
  nMotorEncoder[SDrive] = 0;
  nMotorPIDSpeedCtrl[SDrive] = mtrSpeedReg;
  if (iSonarSweepDir == 1){
    nMotorEncoderTarget[SDrive] = 90;
    motor[SDrive] = 15;
    //while(nMotorEncoder[SDrive] <= 91){}
  }else{
    nMotorEncoderTarget[SDrive] = 90;
    motor[SDrive]= -15;
    //while(nMotorEncoder[SDrive] <= 91){}
    eraseDisplay();
  }
   motor[SDrive]=0;
}

// Sweep to Left, grab sonar reading at 45 degrees and 90 degrees
void SonarSweepLeft(){
   nMotorPIDSpeedCtrl[SDrive] = mtrSpeedReg;
  for (iCount = 0; iCount <= 1; iCount++){
    nMotorEncoder[SDrive] = 0;
    nMotorEncoderTarget[SDrive] = 45;
    motor[SDrive] = -15;
    iSonarValue = SensorValue[Sonar];
    nxtDisplayString(0,"Sonar: %3d",iSonarValue);
    wait10Msec(100);
    iDistanceLeft[iCount] = iSonarValue;
  }

  motor[SDrive] = 0;
  iSonarSweepDir = 1;
}

//  Sweep to Right, grab sonar reading at 45 degrees and 90 degrees
void SonarSweepRight(){
  nMotorPIDSpeedCtrl[SDrive] = mtrSpeedReg;
  for (iCount = 0; iCount <= 1; iCount++){
    nMotorEncoder[SDrive]=0;
    nMotorEncoderTarget[SDrive] = 45;
    motor[SDrive] = 15;
    iSonarValue = SensorValue[Sonar];
    nxtDisplayString(0,"Sonar: %3d",iSonarValue);
    wait10Msec(100);
    iDistanceRight[iCount] = iSonarValue;
  }
  motor[SDrive] = 0;
  iSonarSweepDir = 0;
}

// Compare readings from Sonar Sweep. pick heading with greatest range
void LeftTravel(){
  iTravelLeft[0] = 0;
  iTravelLeft[1] = 0;
  for (iCount = 0; iCount <= 1; iCount++){
    nxtDisplayString(0,"iCount: %3d",iCount);
    nxtDisplayString(1,"Left: %3d",iDistanceLeft[iCount]);
    wait10Msec(100);
    if (iDistanceLeft[iCount] >= iRANGESAFE){
      nxtDisplayString(2,"Safe: %3d",iDistanceLeft[iCount]);
     if (iTravelLeft[1] < iDistanceLeft[iCount]){
       nxtDisplayString(3,"Picked: %3d",iDistanceLeft[iCount]);
        iTravelLeft[0] = (iCount + 1) * 45;
        iTravelLeft[1] = iDistanceLeft[iCount];
      }
    }
  }
}

// Compare readings from Sonar Sweep. pick heading with greatest range
void RightTravel(){
  iTravelRight[0] = 0;
  iTravelRight[0] = 0;
  for (iCount = 0; iCount <= 1; iCount++){
    nxtDisplayString(0,"ICount: %3d",iCount);
    nxtDisplayString(1,"RightTravel: %3d",iDistanceRight[iCount]);
    wait10Msec(100);
    if (iDistanceRight[iCount] >= iRANGESAFE){
      nxtDisplayString(2,"Safe: %3d",iDistanceRight[iCount]);
      if (iTravelRight[1] < iDistanceRight[iCount]){
        nxtDisplayString(3,"Picked: %3d",iDistanceRight[iCount]);
        iTravelRight[0] = (iCount + 1) * 45;
        iTravelRight[1] = iDistanceRight[iCount];
      }
    }
  }
}

task MonitorSonar()
  {
    while(true)
    {
      iSonarValue = SensorValue[Sonar];
      //wait1Msec(100);
    }
  }

task main()
{
   StartTask(MonitorSonar);
  while(true){
    SonarSweepLeft();
    SonarCenter();
    SonarSweepRight();
    SonarCenter();
    LeftTravel();
    eraseDisplay();
    RightTravel();
    eraseDisplay();

  //Compare Left and right distance, pick the one that has a longer safe travel
    nxtDisplayString(0,"LeftTravel: %3d", iTravelLeft[1]);
    nxtDisplayString(1,"RightTravel: %3d", iTravelRight[1]);
    wait10Msec(100);
    if (iTravelLeft[1] >= iTravelRight[1]){
            /*HardTurn(1,iTravelLeft[0]);*/
      PlaySoundFile("Left.rso");
    }else{
        PlaySoundFile("Right.rso");
              /*HardTurn(0,iTravelRight[0]);*/
    }
            /*iLastRun = time1[T1];*/
            wait10Msec(200);
  }
  eraseDisplay();
}


Attachments:
File comment: Code in Post in ROBOTC file.
Testing.c [4.57 KiB]
Downloaded 301 times
Fri Feb 12, 2010 4:32 pm
Profile
Rookie

Joined: Fri Feb 12, 2010 4:16 pm
Posts: 9
Post Re: New to RobotC
Bump

Could use some help on this.


Fri Feb 19, 2010 1:05 pm
Profile
Site Admin
Site Admin
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Post Re: New to RobotC
The problem with your code is that you set the motor to turn a certain number of degrees, but you're not waiting for it to get there. You can do this by checking whether it has reached its destination.

This is a modified example from the Help file:
Code:
nMotorEncoder[motorB] = 0;                // Reset the Motor Encoder of Motor B.
nMotorEncoderTarget[motorB] = 360;        // Set the  target for Motor Encoder of Motor B to 360.
motor[motorB] = 75;                       // Motor B is run at a power level of 75

while(nMotorRunState[motorB] != runStateIdle)  // While Motor B is still running:
{
  wait1Msec(5);
}
motor[motorB] = 0;                       // Motor B is stopped at a power level of 0.


Also consider wrapping your code in [ code ] tags next time (minus the extra spaces).

Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Fri Feb 19, 2010 2:26 pm
Profile WWW
Rookie

Joined: Fri Feb 12, 2010 4:16 pm
Posts: 9
Post Re: New to RobotC
Thank you so much for the reply :D I saw that code somewhere, maybe in the help file. Either the example I saw didn't have it or I overlooked the wait statement in the holding loop since when I tried it it didn't have any affect. I will try it again per your example and report back the results

I apologize for the code not in the code block, won't happen again.... I hope :)


Sun Feb 21, 2010 11:24 pm
Profile
Rookie

Joined: Fri Feb 12, 2010 4:16 pm
Posts: 9
Post Re: New to RobotC
Adding the wait in the loop seemed to do the trick. Thanks Xander!


Tue Feb 23, 2010 6:45 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 5 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.