![]() |
Speed limiting code from last year in C?
I've been working on our EduBot programming all week, and I haven't been able to successfully port over the speed-limiting code from last year (jumper on SW7) to C. I'm trying to cut speed for debug purposes with the variable 'topspeed' (0-254), and also have a table of volts->topspeed variable to mantain speed for DR programs, etc. Sadly, even if it compiles, it makes one of the motors just pulse on and off and the other continues at full speed.
I wish I could post all the ways I tried, I think I still have two snippets left, but I thought it would be best to ask if anyone here has done/knows how to make this code fuction without having to deal with my crappy, innefficient ways 'n such. I can post my code without the limiting section if that will help anyone. pwm01 is left motor drive, pwm02 is right motor drive. Thanks so much! :) |
Re: Speed limiting code from last year in C?
I'm not sure I understand what you're trying to do, but a couple things:
If you are just trying to limit the output to the motors (pwm values), you can use a MIN_MAX macro (essentially a double terniary operator), or MIN/MAX depending on what you are doing. #define MIN(value, min) (value < min ? min : value) (or, if value is less than min, min, otherwise value) and #define MIN_MAX(value, min, max) (value < min ? min : (value > max ? max : value)) (same thing, but checks max after min). edit: careful about using these, as terniary operators take up lots of program space! I suppose if you are trying to limit the actual speed of the wheels, you are going to need some sort of sensory input, and then decrease the value of the motors as you go. That takes a bit more doing. Sorry if I totally misunderstood what ya meant :). |
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! :mad:
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 :P. 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; |
Re: Speed limiting code from last year in C?
Our coder came up with a thing that may help (I hope)
this is in the header file: #define Grabber_Speed p4_x /*port 4 x on OI */ #define Forward_Speed (127 + (Grabber_Speed / 2)) #define Reverse_Speed (127 - (Grabber_Speed / 2)) The Grabber_Speed is linked to a Knob (pot) on the IO We are now just using IO switches to go Forward and Reverse So forward and reverse get pluged in with if statements. The Range for Grabber_Speed is 0 to 254 as it comes from The OI |
Re: Speed limiting code from last year in C?
Looks good, though I would suggest you take advantage of signed math in the following way.
Code:
int tempPwm01;Code:
pwm01 = 127 + ( ((int)pwm01 - 127) * topSpeed / 254 );By the way, just to confirm that this is what you are trying to achieve. The above code will turn a pwm value of 254 into 127 + (topSpeed / 2) and a pwm value of 0 into 127 - (topSpeed / 2). It's the same as the C code that you posted but I just want to make sure that's what you want. |
Re: Speed limiting code from last year in C?
Yep, thats what I wanted to do! Thanks! I'll go test it out this morning :).
Edit: Well, I'm looking at my code (I'll post a copy soon for anyone who wants), and I'm not quite sure where this should go/how to call it. I was thinking of rather than goto mainloop; i'd say goto topspeed_code; then in topspeed_code, call 'mainprogram.' That makes sense to me, BUT - I have timed loops for turning so I can't call the routine while it's excecuting - do i put a copy in the same loop with the turning values? Code:
/* |
Re: Speed limiting code from last year in C?
*bump!*
Well, I know what needs to be done now. I can't limit the speed of timed turns, so I need to use pwm01 for those and temppwm01 for the values that change. Which would be better, trying to add the code to the putdata (I don't know how to edit a .lib?), or putting it at the start of the loop? The problem I can forsee with that is that I have multiple putdatas, some in loops and some not, and to loop the topspeed code wouldn't be clean or efficient. |
| All times are GMT -5. The time now is 18:17. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi