Go to Post I am really glad that FIRST gives teams the choice to use either students or adults in this role. - dtengineering [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 30-12-2003, 00:48
magical hands magical hands is offline
Jigar Patel
AKA: Jigar Patel
FRC #2185 (Etobicoke CI)
Team Role: Mentor
 
Join Date: Dec 2003
Rookie Year: 2004
Location: TORONTO
Posts: 93
magical hands is on a distinguished road
Question hey need some help with writing a code please help me here

i need to write a code that stops when u hit the wall turns and than goes on by changin it's direction. Anyone has any idea of what i can do or a sample of code please send it to me it will be very helpful. my e-mail address is jigarjuhi@yahoo.ca thanks guys for your time and co-operation
  #2   Spotlight this post!  
Unread 30-12-2003, 01:53
Anthony Kesich's Avatar
Anthony Kesich Anthony Kesich is offline
Programmer Turned Engineer
AKA: SonKenshin
#1097 (Site 3 Engineernig)
Team Role: Leadership
 
Join Date: Jan 2003
Rookie Year: 2003
Location: Sacramento, CA
Posts: 186
Anthony Kesich has a spectacular aura aboutAnthony Kesich has a spectacular aura about
Send a message via AIM to Anthony Kesich
Re: hey need some help with writing a code please help me here

well, what sort of sensors are you using and in what places? is it just a limit switch on the front? if so, how are you trying to make sure you go the right direction? Or is it just trial and error? Assuming trial and error with limitswtches on the fron i would do something like this:

Code:
char turning=0;  //in the global vars section

if (BUMP_SENSOR = 1)   //if hit wall set turning timer
then
   turning=40;
else if (turning >= 35)   //back up first 6 cycles or so to give clearance
then
{
   pwm01=pwm02=70;
   turning--;
}
else if (turning != 0)   //turn for the remaining time
then
{
   pwm01=77;     //assuming 2 motor drive train here
   pwm02=187;    //adjust values as necessary such as time needed to turn and turning speed
   turning--;
}
else
   pwm01=pwm02=200;  //if not turning or hitting go forward
i hope this helps, but i really need some more details.
-Kesich
__________________
C is screwing up my English--I'm ending all my sentences in semi-colons;

Horray for most things! -George Carlin

"Sure, I'll play rugby. Besides, I have a lot of experience running from really big guys who want to cream me." -Me, Freshman year
  #3   Spotlight this post!  
Unread 30-12-2003, 15:00
echos's Avatar
echos echos is offline
Jack of all trades
#1125
Team Role: Programmer
 
Join Date: Dec 2003
Location: San Diego, CA
Posts: 61
echos will become famous soon enough
Send a message via ICQ to echos Send a message via AIM to echos Send a message via MSN to echos Send a message via Yahoo to echos
Re: hey need some help with writing a code please help me here

I'm board so I figured I would clean up the code a little bit.

Code:
 unsigned int turning = 0; /* Put at the top of user_routines.c just under the #includes. */
 
 
 /* This code goes in the Default_Routine section of your user_routines.c file */
 
 if (BUMP_SENSOR)   /* If bump sensor evaluates to true then set turning to 40 */
 	{
 	turning = 40;
 	}
 
 if (turning >= 35)   /* Back up until expresson evaluates to false */
 	{
 	pwm01 = pwm02 = 70;
 
 	turning--; /* Deincraments the variable by 1 */
 	}
 
   else if (turning)   /* If turning is still true then turn until it evaluates to false */
 	{
 	pwm01 = 77;	 //assuming 2 motor drive train here
 	pwm02 = 187;	//adjust values as necessary such as time needed to turn and turning speed
 
 	turning--; /* Deincraments the variable by 1 */
 	}
 
   else
 	{
 	pwm01 = pwm02 = 200;  /* If turning variable is false then go forward */
 	}
 
 pwm02 -= 254; /* Reverses the direction of the oppsite motor */
 
 /*
 If statements:
 
 If statements are code constructs that allow a programer to test an expresion.
 The expression can either be true or false. A true expression is any number greater
 than zero, while a false expresion is any number zero and below. One of the key
 things to remember about an if statement is that only one snippet of code can be
 executed per cycle. The other thing is that if statements have a higharcie of
 testing.
 
 
 
 Switches:
 
 Switches are a code construct simular to an if statement except that is able to
 execute multiple snipets of code in one cycle.
 */
  #4   Spotlight this post!  
Unread 30-12-2003, 15:49
magical hands magical hands is offline
Jigar Patel
AKA: Jigar Patel
FRC #2185 (Etobicoke CI)
Team Role: Mentor
 
Join Date: Dec 2003
Rookie Year: 2004
Location: TORONTO
Posts: 93
magical hands is on a distinguished road
Re: hey need some help with writing a code please help me here

okay thanks for that code and since yoiu ask you need detail. what i wan't is basically a code so if i place swith around by robot and when that switch is triggered (when it hits the wall) i want robot to move in opposite direction.
for eg. if i am driving my robot in reverse and it hits the wall as soon as it hits the wall the swithc should trigger and the robot should move forward. by the way thanks guys for all the help you game me it was excellent info.
  #5   Spotlight this post!  
Unread 31-12-2003, 00:33
Anthony Kesich's Avatar
Anthony Kesich Anthony Kesich is offline
Programmer Turned Engineer
AKA: SonKenshin
#1097 (Site 3 Engineernig)
Team Role: Leadership
 
Join Date: Jan 2003
Rookie Year: 2003
Location: Sacramento, CA
Posts: 186
Anthony Kesich has a spectacular aura aboutAnthony Kesich has a spectacular aura about
Send a message via AIM to Anthony Kesich
Re: hey need some help with writing a code please help me here

opps, thats right, no then in C. My bad. I'm sorry, late night, long day, why am i making excusues? but couldnt you leave the turning=40 and the pwm01=pwm02=200 lines without brackets around them seeing as they are single line statements after a conditional? or are the brackets just to clean it up and make it more easily understandable?

one more thing: pwm02 -= 254 does work. if I'm not mistaken. then that statement expands to pwm02=pwm02-254 whic will alwyas give you a negative value which does not work (or a really large value if pwm02 is unsigned). so souldn't it be redone to be pwm02=254-pwm02? but hey, i may be mistaken, correct me if i am for either part.

-Kesich
__________________
C is screwing up my English--I'm ending all my sentences in semi-colons;

Horray for most things! -George Carlin

"Sure, I'll play rugby. Besides, I have a lot of experience running from really big guys who want to cream me." -Me, Freshman year
  #6   Spotlight this post!  
Unread 31-12-2003, 01:32
Mike Soukup's Avatar
Mike Soukup Mike Soukup is offline
Software guy
FRC #0111 (Wildstang)
Team Role: Engineer
 
Join Date: May 2001
Rookie Year: 1996
Location: Schaumburg, IL
Posts: 797
Mike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond repute
Re: hey need some help with writing a code please help me here

Quote:
Originally Posted by Anthony Kesich
but couldnt you leave the turning=40 and the pwm01=pwm02=200 lines without brackets around them seeing as they are single line statements after a conditional? or are the brackets just to clean it up and make it more easily understandable?
It is syntactically correct to omit the braces (fyi brackets are [] and braces are {}) if the code following an if, for, while, etc is a single line. However it is bad practice to do so. Always using braces eliminates the chance that when you add a line later that you want to execute in the conditional, you forget to add the braces and introduce a bug. It may take some getting used to, and it may look sloppy in your eyes, but it will save you headaches in the long run, so always use braces.

Quote:
Originally Posted by Anthony Kesich
one more thing: pwm02 -= 254 does work. if I'm not mistaken. then that statement expands to pwm02=pwm02-254 whic will alwyas give you a negative value which does not work (or a really large value if pwm02 is unsigned). so souldn't it be redone to be pwm02=254-pwm02? but hey, i may be mistaken, correct me if i am for either part.
I really should know this, but I'll have to defer to someone with a difinitive answer. Assuming pwm02 is defined as a 'unsigned char' here are my two lines of thought.
  • the processor does the calculation in terms of unsigned bytes and the subtraction rolls over from 0 to 255. You would get the correct result.
  • since it's a 16 bit processor, it does the calculation in terms of words and the subtraction rolls over from 0 to 65535 then tries to store the result in a byte sized variable. Obviously this won't work.
My initial guess is that the second case is true, and a quick test using gcc tells me that's how my desktop behaves. Can someone confirm that this is how the controller behaves?
  #7   Spotlight this post!  
Unread 31-12-2003, 02:14
Anthony Kesich's Avatar
Anthony Kesich Anthony Kesich is offline
Programmer Turned Engineer
AKA: SonKenshin
#1097 (Site 3 Engineernig)
Team Role: Leadership
 
Join Date: Jan 2003
Rookie Year: 2003
Location: Sacramento, CA
Posts: 186
Anthony Kesich has a spectacular aura aboutAnthony Kesich has a spectacular aura about
Send a message via AIM to Anthony Kesich
Re: hey need some help with writing a code please help me here

I would just be safe and make it pwm02=254-pwm02, who cares if it takes a little longer to type. plus doinging it with and unsgined char and just rolling over throws you off by 1, which could end up giving you 255 as an output which would be an ugly thing.

And regarding the brackets versus braces thing, i'll try to remember later on to use the correct terms (I've just always called braces curly brackets or "little swoopty doptie thingies that i never use").
__________________
C is screwing up my English--I'm ending all my sentences in semi-colons;

