Hello Botball Community,
Here is a snippet of my code:
set_create_distance(0);
int xLoc = 0;
while(xLoc < 500)
{
create_drive_straight(500);
xLoc = get_create_distance(0.1);
printf("%d\n", xLoc);
sleep(0.1);
}
The question I had was, will I get better results by decreasing the time for "sleep" and "get_create_distance" ??
I feel like I herd somewhere that the CBC/Create only tolerate up to tenth's of a second.
You will definitely get more consistent results with a lower delay. I tend to pass 0.0 to the Create functions, and sleep for about 5ms. A larger delay only really makes sense if you're exchanging lots of data with the Create; your use case (2 commands per loop) won't bog down the serial port.
You can also optimize your code by moving create_drive_straight outside of the loop... there's no need to call it continuously unless you're changing the speed.
Hope this helps.
-Jeremy Rand
Senior Programmer, Team SNARC (2012-2013), Norman Advanced (2010-2011), Norman HS (2008-2009), Norman North (2005-2007), Whittier MS (2003-2004)
2012-2013 VP of Tech, 2011 President, Botball YAC (2009-2013)
Mentor, Alcott and Whittier MS
Great! Thanks!
- Ulzee
Btw, I've tested the new code a couple of times, and out of 10 trials, the distance measured with get_create_distance(0.0); overshot by 0~30 millimeters (e.g. i got values of 500, which is ideal, but also got 511, 517,529, etc). Is there a simple way to minimize this overshooting issue?
I would like to note I am using the Simulator for now; i have done no actual testing with the create
Thanks,
- Ulzee
My code:
int i;
for(i = 0; i < 10; i ++)
{
set_create_distance(0);
create_drive_straight(500);
int xLoc = 0;
while(xLoc < 500)
{
xLoc = get_create_distance(0.0);
sleep(0.05);
}
printf("Final Dist = %d\n", xLoc);
}
I would recommend putting the sleep before the get_create_distance. Right now your code is guaranteed to overshoot by at least 50ms. If it's overshooting too much, you could try lowering the sleep value (you have 50ms; I've used 5ms with no issue).
As another note, if you don't need the CBC GUI and other program threads to respond quickly, you could eliminate the sleep completely. Your while loop will continuously poll the Create (each poll takes 6ms in the CBOB and up to 15ms in the Create), and will terminate as soon as you've traversed the distance. This will cause all other threads to lag, but that may be acceptable, and it will definitely be more precise. It should be noted that doing this in KISS-Sim will probably not work very well since the same CPU is handling the Create and your while loop; this is only advisable on a CBC (where the CBOB runs in parallel to your while loop).
-Jeremy Rand
Senior Programmer, Team SNARC (2012-2013), Norman Advanced (2010-2011), Norman HS (2008-2009), Norman North (2005-2007), Whittier MS (2003-2004)
2012-2013 VP of Tech, 2011 President, Botball YAC (2009-2013)
Mentor, Alcott and Whittier MS