Log in

View Full Version : hey need some help with writing a code please help me here


magical hands
30-12-2003, 00:48
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

Anthony Kesich
30-12-2003, 01:53
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:


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

echos
30-12-2003, 15:00
I'm board so I figured I would clean up the code a little bit.


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.
*/

magical hands
30-12-2003, 15:49
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.

Anthony Kesich
31-12-2003, 00:33
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

Mike Soukup
31-12-2003, 01:32
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.

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?

Anthony Kesich
31-12-2003, 02:14
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").

magical hands
31-12-2003, 11:06
so does anyone has the correct code so i can look at it and understand. thanks guys for valuable information and time.

Anthony Kesich
01-01-2004, 21:27
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*/

EricS-Team180
01-01-2004, 21:46
...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