C

Qt Quick Ultralite Automotive Cluster Demo

// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial
#ifndef DRIVETRAIN_H
#define DRIVETRAIN_H

#include <stdint.h>

namespace Simulation {
class Drivetrain
{
public:
    struct DriveData
    {
        float speed;
        float rpm;
        int gear;
        float odo;
        float range;
        float battery;
        float fuel;
        float coolantTemp;
    };

    struct Config
    {
        static const int MAX_GEARS = 10;

        float maxSpeed;
        float maxRpm;
        float minRpm;

        float shiftUpAtRpm;
        float shiftDownAtRpm;

        float tireCircumference; // cm
        float diffRatio;

        int gearNum;
        float gearRatios[MAX_GEARS];

        float batteryFillRatio;
        float batteryDrainRatio;

        float coolantHeatRatio;
        float coolantCooldownRatio;

        float optimalTemp;
        float maxTemp;

        float maxRange;
    };

    Drivetrain(const Config &config);
    void update(uint32_t tick, float acceleration);
    void updateCruiseControl(uint32_t tick, float targetSpeed);
    void reset();
    void resetOdo(float value = 0);
    void resetBattery(float value = 0);

    float getSpeed(float rpm, int gear) const;
    float getRpm(float speed, int gear) const;

    const DriveData &getDriveData() const { return _data; }

private:
    float updateRpm(uint32_t tick, float acceleration);
    void updateGearShift(uint32_t tick, float prevRpm, float acceleration);
    void updateSpeed();
    void applySpeedLimit();
    void updateOdo(uint32_t tick);
    void updateRange();
    void updateBattery(uint32_t tick, float acceleration);
    void updateFuelLevel();
    void updateCoolantTemp(uint32_t tick, float acceleration);
    void shiftGear(int delta);

    DriveData _data;
    const Config _config;
    uint32_t _constSpeedTime;
};

} // namespace Simulation

#endif // DRIVETRAIN_H