Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Java (http://www.chiefdelphi.com/forums/forumdisplay.php?f=184)
-   -   Speeding up and slowing down in autonomous (http://www.chiefdelphi.com/forums/showthread.php?t=126956)

AdamWiwc 20-02-2014 20:52

Speeding up and slowing down in autonomous
 
For some reason whenever I try to run this code; speed remains at 0 and it counts i up to x and then speed is suddenly changed to 1.0. Speed is supposed to be a decimal of x depending on how many times the for loop has run through. I don't understand why this is happening and would be very grateful for some clarity. :)

double speed;
int x = 200;

for(int i = 0; i <= x; i++){
double gyroAngle = gyro.getAngle();
speed = i/x;
chassis.drive(-speed,-gyroAngle * Kp);
System.out.println("Speed- " + speed);
System.out.println("Ticks- " + i);
Timer.delay(0.01);
}
Timer.delay(1.0);
for(int i = x; i >= 0; i--){
double gyroAngle = gyro.getAngle();
speed = i/x;
chassis.drive(-speed,-gyroAngle * Kp);
Timer.delay(0.01);
}
chassis.drive(0.0, 0.0); //full stop

Thanks
~Adam Wiwc

Joe Ross 20-02-2014 23:34

See http://stackoverflow.com/questions/3...oduce-a-double

TFleig78 20-02-2014 23:51

Re: Speeding up and slowing down in autonomous
 
You're performing integer division. When you divide an int by a larger int, you get 0. Java gets rid of the decimal portion of the number. Try making i a double instead of an int.


All times are GMT -5. The time now is 11:45.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi