Skip to content

走行

最新版のDonkeyでは、Simlatorとの連携ができなくなっています。旧simulation.pyをコピーして、実行します。

Simulator.pyのコピー

1
git clone https://github.com/autorope/donkeyparts.git

donkeyparts/donkeyparts/simulation.pywokspace_donkey/donkeycar/donkeycar/parts/に上書きコピーします。

Simulatorでの実行

1
donkey sim --model=models/mypilot

Simulator側の連携

Simulatorとの連携

通信はWebsocketでおこなわれる。Simulatorから30msに1回、160x120ピクセルの画像データが送られてくるので、~/workspace_donkey/donkeycar/parts/keras.pyに渡され、model.predictで、画像データを入力に、SteeringとThrottleの2値を取得し、その値でSimulatorを制御している。

~/workspace_donkey/donkeycar/donkeycar/parts/simulation.py

 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
def telemetry(self, sid, data):
        """
        Callback when we get new data from Unity simulator.
        We use it to process the image, do a forward inference,
        then send controls back to client.
        Takes sid (?) and data, a dictionary of json elements.
        """
        if data:
            # The current steering angle of the car
            last_steering = float(data["steering_angle"])

            # The current throttle of the car
            last_throttle = float(data["throttle"])

            # The current speed of the car
            speed = float(data["speed"])

            # The current image from the center camera of the car
            imgString = data["image"]

            # decode string based data into bytes, then to Image
            image = Image.open(BytesIO(base64.b64decode(imgString)))

            # then as numpy array
            image_array = np.asarray(image)

            # optional change to pre-preocess image before NN sees it
            if self.image_part is not None:
                image_array = self.image_part.run(image_array)

            # forward pass - inference
            steering, throttle = self.kpart.run(image_array)

            # filter throttle here, as our NN doesn't always do a greate job
            throttle = self.throttle_control(last_steering, last_throttle, speed, throttle)

            # simulator will scale our steering based on it's angle based input.
            # but we have an opportunity for more adjustment here.
            steering *= self.steering_scale
            #steering *= 1
            # send command back to Unity simulator
            self.send_control(steering, throttle)