/*
* Calculate braking distances.
*
* $Id: braking.c,v 1.4 2009/01/04 01:04:07 grog Exp $
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
void usage (char *me)
{
fprintf (stderr,
"Usage:\n"
"%s speed reaction deceleration [sample-distance]\n"
"\tspeed is initial speed in km/h\n"
"\treaction is reaction time in seconds\n"
"\tdeceleration is braking deceleration in m/sē\n"
"\tsample-distance is the distance for which to calculate the residual speed\n",
me );
exit (1);
}
int main (int argc, char *argv [])
{
int verbose = 0;
int arg;
float speed;
float reaction_time;
float reaction_distance;
float deceleration;
float deceleration_time;
float deceleration_distance;
float total_time;
float total_distance;
float sample_distance; /* distance to sample speed */
float sample_speed; /* and speed at that time */
if (argc < 4)
usage (argv [0]);
if (! strcmp (argv [1], "-v"))
{
verbose = 1;
arg = 2;
}
else
arg = 1;
speed = atof (argv [arg++]) / 3.6; /* in m/s */
reaction_time = atof (argv [arg++]); /* seconds */
deceleration = atof (argv [arg++]); /* m/sē */
deceleration_time = speed / deceleration; /* time to decelerate to 0 */
reaction_distance = speed * reaction_time;
deceleration_distance = speed * deceleration_time / 2; /* because average speed is half */
total_time = reaction_time + deceleration_time;
total_distance = reaction_distance + deceleration_distance;
if (argc > arg) /* specify speed at given distance */
{
sample_distance = atof (argv [arg++]);
if (sample_distance >= total_distance)
sample_speed = 0.0;
else if (sample_distance <= reaction_distance)
sample_speed = speed;
else
sample_speed = speed * sqrt ((total_distance - sample_distance) / deceleration_distance);
if (verbose)
printf ("Speed\tSpeed\tReact\tReact\tDecel\tDecel\tTotal\tTotal\tSample\tSample\tSample\n"
"(km/h)\t(m/s)\tTime\tDist\tTime\tDistance Time\tDistance Dist\tSpeed\tSpeed\n"
"\t\t\t\t\t\t\t\t\t(km/h)\t(m/s)\n");
printf ("%6.2f\t%6.2f\t%6.2f\t%6.2f\t%6.2f\t%6.2f\t%6.2f\t%6.2f\t%6.2f\t%6.2f\t%6.2f\n",
speed * 3.6,
speed,
reaction_time,
reaction_distance,
deceleration_time,
deceleration_distance,
total_time,
total_distance,
sample_distance,
sample_speed * 3.6,
sample_speed); /* in km/h */
}
else
{
if (verbose)
printf ("Speed\tSpeed\tReact\tReact\tDecel\tDecel\tTotal\tTotal\n"
"(km/h)\t(m/s)\tTime\tDist\tTime\tDistance Time\tDistance\n");
printf ("%6.2f\t%6.2f\t%6.2f\t%6.2f\t%6.2f\t%6.2f\t%6.2f\t%6.2f\n",
speed * 3.6,
speed,
reaction_time,
reaction_distance,
deceleration_time,
deceleration_distance,
total_time,
total_distance );
}
return 0;
}