📄 car.c
字号:
/*****************************************************
car.c - simulates the behavior of a car
----------------------------------------------------
The function SimulateCar should be called in regular
interval (100ms)
*****************************************************/
#include "hidef.h" /* this file declares symbols user by the CodeWarrior environment */
#include <stdlib.h>
#include <math.h>
#include "globals.h"
#include "car.h"
#define GEARS (sizeof(ratio) / sizeof(int)) /* number of gears (calculate elements of array below) */
#define SPEED_TACHO_RATIO 40
static float ratio[] = {1.2,
0.70,
0.45,
0.28,
0.20}; /* gear ratios; one entry per gear */
static float E = 0; // energy of the car ( (m * v^2) / 2 )
static int m = 25; // mass of the car
static float v; // speed of the car
static float Cw = 0.20; // wind resistance coefficient
static int dE = 1000; // energy from the engine
static int fBrake = 0; // brake force
static float rpm = 0; // RPM of the motor
static float rollRes = 1; // rolling resistance
static float Fule = 100; // Fule tank
static float Temp = 0; // temperature
static int vi;
static int rpmi;
static int vTarget = 120;
static int autoSpeed = 1;
static int gear = 0;
#define MAX_E 1950
void SimulateCar(void)
{
float rpml, de, deLim, resistance;
int i;
if (fBrake > 5) {
autoSpeed = 0;
dE = 0;
} else {
fBrake = 0;
}
resistance = 10 * rollRes + rollRes * v + Cw * v * v * v / 1950;
if (Fule > 0) {
if (v < 250) {
if (autoSpeed) {
fBrake = 0;
de = (vTarget - v) * 200 + resistance;
if (de < 0) {
de = 0;
} else {
deLim = v * v / 20 + 500;
if (de > deLim) {
de = deLim;
}
if (de > MAX_E) {
de = MAX_E;
}
}
dE = de;
}
} else {
dE = dE * 0.995;
}
E = E + dE;
Temp = Temp + 0.03 * dE / MAX_E;
Fule = Fule - 0.002 * dE / MAX_E;
}
E = E - fBrake * v; // brake with constant force
E = E - resistance;
if (E < 0) {
E = 0;
}
if (Temp > 60) {
Temp = Temp - 0.02; // cooler fan on
}
Temp = Temp - 0.001;
v = sqrtf ( E / m * 2);
i = 0;
do {
rpml = v * 90 * ratio[i];
i++;
} while (rpml > (4500 - (MAX_E - dE) * 1.7) && i < 5);
if (rpml < 600) rpml = 600;
rpm = rpml;
gear = i;
vi = v;
rpmi = rpm;
GDICData.SpeedPosition = (int)(v * 14);
GDICData.RPMPosition = (int)(rpm * 0.53);
GDICData.FuelPosition = (int)(Fule * 12 + 1000);
GDICData.CoolPosition = (int)(Temp * 12 + 1000);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -