Revs on the BBC Micro

# Modelling the engine

## The maths behind engine torque, gear ratios and power transmission

This article details the calculations that the driving model performs when modelling the engine. It is designed to be read alongside the core driving model calculations.

The following is done as part of calculation 7 in the core driving model. The code for the following is in the ApplyEngine routine, and the aim of this calculation is to calculate the engine torque and rev count.

## Calculate the rev count -----------------------

First, we calculate the rev count at which the gear mechanism would be turning if the clutch were engaged at this speed. This depends on the gear ratio for the current gear, which is defined in the track data file at trackGearRatio, and which describes the relationship between the rev count and the wheel speed for each gear:

```  (A T) = trackGearRatio * playerSpeed * 8
```

So lower gears correspond to more revs at the same wheel speed when compared to higher gears.

## Process the clutch ------------------

Next, we deal with the clutch.

The clutch comes into play once the engine has started and the car is on the ground (if this isn't the case, the logic is at the very start of the ApplyEngine routine will take us off either to the engine-starting routine, or will simply zero the engine torque and stop the calculations).

If the engine has started and we are on the ground, and we are holding down a gear change key, then the clutch is disengaged and bit 7 of clutchEngaged is set, and we skip the rest of this stage. If we are not holding down a gear change key and the clutch is already engaged (in which case bit 7 of clutchEngaged is clear), we skip the rest of this stage.

The rest of this stage only applies when the clutch is disengaged but we are not holding down a gear change key (which will happen when we release the gear change key). At this point, we check to see if any of these are true:

• The throttle is not being applied.
• playerSpeedHi >= 22, so we are moving.
• This is a race and we are not showing the blue lights (so we must be showing the green lights, or no lights at all).
• This is a race, we are showing the blue lights and still have at least 10 iterations until the green lights are shown (i.e. raceStarting = 160 and there are at least 10 iterations until the main loop counter is a multiple of 64 - see the deep dive on starting lights for more details).
• A >= revsOnGearChange, so the revs are higher than at the last gear change.

If any of these are true, then then the clutch engages. This means that following a gear change, the clutch engages if any of the following are true:

• We are not applying the throttle.
• We're going faster than 22 mph.
• We're in a race and the lights are green or haven't been shown yet.
• We're in a race and the lights are blue but are not just about to turn green.
• We rev the engine past the level of last gear change (in revsOnGearChange).

In terms of the race, this means that if we release the gear change key when the lights are blue, the clutch will engage and we will stall, though if we release it just before the lights turn green, the clutch will only engage when the lights turn green, giving us a good start.

If the clutch hasn't been automatically engaged by the above checks, then the clutch remains disengaged and we calculate the following:

```  A = revsOnGearChange

If A >= 108, A = A - 2 and revsOnGearChange = revsOnGearChange - 2
```

So the revs reduce slowly if the clutch remains disengaged and we don't rev the engine, and the threshold for the clutch re-engaging is also reduced.

## Calculate the torque --------------------

We now set the rev count to the value of A, which will either be the value from the first step, or an amended value if the clutch is still disengaged:

```  revCount = A
```

We now calculate the power being generated by the engine at this rev count.

First, we cap the rev count to a maximum of 170:

```  max(170, revCount)
```

so in the following, revCount refers to this capped number.

Next, we calculate the engine power depending on the rev count, as follows:

Rev countCalculation
< 3The engine has stalled
3 to 82A = revCount * 2 + 152
83 to 86A = 186 - (revCount - 83)
87 to 91A = 182 - (revCount - 87) * 4
>= 92A = 162 - (revCount - 92) * 2

In terms of the rev counter on the dashboard, a revCount of 82 shows the needle at the 5,000 revs mark, while 86 is 5,250 revs and 92 is 5,500 revs. This calculation produces the highest engine power between 5,000 and 5,500 revs, with the optimum power of 186 at exactly 5,000 revs.

Also, note that the calculation for the range 3 to 82 produces increasing power up to a rev count of 52, at which point revCount * 2 + 152 wraps around to zero again, only reaching a power of 60 by the time the revs reach 82. The moral of the story is that having a rev count of less than 5,000 produces way less power than a rev count of between 5,000 and 5,500.

If the engine stalls with a rev count of less than 3, then we turn the engine off, zero the torque and jump to the next calculation in the driving model.

Otherwise A now contains the raw power that's being generated by the engine, so the final step in calculating the engine torque is to scale this by the power ratio of the gear, which is defined in the track data file at trackGearPower and which describes how much engine power makes it through to the wheels in any given gear:

```  engineTorque = trackGearPower * A
```

So lower gears create more torque at the same rev count when compared to higher gears.

Finally, we also set the sound target for the new rev count, so the engine gradually changes its sound to the new level:

```  soundRevTarget = revCount + 25
```

And that's how the engine works in Revs.