|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Trouble with Autonomous
Well the title of the thread actually isnt exactly my problem. Autonomous works however one of my functions doesn't work. We are trying to get our robot to rotate (a 0 degree turn). I'm using WPILib so we have to write totally new code.
Well anways here is the function Rotate(): Code:
void Rotate(char direction, int degrees, char speed)
{
int time = (1000 * (degrees/ 300));
char RightMotor = NEUTRAL + (speed * direction * (SPEED_TO_PWM));
char LeftMotor = NEUTRAL + (speed * direction * (SPEED_TO_PWM));
//the SPEED_TO_PWM = 127/10 which converts the speed to PWM value
printf("Rotate()\r");
SetPWM(LEFT_WHEEL, LeftMotor);
SetPWM(LEFT_FRONT_WHEEL, LeftMotor);
SetPWM(RIGHT_WHEEL, RightMotor);
SetPWM(RIGHT_FRONT_WHEEL, RightMotor);
printf("time: %d\r", time);
Wait(time);
SetPWM(LEFT_WHEEL, NEUTRAL);
SetPWM(LEFT_FRONT_WHEEL, NEUTRAL);
SetPWM(RIGHT_WHEEL, NEUTRAL);
SetPWM(RIGHT_FRONT_WHEEL, NEUTRAL);
//NEUTRAL = 127
}
Code:
Rotate(-1, 30, 10) Code:
time = 300; Any help would be appreciated |
|
#2
|
|||
|
|||
|
Re: Trouble with Autonomous
I can almost assure you the problem has to do with your equation in the line "int time...." you're generating a float in the middle of it (30/300)= 0.1, which the integer math in the PIC reduces to 0, before continuing with the rest of the math.
As for how to fix it, you could multiply all the numbers involved by say 1000, and divide by 1000 as your last step... or... something. |
|
#3
|
||||||
|
||||||
|
Re: Trouble with Autonomous
The problem is with your order of operations.
time = (1000 * (degrees/ 300)); is evaluated in the following order (with degrees being 30). ime = (1000 * (degrees/ 300)); time = (1000 * (30/300)); time = (1000 * 0); time = 0; Since you don't really care what order the operations happen, the following will work for you. time = (1000 * degrees) / 300; time = (1000 * 30) / 300; time = (30000) / 300; time = 100; The only problem with the above example is that if degrees gets above 32, you'd overflow the integer value. You could either declare time as a long, or change your constants, since multiplying by 1000 then dividing by 300 is the same as multiplying by 10 then dividing by 3. |
|
#4
|
|||
|
|||
|
Re: Trouble with Autonomous
Quote:
time = (int) ((long) 1000 * degrees / 300); or more simply if the compiler is really ANSI compliant time = (int) (1000L * degrees / 300); |
|
#5
|
|||||
|
|||||
|
Re: Trouble with Autonomous
The other problem I see is your putting parentheses around your SPEED_TO_PWM macro. With them there, you have a similar rounding issue, and the 127/10 ends up being truncated to 12. If you leave the paretheses off but everything else as it is, the division by 10 won't happen until after all the multiplications are done and you'll end up with more like what you want.
|
|
#6
|
|||||
|
|||||
|
Re: Trouble with Autonomous
Sometimes, really simple is really good.
|
|
#7
|
||||
|
||||
|
Re: Trouble with Autonomous
I don't know if it makes a difference, but it might be better if you went:
Code:
int time; char RightMotor; char LeftMotor; time = (1000 * (degrees/ 300)); RightMotor = NEUTRAL + (speed * direction * (SPEED_TO_PWM)); LeftMotor = NEUTRAL + (speed * direction * (SPEED_TO_PWM)); |
|
#8
|
|||
|
|||
|
Re: Trouble with Autonomous
It might be better to simplify that further using some algebra to something like
Code:
time = (int)((long)10 * (long)degrees / (long)3) |
|
#9
|
||||
|
||||
|
Re: Trouble with Autonomous
Well I decided to take advice. And:
Code:
long time = (1000 * degrees) / 300; Code:
Rotate(-1, 30, 10); |
|
#10
|
|||
|
|||
|
Re: Trouble with Autonomous
Quote:
How are you displaying time to know it's zero? Are you still using the %d specifier in your printf? I can't remember if the PICs are little endian or big endian. If they're big endian, you'll only see the high order half of the long value which in this case is zero. |
|
#11
|
|||
|
|||
|
Re: Trouble with Autonomous
Quote:
BTW, you only need the first (long) cast. Once one element of that calcuation is long, the compiler will force the rest to long to complete it. Actually, casting either the 10 or degrees will do it. Casting the 3 to long will force the result to long but the compiler is free to do the calculation in the parentheses as int then promote it to long before the division. |
|
#12
|
||||
|
||||
|
Re: Trouble with Autonomous
I tried it this way too and it doesnt work.
Code:
long time = ((long)1000* degrees)/ 300; //degrees is 30 |
|
#13
|
||||
|
||||
|
Re: Trouble with Autonomous
Quote:
Quote:
|
|
#14
|
|||
|
|||
|
Re: Trouble with Autonomous
Quote:
If you're doing: printf("Time = %d\r\n", time); it won't work since %d is going to display a 2 byte integer and not the 4 byte long. It will only display either the upper or lower two bytes depending on whether the PIC is little or big endian. You need to either: printf("Time = %d\r\n", (int) time); or printf("Time = %ld\r\n", time); Assuming the implementation of printf you're using is complete enough to accept the %ld specification to print longs. I don't know this is your problem since you haven't shown your latest version with the printf, but it's a high probability on my list. |
|
#15
|
||||
|
||||
|
Re: Trouble with Autonomous
O yea we're done working on our code. It's just that I'm gonna be programmin cap next year and I'm trying to get my stuff down. So sry if I seemd to be doin somethin against the rules but thank you for letting me now I guess. Well I'm done anyways. the printf("%ld") works. Thank you
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Trouble with the GTS | Bomberofdoom | Programming | 1 | 04-02-2007 10:13 |
| Has anyone else had trouble staying within the 10 second limit in autonomous? | lkdjm | Programming | 18 | 12-02-2006 18:10 |
| Autonomous Code trouble | The yellowdart | Programming | 16 | 21-01-2006 10:36 |
| My trouble with fund raising. | BaldwinNYRookie | Fundraising | 5 | 03-04-2005 13:34 |
| Trouble with pwm outputs | misterikkit | Programming | 16 | 17-01-2004 10:56 |