OCCRA
Go to Post I even have a t-shirt that says "Actually, I -am- a rocket scientist" :) - dlavery [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 07-31-2005, 05:15 PM
supertw supertw is offline
Registered User
#1560
 
Join Date: Dec 2004
Location: California
Posts: 10
supertw will become famous soon enough
Help with limit mix

what's the point off adding 2000 to the following functions:

pwm01 = pwm03 = Limit_Mix(2000 + PWM_in1 + PWM_in2 - 127); /* LEFT WHEELS */
pwm02 = pwm04 = Limit_Mix(2000 + PWM_in2 - PWM_in1 + 127); /* RIGHT WHEELS */

the coding seems all fuzzy to me, i dont' get it
  #2   Spotlight this post!  
Unread 07-31-2005, 05:20 PM
Morenoh149
 
Posts: n/a
Re: Help with limit mix

I'm n00b myself so maybe I'm wrong.

I had problems with this myself when I came across it.

I think it has something to do with the hardware
it is slower to run the program if it starts getting decimals (floating point #'s)

I think later in the program it negates the 2000

correct me if I'm wrong
  #3   Spotlight this post!  
Unread 07-31-2005, 06:40 PM
Ryan M. Ryan M. is offline
Programming User
FRC #1317 (Digital Fusion)
Team Role: Programmer
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Ohio
Posts: 1,507
Ryan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud of
Re: Help with limit mix

You're right, noob. () It does that just so that it can get decent precision without decimals or negatives.

I think...
__________________


Last edited by Ryan M. : 07-31-2005 at 07:35 PM.
  #4   Spotlight this post!  
Unread 08-01-2005, 01:07 PM
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,126
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Re: Help with limit mix

Quote:
Originally Posted by supertw
what's the point off adding 2000 to the following functions:

pwm01 = pwm03 = Limit_Mix(2000 + PWM_in1 + PWM_in2 - 127); /* LEFT WHEELS */
pwm02 = pwm04 = Limit_Mix(2000 + PWM_in2 - PWM_in1 + 127); /* RIGHT WHEELS */

the coding seems all fuzzy to me, i dont' get it
The compiler we use has the irritating feature of choosing eight-bit arithmetic when adding eight-bit quantities. That often results in overflow and unexpected results. Adding a constant 2000 to the expression forces the compiler to do things using 16-bit arithmetic, and everything then works as it should.

The Limit_Mix() function subtracts 2000 from its result, neatly compensating for the extra 2000 in its input.
  #5   Spotlight this post!  
Unread 08-01-2005, 01:28 PM
Greg Ross's Avatar
Greg Ross Greg Ross is offline
Grammar Curmudgeon
AKA: gwross
FRC #0330 (Beach 'Bots)
Team Role: Mentor
 
Join Date: Jun 2001
Rookie Year: 1998
Location: Hermosa Beach, CA
Posts: 2,245
Greg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond repute
Send a message via AIM to Greg Ross Send a message via Yahoo to Greg Ross
Re: Help with limit mix

The Limit_Mix function assumes that its input value has had 2000 added in, and thus subtracts it back out before returning.

Back in the Basic Stamp days, there were certain circumstances where it was necessary to avoid negative numbers. This Limit_Mix function is a legacy of those days. By rights, the function should now be:
Code:
unsigned char Limit_Mix (int intermediate_value)
{
  static int limited_value;
  
  if (intermediate_value < 0)
  {
    limited_value = 0;
  }
  else if (intermediate_value > 254)
  {
    limited_value = 254;
  }
  else
  {
    limited_value = intermediate_value;
  }
  return (unsigned char) (limited_value);
}
And the calls would be:
Code:
pwm01 = pwm03 = Limit_Mix(PWM_in1 + PWM_in2 - 127); /* LEFT WHEELS */
pwm02 = pwm04 = Limit_Mix(PWM_in2 - PWM_in1 + 127); /* RIGHT WHEELS */
__________________
Greg Ross (The Grammar Curmudgeon formerly known as gwross)
S/W Engineer, Team 330, the Beach 'Bots
<--The Grammar Curmudgeon loves this cartoon.
ôLife should not be a journey to the grave with the intention of arriving safely in a pretty and well preserved body, but rather to skid in broadside in a cloud of smoke, thoroughly used up, totally worn out, and loudly proclaiming "Wow! What a Ride!" Hunter S. Thompson
"Playing a practical joke means doing something mean and calling it funny." Me
  #6   Spotlight this post!  
Unread 08-01-2005, 01:38 PM
Greg Ross's Avatar
Greg Ross Greg Ross is offline
Grammar Curmudgeon
AKA: gwross
FRC #0330 (Beach 'Bots)
Team Role: Mentor
 
Join Date: Jun 2001
Rookie Year: 1998
Location: Hermosa Beach, CA
Posts: 2,245
Greg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond repute
Send a message via AIM to Greg Ross Send a message via Yahoo to Greg Ross
Re: Help with limit mix

Quote:
Originally Posted by Alan Anderson
The compiler we use has the irritating feature of choosing eight-bit arithmetic when adding eight-bit quantities. That often results in overflow and unexpected results. Adding a constant 2000 to the expression forces the compiler to do things using 16-bit arithmetic, and everything then works as it should.

The Limit_Mix() function subtracts 2000 from its result, neatly compensating for the extra 2000 in its input.
OK. I forgot about the 8 bit quirk. Adding the 2000 bias does circumvent this trap, but it's an awfully kludgy way to do it. I assert the following would be clearer: (Assuming that all the definitions remain eight bits each.)
Code:
pwm01 = pwm03 = Limit_Mix((int)PWM_in1 + PWM_in2 - 127); /* LEFT WHEELS */
pwm02 = pwm04 = Limit_Mix((int)PWM_in2 - PWM_in1 + 127); /* RIGHT WHEELS */
__________________
Greg Ross (The Grammar Curmudgeon formerly known as gwross)
S/W Engineer, Team 330, the Beach 'Bots
<--The Grammar Curmudgeon loves this cartoon.
ôLife should not be a journey to the grave with the intention of arriving safely in a pretty and well preserved body, but rather to skid in broadside in a cloud of smoke, thoroughly used up, totally worn out, and loudly proclaiming "Wow! What a Ride!" Hunter S. Thompson
"Playing a practical joke means doing something mean and calling it funny." Me
  #7   Spotlight this post!  
Unread 08-05-2005, 01:49 PM
Morenoh149
 
Posts: n/a
Re: Help with limit mix

Quote:
Originally Posted by GW (Greg) Ross
OK. I forgot about the 8 bit quirk. Adding the 2000 bias does circumvent this trap, but it's an awfully kludgy way to do it. I assert the following would be clearer: (Assuming that all the definitions remain eight bits each.)
Code:
pwm01 = pwm03 = Limit_Mix((int)PWM_in1 + PWM_in2 - 127); /* LEFT WHEELS */
pwm02 = pwm04 = Limit_Mix((int)PWM_in2 - PWM_in1 + 127); /* RIGHT WHEELS */
so what does the (int) do?
does it make the PIC treat all variables (PWM_in2 & PWM_in1) in the Parenthesis like an int instead of a char ?
  #8   Spotlight this post!  
Unread 08-05-2005, 02:03 PM
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,126
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Re: Help with limit mix

Quote:
Originally Posted by Morenoh149
so what does the (int) do?
does it make the PIC treat all variables (PWM_in2 & PWM_in1) in the Parenthesis like an int instead of a char ?
It makes the compiler treat the variable with the (int) in front of it as an int. However, having even a single int in the expression causes the compiler to choose 16-bit operations to evaluate the result, even if everything else is only 8 bits in size.
  #9   Spotlight this post!  
Unread 08-05-2005, 02:06 PM
Mr. Lim Mr. Lim is offline
Registered User
AKA: Mr. Lim
no team
Team Role: Leadership
 
Join Date: Jan 2004
Rookie Year: 1998
Location: Toronto, Ontario
Posts: 1,125
Mr. Lim has a reputation beyond reputeMr. Lim has a reputation beyond reputeMr. Lim has a reputation beyond reputeMr. Lim has a reputation beyond reputeMr. Lim has a reputation beyond reputeMr. Lim has a reputation beyond reputeMr. Lim has a reputation beyond reputeMr. Lim has a reputation beyond reputeMr. Lim has a reputation beyond reputeMr. Lim has a reputation beyond reputeMr. Lim has a reputation beyond repute
Re: Help with limit mix

Quote:
Originally Posted by Morenoh149
so what does the (int) do?
does it make the PIC treat all variables (PWM_in2 & PWM_in1) in the Parenthesis like an int instead of a char ?
The (int) is something called an explicit typecast. It forces the variable right after it to be converted into an integer type, which in our case is a 16-bit signed type.

This "Limit Mix / why add 2000?" question is one that gets asked A LOT by teams up in Canada. I usually refer people to a post on the FIRST Greater Toronto Regional forums:

http://www.firstcanadianregional.org...opic.php?t=500

Hope that sheds some light into the situation!

-SlimBoJones...
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
Robot Weight Limit: Rule Conflict Specialagentjim Rules/Strategy 10 08-06-2005 05:52 PM
limit switches stephenthe1 Programming 28 05-11-2005 04:37 PM
Robot Controller File Size Limit ?? RoboGeek Programming 1 01-17-2005 07:59 PM
material cost limit haverfordfords Fundraising 3 12-06-2004 07:21 PM
Limit Switches help. Xufer Programming 9 04-21-2004 09:21 PM


All times are GMT -5. The time now is 03:32 AM.

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