Darwin Neuroevolution Framework
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
car.h
1 // Copyright 2019 The Darwin Neuroevolution Framework Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <core/sim/camera.h>
18 #include <core/sim/accelerometer.h>
19 #include <core/sim/touch_sensor.h>
20 #include <core/sim/compass.h>
21 #include <core/utils.h>
22 
23 #include <third_party/box2d/box2d.h>
24 
25 namespace sim {
26 
27 struct CarConfig {
28  b2Vec2 position{};
29  float angle = 0;
30  float length = 2.0f;
31  bool camera = false;
32  float camera_fov = 90;
33  int camera_resolution = 64;
34  bool camera_depth = false;
35  bool touch_sensor = false;
36  int touch_resolution = 16;
37  bool accelerometer = false;
38  bool compass = false;
39  float max_forward_force = 10.0f;
40  float max_reverse_force = 4.0f;
41  float max_steer_angle = 40.0f;
42  float density = 0.01f;
43  float tire_traction = 1.5f;
44  float friction = 1.0f;
45  b2Color color;
46 };
47 
48 class Car : public core::NonCopyable {
49  public:
50  Car(b2World* world, const CarConfig& config);
51 
52  void preStep();
53  void postStep(float dt);
54 
57  void accelerate(float force);
58 
61  void steer(float steer_wheel_position);
62 
64  void brake(float intensity);
65 
66  // sensors
67  const Camera* camera() const { return camera_.get(); }
68  const TouchSensor* touchSensor() const { return touch_sensor_.get(); }
69  const Accelerometer* accelerometer() const { return accelerometer_.get(); }
70  const Compass* compass() const { return compass_.get(); }
71 
72  const b2Vec2& actualVelocity() const { return actual_velocity_; }
73 
74  b2Body* body() { return car_body_; }
75  const b2Body* body() const { return car_body_; }
76 
77  const auto& config() const { return config_; }
78 
79  private:
80  void createSensors();
81  void createWheelFixture(b2Body* body, const b2Vec2& pos);
82  void createLightFixture(const b2Vec2& pos, const b2Color& color);
83  b2RevoluteJoint* createTurningWheel(b2World* world, const b2Vec2& pos);
84 
85  void updateSteering();
86  void applyBrakeImpulse(float intensity,
87  const b2Vec2& wheel_normal,
88  const b2Vec2& wheel_center);
89  void applyTireLateralImpulse(const b2Vec2& wheel_normal, const b2Vec2& wheel_center);
90 
91  float width() const { return config_.length * 0.5f; }
92  float frontAxleOffset() const { return config_.length * 0.3f; }
93  float rearAxleOffset() const { return config_.length * -0.3f; }
94 
95  private:
96  b2Body* car_body_ = nullptr;
97  b2RevoluteJoint* left_wheel_joint_ = nullptr;
98  b2RevoluteJoint* right_wheel_joint_ = nullptr;
99  b2Vec2 last_position_ = { 0, 0 };
100  b2Vec2 actual_velocity_ = { 0, 0 };
101  float target_steer_ = 0;
102  float brake_pedal_ = 0;
103  unique_ptr<Camera> camera_;
104  unique_ptr<TouchSensor> touch_sensor_;
105  unique_ptr<Accelerometer> accelerometer_;
106  unique_ptr<Compass> compass_;
107  const CarConfig config_;
108 };
109 
110 } // namespace sim
Definition: accelerometer.cpp:19
Classes derived from this are not copyable or movable.
Definition: utils.h:69