View Single Post
  #4   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