Go to Post It would also level the playing field by bringing the bottom up, rather than the top down. - russell [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 18-02-2002, 14:22
VanWEric VanWEric is offline
Registered User
#0250 (Dynamos)
 
Join Date: Feb 2002
Location: NY
Posts: 86
VanWEric is an unknown quantity at this point
One line power

I got the insane urge to eliminate if statements. Having done that, i had the urge to eliminate statements in general. The result? One line of code per track to make it flip and reverse, rescale the sticks, and ramp the speed to reduce motor wear and tear.
creepbit =creep.bit7
creep = abs(127-creep)
hold = lt
lt = (254-rt -lt *creepbit + lt)*creep/127-creep+127 min (lth-Ramp_Speed) max (lth+Ramp_Speed)
rt = (254-rt -hold *creepbit + rt)*creep/127-creep+127 min (rth-Ramp_Speed) max (rth+Ramp_Speed)
rth = rt max (254-Ramp_Speed-1) min (Ramp_Speed+1)
lth = lt max (254-Ramp_Speed-1) min (Ramp_Speed+1)
sure, it has some maintainance code before hand, but it works.
Creep is usually a wheel input. If it is full forward everything works normally. Full reverse will flip and reverse the tracks. Anything inbetween will rescale the tracks and possibly flip and reverse them too. Lastly, it will prvent motor burn. I got bored. So sue me
  #2   Spotlight this post!  
Unread 04-03-2002, 01:05
Wetzel's Avatar
Wetzel Wetzel is offline
DC Robotics
FRC #2914 (Tiger Pride)
Team Role: Mentor
 
Join Date: Sep 2001
Rookie Year: 1999
Location: DC
Posts: 3,522
Wetzel has a reputation beyond reputeWetzel has a reputation beyond reputeWetzel has a reputation beyond reputeWetzel has a reputation beyond reputeWetzel has a reputation beyond reputeWetzel has a reputation beyond reputeWetzel has a reputation beyond reputeWetzel has a reputation beyond reputeWetzel has a reputation beyond reputeWetzel has a reputation beyond reputeWetzel has a reputation beyond repute
Send a message via AIM to Wetzel
Nice. If I can get my hands on a copy of our optimized code, I'll post it. However, we are not running with the optimized code because the two members of our team over optimzied it to be compleatly unreadable. I got lost as they tried to explain it to me.
Yours I can follow. Theres was optimized with bitshifts, which I still don't understand...
__________________
Viva Olancho!
  #3   Spotlight this post!  
Unread 04-03-2002, 16:02
Manoel's Avatar
Manoel Manoel is offline
Registered User
FRC #0383 (Brazilian Machine)
Team Role: Mentor
 
Join Date: Jun 2001
Rookie Year: 2000
Location: Porto Alegre, RS, Brazil
Posts: 608
Manoel has a reputation beyond reputeManoel has a reputation beyond reputeManoel has a reputation beyond reputeManoel has a reputation beyond reputeManoel has a reputation beyond reputeManoel has a reputation beyond reputeManoel has a reputation beyond reputeManoel has a reputation beyond reputeManoel has a reputation beyond reputeManoel has a reputation beyond reputeManoel has a reputation beyond repute
Send a message via ICQ to Manoel Send a message via MSN to Manoel
I posted something about this subject a while ago and got no response. I'm a big fan of one line code instead of if statements, just like you. Not only it looks nicer, it's a very strong intellectual exercise. However, my doubt is: Which one is really better? One line code obviously takes less RAM space, but which is faster? Is there any other aspect to be considered?

(Our code contains only two if statements, one of them being on the 'DO NOT CHANGE' part of the code. Looks great, works great, and I'm willing to post/e-mail the code to anyone interested. )
__________________
Manoel Flores da Cunha
Mentor
Brazilian Machine
Team # 383
  #4   Spotlight this post!  
Unread 04-03-2002, 18:34
Ian W. Ian W. is offline
College? What?
no team (Gompei and the Herd)
Team Role: College Student
 
Join Date: Jan 2002
Rookie Year: 2002
Location: Worcester, MA | Smithtown, NY
Posts: 1,464
Ian W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to all
Send a message via AIM to Ian W.
if i knew more PBASIC, it would make more sense. also, if knew what each variable was. otherwise, it seems to make logical sense.
__________________
AIM --> Woloi
Email --> ian@woloschin.com
  #5   Spotlight this post!  
Unread 04-03-2002, 20:15
srawls's Avatar
srawls srawls is offline
Registered User
#0122 (Nasa Knights)
 
Join Date: Dec 2001
Location: Yorktown
Posts: 507
srawls is an unknown quantity at this point
Send a message via AIM to srawls
Oh man, I'm glad to hear I'm not alone! I love to make things into one liners, and then optimize them with bitwise operators. Last year I took a whole "paragraph" of code and put it into *one* statement ... let's just say it turned into a "paragraph" line

Hmm ... If statements definitely take longer, I read that somewhere and it makes sense.
  #6   Spotlight this post!  
Unread 04-03-2002, 21:45
Ian W. Ian W. is offline
College? What?
no team (Gompei and the Herd)
Team Role: College Student
 
Join Date: Jan 2002
Rookie Year: 2002
Location: Worcester, MA | Smithtown, NY
Posts: 1,464
Ian W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to all
Send a message via AIM to Ian W.
quick question, what is a bitwise operator? if someone can quickly explain it (i know a bunch of C++, so if someone can explain it in C++ terms that would be cool)? i belive the default code used nitwise operators to initialize the uP when power goes on, but i didn't understand what they did. also, what does "min" or "max" mean?
__________________
AIM --> Woloi
Email --> ian@woloschin.com
  #7   Spotlight this post!  
Unread 04-03-2002, 23:22
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
Bitwise operator

In C the common ones are bitwise OR | bitwise AND & and (please correct me if I'm wrong, my C book is at work) bitwise XOR ^

What the operators do is take the number in binary and perform the operation (or, and, xor) on the corresponding bits.

OR evaluates to 1 if either input is 1, 0 otherwise
AND evaluates to 1 if both inputs are 1, 0 otherwise
XOR evaluates to 1 if only 1 (not both) inputs are 1, 0 otherwise

Example:
148 | 51 -> 10010100 | 00110011 = 10110111 -> 183
148 & 51 -> 10010100 | 00110011 = 00010000 -> 16
148 ^ 51 -> 10010100 | 00110011 = 10100111 -> 167

If you take an intro to computer engineering class in college, you'll learn this within the first couple of weeks. If you have more questions ask away, we're here to help.

Mike
  #8   Spotlight this post!  
Unread 05-03-2002, 00:24
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
MIN and MAX

Quote:
Originally posted by Ian W.
also, what does "min" or "max" mean?
In PBASIC, MIN and MAX are somewhat counterintuitive. (That means they work differently than you might expect. )

Other languages have MIN and MAX functions which compare two (or more) operands, and return the smallest (or largest) value. In PBASIC, the operation is actually exactly the opposite! MIN returns the larger of two values, and MAX returns the smaller!

But it isn't really that confusing. Just think of it as "clamping" the minimum and/or maximum values of a variable or calculation. For example, X = X MIN 10 will leave X unchanged as long as it is greater than (or equal to) 10. However, if its value drops below 10, it is set to 10 instead. MAX works similarly.

BUT BE CAREFUL! MIN 0 DOESN'T DO ANYTHING! You might think that it would prevent the result of a calculation from going negative, but you'd be wrong! MIN and MAX only work with positive numbers, and if you try to give them negative numbers, they will look at them as very large positive numbers! (The reason for this is that the BS2 stores negative numbers in twos complement form. See this message for a very concise description of how twos complements work.)
__________________
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

Last edited by Greg Ross : 05-03-2002 at 00:33.
  #9   Spotlight this post!  
Unread 06-03-2002, 17:18
K-RAD K-RAD is offline
Registered User
#0116 (Epsilon Delta)
 
Join Date: Feb 2002
Location: Herndon, VA
Posts: 3
K-RAD is an unknown quantity at this point
Send a message via AIM to K-RAD Send a message via Yahoo to K-RAD
Exclamation

Although one liners are nice, they're not necessarily faster. Multiplication instructions are always SLOW. Once you get to the point where the lines of code are that horribly long (and having non-bitwise operators), I'd question the speed/code size improvements, if any.

In response to the comment about eliminating 'if' statements for speed: you're right and wrong. With today's modern processors you generally want to avoid using 'if' statements as much as possible due to branch prediction. However, I don't believe the simple processor on the BASIC STAMP implements pipelining. You'd probably be taking more cycles, and probably more memory as well, by doing all of that math than if you simply used 'if' statements.

My favorite line from my optimized code is:
Dr_Motor3 = (40 << (~(basket_lift | basket_lower)) << 2) | (15 >> (~(basket_lift) << 2))
It effectively eliminated a 4 condition branch statement ;-)

PBASIC was so horrible that I *wanted* to program in assembly.
__________________
MTV is my source for new and original music
  #10   Spotlight this post!  
Unread 07-03-2002, 16:29
Ian W. Ian W. is offline
College? What?
no team (Gompei and the Herd)
Team Role: College Student
 
Join Date: Jan 2002
Rookie Year: 2002
Location: Worcester, MA | Smithtown, NY
Posts: 1,464
Ian W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to all
Send a message via AIM to Ian W.
wow, and i thought C++ was somewhat complicated sometimes. i wish we could program in something a bit higher level, but i guess Java and C++ are more application only code. oh well, i'm planning on going into computer science, or something relatred to that, in college, guess it doesn't hurt to know more code.
__________________
AIM --> Woloi
Email --> ian@woloschin.com
  #11   Spotlight this post!  
Unread 09-03-2002, 23:36
ChrisA's Avatar
ChrisA ChrisA is offline
Registered User
#0857 (Superior Roboworks)
Team Role: College Student
 
Join Date: Feb 2002
Location: Michigan Tech
Posts: 157
ChrisA is on a distinguished road
Send a message via AIM to ChrisA
just looking at those one liners makes me cringe...
debuggin one liners definitely takes a lot of thinking on your part, though they have got to make your files a lot smaller (less hd space = good)
  #12   Spotlight this post!  
Unread 10-03-2002, 17:33
Wetzel's Avatar
Wetzel Wetzel is offline
DC Robotics
FRC #2914 (Tiger Pride)
Team Role: Mentor
 
Join Date: Sep 2001
Rookie Year: 1999
Location: DC
Posts: 3,522
Wetzel has a reputation beyond reputeWetzel has a reputation beyond reputeWetzel has a reputation beyond reputeWetzel has a reputation beyond reputeWetzel has a reputation beyond reputeWetzel has a reputation beyond reputeWetzel has a reputation beyond reputeWetzel has a reputation beyond reputeWetzel has a reputation beyond reputeWetzel has a reputation beyond reputeWetzel has a reputation beyond repute
Send a message via AIM to Wetzel
Quote:
Originally posted by ChrisA
just looking at those one liners makes me cringe...
debuggin one liners definitely takes a lot of thinking on your part, though they have got to make your files a lot smaller (less hd space = good)
We arn't running the optimized code for excatly that reason. To hard to debug. But it does only use 18% of the memory. Small enough for ya?
__________________
Viva Olancho!
  #13   Spotlight this post!  
Unread 10-03-2002, 22:27
ChrisA's Avatar
ChrisA ChrisA is offline
Registered User
#0857 (Superior Roboworks)
Team Role: College Student
 
Join Date: Feb 2002
Location: Michigan Tech
Posts: 157
ChrisA is on a distinguished road
Send a message via AIM to ChrisA
you get that down around 10% and i then i might be impressed
__________________
Programmer
----------------
Team#857
Superior Roboworks

WildStang Alum
  #14   Spotlight this post!  
Unread 12-03-2002, 18:06
VanWEric VanWEric is offline
Registered User
#0250 (Dynamos)
 
Join Date: Feb 2002
Location: NY
Posts: 86
VanWEric is an unknown quantity at this point
C++ bitwise ops?

With all this talk of bitwise vs non bitwise i got thinking -- Can I apply this to C++? so far i know that & and ^ are and and or, but it would be helpful to know more. If anyone knows how to do this i would be much obliged.
  #15   Spotlight this post!  
Unread 12-03-2002, 19:56
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: C++ bitwise ops?

Quote:
Originally posted by VanWEric
With all this talk of bitwise vs non bitwise i got thinking -- Can I apply this to C++? so far i know that & and ^ are and and or, but it would be helpful to know more. If anyone knows how to do this i would be much obliged.
C++ has all the same bitwise operators: & does a bitwise AND, | is the bitwise OR, and ^ the bitwise XOR. (I think you misspoke when you said ^ is OR.) << and >> are the bitwise shift operators, and ~ is the unary bitwise negation.
__________________
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
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
Sprockets and Roller Chain Models Madison Inventor 3 04-02-2003 11:51
iParts on firstcadlibrary.com Ed Sparks Inventor 3 08-01-2003 23:30
URGENT!! Need OI power brick specs for 7AM demo tomorrow!!! kmcclary Electrical 8 10-10-2002 10:56
power of globe motor with gear box? Ken Leung Motors 5 21-11-2001 13:43
Max mechanical power of motors, how is it useful? Ken Leung Motors 1 09-11-2001 14:15


All times are GMT -5. The time now is 17:37.

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