Go to Post You can't unwind after build season knowing that competition season is just around the corner! ;) - Hallry [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

 
 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #13   Spotlight this post!  
Unread 27-02-2007, 14:32
Dave Scheck's Avatar
Dave Scheck Dave Scheck is offline
Registered User
FRC #0111 (WildStang)
Team Role: Engineer
 
Join Date: Feb 2003
Rookie Year: 2002
Location: Arlington Heights, IL
Posts: 574
Dave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond repute
Re: Lowering the voltage on a spike

Quote:
Originally Posted by Mr. E View Post
I know I'm not C savy, so things like
#define DRIVE_SLOW_FWD 155
#define DRIVE_SLOW_REV 99

// other code...

pwm01 = DRIVE_SLOW_FWD;

always seems like an extra step that I don't have to do, especially when I can just say pwm01=155 and put in a comment on the side that reminds me what the line is actually doing.
That may be true in small simple cases, but as soon as you start getting more complicated and using the same value all over the place it will make your life easier. For example, if you look in ifi_aliases.h, you'll find
Code:
#define pwm01           txdata.rc_pwm01
Now imagine if you didn't have that alias in place and had to type txdata.rc_pwm01 all over the place. The same goes for constants. Lets say you had a program that was doing a bunch of operations on circles and you needed to use PI all over the place. You could hardcode each line of code to have 3.14, but what happens if you want more precision? You have to search and replace all the 3.14's with 3.1415. If you had a constant, you would only have to change it in one place.

There's no hard rule that says you have to use constants/macros, but they sure do make your life easier.

Quote:
the window turns a motor until it hits a switch. If I use a relay I can use
relay1_fwd = p1_sw_trig & rc_dig_in01; /* FWD only if switch1 is not closed. */
relay1_rev = p1_sw_top & rc_dig_in02; /* REV only if switch2 is not closed. */
How about something like this?
Code:
#define DRIVE_SLOW_FWD 155
#define DRIVE_SLOW_REV 99
....
if((p1_sw_trig == 1) && (rc_dig_in01 == 1))
{
  pwm01 = DRIVE_SLOW_FWD;
}
else if((p1_sw_top == 1) && (rc_dig_in02 == 1))
{
  pwm01 = DRIVE_SLOW_REV;
}
else
{
  pwm01 = 127;
}
A couple of notes:
- The above code assumes that the digital ins report a 1 when the switch is not pressed
- You'll notice that I made explicit comparisons to 1 to determine the switch states. This is for readability and ease of maintenance. I can quickly go in and change a 1 to a 0 and get different behavior. Yes, I could just add a ! before the variable, but then it changes the visual flow of the code.
- You'll also notice that I'm doing a boolean and (&&) instead of a bitwise and (&). While the operation in this case is identical, it's a good habit to get into.
- There is a small error case that is not handled. What happens if both buttons are pressed at the same time? The FWD case will dominate until the switch is pressed then the REV case will take over. As soon as switch 1 is released it will drive FWD again.

Hope this helps
 


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 best for lowering the middle wheel? Jeff K. Technical Discussion 9 28-01-2006 12:49
Lowering the Bar? Aignam Regional Competitions 4 07-03-2004 23:13
Raising the bar...or lowering it? Ben Mitchell General Forum 29 13-01-2004 03:30
Problem with Lowering the Bar Kevin Kolodziej General Forum 14 12-01-2004 21:58
Voltage Byte -> True Voltage Ulibrium Technical Discussion 11 01-03-2002 08:55


All times are GMT -5. The time now is 00:22.

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