aboutsummaryrefslogtreecommitdiffstats
path: root/extras/VehicleSimulator.py
blob: cf790d27e50444fd8dc30e12d499c68a451cd343 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# Copyright (C) 2023 Suchinton Chakravarty
# Copyright (C) 2024 Konsulko Group
#
# SPDX-License-Identifier: Apache-2.0

import logging
import math
import random
import time
import threading
from PyQt5.QtCore import QObject, pyqtSignal
from extras.KuksaClient import KuksaClient

class VehicleSimulator(QObject):
    # Define signals for updating speed and rpm
    speed_changed = pyqtSignal(int)
    rpm_changed = pyqtSignal(int)

    DEFAULT_IDLE_RPM = 1000
    # NOTE: Highway by Nuremberg Messe
    DEFAULT_STARTING_LAT = 49.416410
    DEFAULT_STARTING_LON = 11.110604

    def __init__(self):
        super().__init__()
        self.running = False
        self.lock = threading.Lock()
        self.thread = None
        self.kuksa_client = KuksaClient()
        self.freq = 10
        self.vehicle_speed = 0
        self.engine_speed = self.DEFAULT_IDLE_RPM
        self.latitude = self.DEFAULT_STARTING_LAT
        self.longitude = self.DEFAULT_STARTING_LON
        self.count = 0

        random.seed()

    def start(self):
        if not self.running:
            self.kuksa_client.set_instance()
            self.reset()
            self.running = True
            if not self.thread.is_alive():
                self.thread.start()

    def stop(self):
        self.running = False

    def reset(self):
        with self.lock:
            self.vehicle_speed = 0
            self.engine_speed = self.DEFAULT_IDLE_RPM
            self.latitude = self.DEFAULT_STARTING_LAT
            self.longitude = self.DEFAULT_STARTING_LON
            self.count = 0
            self.thread = threading.Thread(target=self.run)

    def run(self):
        while self.running:
            if not self.running:
                break

            self.set_signal("Vehicle.Powertrain.Transmission.SelectedGear", 127)

            # Simulate acceleration and update speed and rpm
            self.accelerate(60, 1800, 3)
            self.accelerate(65, 1700, 1)
            self.accelerate(80, 2500, 6)
            self.accelerate(100, 3000, 4)
            self.brake(80, 3000, 3)
            self.accelerate(104, 4000, 6)
            self.brake(40, 2000, 4)
            self.accelerate(90, 3000, 5)
            self.brake(1, 650, 5)

            self.set_signal("Vehicle.Powertrain.Transmission.SelectedGear", 126)

            # Ensure reset is called when not in cruise mode
            if not self.running:
                self.reset()

            time.sleep(5)

    def accelerate(self, target_speed, target_rpm, duration):
        if target_speed <= self.vehicle_speed:
            return
        v = (target_speed - self.vehicle_speed) / (duration * self.freq)
        r = (target_rpm - self.engine_speed) / (duration * self.freq)
        while self.vehicle_speed < target_speed and self.running:
            with self.lock:
                self.vehicle_speed += v
                self.engine_speed += r
                self.speed_changed.emit(int(self.vehicle_speed))
                self.rpm_changed.emit(int(self.engine_speed))
                updates = {}
                updates["Vehicle.Speed"] = self.vehicle_speed
                updates["Vehicle.Powertrain.CombustionEngine.Speed"] = self.engine_speed
                self.simulate_position(self.vehicle_speed, v, False, updates)
                self.count = self.count + 1
                if self.count > 1:
                    self.set_signals(updates)
                    self.count = 0
            time.sleep(1 / self.freq)

    def brake(self, target_speed, target_rpm, duration):
        if target_speed >= self.vehicle_speed:
            return
        v = (self.vehicle_speed - target_speed) / (duration * self.freq)
        r = (self.engine_speed - target_rpm) / (duration * self.freq)
        while self.vehicle_speed > target_speed and self.running:
            with self.lock:
                self.vehicle_speed -= v
                self.engine_speed -= r
                self.speed_changed.emit(int(self.vehicle_speed))
                self.rpm_changed.emit(int(self.engine_speed))
                updates = {}
                updates["Vehicle.Speed"] = self.vehicle_speed
                updates["Vehicle.Powertrain.CombustionEngine.Speed"] = self.engine_speed
                self.simulate_position(self.vehicle_speed, v, True, updates)
                self.count = self.count + 1
                if self.count > 1:
                    self.set_signals(updates)
                    self.count = 0
            time.sleep(1 / self.freq)

    def increase(self, bycruise=True):
        if self.CRUISEACTIVE:
            target_speed = self.vehicle_speed + 5
            target_rpm = self.engine_speed * 1.1
            self.accelerate(target_speed, target_rpm, 2, bycruise)

    def decrease(self, bycruise=True):
        if self.CRUISEACTIVE:
            target_speed = self.vehicle_speed - 5
            target_rpm = self.engine_speed * 0.9
            self.brake(target_speed, target_rpm, 2, bycruise)

    def resume(self, bycruise=True):
        target_speed = self.CRUISESPEED
        target_rpm = self.CRUISERPM
        current_speed = self.get_vehicle_speed()
        if target_speed > current_speed:
            self.accelerate(target_speed, target_rpm, 2, bycruise)
        else:
            self.brake(target_speed, target_rpm, 2, bycruise)

    def set_signal(self, signal, value):
        try:
            self.kuksa_client.set(signal, value, 'value')
        except Exception as e:
            logging.error(f"Error sending value to kuksa {e}")

    def set_signals(self, values):
        try:
            self.kuksa_client.setValues(values)
        except Exception as e:
            logging.error(f"Error sending values to kuksa {e}")

    def simulate_position(self, speed_current, speed_delta, braking, updates):
        # From https://stackoverflow.com/questions/1253499/simple-calculations-for-working-with-lat-lon-and-km-distance
        latKmPerDegree = 110.574
        lonKmPerDegree = 111.320 * math.cos((self.latitude * math.pi) / (180))
        time_seconds = 1 / self.freq
        self.latitude += (speed_current * 0.7 * time_seconds / 3600) / latKmPerDegree
        self.longitude += (speed_current * 0.3 * time_seconds / 3600) / lonKmPerDegree

        accelX = (speed_delta * 1000 / 3600) * self.freq
        throttlePosition = accelX / 6 * 100
        brakePosition = 0
        if throttlePosition > 100:
            throttlePosition = 100
        if braking:
            brakePosition = throttlePosition
            throttlePosition = 0
            accelX = -accelX
        accelY = random.randrange(-150, 150) / 100.0
        accelZ = random.randrange(-100, 100) / 100.0

        pitch = random.randrange(-1000, 1000) / 300.0
        roll = random.randrange(-1000, 1000) / 300.0
        yaw = random.randrange(-1000, 1000) / 300.0

        steeringAngle = random.randrange(-90, 90)

        updates["Vehicle.OBD.ThrottlePosition"] = throttlePosition
        updates["Vehicle.Chassis.Brake.PedalPosition"] = brakePosition
        updates["Vehicle.Chassis.SteeringWheel.Angle"] = steeringAngle
        updates["Vehicle.CurrentLocation.Latitude"] = self.latitude
        updates["Vehicle.CurrentLocation.Longitude"] = self.longitude
        updates["Vehicle.Acceleration.Lateral"] = accelX
        updates["Vehicle.Acceleration.Longitudinal"] = accelY
        updates["Vehicle.Acceleration.Vertical"] = accelZ
        updates["Vehicle.AngularVelocity.Pitch"] = pitch
        updates["Vehicle.AngularVelocity.Roll"] = roll
        updates["Vehicle.AngularVelocity.Yaw"] = yaw