ROBOTC.net forumshttp://www.robotc.net/forums/ Need help with dimensions If elsehttp://www.robotc.net/forums/viewtopic.php?f=1&t=2714 Page 1 of 2

 Author: Ziram [ Mon Nov 01, 2010 2:52 pm ] Post subject: Need help with dimensions If else So i'm trying to have an if else statement where the NXT will move forwards with a sonar sensor looking for an opening along a wall at a certain distance then once it finds that distance i want it to see if that opening goes for more than that split second. For example the NXT goes forwards looking for something greater than 20cm away then once it finds it, it will then keep looking for that opening for 20 cms forwards so it sees that there is a box of 20x20Thanks!

 Author: mightor [ Tue Nov 02, 2010 4:08 am ] Post subject: Re: Need help with dimensions If else Hi Ziram,What have you managed to get done so far? Can you attach your code so we can see how to point you in the right direction?- Xander

Author:  Ziram [ Tue Nov 02, 2010 2:16 pm ]
Post subject:  Re: Need help with dimensions If else

Here is the file. I just don't know how to make the robot keep going forwards for however long that 20cm opening is.

Thanks

 Author: mightor [ Wed Nov 03, 2010 3:31 am ] Post subject: Re: Need help with dimensions If else Ziram,I've created some pictures to illustrate the point:Step 1: Drive along the road and scan for an opening in the wall or row of carsStep 2: Start of gap has been detected (your sonar sensor detects a much bigger distance). You make a note of your current wheel encoder count. This is the start of your distance measuring. Now you must drive along until the distance on your sonar sensor gets very small again. Then proceed to the next step.Step 3: You detect the end of the gap. Once again, you make a note of your current wheel encoder count. This is the end of your distance measuring. Now you can subtract the start from the end and get the total distance travelled. IS this big enough for your car to fit in? If so, proceed to the next step.Step 4: Drive ahead a little bit to make sure you're in the right position.Step 5: Turn and start backing up.Step 6: You're almost there, start turning the front wheels back into a straight positionStep 7: All done.Does this clear things up?- Xander

 Author: Ziram [ Thu Nov 04, 2010 12:07 am ] Post subject: Re: Need help with dimensions If else Yes that makes it more clear! Can you show me some of the code as i am new to RobotC and not sure how to use the encoder.-Thanks

Author:  mightor [ Thu Nov 04, 2010 2:30 am ]
Post subject:  Re: Need help with dimensions If else

Try this code below. It displays the current values of the encoders as the motors run. You'll notice that one count increases faster than the other due to the difference in speed.

- Xander

 Code:#pragma config(Motor,  motorA,          MOTOR1,        tmotorNormal, PIDControl, encoder)#pragma config(Motor,  motorB,          MOTOR2,        tmotorNormal, PIDControl, encoder)//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//task main () {  long encoderCountMotor1 = 0;  long encoderCountMotor2 = 0;  // Reset the tachos for both motors.  You only need to do this  // once at the start of your program  nMotorEncoder[MOTOR1] = 0;  nMotorEncoder[MOTOR2] = 0;  wait1Msec(100);  // Start the motors  motor[MOTOR1] = 20;  motor[MOTOR2] = 60;  // Display the encoder counts in a nice way  nxtDisplayCenteredTextLine(0, "Motor Test");  nxtDrawLine(0, 52, 99, 52);  while (true) {    // Read the encoder counts.  You need to use a variable type called    // "long".  An "int" would not be big enough to handle all the possible    // values of the nMotorEncoder.    encoderCountMotor1 = nMotorEncoder[MOTOR1];    encoderCountMotor2 = nMotorEncoder[MOTOR2];    // Put the values on the screen    nxtDisplayTextLine(2, "Motor1: %5d", encoderCountMotor1);    nxtDisplayTextLine(3, "Motor2: %5d", encoderCountMotor2);    // Wait a little bit (50ms)    wait1Msec(50);  }}

Author:  Ziram [ Thu Nov 04, 2010 2:51 pm ]
Post subject:  Re: Need help with dimensions If else

Ok I put that into the program and understand it but not sure how to tell the NXT to stop doing this when the gap closes then tell it to take the amount that the NXT traveled and see if that is enough space then if there is enough space back use like half of the distance it traveled and then back up.

Author:  mightor [ Thu Nov 04, 2010 3:37 pm ]
Post subject:  Re: Need help with dimensions If else

Take a look at this program. I have not tested it but it compiles. It is guaranteed to need work from you to make this work properly. I made it so you can get an idea.

- Xander

 Author: Ziram [ Thu Nov 04, 2010 7:17 pm ] Post subject: Re: Need help with dimensions If else Quick question what does Return;do?-Ziram

Author:  mightor [ Fri Nov 05, 2010 2:41 am ]
Post subject:  Re: Need help with dimensions If else

return will exit the current function and go back to the point where it was called.

Take a look at the code below:
 Code:void function1 () {  // perform action 1     // perform action 2    if (condition == true) {    return; // exit the function, we're done  }     // perform action 3}task main () {  function1();  PlaySound(soundBeepBeep);  wait1Msec(1000);}

Take a good look at function1. Which actions are performed when condition is true? Which actions are performed when condition is false?

Regards,
Xander

 Author: mightor [ Fri Nov 05, 2010 12:26 pm ] Post subject: Re: Need help with dimensions If else Here is a small video on how to get your car out of a tight space: http://vimeo.com/14592941- Xander

Author:  Ziram [ Fri Nov 05, 2010 3:00 pm ]
Post subject:  Re: Need help with dimensions If else

I think i have it just about done!
Haven't been able to test it yet but it looks good!

This has been a great learning experience.

 Author: mightor [ Fri Nov 05, 2010 4:54 pm ] Post subject: Re: Need help with dimensions If else Looks good! Make sure you take a video of it when it is all working and I'll see if I can get it on the ROBOTC's community projects page on the official website!- Xander

 Author: Ziram [ Sun Nov 07, 2010 3:06 pm ] Post subject: Re: Need help with dimensions If else Awesome! Will do!

Author:  Ziram [ Mon Nov 08, 2010 3:54 pm ]
Post subject:  Re: Need help with dimensions If else

( ! )
Actually i have encountered a problem with the programming and i can't figure it out.
What is happening is the robot will do everything up until it has found a gap that is big enough for the robot to park.
The robot will find smaller ones and keep looking so there is nothing wrong there but once it has found a big enough spot the robot will just stop.
I have tried taking out the park(); function and seeing if it will just go back for 1 sec but nothing will work.