View Single Post
  #3   Spotlight this post!  
Unread 28-03-2004, 23:23
Josh Siegel's Avatar
Josh Siegel Josh Siegel is offline
lurker
#0448 (Crandroids)
 
Join Date: Nov 2003
Rookie Year: 2003
Location: Bloomfield Hills, Michigan
Posts: 67
Josh Siegel will become famous soon enough
Send a message via AIM to Josh Siegel Send a message via MSN to Josh Siegel
Re: Speed limiting code from last year in C?

Well, I just spent 18 minutes re-writing the code on here (my laptop isn't with me) and it didn't post!

I'm sorry, I'm exhausted, and I'm going to bed now, but here's what I'm trying to do, with topspeed instead of p1_wheel:


p1_wheel = (((p1_wheel*154)/254)+100) MAX 254 'adjust wheel to 154-254
IF drive_R < 127 THEN drive_R_reverse: 'is the right drive forward
drive_R = (drive_R - 127) MIN 0 'subract 127 to get the forward value
drive_R = (drive_R * p1_wheel)/254 'multiply by the wheel percentage
drive_R = (drive_R + 127) MAX 254 'add 127 back for proper output
GOTO drive_R_done: 'exit the drive right section
drive_R_reverse: 'the right drive is reverse
drive_R = (127 - drive_R) MIN 0 'invert drive-R to get a forward value
drive_R = (drive_R * p1_wheel)/254 'multiply by the wheel percentage
drive_R = (127 - drive_R) MIN 0 'invert drive_R back to normal
drive_R_done: 'drive_R section complete

IF drive_L < 127 THEN drive_L_reverse: 'is the left drive forward
drive_L = (drive_L - 127) MIN 0 'subract 127 to get the forward value
drive_L = (drive_L * p1_wheel)/254 'multiply by the wheel percentage
drive_L = (drive_L + 127) MAX 254 'add 127 back for proper output
GOTO drive_L_done: 'exit the left right section
drive_L_reverse: 'the left drive is reverse
drive_L = (127 - drive_L) MIN 0 'invert drive-L to get a forward value
drive_L = (drive_L * p1_wheel)/254 'multiply by the wheel percentage
drive_L = (127 - drive_L) MIN 0 'invert drive_R back to normal
drive_L_done: 'drive_L section complete



Edit: Okay, so I lied, I'm not going to bed just yet . I have to get this programming bug out of my system!;

Does this look like it would work? Its should be the same as above, except without the error protection (min 0/max 254)

Code:
unsigned int topspeed = 127;

if (pwm01 > 127) {
pwm01 = ((((pwm01 - 127)*topspeed)/254)+127);
}
if (pwm02 > 127) {
pwm02 = ((((pwm02 - 127)*topspeed)/254)+127);
}
if (pwm01 < 127) {
pwm01 = (127-(((127 - pwm01)*topspeed)/254));
}
if (pwm02 < 127) {
pwm02 = (127-(((127 - pwm02)*topspeed)/254));
}

Last edited by Josh Siegel : 28-03-2004 at 23:29.