Horray for most things! -George Carlin

"Sure, I'll play rugby. Besides, I have a lot of experience running from really big guys who want to cream me." -Me, Freshman year

Last edited by Anthony Kesich : 31-12-2003 at 02:17.
  #8   Spotlight this post!  
Unread 31-12-2003, 11:06
magical hands magical hands is offline
Jigar Patel
AKA: Jigar Patel
FRC #2185 (Etobicoke CI)
Team Role: Mentor
 
Join Date: Dec 2003
Rookie Year: 2004
Location: TORONTO
Posts: 93
magical hands is on a distinguished road
Re: hey need some help with writing a code please help me here

so does anyone has the correct code so i can look at it and understand. thanks guys for valuable information and time.
  #9   Spotlight this post!  
Unread 01-01-2004, 21:27
Anthony Kesich's Avatar
Anthony Kesich Anthony Kesich is offline
Programmer Turned Engineer
AKA: SonKenshin
#1097 (Site 3 Engineernig)
Team Role: Leadership
 
Join Date: Jan 2003
Rookie Year: 2003
Location: Sacramento, CA
Posts: 186
Anthony Kesich has a spectacular aura aboutAnthony Kesich has a spectacular aura about
Send a message via AIM to Anthony Kesich
Re: hey need some help with writing a code please help me here

