Keyboard control for the human agent
| 137 | |
| 138 | |
| 139 | class KeyboardControl(object): |
| 140 | |
| 141 | """ |
| 142 | Keyboard control for the human agent |
| 143 | """ |
| 144 | |
| 145 | def __init__(self, path_to_conf_file): |
| 146 | """ |
| 147 | Init |
| 148 | """ |
| 149 | self._control = carla.VehicleControl() |
| 150 | self._steer_cache = 0.0 |
| 151 | self._clock = pygame.time.Clock() |
| 152 | |
| 153 | # Get the mode |
| 154 | if path_to_conf_file: |
| 155 | |
| 156 | with (open(path_to_conf_file, "r")) as f: |
| 157 | lines = f.read().split("\n") |
| 158 | self._mode = lines[0].split(" ")[1] |
| 159 | self._endpoint = lines[1].split(" ")[1] |
| 160 | |
| 161 | # Get the needed vars |
| 162 | if self._mode == "log": |
| 163 | self._log_data = {'records': []} |
| 164 | |
| 165 | elif self._mode == "playback": |
| 166 | self._index = 0 |
| 167 | self._control_list = [] |
| 168 | |
| 169 | with open(self._endpoint) as fd: |
| 170 | try: |
| 171 | self._records = json.load(fd) |
| 172 | self._json_to_control() |
| 173 | except json.JSONDecodeError: |
| 174 | pass |
| 175 | else: |
| 176 | self._mode = "normal" |
| 177 | self._endpoint = None |
| 178 | |
| 179 | def _json_to_control(self): |
| 180 | |
| 181 | # transform strs into VehicleControl commands |
| 182 | for entry in self._records['records']: |
| 183 | control = carla.VehicleControl(throttle=entry['control']['throttle'], |
| 184 | steer=entry['control']['steer'], |
| 185 | brake=entry['control']['brake'], |
| 186 | hand_brake=entry['control']['hand_brake'], |
| 187 | reverse=entry['control']['reverse'], |
| 188 | manual_gear_shift=entry['control']['manual_gear_shift'], |
| 189 | gear=entry['control']['gear']) |
| 190 | self._control_list.append(control) |
| 191 | |
| 192 | def parse_events(self, timestamp): |
| 193 | """ |
| 194 | Parse the keyboard events and set the vehicle controls accordingly |
| 195 | """ |
| 196 | # Move the vehicle |