View unanswered posts | View active topics It is currently Thu Nov 27, 2014 8:32 am






Reply to topic  [ 10 posts ] 
Project Help 
Author Message
Moderator
Moderator
User avatar

Joined: Tue Dec 21, 2010 1:38 pm
Posts: 24
Post Project Help
How do you combine an if/else or while loop with sonar and motor synchronization?

motor synchronization without an if/else or while loop and the sonar sensor work fine but when i combine the loop with the sonar senosr the motors are no longer synced and the sonar senor value is ignored :/

my code that works looks like this



Code:
task main()


{

nSyncedMotors = synchBA;
nSyncedTurnRatio = 100;
motor[motorB] = 50;
wait1Msec (5000)  /* I dont want it to be 5 seconds I want it to be forever or as long as the robot's not within 25 cm of an object */

}


here's the one that doesnt work how i want it to

Code:
task main()

{
 while(true)
 {
 if (SensorValue(sonarSensor) > 25)

 
nSyncedMotors = synchBA;
nSyncedTurnRatio = 100;
motor[motorB] = 50;
wait1Msec (5000)

}
}


Please Help.

_________________
Eric Smalls
About Me: http://www.stanford.edu/~esmalls/
My Projects: http://www.ericsmalls.com/p/my-projects.html


Fri Dec 24, 2010 9:15 am
Profile WWW
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3293
Location: Rotterdam, The Netherlands
Post Re: Project Help
Hi Eric,

Why don't you try this:
Code:
task main() {
while(true) {
  // First check if we're more than 25 cms away and if so,
  // setup the motor synchronisation and speed
  if (SensorValue(sonarSensor) > 25) { 
    nSyncedMotors = synchBA;
    nSyncedTurnRatio = 100;
    motor[motorB] = 50;

    // Now we just wait until until we're not so far away
    while(SensorValue(sonarSensor) > 25)
      wait1Msec (10);
  }
  // Change the motors' behaviour here if you'd like.  You can turn them off, for example
}


- 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 Dec 24, 2010 11:09 am
Profile WWW
Moderator
Moderator
User avatar

Joined: Tue Dec 21, 2010 1:38 pm
Posts: 24
Post Re: Project Help
i tweaked it a little; now it works perfect but when i use it in my primary program the bottom half of code doesnt work

do want me to post the code? it's pretty long.

_________________
Eric Smalls
About Me: http://www.stanford.edu/~esmalls/
My Projects: http://www.ericsmalls.com/p/my-projects.html


Fri Dec 24, 2010 7:53 pm
Profile WWW
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3293
Location: Rotterdam, The Netherlands
Post Re: Project Help
Just attach the program as a whole, instead of pasting it into the message. Just like you would attach a file to a mail :)

- 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]


Sat Dec 25, 2010 1:53 am
Profile WWW
Moderator
Moderator
User avatar

Joined: Tue Dec 21, 2010 1:38 pm
Posts: 24
Post Re: Project Help
I highlighted the parts I'm having problems with


Attachments:
Project Nomad.c [1.95 KiB]
Downloaded 311 times

_________________
Eric Smalls
About Me: http://www.stanford.edu/~esmalls/
My Projects: http://www.ericsmalls.com/p/my-projects.html
Sat Dec 25, 2010 12:10 pm
Profile WWW
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3293
Location: Rotterdam, The Netherlands
Post Re: Project Help
Hi Eric,

I've made quite a few changes to your program. The most important ones being the use of nSyncedTurnRatio. Read my comments in the code.

Some pointers:
  • Use the motor and sensor setup to give your motors and sensors sensible names. I've renamed your motors MLEFT, MRIGHT and MSENSE.
  • The most important thing you need to learn and be religious about is indentation!!! It took me a few minutes just to realign all the brackets. Indentation will make tracking down bugs so much easier. Get into the habit now, you'll thank me later.

- Xander


Attachments:
Project Nomad.c [3.24 KiB]
Downloaded 321 times

_________________
| 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]
Sun Dec 26, 2010 5:14 am
Profile WWW
Moderator
Moderator
User avatar

Joined: Tue Dec 21, 2010 1:38 pm
Posts: 24
Post Re: Project Help
Thank you for your help and i appreciate your advice.

Is it okay for me to post my project to the Cool project and get it on the website?

_________________
Eric Smalls
About Me: http://www.stanford.edu/~esmalls/
My Projects: http://www.ericsmalls.com/p/my-projects.html


Mon Dec 27, 2010 12:06 pm
Profile WWW
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3293
Location: Rotterdam, The Netherlands
Post Re: Project Help
I see no reason why not :)

- 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]


Mon Dec 27, 2010 12:23 pm
Profile WWW
Rookie
User avatar

Joined: Sun Jan 02, 2011 1:57 am
Posts: 31
Post Re: Project Help
hey cool project just one quick tip:

Ctrl+alt+shift+a+f is your friend: auto formats the whole file correctly indented and everything! saves hours of work...

_________________
Karan Hiremath
FTC Team 110- MFS Foxes
Co-Captain
Head Programmer
Builder
Electrical
Service Coordinator


Tue Jan 04, 2011 10:54 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3293
Location: Rotterdam, The Netherlands
Post Re: Project Help
You can also find that (and some other options) here:

Image

- 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]


Wed Jan 05, 2011 3:06 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 10 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.