Code:
char turning=0;  //in the global vars section

if (BUMP_SENSOR)   //if hit wall set turning timer
   turning=40;
if (turning >= 35)   //back up first 6 cycles or so to give clearance
{
   pwm01=pwm02=70;
   turning--;
}
else if (turning)   //turn for the remaining time
{
   pwm01=77;     //assuming 2 motor drive train here
   pwm02=187;    //adjust values as necessary such as time needed to turn and turning speed
   turning--;
}
else
   pwm01=pwm02=200;  //if not turning or hitting go forward 

pwm02=254-pwm02;  /*put this somewhere at the end of your user routines
                     so that it needs not be redone ater every segment of your code*/
__________________
C is screwing up my English--I'm ending all my sentences in semi-colons;

Horray for most things! -George Carlin

"Sure, I'll play rugby. Besides, I have a lot of experience running from really big guys who want to cream me." -Me, Freshman year
  #10   Spotlight this post!  
Unread 01-01-2004, 21:46
EricS-Team180's Avatar
EricS-Team180 EricS-Team180 is offline
SPAM, the lunchmeat of superheroes!
AKA: Eric Schreffler
FRC #0180 (SPAM)
Team Role: Engineer
 
Join Date: Apr 2002
Rookie Year: 2001
Location: Stuart, Florida
Posts: 561
EricS-Team180 has a reputation beyond reputeEricS-Team180 has a reputation beyond reputeEricS-Team180 has a reputation beyond reputeEricS-Team180 has a reputation beyond reputeEricS-Team180 has a reputation beyond reputeEricS-Team180 has a reputation beyond reputeEricS-Team180 has a reputation beyond reputeEricS-Team180 has a reputation beyond reputeEricS-Team180 has a reputation beyond reputeEricS-Team180 has a reputation beyond reputeEricS-Team180 has a reputation beyond repute
Re: hey need some help with writing a code please help me here

...painful experience has taught me that if you are going to reverse the motor in software, first, limit the pwm from 1 to 254. Then subtract that value from 255.

so in c...
/*
** Max the PWM ...see p 51. in Kernighan and Richie :"the c programming
** language"
*/
pwm02 = (pwm02 <= 254) ? pwm02 : 254 ;
/* Min the PWM*/
pwm02 = (pwm02 >= 1 ) ? pwm02 : 1 ;
/* reverse it in software */
pwm02 = 255 - pwm02 ;

...or with if's

if ( pwm02 > 254)
{
pwm02 = 254 ;
}
else if (pwm02 < 1)
{
pwm02 = 1 ;
}
pwm02 = 255 - pwm02 ;

Or, we've also reversed the wiring on the motor


But, we prefer to reverse it in software.

Really though, since Anthony and echos code hardwires the values, you know that you don't have to protect for a rollover and can get away with
pwm02 = 254 - pwm02.


Thanks,
Eric
__________________

Don't PANIC!
S. P. A. M.
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
What is your most prefered programming language? Hailfire Programming 156 19-01-2005 21:42
Autonomous code PBoss Programming 7 14-01-2003 15:29
Does your team use the Default code. Jeff McCune General Forum 2 09-01-2003 14:46
Rookie Programmer has question about the default code DanL Programming 3 26-01-2002 19:59
Patent source code? Kyle Fenton Chit-Chat 3 20-10-2001 17:53


All times are GMT -5. The time now is 10:40.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi