next up previous
Next: Processing after a clock Up: Your job in this Previous: Controlling the accelerator

Changing the current speed

A second task is to write the bodies of the two methods ChangeDueToGrade and ChangeDueToAccel. Make sure you implement code that does precisely what the specification says to do.

The first task is easy: Method ChangeDueToGrade should update the current speed by some amount proportional to the road grade. Going uphill decreases the speed; going downhill increases the speed.

The second task is harder, and we leave it to you to do something reasonable. Method ChangeDueToAccel should update the current speed based on the state of the accelerator. Here are some constraints. Add 1 to the current speed if (and only if) the accelerator is on. But you should also add some amount that depends on the difference between the desired and current speeds. The additional speed change should be proportional to that difference. You can decide on the constant of proportionality. Make it reasonable. For example, if the accelerator is on, it should be able to hold to the current speed when the road grade is 10. If the current speed keeps dropping, perhaps the accelerator isn't powerful enough. If the accelerator is off, just subtract some small amount from the speed, say 0.3 mph.

Note that in this simulation, the road grade and accelerator affect the speed of the car independent of one another.


next up previous
Next: Processing after a clock Up: Your job in this Previous: Controlling the accelerator
Claire Cardie
1998-11-11