/* --------------------------------------------------------------------- // Figure 8.4: Cruise control. // --------------------------------------------------------------------- */ #include #include "throttle.h" void main( void ) { const float eps = 2.5; /* Fuzz factor for comparison. */ const float delta = 5.; /* Change amount for throttle setting. */ float throttle; /* Current throttle setting. */ float target; /* Desired speed. */ float speed; /* Current speed. */ float dif; /* Difference between target and actual speeds. */ while (! read_on_switch()); /* Leave loop when driver sets speed. */ target = read_speed(); /* Initial speed and throttle settings. */ throttle = read_throttle(); while (! read_brake()){ /* Leave loop when driver hits brake. */ speed = read_speed(); dif = target - speed; /* Compare current speed to target */ if (dif > eps) { throttle += delta; if (throttle > 90.0) throttle = 90.0; } else if (dif < -eps) { throttle -= delta; if (throttle < 0.0) throttle = 0.0; } else puts( "Speed is OK; do nothing." ); set_throttle( throttle ); } }