|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
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
|
|||||
|
|||||
|
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
-Kesich |
|
#3
|
||||
|
||||
|
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
|
|||||
|
|||||
|
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 |
|
#5
|
||||
|
||||
|
Re: hey need some help with writing a code please help me here
Quote:
Quote:
|
|
#6
|
|||||
|
|||||
|
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"). Last edited by Anthony Kesich : 31-12-2003 at 02:17. |
|
#7
|
|||
|
|||
|
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.
|
|
#8
|
|||||
|
|||||
|
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*/
|
|
#9
|
|||||
|
|||||
|
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 |
|
#10
|
|||
|
|||
|
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. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
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 |